2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (c) 1991 Regents of the University of California.
6 * Copyright (c) 1994 John S. Dyson
8 * Copyright (c) 1994 David Greenman
10 * Copyright (c) 2005-2010 Alan L. Cox <alc@cs.rice.edu>
11 * All rights reserved.
13 * This code is derived from software contributed to Berkeley by
14 * the Systems Programming Group of the University of Utah Computer
15 * Science Department and William Jolitz of UUNET Technologies Inc.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 * 3. All advertising materials mentioning features or use of this software
26 * must display the following acknowledgement:
27 * This product includes software developed by the University of
28 * California, Berkeley and its contributors.
29 * 4. Neither the name of the University nor the names of its contributors
30 * may be used to endorse or promote products derived from this software
31 * without specific prior written permission.
33 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
34 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
37 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 * from: @(#)pmap.c 7.7 (Berkeley) 5/12/91
48 * Copyright (c) 2003 Networks Associates Technology, Inc.
49 * All rights reserved.
50 * Copyright (c) 2018 The FreeBSD Foundation
51 * All rights reserved.
53 * This software was developed for the FreeBSD Project by Jake Burkholder,
54 * Safeport Network Services, and Network Associates Laboratories, the
55 * Security Research Division of Network Associates, Inc. under
56 * DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA
57 * CHATS research program.
59 * Portions of this software were developed by
60 * Konstantin Belousov <kib@FreeBSD.org> under sponsorship from
61 * the FreeBSD Foundation.
63 * Redistribution and use in source and binary forms, with or without
64 * modification, are permitted provided that the following conditions
66 * 1. Redistributions of source code must retain the above copyright
67 * notice, this list of conditions and the following disclaimer.
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in the
70 * documentation and/or other materials provided with the distribution.
72 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
73 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
74 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
75 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
76 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
77 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
78 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
79 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
80 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
81 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
85 #include <sys/cdefs.h>
86 __FBSDID("$FreeBSD$");
89 * Manages physical address maps.
91 * Since the information managed by this module is
92 * also stored by the logical address mapping module,
93 * this module may throw away valid virtual-to-physical
94 * mappings at almost any time. However, invalidations
95 * of virtual-to-physical mappings must be done as
98 * In order to cope with hardware architectures which
99 * make virtual-to-physical map invalidates expensive,
100 * this module may delay invalidate or reduced protection
101 * operations until such time as they are actually
102 * necessary. This module is given full information as
103 * to which processors are currently using which maps,
104 * and to when physical maps must be made correct.
107 #include "opt_apic.h"
109 #include "opt_pmap.h"
113 #include <sys/param.h>
114 #include <sys/systm.h>
115 #include <sys/kernel.h>
117 #include <sys/lock.h>
118 #include <sys/malloc.h>
119 #include <sys/mman.h>
120 #include <sys/msgbuf.h>
121 #include <sys/mutex.h>
122 #include <sys/proc.h>
123 #include <sys/rwlock.h>
124 #include <sys/sbuf.h>
125 #include <sys/sf_buf.h>
127 #include <sys/vmmeter.h>
128 #include <sys/sched.h>
129 #include <sys/sysctl.h>
131 #include <sys/vmem.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>
142 #include <vm/vm_phys.h>
143 #include <vm/vm_radix.h>
144 #include <vm/vm_reserv.h>
149 #include <machine/intr_machdep.h>
150 #include <x86/apicvar.h>
152 #include <x86/ifunc.h>
153 #include <machine/bootinfo.h>
154 #include <machine/cpu.h>
155 #include <machine/cputypes.h>
156 #include <machine/md_var.h>
157 #include <machine/pcb.h>
158 #include <machine/specialreg.h>
160 #include <machine/smp.h>
162 #include <machine/pmap_base.h>
164 #if !defined(DIAGNOSTIC)
165 #ifdef __GNUC_GNU_INLINE__
166 #define PMAP_INLINE __attribute__((__gnu_inline__)) inline
168 #define PMAP_INLINE extern inline
175 #define PV_STAT(x) do { x ; } while (0)
177 #define PV_STAT(x) do { } while (0)
180 #define pa_index(pa) ((pa) >> PDRSHIFT)
181 #define pa_to_pvh(pa) (&pv_table[pa_index(pa)])
184 * PTmap is recursive pagemap at top of virtual address space.
185 * Within PTmap, the page directory can be found (third indirection).
187 #define PTmap ((pt_entry_t *)(PTDPTDI << PDRSHIFT))
188 #define PTD ((pd_entry_t *)((PTDPTDI << PDRSHIFT) + (PTDPTDI * PAGE_SIZE)))
189 #define PTDpde ((pd_entry_t *)((PTDPTDI << PDRSHIFT) + (PTDPTDI * PAGE_SIZE) + \
190 (PTDPTDI * PDESIZE)))
193 * Translate a virtual address to the kernel virtual address of its page table
194 * entry (PTE). This can be used recursively. If the address of a PTE as
195 * previously returned by this macro is itself given as the argument, then the
196 * address of the page directory entry (PDE) that maps the PTE will be
199 * This macro may be used before pmap_bootstrap() is called.
201 #define vtopte(va) (PTmap + i386_btop(va))
204 * Get PDEs and PTEs for user/kernel address space
206 #define pmap_pde(m, v) (&((m)->pm_pdir[(vm_offset_t)(v) >> PDRSHIFT]))
207 #define pdir_pde(m, v) (m[(vm_offset_t)(v) >> PDRSHIFT])
209 #define pmap_pde_v(pte) ((*(int *)pte & PG_V) != 0)
210 #define pmap_pte_w(pte) ((*(int *)pte & PG_W) != 0)
211 #define pmap_pte_m(pte) ((*(int *)pte & PG_M) != 0)
212 #define pmap_pte_u(pte) ((*(int *)pte & PG_A) != 0)
213 #define pmap_pte_v(pte) ((*(int *)pte & PG_V) != 0)
215 #define pmap_pte_set_w(pte, v) ((v) ? atomic_set_int((u_int *)(pte), PG_W) : \
216 atomic_clear_int((u_int *)(pte), PG_W))
217 #define pmap_pte_set_prot(pte, v) ((*(int *)pte &= ~PG_PROT), (*(int *)pte |= (v)))
219 static int pgeflag = 0; /* PG_G or-in */
220 static int pseflag = 0; /* PG_PS or-in */
222 static int nkpt = NKPT;
226 static uma_zone_t pdptzone;
231 _Static_assert(VM_MAXUSER_ADDRESS == VADDR(TRPTDI, 0), "VM_MAXUSER_ADDRESS");
232 _Static_assert(VM_MAX_KERNEL_ADDRESS <= VADDR(PTDPTDI, 0),
233 "VM_MAX_KERNEL_ADDRESS");
234 _Static_assert(PMAP_MAP_LOW == VADDR(LOWPTDI, 0), "PMAP_MAP_LOW");
235 _Static_assert(KERNLOAD == (KERNPTDI << PDRSHIFT), "KERNLOAD");
237 extern int pat_works;
238 extern int pg_ps_enabled;
240 extern int elf32_nxstack;
242 #define PAT_INDEX_SIZE 8
243 static int pat_index[PAT_INDEX_SIZE]; /* cache mode to PAT index conversion */
246 * pmap_mapdev support pre initialization (i.e. console)
248 #define PMAP_PREINIT_MAPPING_COUNT 8
249 static struct pmap_preinit_mapping {
254 } pmap_preinit_mapping[PMAP_PREINIT_MAPPING_COUNT];
255 static int pmap_initialized;
257 static struct rwlock_padalign pvh_global_lock;
260 * Data for the pv entry allocation mechanism
262 static TAILQ_HEAD(pch, pv_chunk) pv_chunks = TAILQ_HEAD_INITIALIZER(pv_chunks);
263 extern int pv_entry_max, pv_entry_count;
264 static int pv_entry_high_water = 0;
265 static struct md_page *pv_table;
266 extern int shpgperproc;
268 static struct pv_chunk *pv_chunkbase; /* KVA block for pv_chunks */
269 static int pv_maxchunks; /* How many chunks we have KVA for */
270 static vm_offset_t pv_vafree; /* freelist stored in the PTE */
273 * All those kernel PT submaps that BSD is so fond of
275 static pt_entry_t *CMAP3;
276 static pd_entry_t *KPTD;
277 static caddr_t CADDR3;
282 static caddr_t crashdumpmap;
284 static pt_entry_t *PMAP1 = NULL, *PMAP2, *PMAP3;
285 static pt_entry_t *PADDR1 = NULL, *PADDR2, *PADDR3;
287 static int PMAP1cpu, PMAP3cpu;
288 extern int PMAP1changedcpu;
290 extern int PMAP1changed;
291 extern int PMAP1unchanged;
292 static struct mtx PMAP2mutex;
295 * Internal flags for pmap_enter()'s helper functions.
297 #define PMAP_ENTER_NORECLAIM 0x1000000 /* Don't reclaim PV entries. */
298 #define PMAP_ENTER_NOREPLACE 0x2000000 /* Don't replace mappings. */
300 static void free_pv_chunk(struct pv_chunk *pc);
301 static void free_pv_entry(pmap_t pmap, pv_entry_t pv);
302 static pv_entry_t get_pv_entry(pmap_t pmap, boolean_t try);
303 static void pmap_pv_demote_pde(pmap_t pmap, vm_offset_t va, vm_paddr_t pa);
304 static bool pmap_pv_insert_pde(pmap_t pmap, vm_offset_t va, pd_entry_t pde,
306 #if VM_NRESERVLEVEL > 0
307 static void pmap_pv_promote_pde(pmap_t pmap, vm_offset_t va, vm_paddr_t pa);
309 static void pmap_pvh_free(struct md_page *pvh, pmap_t pmap, vm_offset_t va);
310 static pv_entry_t pmap_pvh_remove(struct md_page *pvh, pmap_t pmap,
312 static int pmap_pvh_wired_mappings(struct md_page *pvh, int count);
314 static void pmap_abort_ptp(pmap_t pmap, vm_offset_t va, vm_page_t mpte);
315 static boolean_t pmap_demote_pde(pmap_t pmap, pd_entry_t *pde, vm_offset_t va);
316 static bool pmap_enter_4mpage(pmap_t pmap, vm_offset_t va, vm_page_t m,
318 static int pmap_enter_pde(pmap_t pmap, vm_offset_t va, pd_entry_t newpde,
319 u_int flags, vm_page_t m);
320 static vm_page_t pmap_enter_quick_locked(pmap_t pmap, vm_offset_t va,
321 vm_page_t m, vm_prot_t prot, vm_page_t mpte);
322 static int pmap_insert_pt_page(pmap_t pmap, vm_page_t mpte, bool promoted);
323 static void pmap_invalidate_pde_page(pmap_t pmap, vm_offset_t va,
325 static void pmap_fill_ptp(pt_entry_t *firstpte, pt_entry_t newpte);
326 static boolean_t pmap_is_modified_pvh(struct md_page *pvh);
327 static boolean_t pmap_is_referenced_pvh(struct md_page *pvh);
328 static void pmap_kenter_attr(vm_offset_t va, vm_paddr_t pa, int mode);
329 static void pmap_kenter_pde(vm_offset_t va, pd_entry_t newpde);
330 static void pmap_pde_attr(pd_entry_t *pde, int cache_bits);
331 #if VM_NRESERVLEVEL > 0
332 static void pmap_promote_pde(pmap_t pmap, pd_entry_t *pde, vm_offset_t va);
334 static boolean_t pmap_protect_pde(pmap_t pmap, pd_entry_t *pde, vm_offset_t sva,
336 static void pmap_pte_attr(pt_entry_t *pte, int cache_bits);
337 static void pmap_remove_pde(pmap_t pmap, pd_entry_t *pdq, vm_offset_t sva,
338 struct spglist *free);
339 static int pmap_remove_pte(pmap_t pmap, pt_entry_t *ptq, vm_offset_t sva,
340 struct spglist *free);
341 static vm_page_t pmap_remove_pt_page(pmap_t pmap, vm_offset_t va);
342 static void pmap_remove_page(pmap_t pmap, vm_offset_t va, struct spglist *free);
343 static bool pmap_remove_ptes(pmap_t pmap, vm_offset_t sva, vm_offset_t eva,
344 struct spglist *free);
345 static void pmap_remove_entry(pmap_t pmap, vm_page_t m, vm_offset_t va);
346 static void pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t m);
347 static boolean_t pmap_try_insert_pv_entry(pmap_t pmap, vm_offset_t va,
349 static void pmap_update_pde(pmap_t pmap, vm_offset_t va, pd_entry_t *pde,
351 static void pmap_update_pde_invalidate(vm_offset_t va, pd_entry_t newpde);
353 static vm_page_t pmap_allocpte(pmap_t pmap, vm_offset_t va, u_int flags);
355 static vm_page_t _pmap_allocpte(pmap_t pmap, u_int ptepindex, u_int flags);
356 static void _pmap_unwire_ptp(pmap_t pmap, vm_page_t m, struct spglist *free);
357 static pt_entry_t *pmap_pte_quick(pmap_t pmap, vm_offset_t va);
358 static void pmap_pte_release(pt_entry_t *pte);
359 static int pmap_unuse_pt(pmap_t, vm_offset_t, struct spglist *);
361 static void *pmap_pdpt_allocf(uma_zone_t zone, vm_size_t bytes, int domain,
362 uint8_t *flags, int wait);
364 static void pmap_init_trm(void);
365 static void pmap_invalidate_all_int(pmap_t pmap);
367 static __inline void pagezero(void *page);
369 CTASSERT(1 << PDESHIFT == sizeof(pd_entry_t));
370 CTASSERT(1 << PTESHIFT == sizeof(pt_entry_t));
373 extern u_long physfree; /* phys addr of next free page */
374 extern u_long vm86phystk;/* PA of vm86/bios stack */
375 extern u_long vm86paddr;/* address of vm86 region */
376 extern int vm86pa; /* phys addr of vm86 region */
377 extern u_long KERNend; /* phys addr end of kernel (just after bss) */
379 pd_entry_t *IdlePTD_pae; /* phys addr of kernel PTD */
380 pdpt_entry_t *IdlePDPT; /* phys addr of kernel PDPT */
381 pt_entry_t *KPTmap_pae; /* address of kernel page tables */
382 #define IdlePTD IdlePTD_pae
383 #define KPTmap KPTmap_pae
385 pd_entry_t *IdlePTD_nopae;
386 pt_entry_t *KPTmap_nopae;
387 #define IdlePTD IdlePTD_nopae
388 #define KPTmap KPTmap_nopae
390 extern u_long KPTphys; /* phys addr of kernel page tables */
391 extern u_long tramp_idleptd;
394 allocpages(u_int cnt, u_long *physfree)
399 *physfree += PAGE_SIZE * cnt;
400 bzero((void *)res, PAGE_SIZE * cnt);
405 pmap_cold_map(u_long pa, u_long va, u_long cnt)
409 for (pt = (pt_entry_t *)KPTphys + atop(va); cnt > 0;
410 cnt--, pt++, va += PAGE_SIZE, pa += PAGE_SIZE)
411 *pt = pa | PG_V | PG_RW | PG_A | PG_M;
415 pmap_cold_mapident(u_long pa, u_long cnt)
418 pmap_cold_map(pa, pa, cnt);
421 _Static_assert(LOWPTDI * 2 * NBPDR == KERNBASE,
422 "Broken double-map of zero PTD");
425 __CONCAT(PMTYPE, remap_lower)(bool enable)
429 for (i = 0; i < LOWPTDI; i++)
430 IdlePTD[i] = enable ? IdlePTD[LOWPTDI + i] : 0;
431 load_cr3(rcr3()); /* invalidate TLB */
435 * Called from locore.s before paging is enabled. Sets up the first
436 * kernel page table. Since kernel is mapped with PA == VA, this code
437 * does not require relocations.
440 __CONCAT(PMTYPE, cold)(void)
446 physfree = (u_long)&_end;
447 if (bootinfo.bi_esymtab != 0)
448 physfree = bootinfo.bi_esymtab;
449 if (bootinfo.bi_kernend != 0)
450 physfree = bootinfo.bi_kernend;
451 physfree = roundup2(physfree, NBPDR);
454 /* Allocate Kernel Page Tables */
455 KPTphys = allocpages(NKPT, &physfree);
456 KPTmap = (pt_entry_t *)KPTphys;
458 /* Allocate Page Table Directory */
460 /* XXX only need 32 bytes (easier for now) */
461 IdlePDPT = (pdpt_entry_t *)allocpages(1, &physfree);
463 IdlePTD = (pd_entry_t *)allocpages(NPGPTD, &physfree);
466 * Allocate KSTACK. Leave a guard page between IdlePTD and
467 * proc0kstack, to control stack overflow for thread0 and
468 * prevent corruption of the page table. We leak the guard
469 * physical memory due to 1:1 mappings.
471 allocpages(1, &physfree);
472 proc0kstack = allocpages(TD0_KSTACK_PAGES, &physfree);
474 /* vm86/bios stack */
475 vm86phystk = allocpages(1, &physfree);
477 /* pgtable + ext + IOPAGES */
478 vm86paddr = vm86pa = allocpages(3, &physfree);
480 /* Install page tables into PTD. Page table page 1 is wasted. */
481 for (a = 0; a < NKPT; a++)
482 IdlePTD[a] = (KPTphys + ptoa(a)) | PG_V | PG_RW | PG_A | PG_M;
485 /* PAE install PTD pointers into PDPT */
486 for (a = 0; a < NPGPTD; a++)
487 IdlePDPT[a] = ((u_int)IdlePTD + ptoa(a)) | PG_V;
491 * Install recursive mapping for kernel page tables into
494 for (a = 0; a < NPGPTD; a++)
495 IdlePTD[PTDPTDI + a] = ((u_int)IdlePTD + ptoa(a)) | PG_V |
499 * Initialize page table pages mapping physical address zero
500 * through the (physical) end of the kernel. Many of these
501 * pages must be reserved, and we reserve them all and map
502 * them linearly for convenience. We do this even if we've
503 * enabled PSE above; we'll just switch the corresponding
504 * kernel PDEs before we turn on paging.
506 * This and all other page table entries allow read and write
507 * access for various reasons. Kernel mappings never have any
508 * access restrictions.
510 pmap_cold_mapident(0, atop(NBPDR) * LOWPTDI);
511 pmap_cold_map(0, NBPDR * LOWPTDI, atop(NBPDR) * LOWPTDI);
512 pmap_cold_mapident(KERNBASE, atop(KERNend - KERNBASE));
514 /* Map page table directory */
516 pmap_cold_mapident((u_long)IdlePDPT, 1);
518 pmap_cold_mapident((u_long)IdlePTD, NPGPTD);
520 /* Map early KPTmap. It is really pmap_cold_mapident. */
521 pmap_cold_map(KPTphys, (u_long)KPTmap, NKPT);
523 /* Map proc0kstack */
524 pmap_cold_mapident(proc0kstack, TD0_KSTACK_PAGES);
525 /* ISA hole already mapped */
527 pmap_cold_mapident(vm86phystk, 1);
528 pmap_cold_mapident(vm86pa, 3);
530 /* Map page 0 into the vm86 page table */
531 *(pt_entry_t *)vm86pa = 0 | PG_RW | PG_U | PG_A | PG_M | PG_V;
533 /* ...likewise for the ISA hole for vm86 */
534 for (pt = (pt_entry_t *)vm86pa + atop(ISA_HOLE_START), a = 0;
535 a < atop(ISA_HOLE_LENGTH); a++, pt++)
536 *pt = (ISA_HOLE_START + ptoa(a)) | PG_RW | PG_U | PG_A |
539 /* Enable PSE, PGE, VME, and PAE if configured. */
541 if ((cpu_feature & CPUID_PSE) != 0) {
545 * Superpage mapping of the kernel text. Existing 4k
546 * page table pages are wasted.
548 for (a = KERNBASE; a < KERNend; a += NBPDR)
549 IdlePTD[a >> PDRSHIFT] = a | PG_PS | PG_A | PG_M |
552 if ((cpu_feature & CPUID_PGE) != 0) {
556 ncr4 |= (cpu_feature & CPUID_VME) != 0 ? CR4_VME : 0;
561 load_cr4(rcr4() | ncr4);
563 /* Now enable paging */
565 cr3 = (u_int)IdlePDPT;
566 if ((cpu_feature & CPUID_PAT) == 0)
569 cr3 = (u_int)IdlePTD;
573 load_cr0(rcr0() | CR0_PG);
576 * Now running relocated at KERNBASE where the system is
581 * Remove the lowest part of the double mapping of low memory
582 * to get some null pointer checks.
584 __CONCAT(PMTYPE, remap_lower)(false);
586 kernel_vm_end = /* 0 + */ NKPT * NBPDR;
588 i386_pmap_VM_NFREEORDER = VM_NFREEORDER_PAE;
589 i386_pmap_VM_LEVEL_0_ORDER = VM_LEVEL_0_ORDER_PAE;
590 i386_pmap_PDRSHIFT = PDRSHIFT_PAE;
592 i386_pmap_VM_NFREEORDER = VM_NFREEORDER_NOPAE;
593 i386_pmap_VM_LEVEL_0_ORDER = VM_LEVEL_0_ORDER_NOPAE;
594 i386_pmap_PDRSHIFT = PDRSHIFT_NOPAE;
599 __CONCAT(PMTYPE, set_nx)(void)
603 if ((amd_feature & AMDID_NX) == 0)
607 /* EFER.EFER_NXE is set in initializecpu(). */
612 * Bootstrap the system enough to run with virtual memory.
614 * On the i386 this is called after pmap_cold() created initial
615 * kernel page table and enabled paging, and just syncs the pmap
616 * module with what has already been done.
619 __CONCAT(PMTYPE, bootstrap)(vm_paddr_t firstaddr)
622 pt_entry_t *pte, *unused;
627 res = atop(firstaddr - (vm_paddr_t)KERNLOAD);
630 * Add a physical memory segment (vm_phys_seg) corresponding to the
631 * preallocated kernel page table pages so that vm_page structures
632 * representing these pages will be created. The vm_page structures
633 * are required for promotion of the corresponding kernel virtual
634 * addresses to superpage mappings.
636 vm_phys_early_add_seg(KPTphys, KPTphys + ptoa(nkpt));
639 * Initialize the first available kernel virtual address.
640 * However, using "firstaddr" may waste a few pages of the
641 * kernel virtual address space, because pmap_cold() may not
642 * have mapped every physical page that it allocated.
643 * Preferably, pmap_cold() would provide a first unused
644 * virtual address in addition to "firstaddr".
646 virtual_avail = (vm_offset_t)firstaddr;
647 virtual_end = VM_MAX_KERNEL_ADDRESS;
650 * Initialize the kernel pmap (which is statically allocated).
651 * Count bootstrap data as being resident in case any of this data is
652 * later unmapped (using pmap_remove()) and freed.
654 PMAP_LOCK_INIT(kernel_pmap);
655 kernel_pmap->pm_pdir = IdlePTD;
657 kernel_pmap->pm_pdpt = IdlePDPT;
659 CPU_FILL(&kernel_pmap->pm_active); /* don't allow deactivation */
660 kernel_pmap->pm_stats.resident_count = res;
661 TAILQ_INIT(&kernel_pmap->pm_pvchunk);
664 * Initialize the global pv list lock.
666 rw_init(&pvh_global_lock, "pmap pv global");
669 * Reserve some special page table entries/VA space for temporary
672 #define SYSMAP(c, p, v, n) \
673 v = (c)va; va += ((n)*PAGE_SIZE); p = pte; pte += (n);
679 * Initialize temporary map objects on the current CPU for use
681 * CMAP1/CMAP2 are used for zeroing and copying pages.
682 * CMAP3 is used for the boot-time memory test.
685 mtx_init(&pc->pc_cmap_lock, "SYSMAPS", NULL, MTX_DEF);
686 SYSMAP(caddr_t, pc->pc_cmap_pte1, pc->pc_cmap_addr1, 1)
687 SYSMAP(caddr_t, pc->pc_cmap_pte2, pc->pc_cmap_addr2, 1)
688 SYSMAP(vm_offset_t, pte, pc->pc_qmap_addr, 1)
690 SYSMAP(caddr_t, CMAP3, CADDR3, 1);
695 SYSMAP(caddr_t, unused, crashdumpmap, MAXDUMPPGS)
698 * ptvmmap is used for reading arbitrary physical pages via /dev/mem.
700 SYSMAP(caddr_t, unused, ptvmmap, 1)
703 * msgbufp is used to map the system message buffer.
705 SYSMAP(struct msgbuf *, unused, msgbufp, atop(round_page(msgbufsize)))
708 * KPTmap is used by pmap_kextract().
710 * KPTmap is first initialized by pmap_cold(). However, that initial
711 * KPTmap can only support NKPT page table pages. Here, a larger
712 * KPTmap is created that can support KVA_PAGES page table pages.
714 SYSMAP(pt_entry_t *, KPTD, KPTmap, KVA_PAGES)
716 for (i = 0; i < NKPT; i++)
717 KPTD[i] = (KPTphys + ptoa(i)) | PG_RW | PG_V;
720 * PADDR1 and PADDR2 are used by pmap_pte_quick() and pmap_pte(),
723 SYSMAP(pt_entry_t *, PMAP1, PADDR1, 1)
724 SYSMAP(pt_entry_t *, PMAP2, PADDR2, 1)
725 SYSMAP(pt_entry_t *, PMAP3, PADDR3, 1)
727 mtx_init(&PMAP2mutex, "PMAP2", NULL, MTX_DEF);
732 * Initialize the PAT MSR if present.
733 * pmap_init_pat() clears and sets CR4_PGE, which, as a
734 * side-effect, invalidates stale PG_G TLB entries that might
735 * have been created in our pre-boot environment. We assume
736 * that PAT support implies PGE and in reverse, PGE presence
737 * comes with PAT. Both features were added for Pentium Pro.
743 pmap_init_reserved_pages(void)
758 mtx_init(&pc->pc_copyout_mlock, "cpmlk", NULL, MTX_DEF |
760 pc->pc_copyout_maddr = kva_alloc(ptoa(2));
761 if (pc->pc_copyout_maddr == 0)
762 panic("unable to allocate non-sleepable copyout KVA");
763 sx_init(&pc->pc_copyout_slock, "cpslk");
764 pc->pc_copyout_saddr = kva_alloc(ptoa(2));
765 if (pc->pc_copyout_saddr == 0)
766 panic("unable to allocate sleepable copyout KVA");
767 pc->pc_pmap_eh_va = kva_alloc(ptoa(1));
768 if (pc->pc_pmap_eh_va == 0)
769 panic("unable to allocate pmap_extract_and_hold KVA");
770 pc->pc_pmap_eh_ptep = (char *)vtopte(pc->pc_pmap_eh_va);
773 * Skip if the mappings have already been initialized,
774 * i.e. this is the BSP.
776 if (pc->pc_cmap_addr1 != 0)
779 mtx_init(&pc->pc_cmap_lock, "SYSMAPS", NULL, MTX_DEF);
780 pages = kva_alloc(PAGE_SIZE * 3);
782 panic("unable to allocate CMAP KVA");
783 pc->pc_cmap_pte1 = vtopte(pages);
784 pc->pc_cmap_pte2 = vtopte(pages + PAGE_SIZE);
785 pc->pc_cmap_addr1 = (caddr_t)pages;
786 pc->pc_cmap_addr2 = (caddr_t)(pages + PAGE_SIZE);
787 pc->pc_qmap_addr = pages + ptoa(2);
791 SYSINIT(rpages_init, SI_SUB_CPU, SI_ORDER_ANY, pmap_init_reserved_pages, NULL);
797 __CONCAT(PMTYPE, init_pat)(void)
799 int pat_table[PAT_INDEX_SIZE];
804 /* Set default PAT index table. */
805 for (i = 0; i < PAT_INDEX_SIZE; i++)
807 pat_table[PAT_WRITE_BACK] = 0;
808 pat_table[PAT_WRITE_THROUGH] = 1;
809 pat_table[PAT_UNCACHEABLE] = 3;
810 pat_table[PAT_WRITE_COMBINING] = 3;
811 pat_table[PAT_WRITE_PROTECTED] = 3;
812 pat_table[PAT_UNCACHED] = 3;
815 * Bail if this CPU doesn't implement PAT.
816 * We assume that PAT support implies PGE.
818 if ((cpu_feature & CPUID_PAT) == 0) {
819 for (i = 0; i < PAT_INDEX_SIZE; i++)
820 pat_index[i] = pat_table[i];
826 * Due to some Intel errata, we can only safely use the lower 4
829 * Intel Pentium III Processor Specification Update
830 * Errata E.27 (Upper Four PAT Entries Not Usable With Mode B
833 * Intel Pentium IV Processor Specification Update
834 * Errata N46 (PAT Index MSB May Be Calculated Incorrectly)
836 if (cpu_vendor_id == CPU_VENDOR_INTEL &&
837 !(CPUID_TO_FAMILY(cpu_id) == 6 && CPUID_TO_MODEL(cpu_id) >= 0xe))
840 /* Initialize default PAT entries. */
841 pat_msr = PAT_VALUE(0, PAT_WRITE_BACK) |
842 PAT_VALUE(1, PAT_WRITE_THROUGH) |
843 PAT_VALUE(2, PAT_UNCACHED) |
844 PAT_VALUE(3, PAT_UNCACHEABLE) |
845 PAT_VALUE(4, PAT_WRITE_BACK) |
846 PAT_VALUE(5, PAT_WRITE_THROUGH) |
847 PAT_VALUE(6, PAT_UNCACHED) |
848 PAT_VALUE(7, PAT_UNCACHEABLE);
852 * Leave the indices 0-3 at the default of WB, WT, UC-, and UC.
853 * Program 5 and 6 as WP and WC.
854 * Leave 4 and 7 as WB and UC.
856 pat_msr &= ~(PAT_MASK(5) | PAT_MASK(6));
857 pat_msr |= PAT_VALUE(5, PAT_WRITE_PROTECTED) |
858 PAT_VALUE(6, PAT_WRITE_COMBINING);
859 pat_table[PAT_UNCACHED] = 2;
860 pat_table[PAT_WRITE_PROTECTED] = 5;
861 pat_table[PAT_WRITE_COMBINING] = 6;
864 * Just replace PAT Index 2 with WC instead of UC-.
866 pat_msr &= ~PAT_MASK(2);
867 pat_msr |= PAT_VALUE(2, PAT_WRITE_COMBINING);
868 pat_table[PAT_WRITE_COMBINING] = 2;
873 load_cr4(cr4 & ~CR4_PGE);
875 /* Disable caches (CD = 1, NW = 0). */
877 load_cr0((cr0 & ~CR0_NW) | CR0_CD);
879 /* Flushes caches and TLBs. */
883 /* Update PAT and index table. */
884 wrmsr(MSR_PAT, pat_msr);
885 for (i = 0; i < PAT_INDEX_SIZE; i++)
886 pat_index[i] = pat_table[i];
888 /* Flush caches and TLBs again. */
892 /* Restore caches and PGE. */
899 pmap_pdpt_allocf(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *flags,
903 /* Inform UMA that this allocator uses kernel_map/object. */
904 *flags = UMA_SLAB_KERNEL;
905 return ((void *)kmem_alloc_contig_domainset(DOMAINSET_FIXED(domain),
906 bytes, wait, 0x0ULL, 0xffffffffULL, 1, 0, VM_MEMATTR_DEFAULT));
911 * Abuse the pte nodes for unmapped kva to thread a kva freelist through.
913 * - Must deal with pages in order to ensure that none of the PG_* bits
914 * are ever set, PG_V in particular.
915 * - Assumes we can write to ptes without pte_store() atomic ops, even
916 * on PAE systems. This should be ok.
917 * - Assumes nothing will ever test these addresses for 0 to indicate
918 * no mapping instead of correctly checking PG_V.
919 * - Assumes a vm_offset_t will fit in a pte (true for i386).
920 * Because PG_V is never set, there can be no mappings to invalidate.
923 pmap_ptelist_alloc(vm_offset_t *head)
930 panic("pmap_ptelist_alloc: exhausted ptelist KVA");
934 panic("pmap_ptelist_alloc: va with PG_V set!");
940 pmap_ptelist_free(vm_offset_t *head, vm_offset_t va)
945 panic("pmap_ptelist_free: freeing va with PG_V set!");
947 *pte = *head; /* virtual! PG_V is 0 though */
952 pmap_ptelist_init(vm_offset_t *head, void *base, int npages)
958 for (i = npages - 1; i >= 0; i--) {
959 va = (vm_offset_t)base + i * PAGE_SIZE;
960 pmap_ptelist_free(head, va);
965 * Initialize the pmap module.
966 * Called by vm_init, to initialize any structures that the pmap
967 * system needs to map virtual memory.
970 __CONCAT(PMTYPE, init)(void)
972 struct pmap_preinit_mapping *ppim;
978 * Initialize the vm page array entries for the kernel pmap's
981 PMAP_LOCK(kernel_pmap);
982 for (i = 0; i < NKPT; i++) {
983 mpte = PHYS_TO_VM_PAGE(KPTphys + ptoa(i));
984 KASSERT(mpte >= vm_page_array &&
985 mpte < &vm_page_array[vm_page_array_size],
986 ("pmap_init: page table page is out of range"));
987 mpte->pindex = i + KPTDI;
988 mpte->phys_addr = KPTphys + ptoa(i);
992 * Collect the page table pages that were replaced by a 2/4MB
993 * page. They are filled with equivalent 4KB page mappings.
996 KERNBASE <= i << PDRSHIFT && i << PDRSHIFT < KERNend &&
997 pmap_insert_pt_page(kernel_pmap, mpte, true))
998 panic("pmap_init: pmap_insert_pt_page failed");
1000 PMAP_UNLOCK(kernel_pmap);
1004 * Initialize the address space (zone) for the pv entries. Set a
1005 * high water mark so that the system can recover from excessive
1006 * numbers of pv entries.
1008 TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
1009 pv_entry_max = shpgperproc * maxproc + vm_cnt.v_page_count;
1010 TUNABLE_INT_FETCH("vm.pmap.pv_entries", &pv_entry_max);
1011 pv_entry_max = roundup(pv_entry_max, _NPCPV);
1012 pv_entry_high_water = 9 * (pv_entry_max / 10);
1015 * If the kernel is running on a virtual machine, then it must assume
1016 * that MCA is enabled by the hypervisor. Moreover, the kernel must
1017 * be prepared for the hypervisor changing the vendor and family that
1018 * are reported by CPUID. Consequently, the workaround for AMD Family
1019 * 10h Erratum 383 is enabled if the processor's feature set does not
1020 * include at least one feature that is only supported by older Intel
1021 * or newer AMD processors.
1023 if (vm_guest != VM_GUEST_NO && (cpu_feature & CPUID_SS) == 0 &&
1024 (cpu_feature2 & (CPUID2_SSSE3 | CPUID2_SSE41 | CPUID2_AESNI |
1025 CPUID2_AVX | CPUID2_XSAVE)) == 0 && (amd_feature2 & (AMDID2_XOP |
1027 workaround_erratum383 = 1;
1030 * Are large page mappings supported and enabled?
1032 TUNABLE_INT_FETCH("vm.pmap.pg_ps_enabled", &pg_ps_enabled);
1035 else if (pg_ps_enabled) {
1036 KASSERT(MAXPAGESIZES > 1 && pagesizes[1] == 0,
1037 ("pmap_init: can't assign to pagesizes[1]"));
1038 pagesizes[1] = NBPDR;
1042 * Calculate the size of the pv head table for superpages.
1043 * Handle the possibility that "vm_phys_segs[...].end" is zero.
1045 pv_npg = trunc_4mpage(vm_phys_segs[vm_phys_nsegs - 1].end -
1046 PAGE_SIZE) / NBPDR + 1;
1049 * Allocate memory for the pv head table for superpages.
1051 s = (vm_size_t)(pv_npg * sizeof(struct md_page));
1053 pv_table = (struct md_page *)kmem_malloc(s, M_WAITOK | M_ZERO);
1054 for (i = 0; i < pv_npg; i++)
1055 TAILQ_INIT(&pv_table[i].pv_list);
1057 pv_maxchunks = MAX(pv_entry_max / _NPCPV, maxproc);
1058 pv_chunkbase = (struct pv_chunk *)kva_alloc(PAGE_SIZE * pv_maxchunks);
1059 if (pv_chunkbase == NULL)
1060 panic("pmap_init: not enough kvm for pv chunks");
1061 pmap_ptelist_init(&pv_vafree, pv_chunkbase, pv_maxchunks);
1062 #ifdef PMAP_PAE_COMP
1063 pdptzone = uma_zcreate("PDPT", NPGPTD * sizeof(pdpt_entry_t), NULL,
1064 NULL, NULL, NULL, (NPGPTD * sizeof(pdpt_entry_t)) - 1,
1065 UMA_ZONE_CONTIG | UMA_ZONE_VM | UMA_ZONE_NOFREE);
1066 uma_zone_set_allocf(pdptzone, pmap_pdpt_allocf);
1069 pmap_initialized = 1;
1074 for (i = 0; i < PMAP_PREINIT_MAPPING_COUNT; i++) {
1075 ppim = pmap_preinit_mapping + i;
1078 printf("PPIM %u: PA=%#jx, VA=%#x, size=%#x, mode=%#x\n", i,
1079 (uintmax_t)ppim->pa, ppim->va, ppim->sz, ppim->mode);
1084 extern u_long pmap_pde_demotions;
1085 extern u_long pmap_pde_mappings;
1086 extern u_long pmap_pde_p_failures;
1087 extern u_long pmap_pde_promotions;
1089 /***************************************************
1090 * Low level helper routines.....
1091 ***************************************************/
1094 __CONCAT(PMTYPE, is_valid_memattr)(pmap_t pmap __unused, vm_memattr_t mode)
1097 return (mode >= 0 && mode < PAT_INDEX_SIZE &&
1098 pat_index[(int)mode] >= 0);
1102 * Determine the appropriate bits to set in a PTE or PDE for a specified
1106 __CONCAT(PMTYPE, cache_bits)(pmap_t pmap, int mode, boolean_t is_pde)
1108 int cache_bits, pat_flag, pat_idx;
1110 if (!pmap_is_valid_memattr(pmap, mode))
1111 panic("Unknown caching mode %d\n", mode);
1113 /* The PAT bit is different for PTE's and PDE's. */
1114 pat_flag = is_pde ? PG_PDE_PAT : PG_PTE_PAT;
1116 /* Map the caching mode to a PAT index. */
1117 pat_idx = pat_index[mode];
1119 /* Map the 3-bit index value into the PAT, PCD, and PWT bits. */
1122 cache_bits |= pat_flag;
1124 cache_bits |= PG_NC_PCD;
1126 cache_bits |= PG_NC_PWT;
1127 return (cache_bits);
1131 pmap_pat_index(pmap_t pmap, pt_entry_t pte, bool is_pde)
1133 int pat_flag, pat_idx;
1135 if ((cpu_feature & CPUID_PAT) == 0)
1139 /* The PAT bit is different for PTE's and PDE's. */
1140 pat_flag = is_pde ? PG_PDE_PAT : PG_PTE_PAT;
1142 if ((pte & pat_flag) != 0)
1144 if ((pte & PG_NC_PCD) != 0)
1146 if ((pte & PG_NC_PWT) != 0)
1149 /* See pmap_init_pat(). */
1163 __CONCAT(PMTYPE, ps_enabled)(pmap_t pmap __unused)
1166 return (pg_ps_enabled);
1170 * The caller is responsible for maintaining TLB consistency.
1173 pmap_kenter_pde(vm_offset_t va, pd_entry_t newpde)
1177 pde = pmap_pde(kernel_pmap, va);
1178 pde_store(pde, newpde);
1182 * After changing the page size for the specified virtual address in the page
1183 * table, flush the corresponding entries from the processor's TLB. Only the
1184 * calling processor's TLB is affected.
1186 * The calling thread must be pinned to a processor.
1189 pmap_update_pde_invalidate(vm_offset_t va, pd_entry_t newpde)
1192 if ((newpde & PG_PS) == 0)
1193 /* Demotion: flush a specific 2MB page mapping. */
1195 else /* if ((newpde & PG_G) == 0) */
1197 * Promotion: flush every 4KB page mapping from the TLB
1198 * because there are too many to flush individually.
1206 pmap_curcpu_cb_dummy(pmap_t pmap __unused, vm_offset_t addr1 __unused,
1207 vm_offset_t addr2 __unused)
1212 * For SMP, these functions have to use the IPI mechanism for coherence.
1214 * N.B.: Before calling any of the following TLB invalidation functions,
1215 * the calling processor must ensure that all stores updating a non-
1216 * kernel page table are globally performed. Otherwise, another
1217 * processor could cache an old, pre-update entry without being
1218 * invalidated. This can happen one of two ways: (1) The pmap becomes
1219 * active on another processor after its pm_active field is checked by
1220 * one of the following functions but before a store updating the page
1221 * table is globally performed. (2) The pmap becomes active on another
1222 * processor before its pm_active field is checked but due to
1223 * speculative loads one of the following functions stills reads the
1224 * pmap as inactive on the other processor.
1226 * The kernel page table is exempt because its pm_active field is
1227 * immutable. The kernel page table is always active on every
1231 pmap_invalidate_page_int(pmap_t pmap, vm_offset_t va)
1233 cpuset_t *mask, other_cpus;
1237 if (pmap == kernel_pmap) {
1240 } else if (!CPU_CMP(&pmap->pm_active, &all_cpus)) {
1243 cpuid = PCPU_GET(cpuid);
1244 other_cpus = all_cpus;
1245 CPU_CLR(cpuid, &other_cpus);
1246 CPU_AND(&other_cpus, &pmap->pm_active);
1249 smp_masked_invlpg(*mask, va, pmap, pmap_curcpu_cb_dummy);
1253 /* 4k PTEs -- Chosen to exceed the total size of Broadwell L2 TLB */
1254 #define PMAP_INVLPG_THRESHOLD (4 * 1024 * PAGE_SIZE)
1257 pmap_invalidate_range_int(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
1259 cpuset_t *mask, other_cpus;
1263 if (eva - sva >= PMAP_INVLPG_THRESHOLD) {
1264 pmap_invalidate_all_int(pmap);
1269 if (pmap == kernel_pmap) {
1270 for (addr = sva; addr < eva; addr += PAGE_SIZE)
1273 } else if (!CPU_CMP(&pmap->pm_active, &all_cpus)) {
1276 cpuid = PCPU_GET(cpuid);
1277 other_cpus = all_cpus;
1278 CPU_CLR(cpuid, &other_cpus);
1279 CPU_AND(&other_cpus, &pmap->pm_active);
1282 smp_masked_invlpg_range(*mask, sva, eva, pmap, pmap_curcpu_cb_dummy);
1287 pmap_invalidate_all_int(pmap_t pmap)
1289 cpuset_t *mask, other_cpus;
1293 if (pmap == kernel_pmap) {
1296 } else if (!CPU_CMP(&pmap->pm_active, &all_cpus)) {
1299 cpuid = PCPU_GET(cpuid);
1300 other_cpus = all_cpus;
1301 CPU_CLR(cpuid, &other_cpus);
1302 CPU_AND(&other_cpus, &pmap->pm_active);
1305 smp_masked_invltlb(*mask, pmap, pmap_curcpu_cb_dummy);
1310 pmap_invalidate_cache_curcpu_cb(pmap_t pmap __unused,
1311 vm_offset_t addr1 __unused, vm_offset_t addr2 __unused)
1317 __CONCAT(PMTYPE, invalidate_cache)(void)
1319 smp_cache_flush(pmap_invalidate_cache_curcpu_cb);
1323 cpuset_t invalidate; /* processors that invalidate their TLB */
1327 u_int store; /* processor that updates the PDE */
1331 pmap_update_pde_kernel(void *arg)
1333 struct pde_action *act = arg;
1336 if (act->store == PCPU_GET(cpuid)) {
1337 pde = pmap_pde(kernel_pmap, act->va);
1338 pde_store(pde, act->newpde);
1343 pmap_update_pde_user(void *arg)
1345 struct pde_action *act = arg;
1347 if (act->store == PCPU_GET(cpuid))
1348 pde_store(act->pde, act->newpde);
1352 pmap_update_pde_teardown(void *arg)
1354 struct pde_action *act = arg;
1356 if (CPU_ISSET(PCPU_GET(cpuid), &act->invalidate))
1357 pmap_update_pde_invalidate(act->va, act->newpde);
1361 * Change the page size for the specified virtual address in a way that
1362 * prevents any possibility of the TLB ever having two entries that map the
1363 * same virtual address using different page sizes. This is the recommended
1364 * workaround for Erratum 383 on AMD Family 10h processors. It prevents a
1365 * machine check exception for a TLB state that is improperly diagnosed as a
1369 pmap_update_pde(pmap_t pmap, vm_offset_t va, pd_entry_t *pde, pd_entry_t newpde)
1371 struct pde_action act;
1372 cpuset_t active, other_cpus;
1376 cpuid = PCPU_GET(cpuid);
1377 other_cpus = all_cpus;
1378 CPU_CLR(cpuid, &other_cpus);
1379 if (pmap == kernel_pmap)
1382 active = pmap->pm_active;
1383 if (CPU_OVERLAP(&active, &other_cpus)) {
1385 act.invalidate = active;
1388 act.newpde = newpde;
1389 CPU_SET(cpuid, &active);
1390 smp_rendezvous_cpus(active,
1391 smp_no_rendezvous_barrier, pmap == kernel_pmap ?
1392 pmap_update_pde_kernel : pmap_update_pde_user,
1393 pmap_update_pde_teardown, &act);
1395 if (pmap == kernel_pmap)
1396 pmap_kenter_pde(va, newpde);
1398 pde_store(pde, newpde);
1399 if (CPU_ISSET(cpuid, &active))
1400 pmap_update_pde_invalidate(va, newpde);
1406 * Normal, non-SMP, 486+ invalidation functions.
1407 * We inline these within pmap.c for speed.
1410 pmap_invalidate_page_int(pmap_t pmap, vm_offset_t va)
1413 if (pmap == kernel_pmap)
1418 pmap_invalidate_range_int(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
1422 if (pmap == kernel_pmap)
1423 for (addr = sva; addr < eva; addr += PAGE_SIZE)
1428 pmap_invalidate_all_int(pmap_t pmap)
1431 if (pmap == kernel_pmap)
1436 __CONCAT(PMTYPE, invalidate_cache)(void)
1443 pmap_update_pde(pmap_t pmap, vm_offset_t va, pd_entry_t *pde, pd_entry_t newpde)
1446 if (pmap == kernel_pmap)
1447 pmap_kenter_pde(va, newpde);
1449 pde_store(pde, newpde);
1450 if (pmap == kernel_pmap || !CPU_EMPTY(&pmap->pm_active))
1451 pmap_update_pde_invalidate(va, newpde);
1456 __CONCAT(PMTYPE, invalidate_page)(pmap_t pmap, vm_offset_t va)
1459 pmap_invalidate_page_int(pmap, va);
1463 __CONCAT(PMTYPE, invalidate_range)(pmap_t pmap, vm_offset_t sva,
1467 pmap_invalidate_range_int(pmap, sva, eva);
1471 __CONCAT(PMTYPE, invalidate_all)(pmap_t pmap)
1474 pmap_invalidate_all_int(pmap);
1478 pmap_invalidate_pde_page(pmap_t pmap, vm_offset_t va, pd_entry_t pde)
1482 * When the PDE has PG_PROMOTED set, the 2- or 4MB page mapping was
1483 * created by a promotion that did not invalidate the 512 or 1024 4KB
1484 * page mappings that might exist in the TLB. Consequently, at this
1485 * point, the TLB may hold both 4KB and 2- or 4MB page mappings for
1486 * the address range [va, va + NBPDR). Therefore, the entire range
1487 * must be invalidated here. In contrast, when PG_PROMOTED is clear,
1488 * the TLB will not hold any 4KB page mappings for the address range
1489 * [va, va + NBPDR), and so a single INVLPG suffices to invalidate the
1490 * 2- or 4MB page mapping from the TLB.
1492 if ((pde & PG_PROMOTED) != 0)
1493 pmap_invalidate_range_int(pmap, va, va + NBPDR - 1);
1495 pmap_invalidate_page_int(pmap, va);
1499 * Are we current address space or kernel?
1502 pmap_is_current(pmap_t pmap)
1505 return (pmap == kernel_pmap);
1509 * If the given pmap is not the current or kernel pmap, the returned pte must
1510 * be released by passing it to pmap_pte_release().
1513 __CONCAT(PMTYPE, pte)(pmap_t pmap, vm_offset_t va)
1518 pde = pmap_pde(pmap, va);
1522 /* are we current address space or kernel? */
1523 if (pmap_is_current(pmap))
1524 return (vtopte(va));
1525 mtx_lock(&PMAP2mutex);
1526 newpf = *pde & PG_FRAME;
1527 if ((*PMAP2 & PG_FRAME) != newpf) {
1528 *PMAP2 = newpf | PG_RW | PG_V | PG_A | PG_M;
1529 pmap_invalidate_page_int(kernel_pmap,
1530 (vm_offset_t)PADDR2);
1532 return (PADDR2 + (i386_btop(va) & (NPTEPG - 1)));
1538 * Releases a pte that was obtained from pmap_pte(). Be prepared for the pte
1541 static __inline void
1542 pmap_pte_release(pt_entry_t *pte)
1545 if ((pt_entry_t *)((vm_offset_t)pte & ~PAGE_MASK) == PADDR2)
1546 mtx_unlock(&PMAP2mutex);
1550 * NB: The sequence of updating a page table followed by accesses to the
1551 * corresponding pages is subject to the situation described in the "AMD64
1552 * Architecture Programmer's Manual Volume 2: System Programming" rev. 3.23,
1553 * "7.3.1 Special Coherency Considerations". Therefore, issuing the INVLPG
1554 * right after modifying the PTE bits is crucial.
1556 static __inline void
1557 invlcaddr(void *caddr)
1560 invlpg((u_int)caddr);
1564 * Super fast pmap_pte routine best used when scanning
1565 * the pv lists. This eliminates many coarse-grained
1566 * invltlb calls. Note that many of the pv list
1567 * scans are across different pmaps. It is very wasteful
1568 * to do an entire invltlb for checking a single mapping.
1570 * If the given pmap is not the current pmap, pvh_global_lock
1571 * must be held and curthread pinned to a CPU.
1574 pmap_pte_quick(pmap_t pmap, vm_offset_t va)
1579 pde = pmap_pde(pmap, va);
1583 /* are we current address space or kernel? */
1584 if (pmap_is_current(pmap))
1585 return (vtopte(va));
1586 rw_assert(&pvh_global_lock, RA_WLOCKED);
1587 KASSERT(curthread->td_pinned > 0, ("curthread not pinned"));
1588 newpf = *pde & PG_FRAME;
1589 if ((*PMAP1 & PG_FRAME) != newpf) {
1590 *PMAP1 = newpf | PG_RW | PG_V | PG_A | PG_M;
1592 PMAP1cpu = PCPU_GET(cpuid);
1598 if (PMAP1cpu != PCPU_GET(cpuid)) {
1599 PMAP1cpu = PCPU_GET(cpuid);
1605 return (PADDR1 + (i386_btop(va) & (NPTEPG - 1)));
1611 pmap_pte_quick3(pmap_t pmap, vm_offset_t va)
1616 pde = pmap_pde(pmap, va);
1620 rw_assert(&pvh_global_lock, RA_WLOCKED);
1621 KASSERT(curthread->td_pinned > 0, ("curthread not pinned"));
1622 newpf = *pde & PG_FRAME;
1623 if ((*PMAP3 & PG_FRAME) != newpf) {
1624 *PMAP3 = newpf | PG_RW | PG_V | PG_A | PG_M;
1626 PMAP3cpu = PCPU_GET(cpuid);
1632 if (PMAP3cpu != PCPU_GET(cpuid)) {
1633 PMAP3cpu = PCPU_GET(cpuid);
1639 return (PADDR3 + (i386_btop(va) & (NPTEPG - 1)));
1645 pmap_pte_ufast(pmap_t pmap, vm_offset_t va, pd_entry_t pde)
1647 pt_entry_t *eh_ptep, pte, *ptep;
1649 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1652 eh_ptep = (pt_entry_t *)PCPU_GET(pmap_eh_ptep);
1653 if ((*eh_ptep & PG_FRAME) != pde) {
1654 *eh_ptep = pde | PG_RW | PG_V | PG_A | PG_M;
1655 invlcaddr((void *)PCPU_GET(pmap_eh_va));
1657 ptep = (pt_entry_t *)PCPU_GET(pmap_eh_va) + (i386_btop(va) &
1665 * Extract from the kernel page table the physical address that is mapped by
1666 * the given virtual address "va".
1668 * This function may be used before pmap_bootstrap() is called.
1671 __CONCAT(PMTYPE, kextract)(vm_offset_t va)
1675 if ((pa = pte_load(&PTD[va >> PDRSHIFT])) & PG_PS) {
1676 pa = (pa & PG_PS_FRAME) | (va & PDRMASK);
1679 * Beware of a concurrent promotion that changes the PDE at
1680 * this point! For example, vtopte() must not be used to
1681 * access the PTE because it would use the new PDE. It is,
1682 * however, safe to use the old PDE because the page table
1683 * page is preserved by the promotion.
1685 pa = KPTmap[i386_btop(va)];
1686 pa = (pa & PG_FRAME) | (va & PAGE_MASK);
1692 * Routine: pmap_extract
1694 * Extract the physical page address associated
1695 * with the given map/virtual_address pair.
1698 __CONCAT(PMTYPE, extract)(pmap_t pmap, vm_offset_t va)
1706 pde = pmap->pm_pdir[va >> PDRSHIFT];
1708 if ((pde & PG_PS) != 0)
1709 rtval = (pde & PG_PS_FRAME) | (va & PDRMASK);
1711 pte = pmap_pte_ufast(pmap, va, pde);
1712 rtval = (pte & PG_FRAME) | (va & PAGE_MASK);
1720 * Routine: pmap_extract_and_hold
1722 * Atomically extract and hold the physical page
1723 * with the given pmap and virtual address pair
1724 * if that mapping permits the given protection.
1727 __CONCAT(PMTYPE, extract_and_hold)(pmap_t pmap, vm_offset_t va, vm_prot_t prot)
1735 pde = *pmap_pde(pmap, va);
1738 if ((pde & PG_RW) || (prot & VM_PROT_WRITE) == 0)
1739 m = PHYS_TO_VM_PAGE((pde & PG_PS_FRAME) |
1742 pte = pmap_pte_ufast(pmap, va, pde);
1744 ((pte & PG_RW) || (prot & VM_PROT_WRITE) == 0))
1745 m = PHYS_TO_VM_PAGE(pte & PG_FRAME);
1747 if (m != NULL && !vm_page_wire_mapped(m))
1754 /***************************************************
1755 * Low level mapping routines.....
1756 ***************************************************/
1759 * Add a wired page to the kva.
1760 * Note: not SMP coherent.
1762 * This function may be used before pmap_bootstrap() is called.
1765 __CONCAT(PMTYPE, kenter)(vm_offset_t va, vm_paddr_t pa)
1770 pte_store(pte, pa | PG_RW | PG_V);
1773 static __inline void
1774 pmap_kenter_attr(vm_offset_t va, vm_paddr_t pa, int mode)
1779 pte_store(pte, pa | PG_RW | PG_V | pmap_cache_bits(kernel_pmap,
1784 * Remove a page from the kernel pagetables.
1785 * Note: not SMP coherent.
1787 * This function may be used before pmap_bootstrap() is called.
1790 __CONCAT(PMTYPE, kremove)(vm_offset_t va)
1799 * Used to map a range of physical addresses into kernel
1800 * virtual address space.
1802 * The value passed in '*virt' is a suggested virtual address for
1803 * the mapping. Architectures which can support a direct-mapped
1804 * physical to virtual region can return the appropriate address
1805 * within that region, leaving '*virt' unchanged. Other
1806 * architectures should map the pages starting at '*virt' and
1807 * update '*virt' with the first usable address after the mapped
1811 __CONCAT(PMTYPE, map)(vm_offset_t *virt, vm_paddr_t start, vm_paddr_t end,
1814 vm_offset_t va, sva;
1815 vm_paddr_t superpage_offset;
1820 * Does the physical address range's size and alignment permit at
1821 * least one superpage mapping to be created?
1823 superpage_offset = start & PDRMASK;
1824 if ((end - start) - ((NBPDR - superpage_offset) & PDRMASK) >= NBPDR) {
1826 * Increase the starting virtual address so that its alignment
1827 * does not preclude the use of superpage mappings.
1829 if ((va & PDRMASK) < superpage_offset)
1830 va = (va & ~PDRMASK) + superpage_offset;
1831 else if ((va & PDRMASK) > superpage_offset)
1832 va = ((va + PDRMASK) & ~PDRMASK) + superpage_offset;
1835 while (start < end) {
1836 if ((start & PDRMASK) == 0 && end - start >= NBPDR &&
1838 KASSERT((va & PDRMASK) == 0,
1839 ("pmap_map: misaligned va %#x", va));
1840 newpde = start | PG_PS | PG_RW | PG_V;
1841 pmap_kenter_pde(va, newpde);
1845 pmap_kenter(va, start);
1850 pmap_invalidate_range_int(kernel_pmap, sva, va);
1856 * Add a list of wired pages to the kva
1857 * this routine is only used for temporary
1858 * kernel mappings that do not need to have
1859 * page modification or references recorded.
1860 * Note that old mappings are simply written
1861 * over. The page *must* be wired.
1862 * Note: SMP coherent. Uses a ranged shootdown IPI.
1865 __CONCAT(PMTYPE, qenter)(vm_offset_t sva, vm_page_t *ma, int count)
1867 pt_entry_t *endpte, oldpte, pa, *pte;
1872 endpte = pte + count;
1873 while (pte < endpte) {
1875 pa = VM_PAGE_TO_PHYS(m) | pmap_cache_bits(kernel_pmap,
1877 if ((*pte & (PG_FRAME | PG_PTE_CACHE)) != pa) {
1879 pte_store(pte, pa | pg_nx | PG_RW | PG_V);
1883 if (__predict_false((oldpte & PG_V) != 0))
1884 pmap_invalidate_range_int(kernel_pmap, sva, sva + count *
1889 * This routine tears out page mappings from the
1890 * kernel -- it is meant only for temporary mappings.
1891 * Note: SMP coherent. Uses a ranged shootdown IPI.
1894 __CONCAT(PMTYPE, qremove)(vm_offset_t sva, int count)
1899 while (count-- > 0) {
1903 pmap_invalidate_range_int(kernel_pmap, sva, va);
1906 /***************************************************
1907 * Page table page management routines.....
1908 ***************************************************/
1910 * Schedule the specified unused page table page to be freed. Specifically,
1911 * add the page to the specified list of pages that will be released to the
1912 * physical memory manager after the TLB has been updated.
1914 static __inline void
1915 pmap_add_delayed_free_list(vm_page_t m, struct spglist *free,
1916 boolean_t set_PG_ZERO)
1920 m->flags |= PG_ZERO;
1922 m->flags &= ~PG_ZERO;
1923 SLIST_INSERT_HEAD(free, m, plinks.s.ss);
1927 * Inserts the specified page table page into the specified pmap's collection
1928 * of idle page table pages. Each of a pmap's page table pages is responsible
1929 * for mapping a distinct range of virtual addresses. The pmap's collection is
1930 * ordered by this virtual address range.
1932 * If "promoted" is false, then the page table page "mpte" must be zero filled.
1935 pmap_insert_pt_page(pmap_t pmap, vm_page_t mpte, bool promoted)
1938 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1939 mpte->valid = promoted ? VM_PAGE_BITS_ALL : 0;
1940 return (vm_radix_insert(&pmap->pm_root, mpte));
1944 * Removes the page table page mapping the specified virtual address from the
1945 * specified pmap's collection of idle page table pages, and returns it.
1946 * Otherwise, returns NULL if there is no page table page corresponding to the
1947 * specified virtual address.
1949 static __inline vm_page_t
1950 pmap_remove_pt_page(pmap_t pmap, vm_offset_t va)
1953 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1954 return (vm_radix_remove(&pmap->pm_root, va >> PDRSHIFT));
1958 * Decrements a page table page's reference count, which is used to record the
1959 * number of valid page table entries within the page. If the reference count
1960 * drops to zero, then the page table page is unmapped. Returns TRUE if the
1961 * page table page was unmapped and FALSE otherwise.
1963 static inline boolean_t
1964 pmap_unwire_ptp(pmap_t pmap, vm_page_t m, struct spglist *free)
1968 if (m->ref_count == 0) {
1969 _pmap_unwire_ptp(pmap, m, free);
1976 _pmap_unwire_ptp(pmap_t pmap, vm_page_t m, struct spglist *free)
1980 * unmap the page table page
1982 pmap->pm_pdir[m->pindex] = 0;
1983 --pmap->pm_stats.resident_count;
1986 * There is not need to invalidate the recursive mapping since
1987 * we never instantiate such mapping for the usermode pmaps,
1988 * and never remove page table pages from the kernel pmap.
1989 * Put page on a list so that it is released since all TLB
1990 * shootdown is done.
1992 MPASS(pmap != kernel_pmap);
1993 pmap_add_delayed_free_list(m, free, TRUE);
1997 * After removing a page table entry, this routine is used to
1998 * conditionally free the page, and manage the reference count.
2001 pmap_unuse_pt(pmap_t pmap, vm_offset_t va, struct spglist *free)
2006 if (pmap == kernel_pmap)
2008 ptepde = *pmap_pde(pmap, va);
2009 mpte = PHYS_TO_VM_PAGE(ptepde & PG_FRAME);
2010 return (pmap_unwire_ptp(pmap, mpte, free));
2014 * Release a page table page reference after a failed attempt to create a
2018 pmap_abort_ptp(pmap_t pmap, vm_offset_t va, vm_page_t mpte)
2020 struct spglist free;
2023 if (pmap_unwire_ptp(pmap, mpte, &free)) {
2025 * Although "va" was never mapped, paging-structure caches
2026 * could nonetheless have entries that refer to the freed
2027 * page table pages. Invalidate those entries.
2029 pmap_invalidate_page_int(pmap, va);
2030 vm_page_free_pages_toq(&free, true);
2035 * Initialize the pmap for the swapper process.
2038 __CONCAT(PMTYPE, pinit0)(pmap_t pmap)
2041 PMAP_LOCK_INIT(pmap);
2042 pmap->pm_pdir = IdlePTD;
2043 #ifdef PMAP_PAE_COMP
2044 pmap->pm_pdpt = IdlePDPT;
2046 pmap->pm_root.rt_root = 0;
2047 CPU_ZERO(&pmap->pm_active);
2048 TAILQ_INIT(&pmap->pm_pvchunk);
2049 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
2050 pmap_activate_boot(pmap);
2054 * Initialize a preallocated and zeroed pmap structure,
2055 * such as one in a vmspace structure.
2058 __CONCAT(PMTYPE, pinit)(pmap_t pmap)
2064 * No need to allocate page table space yet but we do need a valid
2065 * page directory table.
2067 if (pmap->pm_pdir == NULL) {
2068 pmap->pm_pdir = (pd_entry_t *)kva_alloc(NBPTD);
2069 if (pmap->pm_pdir == NULL)
2071 #ifdef PMAP_PAE_COMP
2072 pmap->pm_pdpt = uma_zalloc(pdptzone, M_WAITOK | M_ZERO);
2073 KASSERT(((vm_offset_t)pmap->pm_pdpt &
2074 ((NPGPTD * sizeof(pdpt_entry_t)) - 1)) == 0,
2075 ("pmap_pinit: pdpt misaligned"));
2076 KASSERT(pmap_kextract((vm_offset_t)pmap->pm_pdpt) < (4ULL<<30),
2077 ("pmap_pinit: pdpt above 4g"));
2079 pmap->pm_root.rt_root = 0;
2081 KASSERT(vm_radix_is_empty(&pmap->pm_root),
2082 ("pmap_pinit: pmap has reserved page table page(s)"));
2085 * allocate the page directory page(s)
2087 for (i = 0; i < NPGPTD; i++) {
2088 m = vm_page_alloc(NULL, 0, VM_ALLOC_NORMAL | VM_ALLOC_NOOBJ |
2089 VM_ALLOC_WIRED | VM_ALLOC_ZERO | VM_ALLOC_WAITOK);
2090 pmap->pm_ptdpg[i] = m;
2091 #ifdef PMAP_PAE_COMP
2092 pmap->pm_pdpt[i] = VM_PAGE_TO_PHYS(m) | PG_V;
2096 pmap_qenter((vm_offset_t)pmap->pm_pdir, pmap->pm_ptdpg, NPGPTD);
2097 #ifdef PMAP_PAE_COMP
2098 if ((cpu_feature & CPUID_PAT) == 0) {
2099 pmap_invalidate_cache_range(
2100 trunc_page((vm_offset_t)pmap->pm_pdpt),
2101 round_page((vm_offset_t)pmap->pm_pdpt +
2102 NPGPTD * sizeof(pdpt_entry_t)));
2106 for (i = 0; i < NPGPTD; i++)
2107 if ((pmap->pm_ptdpg[i]->flags & PG_ZERO) == 0)
2108 pagezero(pmap->pm_pdir + (i * NPDEPG));
2110 /* Install the trampoline mapping. */
2111 pmap->pm_pdir[TRPTDI] = PTD[TRPTDI];
2113 CPU_ZERO(&pmap->pm_active);
2114 TAILQ_INIT(&pmap->pm_pvchunk);
2115 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
2121 * this routine is called if the page table page is not
2125 _pmap_allocpte(pmap_t pmap, u_int ptepindex, u_int flags)
2131 * Allocate a page table page.
2133 if ((m = vm_page_alloc(NULL, ptepindex, VM_ALLOC_NOOBJ |
2134 VM_ALLOC_WIRED | VM_ALLOC_ZERO)) == NULL) {
2135 if ((flags & PMAP_ENTER_NOSLEEP) == 0) {
2137 rw_wunlock(&pvh_global_lock);
2139 rw_wlock(&pvh_global_lock);
2144 * Indicate the need to retry. While waiting, the page table
2145 * page may have been allocated.
2149 if ((m->flags & PG_ZERO) == 0)
2153 * Map the pagetable page into the process address space, if
2154 * it isn't already there.
2157 pmap->pm_stats.resident_count++;
2159 ptepa = VM_PAGE_TO_PHYS(m);
2160 pmap->pm_pdir[ptepindex] =
2161 (pd_entry_t) (ptepa | PG_U | PG_RW | PG_V | PG_A | PG_M);
2167 pmap_allocpte(pmap_t pmap, vm_offset_t va, u_int flags)
2174 * Calculate pagetable page index
2176 ptepindex = va >> PDRSHIFT;
2179 * Get the page directory entry
2181 ptepa = pmap->pm_pdir[ptepindex];
2184 * This supports switching from a 4MB page to a
2187 if (ptepa & PG_PS) {
2188 (void)pmap_demote_pde(pmap, &pmap->pm_pdir[ptepindex], va);
2189 ptepa = pmap->pm_pdir[ptepindex];
2193 * If the page table page is mapped, we just increment the
2194 * hold count, and activate it.
2197 m = PHYS_TO_VM_PAGE(ptepa & PG_FRAME);
2201 * Here if the pte page isn't mapped, or if it has
2204 m = _pmap_allocpte(pmap, ptepindex, flags);
2205 if (m == NULL && (flags & PMAP_ENTER_NOSLEEP) == 0)
2211 /***************************************************
2212 * Pmap allocation/deallocation routines.
2213 ***************************************************/
2216 * Release any resources held by the given physical map.
2217 * Called when a pmap initialized by pmap_pinit is being released.
2218 * Should only be called if the map contains no valid mappings.
2221 __CONCAT(PMTYPE, release)(pmap_t pmap)
2226 KASSERT(pmap->pm_stats.resident_count == 0,
2227 ("pmap_release: pmap resident count %ld != 0",
2228 pmap->pm_stats.resident_count));
2229 KASSERT(vm_radix_is_empty(&pmap->pm_root),
2230 ("pmap_release: pmap has reserved page table page(s)"));
2231 KASSERT(CPU_EMPTY(&pmap->pm_active),
2232 ("releasing active pmap %p", pmap));
2234 pmap_qremove((vm_offset_t)pmap->pm_pdir, NPGPTD);
2236 for (i = 0; i < NPGPTD; i++) {
2237 m = pmap->pm_ptdpg[i];
2238 #ifdef PMAP_PAE_COMP
2239 KASSERT(VM_PAGE_TO_PHYS(m) == (pmap->pm_pdpt[i] & PG_FRAME),
2240 ("pmap_release: got wrong ptd page"));
2242 vm_page_unwire_noq(m);
2248 * grow the number of kernel page table entries, if needed
2251 __CONCAT(PMTYPE, growkernel)(vm_offset_t addr)
2253 vm_paddr_t ptppaddr;
2257 mtx_assert(&kernel_map->system_mtx, MA_OWNED);
2258 addr = roundup2(addr, NBPDR);
2259 if (addr - 1 >= vm_map_max(kernel_map))
2260 addr = vm_map_max(kernel_map);
2261 while (kernel_vm_end < addr) {
2262 if (pdir_pde(PTD, kernel_vm_end)) {
2263 kernel_vm_end = (kernel_vm_end + NBPDR) & ~PDRMASK;
2264 if (kernel_vm_end - 1 >= vm_map_max(kernel_map)) {
2265 kernel_vm_end = vm_map_max(kernel_map);
2271 nkpg = vm_page_alloc(NULL, kernel_vm_end >> PDRSHIFT,
2272 VM_ALLOC_INTERRUPT | VM_ALLOC_NOOBJ | VM_ALLOC_WIRED |
2275 panic("pmap_growkernel: no memory to grow kernel");
2279 if ((nkpg->flags & PG_ZERO) == 0)
2280 pmap_zero_page(nkpg);
2281 ptppaddr = VM_PAGE_TO_PHYS(nkpg);
2282 newpdir = (pd_entry_t) (ptppaddr | PG_V | PG_RW | PG_A | PG_M);
2283 pdir_pde(KPTD, kernel_vm_end) = newpdir;
2285 pmap_kenter_pde(kernel_vm_end, newpdir);
2286 kernel_vm_end = (kernel_vm_end + NBPDR) & ~PDRMASK;
2287 if (kernel_vm_end - 1 >= vm_map_max(kernel_map)) {
2288 kernel_vm_end = vm_map_max(kernel_map);
2294 /***************************************************
2295 * page management routines.
2296 ***************************************************/
2298 CTASSERT(sizeof(struct pv_chunk) == PAGE_SIZE);
2299 CTASSERT(_NPCM == 11);
2300 CTASSERT(_NPCPV == 336);
2302 static __inline struct pv_chunk *
2303 pv_to_chunk(pv_entry_t pv)
2306 return ((struct pv_chunk *)((uintptr_t)pv & ~(uintptr_t)PAGE_MASK));
2309 #define PV_PMAP(pv) (pv_to_chunk(pv)->pc_pmap)
2311 #define PC_FREE0_9 0xfffffffful /* Free values for index 0 through 9 */
2312 #define PC_FREE10 0x0000fffful /* Free values for index 10 */
2314 static const uint32_t pc_freemask[_NPCM] = {
2315 PC_FREE0_9, PC_FREE0_9, PC_FREE0_9,
2316 PC_FREE0_9, PC_FREE0_9, PC_FREE0_9,
2317 PC_FREE0_9, PC_FREE0_9, PC_FREE0_9,
2318 PC_FREE0_9, PC_FREE10
2322 extern int pc_chunk_count, pc_chunk_allocs, pc_chunk_frees, pc_chunk_tryfail;
2323 extern long pv_entry_frees, pv_entry_allocs;
2324 extern int pv_entry_spare;
2328 * We are in a serious low memory condition. Resort to
2329 * drastic measures to free some pages so we can allocate
2330 * another pv entry chunk.
2333 pmap_pv_reclaim(pmap_t locked_pmap)
2336 struct pv_chunk *pc;
2337 struct md_page *pvh;
2340 pt_entry_t *pte, tpte;
2344 struct spglist free;
2346 int bit, field, freed;
2348 PMAP_LOCK_ASSERT(locked_pmap, MA_OWNED);
2352 TAILQ_INIT(&newtail);
2353 while ((pc = TAILQ_FIRST(&pv_chunks)) != NULL && (pv_vafree == 0 ||
2354 SLIST_EMPTY(&free))) {
2355 TAILQ_REMOVE(&pv_chunks, pc, pc_lru);
2356 if (pmap != pc->pc_pmap) {
2358 pmap_invalidate_all_int(pmap);
2359 if (pmap != locked_pmap)
2363 /* Avoid deadlock and lock recursion. */
2364 if (pmap > locked_pmap)
2366 else if (pmap != locked_pmap && !PMAP_TRYLOCK(pmap)) {
2368 TAILQ_INSERT_TAIL(&newtail, pc, pc_lru);
2374 * Destroy every non-wired, 4 KB page mapping in the chunk.
2377 for (field = 0; field < _NPCM; field++) {
2378 for (inuse = ~pc->pc_map[field] & pc_freemask[field];
2379 inuse != 0; inuse &= ~(1UL << bit)) {
2381 pv = &pc->pc_pventry[field * 32 + bit];
2383 pde = pmap_pde(pmap, va);
2384 if ((*pde & PG_PS) != 0)
2386 pte = __CONCAT(PMTYPE, pte)(pmap, va);
2388 if ((tpte & PG_W) == 0)
2389 tpte = pte_load_clear(pte);
2390 pmap_pte_release(pte);
2391 if ((tpte & PG_W) != 0)
2394 ("pmap_pv_reclaim: pmap %p va %x zero pte",
2396 if ((tpte & PG_G) != 0)
2397 pmap_invalidate_page_int(pmap, va);
2398 m = PHYS_TO_VM_PAGE(tpte & PG_FRAME);
2399 if ((tpte & (PG_M | PG_RW)) == (PG_M | PG_RW))
2401 if ((tpte & PG_A) != 0)
2402 vm_page_aflag_set(m, PGA_REFERENCED);
2403 TAILQ_REMOVE(&m->md.pv_list, pv, pv_next);
2404 if (TAILQ_EMPTY(&m->md.pv_list) &&
2405 (m->flags & PG_FICTITIOUS) == 0) {
2406 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
2407 if (TAILQ_EMPTY(&pvh->pv_list)) {
2408 vm_page_aflag_clear(m,
2412 pc->pc_map[field] |= 1UL << bit;
2413 pmap_unuse_pt(pmap, va, &free);
2418 TAILQ_INSERT_TAIL(&newtail, pc, pc_lru);
2421 /* Every freed mapping is for a 4 KB page. */
2422 pmap->pm_stats.resident_count -= freed;
2423 PV_STAT(pv_entry_frees += freed);
2424 PV_STAT(pv_entry_spare += freed);
2425 pv_entry_count -= freed;
2426 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
2427 for (field = 0; field < _NPCM; field++)
2428 if (pc->pc_map[field] != pc_freemask[field]) {
2429 TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc,
2431 TAILQ_INSERT_TAIL(&newtail, pc, pc_lru);
2434 * One freed pv entry in locked_pmap is
2437 if (pmap == locked_pmap)
2441 if (field == _NPCM) {
2442 PV_STAT(pv_entry_spare -= _NPCPV);
2443 PV_STAT(pc_chunk_count--);
2444 PV_STAT(pc_chunk_frees++);
2445 /* Entire chunk is free; return it. */
2446 m_pc = PHYS_TO_VM_PAGE(pmap_kextract((vm_offset_t)pc));
2447 pmap_qremove((vm_offset_t)pc, 1);
2448 pmap_ptelist_free(&pv_vafree, (vm_offset_t)pc);
2453 TAILQ_CONCAT(&pv_chunks, &newtail, pc_lru);
2455 pmap_invalidate_all_int(pmap);
2456 if (pmap != locked_pmap)
2459 if (m_pc == NULL && pv_vafree != 0 && SLIST_EMPTY(&free)) {
2460 m_pc = SLIST_FIRST(&free);
2461 SLIST_REMOVE_HEAD(&free, plinks.s.ss);
2462 /* Recycle a freed page table page. */
2463 m_pc->ref_count = 1;
2465 vm_page_free_pages_toq(&free, true);
2470 * free the pv_entry back to the free list
2473 free_pv_entry(pmap_t pmap, pv_entry_t pv)
2475 struct pv_chunk *pc;
2476 int idx, field, bit;
2478 rw_assert(&pvh_global_lock, RA_WLOCKED);
2479 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2480 PV_STAT(pv_entry_frees++);
2481 PV_STAT(pv_entry_spare++);
2483 pc = pv_to_chunk(pv);
2484 idx = pv - &pc->pc_pventry[0];
2487 pc->pc_map[field] |= 1ul << bit;
2488 for (idx = 0; idx < _NPCM; idx++)
2489 if (pc->pc_map[idx] != pc_freemask[idx]) {
2491 * 98% of the time, pc is already at the head of the
2492 * list. If it isn't already, move it to the head.
2494 if (__predict_false(TAILQ_FIRST(&pmap->pm_pvchunk) !=
2496 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
2497 TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc,
2502 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
2507 free_pv_chunk(struct pv_chunk *pc)
2511 TAILQ_REMOVE(&pv_chunks, pc, pc_lru);
2512 PV_STAT(pv_entry_spare -= _NPCPV);
2513 PV_STAT(pc_chunk_count--);
2514 PV_STAT(pc_chunk_frees++);
2515 /* entire chunk is free, return it */
2516 m = PHYS_TO_VM_PAGE(pmap_kextract((vm_offset_t)pc));
2517 pmap_qremove((vm_offset_t)pc, 1);
2518 vm_page_unwire_noq(m);
2520 pmap_ptelist_free(&pv_vafree, (vm_offset_t)pc);
2524 * get a new pv_entry, allocating a block from the system
2528 get_pv_entry(pmap_t pmap, boolean_t try)
2530 static const struct timeval printinterval = { 60, 0 };
2531 static struct timeval lastprint;
2534 struct pv_chunk *pc;
2537 rw_assert(&pvh_global_lock, RA_WLOCKED);
2538 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2539 PV_STAT(pv_entry_allocs++);
2541 if (pv_entry_count > pv_entry_high_water)
2542 if (ratecheck(&lastprint, &printinterval))
2543 printf("Approaching the limit on PV entries, consider "
2544 "increasing either the vm.pmap.shpgperproc or the "
2545 "vm.pmap.pv_entries tunable.\n");
2547 pc = TAILQ_FIRST(&pmap->pm_pvchunk);
2549 for (field = 0; field < _NPCM; field++) {
2550 if (pc->pc_map[field]) {
2551 bit = bsfl(pc->pc_map[field]);
2555 if (field < _NPCM) {
2556 pv = &pc->pc_pventry[field * 32 + bit];
2557 pc->pc_map[field] &= ~(1ul << bit);
2558 /* If this was the last item, move it to tail */
2559 for (field = 0; field < _NPCM; field++)
2560 if (pc->pc_map[field] != 0) {
2561 PV_STAT(pv_entry_spare--);
2562 return (pv); /* not full, return */
2564 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
2565 TAILQ_INSERT_TAIL(&pmap->pm_pvchunk, pc, pc_list);
2566 PV_STAT(pv_entry_spare--);
2571 * Access to the ptelist "pv_vafree" is synchronized by the pvh
2572 * global lock. If "pv_vafree" is currently non-empty, it will
2573 * remain non-empty until pmap_ptelist_alloc() completes.
2575 if (pv_vafree == 0 || (m = vm_page_alloc(NULL, 0, VM_ALLOC_NORMAL |
2576 VM_ALLOC_NOOBJ | VM_ALLOC_WIRED)) == NULL) {
2579 PV_STAT(pc_chunk_tryfail++);
2582 m = pmap_pv_reclaim(pmap);
2586 PV_STAT(pc_chunk_count++);
2587 PV_STAT(pc_chunk_allocs++);
2588 pc = (struct pv_chunk *)pmap_ptelist_alloc(&pv_vafree);
2589 pmap_qenter((vm_offset_t)pc, &m, 1);
2591 pc->pc_map[0] = pc_freemask[0] & ~1ul; /* preallocated bit 0 */
2592 for (field = 1; field < _NPCM; field++)
2593 pc->pc_map[field] = pc_freemask[field];
2594 TAILQ_INSERT_TAIL(&pv_chunks, pc, pc_lru);
2595 pv = &pc->pc_pventry[0];
2596 TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc, pc_list);
2597 PV_STAT(pv_entry_spare += _NPCPV - 1);
2601 static __inline pv_entry_t
2602 pmap_pvh_remove(struct md_page *pvh, pmap_t pmap, vm_offset_t va)
2606 rw_assert(&pvh_global_lock, RA_WLOCKED);
2607 TAILQ_FOREACH(pv, &pvh->pv_list, pv_next) {
2608 if (pmap == PV_PMAP(pv) && va == pv->pv_va) {
2609 TAILQ_REMOVE(&pvh->pv_list, pv, pv_next);
2617 pmap_pv_demote_pde(pmap_t pmap, vm_offset_t va, vm_paddr_t pa)
2619 struct md_page *pvh;
2621 vm_offset_t va_last;
2624 rw_assert(&pvh_global_lock, RA_WLOCKED);
2625 KASSERT((pa & PDRMASK) == 0,
2626 ("pmap_pv_demote_pde: pa is not 4mpage aligned"));
2629 * Transfer the 4mpage's pv entry for this mapping to the first
2632 pvh = pa_to_pvh(pa);
2633 va = trunc_4mpage(va);
2634 pv = pmap_pvh_remove(pvh, pmap, va);
2635 KASSERT(pv != NULL, ("pmap_pv_demote_pde: pv not found"));
2636 m = PHYS_TO_VM_PAGE(pa);
2637 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_next);
2638 /* Instantiate the remaining NPTEPG - 1 pv entries. */
2639 va_last = va + NBPDR - PAGE_SIZE;
2642 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2643 ("pmap_pv_demote_pde: page %p is not managed", m));
2645 pmap_insert_entry(pmap, va, m);
2646 } while (va < va_last);
2649 #if VM_NRESERVLEVEL > 0
2651 pmap_pv_promote_pde(pmap_t pmap, vm_offset_t va, vm_paddr_t pa)
2653 struct md_page *pvh;
2655 vm_offset_t va_last;
2658 rw_assert(&pvh_global_lock, RA_WLOCKED);
2659 KASSERT((pa & PDRMASK) == 0,
2660 ("pmap_pv_promote_pde: pa is not 4mpage aligned"));
2663 * Transfer the first page's pv entry for this mapping to the
2664 * 4mpage's pv list. Aside from avoiding the cost of a call
2665 * to get_pv_entry(), a transfer avoids the possibility that
2666 * get_pv_entry() calls pmap_collect() and that pmap_collect()
2667 * removes one of the mappings that is being promoted.
2669 m = PHYS_TO_VM_PAGE(pa);
2670 va = trunc_4mpage(va);
2671 pv = pmap_pvh_remove(&m->md, pmap, va);
2672 KASSERT(pv != NULL, ("pmap_pv_promote_pde: pv not found"));
2673 pvh = pa_to_pvh(pa);
2674 TAILQ_INSERT_TAIL(&pvh->pv_list, pv, pv_next);
2675 /* Free the remaining NPTEPG - 1 pv entries. */
2676 va_last = va + NBPDR - PAGE_SIZE;
2680 pmap_pvh_free(&m->md, pmap, va);
2681 } while (va < va_last);
2683 #endif /* VM_NRESERVLEVEL > 0 */
2686 pmap_pvh_free(struct md_page *pvh, pmap_t pmap, vm_offset_t va)
2690 pv = pmap_pvh_remove(pvh, pmap, va);
2691 KASSERT(pv != NULL, ("pmap_pvh_free: pv not found"));
2692 free_pv_entry(pmap, pv);
2696 pmap_remove_entry(pmap_t pmap, vm_page_t m, vm_offset_t va)
2698 struct md_page *pvh;
2700 rw_assert(&pvh_global_lock, RA_WLOCKED);
2701 pmap_pvh_free(&m->md, pmap, va);
2702 if (TAILQ_EMPTY(&m->md.pv_list) && (m->flags & PG_FICTITIOUS) == 0) {
2703 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
2704 if (TAILQ_EMPTY(&pvh->pv_list))
2705 vm_page_aflag_clear(m, PGA_WRITEABLE);
2710 * Create a pv entry for page at pa for
2714 pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t m)
2718 rw_assert(&pvh_global_lock, RA_WLOCKED);
2719 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2720 pv = get_pv_entry(pmap, FALSE);
2722 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_next);
2726 * Conditionally create a pv entry.
2729 pmap_try_insert_pv_entry(pmap_t pmap, vm_offset_t va, vm_page_t m)
2733 rw_assert(&pvh_global_lock, RA_WLOCKED);
2734 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2735 if (pv_entry_count < pv_entry_high_water &&
2736 (pv = get_pv_entry(pmap, TRUE)) != NULL) {
2738 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_next);
2745 * Create the pv entries for each of the pages within a superpage.
2748 pmap_pv_insert_pde(pmap_t pmap, vm_offset_t va, pd_entry_t pde, u_int flags)
2750 struct md_page *pvh;
2754 rw_assert(&pvh_global_lock, RA_WLOCKED);
2755 noreclaim = (flags & PMAP_ENTER_NORECLAIM) != 0;
2756 if ((noreclaim && pv_entry_count >= pv_entry_high_water) ||
2757 (pv = get_pv_entry(pmap, noreclaim)) == NULL)
2760 pvh = pa_to_pvh(pde & PG_PS_FRAME);
2761 TAILQ_INSERT_TAIL(&pvh->pv_list, pv, pv_next);
2766 * Fills a page table page with mappings to consecutive physical pages.
2769 pmap_fill_ptp(pt_entry_t *firstpte, pt_entry_t newpte)
2773 for (pte = firstpte; pte < firstpte + NPTEPG; pte++) {
2775 newpte += PAGE_SIZE;
2780 * Tries to demote a 2- or 4MB page mapping. If demotion fails, the
2781 * 2- or 4MB page mapping is invalidated.
2784 pmap_demote_pde(pmap_t pmap, pd_entry_t *pde, vm_offset_t va)
2786 pd_entry_t newpde, oldpde;
2787 pt_entry_t *firstpte, newpte;
2790 struct spglist free;
2793 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2795 KASSERT((oldpde & (PG_PS | PG_V)) == (PG_PS | PG_V),
2796 ("pmap_demote_pde: oldpde is missing PG_PS and/or PG_V"));
2797 if ((oldpde & PG_A) == 0 || (mpte = pmap_remove_pt_page(pmap, va)) ==
2799 KASSERT((oldpde & PG_W) == 0,
2800 ("pmap_demote_pde: page table page for a wired mapping"
2804 * Invalidate the 2- or 4MB page mapping and return
2805 * "failure" if the mapping was never accessed or the
2806 * allocation of the new page table page fails.
2808 if ((oldpde & PG_A) == 0 || (mpte = vm_page_alloc(NULL,
2809 va >> PDRSHIFT, VM_ALLOC_NOOBJ | VM_ALLOC_NORMAL |
2810 VM_ALLOC_WIRED)) == NULL) {
2812 sva = trunc_4mpage(va);
2813 pmap_remove_pde(pmap, pde, sva, &free);
2814 if ((oldpde & PG_G) == 0)
2815 pmap_invalidate_pde_page(pmap, sva, oldpde);
2816 vm_page_free_pages_toq(&free, true);
2817 CTR2(KTR_PMAP, "pmap_demote_pde: failure for va %#x"
2818 " in pmap %p", va, pmap);
2821 if (pmap != kernel_pmap) {
2822 mpte->ref_count = NPTEPG;
2823 pmap->pm_stats.resident_count++;
2826 mptepa = VM_PAGE_TO_PHYS(mpte);
2829 * If the page mapping is in the kernel's address space, then the
2830 * KPTmap can provide access to the page table page. Otherwise,
2831 * temporarily map the page table page (mpte) into the kernel's
2832 * address space at either PADDR1 or PADDR2.
2834 if (pmap == kernel_pmap)
2835 firstpte = &KPTmap[i386_btop(trunc_4mpage(va))];
2836 else if (curthread->td_pinned > 0 && rw_wowned(&pvh_global_lock)) {
2837 if ((*PMAP1 & PG_FRAME) != mptepa) {
2838 *PMAP1 = mptepa | PG_RW | PG_V | PG_A | PG_M;
2840 PMAP1cpu = PCPU_GET(cpuid);
2846 if (PMAP1cpu != PCPU_GET(cpuid)) {
2847 PMAP1cpu = PCPU_GET(cpuid);
2855 mtx_lock(&PMAP2mutex);
2856 if ((*PMAP2 & PG_FRAME) != mptepa) {
2857 *PMAP2 = mptepa | PG_RW | PG_V | PG_A | PG_M;
2858 pmap_invalidate_page_int(kernel_pmap,
2859 (vm_offset_t)PADDR2);
2863 newpde = mptepa | PG_M | PG_A | (oldpde & PG_U) | PG_RW | PG_V;
2864 KASSERT((oldpde & PG_A) != 0,
2865 ("pmap_demote_pde: oldpde is missing PG_A"));
2866 KASSERT((oldpde & (PG_M | PG_RW)) != PG_RW,
2867 ("pmap_demote_pde: oldpde is missing PG_M"));
2868 newpte = oldpde & ~PG_PS;
2869 if ((newpte & PG_PDE_PAT) != 0)
2870 newpte ^= PG_PDE_PAT | PG_PTE_PAT;
2873 * If the page table page is not leftover from an earlier promotion,
2876 if (mpte->valid == 0)
2877 pmap_fill_ptp(firstpte, newpte);
2879 KASSERT((*firstpte & PG_FRAME) == (newpte & PG_FRAME),
2880 ("pmap_demote_pde: firstpte and newpte map different physical"
2884 * If the mapping has changed attributes, update the page table
2887 if ((*firstpte & PG_PTE_PROMOTE) != (newpte & PG_PTE_PROMOTE))
2888 pmap_fill_ptp(firstpte, newpte);
2891 * Demote the mapping. This pmap is locked. The old PDE has
2892 * PG_A set. If the old PDE has PG_RW set, it also has PG_M
2893 * set. Thus, there is no danger of a race with another
2894 * processor changing the setting of PG_A and/or PG_M between
2895 * the read above and the store below.
2897 if (workaround_erratum383)
2898 pmap_update_pde(pmap, va, pde, newpde);
2899 else if (pmap == kernel_pmap)
2900 pmap_kenter_pde(va, newpde);
2902 pde_store(pde, newpde);
2903 if (firstpte == PADDR2)
2904 mtx_unlock(&PMAP2mutex);
2907 * Invalidate the recursive mapping of the page table page.
2909 pmap_invalidate_page_int(pmap, (vm_offset_t)vtopte(va));
2912 * Demote the pv entry. This depends on the earlier demotion
2913 * of the mapping. Specifically, the (re)creation of a per-
2914 * page pv entry might trigger the execution of pmap_collect(),
2915 * which might reclaim a newly (re)created per-page pv entry
2916 * and destroy the associated mapping. In order to destroy
2917 * the mapping, the PDE must have already changed from mapping
2918 * the 2mpage to referencing the page table page.
2920 if ((oldpde & PG_MANAGED) != 0)
2921 pmap_pv_demote_pde(pmap, va, oldpde & PG_PS_FRAME);
2923 pmap_pde_demotions++;
2924 CTR2(KTR_PMAP, "pmap_demote_pde: success for va %#x"
2925 " in pmap %p", va, pmap);
2930 * Removes a 2- or 4MB page mapping from the kernel pmap.
2933 pmap_remove_kernel_pde(pmap_t pmap, pd_entry_t *pde, vm_offset_t va)
2939 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2940 mpte = pmap_remove_pt_page(pmap, va);
2942 panic("pmap_remove_kernel_pde: Missing pt page.");
2944 mptepa = VM_PAGE_TO_PHYS(mpte);
2945 newpde = mptepa | PG_M | PG_A | PG_RW | PG_V;
2948 * If this page table page was unmapped by a promotion, then it
2949 * contains valid mappings. Zero it to invalidate those mappings.
2951 if (mpte->valid != 0)
2952 pagezero((void *)&KPTmap[i386_btop(trunc_4mpage(va))]);
2955 * Remove the mapping.
2957 if (workaround_erratum383)
2958 pmap_update_pde(pmap, va, pde, newpde);
2960 pmap_kenter_pde(va, newpde);
2963 * Invalidate the recursive mapping of the page table page.
2965 pmap_invalidate_page_int(pmap, (vm_offset_t)vtopte(va));
2969 * pmap_remove_pde: do the things to unmap a superpage in a process
2972 pmap_remove_pde(pmap_t pmap, pd_entry_t *pdq, vm_offset_t sva,
2973 struct spglist *free)
2975 struct md_page *pvh;
2977 vm_offset_t eva, va;
2980 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2981 KASSERT((sva & PDRMASK) == 0,
2982 ("pmap_remove_pde: sva is not 4mpage aligned"));
2983 oldpde = pte_load_clear(pdq);
2985 pmap->pm_stats.wired_count -= NBPDR / PAGE_SIZE;
2988 * Machines that don't support invlpg, also don't support
2991 if ((oldpde & PG_G) != 0)
2992 pmap_invalidate_pde_page(kernel_pmap, sva, oldpde);
2994 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
2995 if (oldpde & PG_MANAGED) {
2996 pvh = pa_to_pvh(oldpde & PG_PS_FRAME);
2997 pmap_pvh_free(pvh, pmap, sva);
2999 for (va = sva, m = PHYS_TO_VM_PAGE(oldpde & PG_PS_FRAME);
3000 va < eva; va += PAGE_SIZE, m++) {
3001 if ((oldpde & (PG_M | PG_RW)) == (PG_M | PG_RW))
3004 vm_page_aflag_set(m, PGA_REFERENCED);
3005 if (TAILQ_EMPTY(&m->md.pv_list) &&
3006 TAILQ_EMPTY(&pvh->pv_list))
3007 vm_page_aflag_clear(m, PGA_WRITEABLE);
3010 if (pmap == kernel_pmap) {
3011 pmap_remove_kernel_pde(pmap, pdq, sva);
3013 mpte = pmap_remove_pt_page(pmap, sva);
3015 KASSERT(mpte->valid == VM_PAGE_BITS_ALL,
3016 ("pmap_remove_pde: pte page not promoted"));
3017 pmap->pm_stats.resident_count--;
3018 KASSERT(mpte->ref_count == NPTEPG,
3019 ("pmap_remove_pde: pte page ref count error"));
3020 mpte->ref_count = 0;
3021 pmap_add_delayed_free_list(mpte, free, FALSE);
3027 * pmap_remove_pte: do the things to unmap a page in a process
3030 pmap_remove_pte(pmap_t pmap, pt_entry_t *ptq, vm_offset_t va,
3031 struct spglist *free)
3036 rw_assert(&pvh_global_lock, RA_WLOCKED);
3037 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3038 oldpte = pte_load_clear(ptq);
3039 KASSERT(oldpte != 0,
3040 ("pmap_remove_pte: pmap %p va %x zero pte", pmap, va));
3042 pmap->pm_stats.wired_count -= 1;
3044 * Machines that don't support invlpg, also don't support
3048 pmap_invalidate_page_int(kernel_pmap, va);
3049 pmap->pm_stats.resident_count -= 1;
3050 if (oldpte & PG_MANAGED) {
3051 m = PHYS_TO_VM_PAGE(oldpte & PG_FRAME);
3052 if ((oldpte & (PG_M | PG_RW)) == (PG_M | PG_RW))
3055 vm_page_aflag_set(m, PGA_REFERENCED);
3056 pmap_remove_entry(pmap, m, va);
3058 return (pmap_unuse_pt(pmap, va, free));
3062 * Remove a single page from a process address space
3065 pmap_remove_page(pmap_t pmap, vm_offset_t va, struct spglist *free)
3069 rw_assert(&pvh_global_lock, RA_WLOCKED);
3070 KASSERT(curthread->td_pinned > 0, ("curthread not pinned"));
3071 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3072 if ((pte = pmap_pte_quick(pmap, va)) == NULL || *pte == 0)
3074 pmap_remove_pte(pmap, pte, va, free);
3075 pmap_invalidate_page_int(pmap, va);
3079 * Removes the specified range of addresses from the page table page.
3082 pmap_remove_ptes(pmap_t pmap, vm_offset_t sva, vm_offset_t eva,
3083 struct spglist *free)
3088 rw_assert(&pvh_global_lock, RA_WLOCKED);
3089 KASSERT(curthread->td_pinned > 0, ("curthread not pinned"));
3090 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3092 for (pte = pmap_pte_quick(pmap, sva); sva != eva; pte++,
3098 * The TLB entry for a PG_G mapping is invalidated by
3099 * pmap_remove_pte().
3101 if ((*pte & PG_G) == 0)
3104 if (pmap_remove_pte(pmap, pte, sva, free))
3111 * Remove the given range of addresses from the specified map.
3113 * It is assumed that the start and end are properly
3114 * rounded to the page size.
3117 __CONCAT(PMTYPE, remove)(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
3121 struct spglist free;
3125 * Perform an unsynchronized read. This is, however, safe.
3127 if (pmap->pm_stats.resident_count == 0)
3133 rw_wlock(&pvh_global_lock);
3138 * special handling of removing one page. a very
3139 * common operation and easy to short circuit some
3142 if ((sva + PAGE_SIZE == eva) &&
3143 ((pmap->pm_pdir[(sva >> PDRSHIFT)] & PG_PS) == 0)) {
3144 pmap_remove_page(pmap, sva, &free);
3148 for (; sva < eva; sva = pdnxt) {
3152 * Calculate index for next page table.
3154 pdnxt = (sva + NBPDR) & ~PDRMASK;
3157 if (pmap->pm_stats.resident_count == 0)
3160 pdirindex = sva >> PDRSHIFT;
3161 ptpaddr = pmap->pm_pdir[pdirindex];
3164 * Weed out invalid mappings. Note: we assume that the page
3165 * directory table is always allocated, and in kernel virtual.
3171 * Check for large page.
3173 if ((ptpaddr & PG_PS) != 0) {
3175 * Are we removing the entire large page? If not,
3176 * demote the mapping and fall through.
3178 if (sva + NBPDR == pdnxt && eva >= pdnxt) {
3180 * The TLB entry for a PG_G mapping is
3181 * invalidated by pmap_remove_pde().
3183 if ((ptpaddr & PG_G) == 0)
3185 pmap_remove_pde(pmap,
3186 &pmap->pm_pdir[pdirindex], sva, &free);
3188 } else if (!pmap_demote_pde(pmap,
3189 &pmap->pm_pdir[pdirindex], sva)) {
3190 /* The large page mapping was destroyed. */
3196 * Limit our scan to either the end of the va represented
3197 * by the current page table page, or to the end of the
3198 * range being removed.
3203 if (pmap_remove_ptes(pmap, sva, pdnxt, &free))
3209 pmap_invalidate_all_int(pmap);
3210 rw_wunlock(&pvh_global_lock);
3212 vm_page_free_pages_toq(&free, true);
3216 * Routine: pmap_remove_all
3218 * Removes this physical page from
3219 * all physical maps in which it resides.
3220 * Reflects back modify bits to the pager.
3223 * Original versions of this routine were very
3224 * inefficient because they iteratively called
3225 * pmap_remove (slow...)
3229 __CONCAT(PMTYPE, remove_all)(vm_page_t m)
3231 struct md_page *pvh;
3234 pt_entry_t *pte, tpte;
3237 struct spglist free;
3239 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
3240 ("pmap_remove_all: page %p is not managed", m));
3242 rw_wlock(&pvh_global_lock);
3244 if ((m->flags & PG_FICTITIOUS) != 0)
3245 goto small_mappings;
3246 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
3247 while ((pv = TAILQ_FIRST(&pvh->pv_list)) != NULL) {
3251 pde = pmap_pde(pmap, va);
3252 (void)pmap_demote_pde(pmap, pde, va);
3256 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
3259 pmap->pm_stats.resident_count--;
3260 pde = pmap_pde(pmap, pv->pv_va);
3261 KASSERT((*pde & PG_PS) == 0, ("pmap_remove_all: found"
3262 " a 4mpage in page %p's pv list", m));
3263 pte = pmap_pte_quick(pmap, pv->pv_va);
3264 tpte = pte_load_clear(pte);
3265 KASSERT(tpte != 0, ("pmap_remove_all: pmap %p va %x zero pte",
3268 pmap->pm_stats.wired_count--;
3270 vm_page_aflag_set(m, PGA_REFERENCED);
3273 * Update the vm_page_t clean and reference bits.
3275 if ((tpte & (PG_M | PG_RW)) == (PG_M | PG_RW))
3277 pmap_unuse_pt(pmap, pv->pv_va, &free);
3278 pmap_invalidate_page_int(pmap, pv->pv_va);
3279 TAILQ_REMOVE(&m->md.pv_list, pv, pv_next);
3280 free_pv_entry(pmap, pv);
3283 vm_page_aflag_clear(m, PGA_WRITEABLE);
3285 rw_wunlock(&pvh_global_lock);
3286 vm_page_free_pages_toq(&free, true);
3290 * pmap_protect_pde: do the things to protect a 4mpage in a process
3293 pmap_protect_pde(pmap_t pmap, pd_entry_t *pde, vm_offset_t sva, vm_prot_t prot)
3295 pd_entry_t newpde, oldpde;
3297 boolean_t anychanged;
3299 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3300 KASSERT((sva & PDRMASK) == 0,
3301 ("pmap_protect_pde: sva is not 4mpage aligned"));
3304 oldpde = newpde = *pde;
3305 if ((prot & VM_PROT_WRITE) == 0) {
3306 if ((oldpde & (PG_MANAGED | PG_M | PG_RW)) ==
3307 (PG_MANAGED | PG_M | PG_RW)) {
3308 m = PHYS_TO_VM_PAGE(oldpde & PG_PS_FRAME);
3309 for (mt = m; mt < &m[NBPDR / PAGE_SIZE]; mt++)
3312 newpde &= ~(PG_RW | PG_M);
3314 #ifdef PMAP_PAE_COMP
3315 if ((prot & VM_PROT_EXECUTE) == 0 && !i386_read_exec)
3318 if (newpde != oldpde) {
3320 * As an optimization to future operations on this PDE, clear
3321 * PG_PROMOTED. The impending invalidation will remove any
3322 * lingering 4KB page mappings from the TLB.
3324 if (!pde_cmpset(pde, oldpde, newpde & ~PG_PROMOTED))
3326 if ((oldpde & PG_G) != 0)
3327 pmap_invalidate_pde_page(kernel_pmap, sva, oldpde);
3331 return (anychanged);
3335 * Set the physical protection on the
3336 * specified range of this map as requested.
3339 __CONCAT(PMTYPE, protect)(pmap_t pmap, vm_offset_t sva, vm_offset_t eva,
3345 boolean_t anychanged, pv_lists_locked;
3347 KASSERT((prot & ~VM_PROT_ALL) == 0, ("invalid prot %x", prot));
3348 if (prot == VM_PROT_NONE) {
3349 pmap_remove(pmap, sva, eva);
3353 #ifdef PMAP_PAE_COMP
3354 if ((prot & (VM_PROT_WRITE | VM_PROT_EXECUTE)) ==
3355 (VM_PROT_WRITE | VM_PROT_EXECUTE))
3358 if (prot & VM_PROT_WRITE)
3362 if (pmap_is_current(pmap))
3363 pv_lists_locked = FALSE;
3365 pv_lists_locked = TRUE;
3367 rw_wlock(&pvh_global_lock);
3373 for (; sva < eva; sva = pdnxt) {
3374 pt_entry_t obits, pbits;
3377 pdnxt = (sva + NBPDR) & ~PDRMASK;
3381 pdirindex = sva >> PDRSHIFT;
3382 ptpaddr = pmap->pm_pdir[pdirindex];
3385 * Weed out invalid mappings. Note: we assume that the page
3386 * directory table is always allocated, and in kernel virtual.
3392 * Check for large page.
3394 if ((ptpaddr & PG_PS) != 0) {
3396 * Are we protecting the entire large page? If not,
3397 * demote the mapping and fall through.
3399 if (sva + NBPDR == pdnxt && eva >= pdnxt) {
3401 * The TLB entry for a PG_G mapping is
3402 * invalidated by pmap_protect_pde().
3404 if (pmap_protect_pde(pmap,
3405 &pmap->pm_pdir[pdirindex], sva, prot))
3409 if (!pv_lists_locked) {
3410 pv_lists_locked = TRUE;
3411 if (!rw_try_wlock(&pvh_global_lock)) {
3413 pmap_invalidate_all_int(
3420 if (!pmap_demote_pde(pmap,
3421 &pmap->pm_pdir[pdirindex], sva)) {
3423 * The large page mapping was
3434 for (pte = pmap_pte_quick(pmap, sva); sva != pdnxt; pte++,
3440 * Regardless of whether a pte is 32 or 64 bits in
3441 * size, PG_RW, PG_A, and PG_M are among the least
3442 * significant 32 bits.
3444 obits = pbits = *pte;
3445 if ((pbits & PG_V) == 0)
3448 if ((prot & VM_PROT_WRITE) == 0) {
3449 if ((pbits & (PG_MANAGED | PG_M | PG_RW)) ==
3450 (PG_MANAGED | PG_M | PG_RW)) {
3451 m = PHYS_TO_VM_PAGE(pbits & PG_FRAME);
3454 pbits &= ~(PG_RW | PG_M);
3456 #ifdef PMAP_PAE_COMP
3457 if ((prot & VM_PROT_EXECUTE) == 0 && !i386_read_exec)
3461 if (pbits != obits) {
3462 #ifdef PMAP_PAE_COMP
3463 if (!atomic_cmpset_64(pte, obits, pbits))
3466 if (!atomic_cmpset_int((u_int *)pte, obits,
3471 pmap_invalidate_page_int(pmap, sva);
3478 pmap_invalidate_all_int(pmap);
3479 if (pv_lists_locked) {
3481 rw_wunlock(&pvh_global_lock);
3486 #if VM_NRESERVLEVEL > 0
3488 * Tries to promote the 512 or 1024, contiguous 4KB page mappings that are
3489 * within a single page table page (PTP) to a single 2- or 4MB page mapping.
3490 * For promotion to occur, two conditions must be met: (1) the 4KB page
3491 * mappings must map aligned, contiguous physical memory and (2) the 4KB page
3492 * mappings must have identical characteristics.
3494 * Managed (PG_MANAGED) mappings within the kernel address space are not
3495 * promoted. The reason is that kernel PDEs are replicated in each pmap but
3496 * pmap_clear_ptes() and pmap_ts_referenced() only read the PDE from the kernel
3500 pmap_promote_pde(pmap_t pmap, pd_entry_t *pde, vm_offset_t va)
3503 pt_entry_t *firstpte, oldpte, pa, *pte;
3504 vm_offset_t oldpteva;
3507 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3510 * Examine the first PTE in the specified PTP. Abort if this PTE is
3511 * either invalid, unused, or does not map the first 4KB physical page
3512 * within a 2- or 4MB page.
3514 firstpte = pmap_pte_quick(pmap, trunc_4mpage(va));
3517 if ((newpde & ((PG_FRAME & PDRMASK) | PG_A | PG_V)) != (PG_A | PG_V)) {
3518 pmap_pde_p_failures++;
3519 CTR2(KTR_PMAP, "pmap_promote_pde: failure for va %#x"
3520 " in pmap %p", va, pmap);
3523 if ((*firstpte & PG_MANAGED) != 0 && pmap == kernel_pmap) {
3524 pmap_pde_p_failures++;
3525 CTR2(KTR_PMAP, "pmap_promote_pde: failure for va %#x"
3526 " in pmap %p", va, pmap);
3529 if ((newpde & (PG_M | PG_RW)) == PG_RW) {
3531 * When PG_M is already clear, PG_RW can be cleared without
3532 * a TLB invalidation.
3534 if (!atomic_cmpset_int((u_int *)firstpte, newpde, newpde &
3541 * Examine each of the other PTEs in the specified PTP. Abort if this
3542 * PTE maps an unexpected 4KB physical page or does not have identical
3543 * characteristics to the first PTE.
3545 pa = (newpde & (PG_PS_FRAME | PG_A | PG_V)) + NBPDR - PAGE_SIZE;
3546 for (pte = firstpte + NPTEPG - 1; pte > firstpte; pte--) {
3549 if ((oldpte & (PG_FRAME | PG_A | PG_V)) != pa) {
3550 pmap_pde_p_failures++;
3551 CTR2(KTR_PMAP, "pmap_promote_pde: failure for va %#x"
3552 " in pmap %p", va, pmap);
3555 if ((oldpte & (PG_M | PG_RW)) == PG_RW) {
3557 * When PG_M is already clear, PG_RW can be cleared
3558 * without a TLB invalidation.
3560 if (!atomic_cmpset_int((u_int *)pte, oldpte,
3564 oldpteva = (oldpte & PG_FRAME & PDRMASK) |
3566 CTR2(KTR_PMAP, "pmap_promote_pde: protect for va %#x"
3567 " in pmap %p", oldpteva, pmap);
3569 if ((oldpte & PG_PTE_PROMOTE) != (newpde & PG_PTE_PROMOTE)) {
3570 pmap_pde_p_failures++;
3571 CTR2(KTR_PMAP, "pmap_promote_pde: failure for va %#x"
3572 " in pmap %p", va, pmap);
3579 * Save the page table page in its current state until the PDE
3580 * mapping the superpage is demoted by pmap_demote_pde() or
3581 * destroyed by pmap_remove_pde().
3583 mpte = PHYS_TO_VM_PAGE(*pde & PG_FRAME);
3584 KASSERT(mpte >= vm_page_array &&
3585 mpte < &vm_page_array[vm_page_array_size],
3586 ("pmap_promote_pde: page table page is out of range"));
3587 KASSERT(mpte->pindex == va >> PDRSHIFT,
3588 ("pmap_promote_pde: page table page's pindex is wrong"));
3589 if (pmap_insert_pt_page(pmap, mpte, true)) {
3590 pmap_pde_p_failures++;
3592 "pmap_promote_pde: failure for va %#x in pmap %p", va,
3598 * Promote the pv entries.
3600 if ((newpde & PG_MANAGED) != 0)
3601 pmap_pv_promote_pde(pmap, va, newpde & PG_PS_FRAME);
3604 * Propagate the PAT index to its proper position.
3606 if ((newpde & PG_PTE_PAT) != 0)
3607 newpde ^= PG_PDE_PAT | PG_PTE_PAT;
3610 * Map the superpage.
3612 if (workaround_erratum383)
3613 pmap_update_pde(pmap, va, pde, PG_PS | newpde);
3614 else if (pmap == kernel_pmap)
3615 pmap_kenter_pde(va, PG_PROMOTED | PG_PS | newpde);
3617 pde_store(pde, PG_PROMOTED | PG_PS | newpde);
3619 pmap_pde_promotions++;
3620 CTR2(KTR_PMAP, "pmap_promote_pde: success for va %#x"
3621 " in pmap %p", va, pmap);
3623 #endif /* VM_NRESERVLEVEL > 0 */
3626 * Insert the given physical page (p) at
3627 * the specified virtual address (v) in the
3628 * target physical map with the protection requested.
3630 * If specified, the page will be wired down, meaning
3631 * that the related pte can not be reclaimed.
3633 * NB: This is the only routine which MAY NOT lazy-evaluate
3634 * or lose information. That is, this routine must actually
3635 * insert this page into the given map NOW.
3638 __CONCAT(PMTYPE, enter)(pmap_t pmap, vm_offset_t va, vm_page_t m,
3639 vm_prot_t prot, u_int flags, int8_t psind)
3643 pt_entry_t newpte, origpte;
3649 va = trunc_page(va);
3650 KASSERT((pmap == kernel_pmap && va < VM_MAX_KERNEL_ADDRESS) ||
3651 (pmap != kernel_pmap && va < VM_MAXUSER_ADDRESS),
3652 ("pmap_enter: toobig k%d %#x", pmap == kernel_pmap, va));
3653 KASSERT(va < PMAP_TRM_MIN_ADDRESS,
3654 ("pmap_enter: invalid to pmap_enter into trampoline (va: 0x%x)",
3656 KASSERT(pmap != kernel_pmap || (m->oflags & VPO_UNMANAGED) != 0 ||
3657 va < kmi.clean_sva || va >= kmi.clean_eva,
3658 ("pmap_enter: managed mapping within the clean submap"));
3659 if ((m->oflags & VPO_UNMANAGED) == 0)
3660 VM_PAGE_OBJECT_BUSY_ASSERT(m);
3661 KASSERT((flags & PMAP_ENTER_RESERVED) == 0,
3662 ("pmap_enter: flags %u has reserved bits set", flags));
3663 pa = VM_PAGE_TO_PHYS(m);
3664 newpte = (pt_entry_t)(pa | PG_A | PG_V);
3665 if ((flags & VM_PROT_WRITE) != 0)
3667 if ((prot & VM_PROT_WRITE) != 0)
3669 KASSERT((newpte & (PG_M | PG_RW)) != PG_M,
3670 ("pmap_enter: flags includes VM_PROT_WRITE but prot doesn't"));
3671 #ifdef PMAP_PAE_COMP
3672 if ((prot & VM_PROT_EXECUTE) == 0 && !i386_read_exec)
3675 if ((flags & PMAP_ENTER_WIRED) != 0)
3677 if (pmap != kernel_pmap)
3679 newpte |= pmap_cache_bits(pmap, m->md.pat_mode, psind > 0);
3680 if ((m->oflags & VPO_UNMANAGED) == 0)
3681 newpte |= PG_MANAGED;
3683 rw_wlock(&pvh_global_lock);
3687 /* Assert the required virtual and physical alignment. */
3688 KASSERT((va & PDRMASK) == 0, ("pmap_enter: va unaligned"));
3689 KASSERT(m->psind > 0, ("pmap_enter: m->psind < psind"));
3690 rv = pmap_enter_pde(pmap, va, newpte | PG_PS, flags, m);
3694 pde = pmap_pde(pmap, va);
3695 if (pmap != kernel_pmap) {
3698 * In the case that a page table page is not resident,
3699 * we are creating it here. pmap_allocpte() handles
3702 mpte = pmap_allocpte(pmap, va, flags);
3704 KASSERT((flags & PMAP_ENTER_NOSLEEP) != 0,
3705 ("pmap_allocpte failed with sleep allowed"));
3706 rv = KERN_RESOURCE_SHORTAGE;
3711 * va is for KVA, so pmap_demote_pde() will never fail
3712 * to install a page table page. PG_V is also
3713 * asserted by pmap_demote_pde().
3716 KASSERT(pde != NULL && (*pde & PG_V) != 0,
3717 ("KVA %#x invalid pde pdir %#jx", va,
3718 (uintmax_t)pmap->pm_pdir[PTDPTDI]));
3719 if ((*pde & PG_PS) != 0)
3720 pmap_demote_pde(pmap, pde, va);
3722 pte = pmap_pte_quick(pmap, va);
3725 * Page Directory table entry is not valid, which should not
3726 * happen. We should have either allocated the page table
3727 * page or demoted the existing mapping above.
3730 panic("pmap_enter: invalid page directory pdir=%#jx, va=%#x",
3731 (uintmax_t)pmap->pm_pdir[PTDPTDI], va);
3738 * Is the specified virtual address already mapped?
3740 if ((origpte & PG_V) != 0) {
3742 * Wiring change, just update stats. We don't worry about
3743 * wiring PT pages as they remain resident as long as there
3744 * are valid mappings in them. Hence, if a user page is wired,
3745 * the PT page will be also.
3747 if ((newpte & PG_W) != 0 && (origpte & PG_W) == 0)
3748 pmap->pm_stats.wired_count++;
3749 else if ((newpte & PG_W) == 0 && (origpte & PG_W) != 0)
3750 pmap->pm_stats.wired_count--;
3753 * Remove the extra PT page reference.
3757 KASSERT(mpte->ref_count > 0,
3758 ("pmap_enter: missing reference to page table page,"
3763 * Has the physical page changed?
3765 opa = origpte & PG_FRAME;
3768 * No, might be a protection or wiring change.
3770 if ((origpte & PG_MANAGED) != 0 &&
3771 (newpte & PG_RW) != 0)
3772 vm_page_aflag_set(m, PGA_WRITEABLE);
3773 if (((origpte ^ newpte) & ~(PG_M | PG_A)) == 0)
3779 * The physical page has changed. Temporarily invalidate
3780 * the mapping. This ensures that all threads sharing the
3781 * pmap keep a consistent view of the mapping, which is
3782 * necessary for the correct handling of COW faults. It
3783 * also permits reuse of the old mapping's PV entry,
3784 * avoiding an allocation.
3786 * For consistency, handle unmanaged mappings the same way.
3788 origpte = pte_load_clear(pte);
3789 KASSERT((origpte & PG_FRAME) == opa,
3790 ("pmap_enter: unexpected pa update for %#x", va));
3791 if ((origpte & PG_MANAGED) != 0) {
3792 om = PHYS_TO_VM_PAGE(opa);
3795 * The pmap lock is sufficient to synchronize with
3796 * concurrent calls to pmap_page_test_mappings() and
3797 * pmap_ts_referenced().
3799 if ((origpte & (PG_M | PG_RW)) == (PG_M | PG_RW))
3801 if ((origpte & PG_A) != 0) {
3802 pmap_invalidate_page_int(pmap, va);
3803 vm_page_aflag_set(om, PGA_REFERENCED);
3805 pv = pmap_pvh_remove(&om->md, pmap, va);
3807 ("pmap_enter: no PV entry for %#x", va));
3808 if ((newpte & PG_MANAGED) == 0)
3809 free_pv_entry(pmap, pv);
3810 if ((om->a.flags & PGA_WRITEABLE) != 0 &&
3811 TAILQ_EMPTY(&om->md.pv_list) &&
3812 ((om->flags & PG_FICTITIOUS) != 0 ||
3813 TAILQ_EMPTY(&pa_to_pvh(opa)->pv_list)))
3814 vm_page_aflag_clear(om, PGA_WRITEABLE);
3817 * Since this mapping is unmanaged, assume that PG_A
3820 pmap_invalidate_page_int(pmap, va);
3825 * Increment the counters.
3827 if ((newpte & PG_W) != 0)
3828 pmap->pm_stats.wired_count++;
3829 pmap->pm_stats.resident_count++;
3833 * Enter on the PV list if part of our managed memory.
3835 if ((newpte & PG_MANAGED) != 0) {
3837 pv = get_pv_entry(pmap, FALSE);
3840 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_next);
3841 if ((newpte & PG_RW) != 0)
3842 vm_page_aflag_set(m, PGA_WRITEABLE);
3848 if ((origpte & PG_V) != 0) {
3850 origpte = pte_load_store(pte, newpte);
3851 KASSERT((origpte & PG_FRAME) == pa,
3852 ("pmap_enter: unexpected pa update for %#x", va));
3853 if ((newpte & PG_M) == 0 && (origpte & (PG_M | PG_RW)) ==
3855 if ((origpte & PG_MANAGED) != 0)
3859 * Although the PTE may still have PG_RW set, TLB
3860 * invalidation may nonetheless be required because
3861 * the PTE no longer has PG_M set.
3864 #ifdef PMAP_PAE_COMP
3865 else if ((origpte & PG_NX) != 0 || (newpte & PG_NX) == 0) {
3867 * This PTE change does not require TLB invalidation.
3872 if ((origpte & PG_A) != 0)
3873 pmap_invalidate_page_int(pmap, va);
3875 pte_store_zero(pte, newpte);
3879 #if VM_NRESERVLEVEL > 0
3881 * If both the page table page and the reservation are fully
3882 * populated, then attempt promotion.
3884 if ((mpte == NULL || mpte->ref_count == NPTEPG) &&
3885 pg_ps_enabled && (m->flags & PG_FICTITIOUS) == 0 &&
3886 vm_reserv_level_iffullpop(m) == 0)
3887 pmap_promote_pde(pmap, pde, va);
3893 rw_wunlock(&pvh_global_lock);
3899 * Tries to create a read- and/or execute-only 2 or 4 MB page mapping. Returns
3900 * true if successful. Returns false if (1) a mapping already exists at the
3901 * specified virtual address or (2) a PV entry cannot be allocated without
3902 * reclaiming another PV entry.
3905 pmap_enter_4mpage(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot)
3909 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3910 newpde = VM_PAGE_TO_PHYS(m) | pmap_cache_bits(pmap, m->md.pat_mode, 1) |
3912 if ((m->oflags & VPO_UNMANAGED) == 0)
3913 newpde |= PG_MANAGED;
3914 #ifdef PMAP_PAE_COMP
3915 if ((prot & VM_PROT_EXECUTE) == 0 && !i386_read_exec)
3918 if (pmap != kernel_pmap)
3920 return (pmap_enter_pde(pmap, va, newpde, PMAP_ENTER_NOSLEEP |
3921 PMAP_ENTER_NOREPLACE | PMAP_ENTER_NORECLAIM, NULL) ==
3926 * Returns true if every page table entry in the page table page that maps
3927 * the specified kernel virtual address is zero.
3930 pmap_every_pte_zero(vm_offset_t va)
3932 pt_entry_t *pt_end, *pte;
3934 KASSERT((va & PDRMASK) == 0, ("va is misaligned"));
3936 for (pt_end = pte + NPTEPG; pte < pt_end; pte++) {
3944 * Tries to create the specified 2 or 4 MB page mapping. Returns KERN_SUCCESS
3945 * if the mapping was created, and either KERN_FAILURE or
3946 * KERN_RESOURCE_SHORTAGE otherwise. Returns KERN_FAILURE if
3947 * PMAP_ENTER_NOREPLACE was specified and a mapping already exists at the
3948 * specified virtual address. Returns KERN_RESOURCE_SHORTAGE if
3949 * PMAP_ENTER_NORECLAIM was specified and a PV entry allocation failed.
3951 * The parameter "m" is only used when creating a managed, writeable mapping.
3954 pmap_enter_pde(pmap_t pmap, vm_offset_t va, pd_entry_t newpde, u_int flags,
3957 struct spglist free;
3958 pd_entry_t oldpde, *pde;
3961 rw_assert(&pvh_global_lock, RA_WLOCKED);
3962 KASSERT((newpde & (PG_M | PG_RW)) != PG_RW,
3963 ("pmap_enter_pde: newpde is missing PG_M"));
3964 KASSERT(pmap == kernel_pmap || (newpde & PG_W) == 0,
3965 ("pmap_enter_pde: cannot create wired user mapping"));
3966 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3967 pde = pmap_pde(pmap, va);
3969 if ((oldpde & PG_V) != 0) {
3970 if ((flags & PMAP_ENTER_NOREPLACE) != 0 && (pmap !=
3971 kernel_pmap || (oldpde & PG_PS) != 0 ||
3972 !pmap_every_pte_zero(va))) {
3973 CTR2(KTR_PMAP, "pmap_enter_pde: failure for va %#lx"
3974 " in pmap %p", va, pmap);
3975 return (KERN_FAILURE);
3977 /* Break the existing mapping(s). */
3979 if ((oldpde & PG_PS) != 0) {
3981 * If the PDE resulted from a promotion, then a
3982 * reserved PT page could be freed.
3984 (void)pmap_remove_pde(pmap, pde, va, &free);
3985 if ((oldpde & PG_G) == 0)
3986 pmap_invalidate_pde_page(pmap, va, oldpde);
3988 if (pmap_remove_ptes(pmap, va, va + NBPDR, &free))
3989 pmap_invalidate_all_int(pmap);
3991 if (pmap != kernel_pmap) {
3992 vm_page_free_pages_toq(&free, true);
3993 KASSERT(*pde == 0, ("pmap_enter_pde: non-zero pde %p",
3996 KASSERT(SLIST_EMPTY(&free),
3997 ("pmap_enter_pde: freed kernel page table page"));
4000 * Both pmap_remove_pde() and pmap_remove_ptes() will
4001 * leave the kernel page table page zero filled.
4003 mt = PHYS_TO_VM_PAGE(*pde & PG_FRAME);
4004 if (pmap_insert_pt_page(pmap, mt, false))
4005 panic("pmap_enter_pde: trie insert failed");
4008 if ((newpde & PG_MANAGED) != 0) {
4010 * Abort this mapping if its PV entry could not be created.
4012 if (!pmap_pv_insert_pde(pmap, va, newpde, flags)) {
4013 CTR2(KTR_PMAP, "pmap_enter_pde: failure for va %#lx"
4014 " in pmap %p", va, pmap);
4015 return (KERN_RESOURCE_SHORTAGE);
4017 if ((newpde & PG_RW) != 0) {
4018 for (mt = m; mt < &m[NBPDR / PAGE_SIZE]; mt++)
4019 vm_page_aflag_set(mt, PGA_WRITEABLE);
4024 * Increment counters.
4026 if ((newpde & PG_W) != 0)
4027 pmap->pm_stats.wired_count += NBPDR / PAGE_SIZE;
4028 pmap->pm_stats.resident_count += NBPDR / PAGE_SIZE;
4031 * Map the superpage. (This is not a promoted mapping; there will not
4032 * be any lingering 4KB page mappings in the TLB.)
4034 pde_store(pde, newpde);
4036 pmap_pde_mappings++;
4037 CTR2(KTR_PMAP, "pmap_enter_pde: success for va %#lx in pmap %p",
4039 return (KERN_SUCCESS);
4043 * Maps a sequence of resident pages belonging to the same object.
4044 * The sequence begins with the given page m_start. This page is
4045 * mapped at the given virtual address start. Each subsequent page is
4046 * mapped at a virtual address that is offset from start by the same
4047 * amount as the page is offset from m_start within the object. The
4048 * last page in the sequence is the page with the largest offset from
4049 * m_start that can be mapped at a virtual address less than the given
4050 * virtual address end. Not every virtual page between start and end
4051 * is mapped; only those for which a resident page exists with the
4052 * corresponding offset from m_start are mapped.
4055 __CONCAT(PMTYPE, enter_object)(pmap_t pmap, vm_offset_t start, vm_offset_t end,
4056 vm_page_t m_start, vm_prot_t prot)
4060 vm_pindex_t diff, psize;
4062 VM_OBJECT_ASSERT_LOCKED(m_start->object);
4064 psize = atop(end - start);
4067 rw_wlock(&pvh_global_lock);
4069 while (m != NULL && (diff = m->pindex - m_start->pindex) < psize) {
4070 va = start + ptoa(diff);
4071 if ((va & PDRMASK) == 0 && va + NBPDR <= end &&
4072 m->psind == 1 && pg_ps_enabled &&
4073 pmap_enter_4mpage(pmap, va, m, prot))
4074 m = &m[NBPDR / PAGE_SIZE - 1];
4076 mpte = pmap_enter_quick_locked(pmap, va, m, prot,
4078 m = TAILQ_NEXT(m, listq);
4080 rw_wunlock(&pvh_global_lock);
4085 * this code makes some *MAJOR* assumptions:
4086 * 1. Current pmap & pmap exists.
4089 * 4. No page table pages.
4090 * but is *MUCH* faster than pmap_enter...
4094 __CONCAT(PMTYPE, enter_quick)(pmap_t pmap, vm_offset_t va, vm_page_t m,
4098 rw_wlock(&pvh_global_lock);
4100 (void)pmap_enter_quick_locked(pmap, va, m, prot, NULL);
4101 rw_wunlock(&pvh_global_lock);
4106 pmap_enter_quick_locked(pmap_t pmap, vm_offset_t va, vm_page_t m,
4107 vm_prot_t prot, vm_page_t mpte)
4109 pt_entry_t newpte, *pte;
4111 KASSERT(pmap != kernel_pmap || va < kmi.clean_sva ||
4112 va >= kmi.clean_eva || (m->oflags & VPO_UNMANAGED) != 0,
4113 ("pmap_enter_quick_locked: managed mapping within the clean submap"));
4114 rw_assert(&pvh_global_lock, RA_WLOCKED);
4115 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
4118 * In the case that a page table page is not
4119 * resident, we are creating it here.
4121 if (pmap != kernel_pmap) {
4126 * Calculate pagetable page index
4128 ptepindex = va >> PDRSHIFT;
4129 if (mpte && (mpte->pindex == ptepindex)) {
4133 * Get the page directory entry
4135 ptepa = pmap->pm_pdir[ptepindex];
4138 * If the page table page is mapped, we just increment
4139 * the hold count, and activate it.
4144 mpte = PHYS_TO_VM_PAGE(ptepa & PG_FRAME);
4147 mpte = _pmap_allocpte(pmap, ptepindex,
4148 PMAP_ENTER_NOSLEEP);
4158 pte = pmap_pte_quick(pmap, va);
4167 * Enter on the PV list if part of our managed memory.
4169 if ((m->oflags & VPO_UNMANAGED) == 0 &&
4170 !pmap_try_insert_pv_entry(pmap, va, m)) {
4172 pmap_abort_ptp(pmap, va, mpte);
4178 * Increment counters
4180 pmap->pm_stats.resident_count++;
4182 newpte = VM_PAGE_TO_PHYS(m) | PG_V |
4183 pmap_cache_bits(pmap, m->md.pat_mode, 0);
4184 if ((m->oflags & VPO_UNMANAGED) == 0)
4185 newpte |= PG_MANAGED;
4186 #ifdef PMAP_PAE_COMP
4187 if ((prot & VM_PROT_EXECUTE) == 0 && !i386_read_exec)
4190 if (pmap != kernel_pmap)
4192 pte_store_zero(pte, newpte);
4198 * Make a temporary mapping for a physical address. This is only intended
4199 * to be used for panic dumps.
4202 __CONCAT(PMTYPE, kenter_temporary)(vm_paddr_t pa, int i)
4206 va = (vm_offset_t)crashdumpmap + (i * PAGE_SIZE);
4207 pmap_kenter(va, pa);
4209 return ((void *)crashdumpmap);
4213 * This code maps large physical mmap regions into the
4214 * processor address space. Note that some shortcuts
4215 * are taken, but the code works.
4218 __CONCAT(PMTYPE, object_init_pt)(pmap_t pmap, vm_offset_t addr,
4219 vm_object_t object, vm_pindex_t pindex, vm_size_t size)
4222 vm_paddr_t pa, ptepa;
4226 VM_OBJECT_ASSERT_WLOCKED(object);
4227 KASSERT(object->type == OBJT_DEVICE || object->type == OBJT_SG,
4228 ("pmap_object_init_pt: non-device object"));
4229 if (pg_ps_enabled &&
4230 (addr & (NBPDR - 1)) == 0 && (size & (NBPDR - 1)) == 0) {
4231 if (!vm_object_populate(object, pindex, pindex + atop(size)))
4233 p = vm_page_lookup(object, pindex);
4234 KASSERT(p->valid == VM_PAGE_BITS_ALL,
4235 ("pmap_object_init_pt: invalid page %p", p));
4236 pat_mode = p->md.pat_mode;
4239 * Abort the mapping if the first page is not physically
4240 * aligned to a 2/4MB page boundary.
4242 ptepa = VM_PAGE_TO_PHYS(p);
4243 if (ptepa & (NBPDR - 1))
4247 * Skip the first page. Abort the mapping if the rest of
4248 * the pages are not physically contiguous or have differing
4249 * memory attributes.
4251 p = TAILQ_NEXT(p, listq);
4252 for (pa = ptepa + PAGE_SIZE; pa < ptepa + size;
4254 KASSERT(p->valid == VM_PAGE_BITS_ALL,
4255 ("pmap_object_init_pt: invalid page %p", p));
4256 if (pa != VM_PAGE_TO_PHYS(p) ||
4257 pat_mode != p->md.pat_mode)
4259 p = TAILQ_NEXT(p, listq);
4263 * Map using 2/4MB pages. Since "ptepa" is 2/4M aligned and
4264 * "size" is a multiple of 2/4M, adding the PAT setting to
4265 * "pa" will not affect the termination of this loop.
4268 for (pa = ptepa | pmap_cache_bits(pmap, pat_mode, 1);
4269 pa < ptepa + size; pa += NBPDR) {
4270 pde = pmap_pde(pmap, addr);
4272 pde_store(pde, pa | PG_PS | PG_M | PG_A |
4273 PG_U | PG_RW | PG_V);
4274 pmap->pm_stats.resident_count += NBPDR /
4276 pmap_pde_mappings++;
4278 /* Else continue on if the PDE is already valid. */
4286 * Clear the wired attribute from the mappings for the specified range of
4287 * addresses in the given pmap. Every valid mapping within that range
4288 * must have the wired attribute set. In contrast, invalid mappings
4289 * cannot have the wired attribute set, so they are ignored.
4291 * The wired attribute of the page table entry is not a hardware feature,
4292 * so there is no need to invalidate any TLB entries.
4295 __CONCAT(PMTYPE, unwire)(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
4300 boolean_t pv_lists_locked;
4302 if (pmap_is_current(pmap))
4303 pv_lists_locked = FALSE;
4305 pv_lists_locked = TRUE;
4307 rw_wlock(&pvh_global_lock);
4311 for (; sva < eva; sva = pdnxt) {
4312 pdnxt = (sva + NBPDR) & ~PDRMASK;
4315 pde = pmap_pde(pmap, sva);
4316 if ((*pde & PG_V) == 0)
4318 if ((*pde & PG_PS) != 0) {
4319 if ((*pde & PG_W) == 0)
4320 panic("pmap_unwire: pde %#jx is missing PG_W",
4324 * Are we unwiring the entire large page? If not,
4325 * demote the mapping and fall through.
4327 if (sva + NBPDR == pdnxt && eva >= pdnxt) {
4329 * Regardless of whether a pde (or pte) is 32
4330 * or 64 bits in size, PG_W is among the least
4331 * significant 32 bits.
4333 atomic_clear_int((u_int *)pde, PG_W);
4334 pmap->pm_stats.wired_count -= NBPDR /
4338 if (!pv_lists_locked) {
4339 pv_lists_locked = TRUE;
4340 if (!rw_try_wlock(&pvh_global_lock)) {
4347 if (!pmap_demote_pde(pmap, pde, sva))
4348 panic("pmap_unwire: demotion failed");
4353 for (pte = pmap_pte_quick(pmap, sva); sva != pdnxt; pte++,
4355 if ((*pte & PG_V) == 0)
4357 if ((*pte & PG_W) == 0)
4358 panic("pmap_unwire: pte %#jx is missing PG_W",
4362 * PG_W must be cleared atomically. Although the pmap
4363 * lock synchronizes access to PG_W, another processor
4364 * could be setting PG_M and/or PG_A concurrently.
4366 * PG_W is among the least significant 32 bits.
4368 atomic_clear_int((u_int *)pte, PG_W);
4369 pmap->pm_stats.wired_count--;
4372 if (pv_lists_locked) {
4374 rw_wunlock(&pvh_global_lock);
4380 * Copy the range specified by src_addr/len
4381 * from the source map to the range dst_addr/len
4382 * in the destination map.
4384 * This routine is only advisory and need not do anything. Since
4385 * current pmap is always the kernel pmap when executing in
4386 * kernel, and we do not copy from the kernel pmap to a user
4387 * pmap, this optimization is not usable in 4/4G full split i386
4392 __CONCAT(PMTYPE, copy)(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr,
4393 vm_size_t len, vm_offset_t src_addr)
4395 pt_entry_t *src_pte, *dst_pte, ptetemp;
4396 pd_entry_t srcptepaddr;
4397 vm_page_t dstmpte, srcmpte;
4398 vm_offset_t addr, end_addr, pdnxt;
4401 if (dst_addr != src_addr)
4404 end_addr = src_addr + len;
4406 rw_wlock(&pvh_global_lock);
4407 if (dst_pmap < src_pmap) {
4408 PMAP_LOCK(dst_pmap);
4409 PMAP_LOCK(src_pmap);
4411 PMAP_LOCK(src_pmap);
4412 PMAP_LOCK(dst_pmap);
4415 for (addr = src_addr; addr < end_addr; addr = pdnxt) {
4416 KASSERT(addr < PMAP_TRM_MIN_ADDRESS,
4417 ("pmap_copy: invalid to pmap_copy the trampoline"));
4419 pdnxt = (addr + NBPDR) & ~PDRMASK;
4422 ptepindex = addr >> PDRSHIFT;
4424 srcptepaddr = src_pmap->pm_pdir[ptepindex];
4425 if (srcptepaddr == 0)
4428 if (srcptepaddr & PG_PS) {
4429 if ((addr & PDRMASK) != 0 || addr + NBPDR > end_addr)
4431 if (dst_pmap->pm_pdir[ptepindex] == 0 &&
4432 ((srcptepaddr & PG_MANAGED) == 0 ||
4433 pmap_pv_insert_pde(dst_pmap, addr, srcptepaddr,
4434 PMAP_ENTER_NORECLAIM))) {
4435 dst_pmap->pm_pdir[ptepindex] = srcptepaddr &
4437 dst_pmap->pm_stats.resident_count +=
4439 pmap_pde_mappings++;
4444 srcmpte = PHYS_TO_VM_PAGE(srcptepaddr & PG_FRAME);
4445 KASSERT(srcmpte->ref_count > 0,
4446 ("pmap_copy: source page table page is unused"));
4448 if (pdnxt > end_addr)
4451 src_pte = pmap_pte_quick3(src_pmap, addr);
4452 while (addr < pdnxt) {
4455 * we only virtual copy managed pages
4457 if ((ptetemp & PG_MANAGED) != 0) {
4458 dstmpte = pmap_allocpte(dst_pmap, addr,
4459 PMAP_ENTER_NOSLEEP);
4460 if (dstmpte == NULL)
4462 dst_pte = pmap_pte_quick(dst_pmap, addr);
4463 if (*dst_pte == 0 &&
4464 pmap_try_insert_pv_entry(dst_pmap, addr,
4465 PHYS_TO_VM_PAGE(ptetemp & PG_FRAME))) {
4467 * Clear the wired, modified, and
4468 * accessed (referenced) bits
4471 *dst_pte = ptetemp & ~(PG_W | PG_M |
4473 dst_pmap->pm_stats.resident_count++;
4475 pmap_abort_ptp(dst_pmap, addr, dstmpte);
4478 if (dstmpte->ref_count >= srcmpte->ref_count)
4487 rw_wunlock(&pvh_global_lock);
4488 PMAP_UNLOCK(src_pmap);
4489 PMAP_UNLOCK(dst_pmap);
4493 * Zero 1 page of virtual memory mapped from a hardware page by the caller.
4495 static __inline void
4496 pagezero(void *page)
4498 #if defined(I686_CPU)
4499 if (cpu_class == CPUCLASS_686) {
4500 if (cpu_feature & CPUID_SSE2)
4501 sse2_pagezero(page);
4503 i686_pagezero(page);
4506 bzero(page, PAGE_SIZE);
4510 * Zero the specified hardware page.
4513 __CONCAT(PMTYPE, zero_page)(vm_page_t m)
4515 pt_entry_t *cmap_pte2;
4520 cmap_pte2 = pc->pc_cmap_pte2;
4521 mtx_lock(&pc->pc_cmap_lock);
4523 panic("pmap_zero_page: CMAP2 busy");
4524 *cmap_pte2 = PG_V | PG_RW | VM_PAGE_TO_PHYS(m) | PG_A | PG_M |
4525 pmap_cache_bits(kernel_pmap, m->md.pat_mode, 0);
4526 invlcaddr(pc->pc_cmap_addr2);
4527 pagezero(pc->pc_cmap_addr2);
4531 * Unpin the thread before releasing the lock. Otherwise the thread
4532 * could be rescheduled while still bound to the current CPU, only
4533 * to unpin itself immediately upon resuming execution.
4536 mtx_unlock(&pc->pc_cmap_lock);
4540 * Zero an an area within a single hardware page. off and size must not
4541 * cover an area beyond a single hardware page.
4544 __CONCAT(PMTYPE, zero_page_area)(vm_page_t m, int off, int size)
4546 pt_entry_t *cmap_pte2;
4551 cmap_pte2 = pc->pc_cmap_pte2;
4552 mtx_lock(&pc->pc_cmap_lock);
4554 panic("pmap_zero_page_area: CMAP2 busy");
4555 *cmap_pte2 = PG_V | PG_RW | VM_PAGE_TO_PHYS(m) | PG_A | PG_M |
4556 pmap_cache_bits(kernel_pmap, m->md.pat_mode, 0);
4557 invlcaddr(pc->pc_cmap_addr2);
4558 if (off == 0 && size == PAGE_SIZE)
4559 pagezero(pc->pc_cmap_addr2);
4561 bzero(pc->pc_cmap_addr2 + off, size);
4564 mtx_unlock(&pc->pc_cmap_lock);
4568 * Copy 1 specified hardware page to another.
4571 __CONCAT(PMTYPE, copy_page)(vm_page_t src, vm_page_t dst)
4573 pt_entry_t *cmap_pte1, *cmap_pte2;
4578 cmap_pte1 = pc->pc_cmap_pte1;
4579 cmap_pte2 = pc->pc_cmap_pte2;
4580 mtx_lock(&pc->pc_cmap_lock);
4582 panic("pmap_copy_page: CMAP1 busy");
4584 panic("pmap_copy_page: CMAP2 busy");
4585 *cmap_pte1 = PG_V | VM_PAGE_TO_PHYS(src) | PG_A |
4586 pmap_cache_bits(kernel_pmap, src->md.pat_mode, 0);
4587 invlcaddr(pc->pc_cmap_addr1);
4588 *cmap_pte2 = PG_V | PG_RW | VM_PAGE_TO_PHYS(dst) | PG_A | PG_M |
4589 pmap_cache_bits(kernel_pmap, dst->md.pat_mode, 0);
4590 invlcaddr(pc->pc_cmap_addr2);
4591 bcopy(pc->pc_cmap_addr1, pc->pc_cmap_addr2, PAGE_SIZE);
4595 mtx_unlock(&pc->pc_cmap_lock);
4599 __CONCAT(PMTYPE, copy_pages)(vm_page_t ma[], vm_offset_t a_offset,
4600 vm_page_t mb[], vm_offset_t b_offset, int xfersize)
4602 vm_page_t a_pg, b_pg;
4604 vm_offset_t a_pg_offset, b_pg_offset;
4605 pt_entry_t *cmap_pte1, *cmap_pte2;
4611 cmap_pte1 = pc->pc_cmap_pte1;
4612 cmap_pte2 = pc->pc_cmap_pte2;
4613 mtx_lock(&pc->pc_cmap_lock);
4614 if (*cmap_pte1 != 0)
4615 panic("pmap_copy_pages: CMAP1 busy");
4616 if (*cmap_pte2 != 0)
4617 panic("pmap_copy_pages: CMAP2 busy");
4618 while (xfersize > 0) {
4619 a_pg = ma[a_offset >> PAGE_SHIFT];
4620 a_pg_offset = a_offset & PAGE_MASK;
4621 cnt = min(xfersize, PAGE_SIZE - a_pg_offset);
4622 b_pg = mb[b_offset >> PAGE_SHIFT];
4623 b_pg_offset = b_offset & PAGE_MASK;
4624 cnt = min(cnt, PAGE_SIZE - b_pg_offset);
4625 *cmap_pte1 = PG_V | VM_PAGE_TO_PHYS(a_pg) | PG_A |
4626 pmap_cache_bits(kernel_pmap, a_pg->md.pat_mode, 0);
4627 invlcaddr(pc->pc_cmap_addr1);
4628 *cmap_pte2 = PG_V | PG_RW | VM_PAGE_TO_PHYS(b_pg) | PG_A |
4629 PG_M | pmap_cache_bits(kernel_pmap, b_pg->md.pat_mode, 0);
4630 invlcaddr(pc->pc_cmap_addr2);
4631 a_cp = pc->pc_cmap_addr1 + a_pg_offset;
4632 b_cp = pc->pc_cmap_addr2 + b_pg_offset;
4633 bcopy(a_cp, b_cp, cnt);
4641 mtx_unlock(&pc->pc_cmap_lock);
4645 * Returns true if the pmap's pv is one of the first
4646 * 16 pvs linked to from this page. This count may
4647 * be changed upwards or downwards in the future; it
4648 * is only necessary that true be returned for a small
4649 * subset of pmaps for proper page aging.
4652 __CONCAT(PMTYPE, page_exists_quick)(pmap_t pmap, vm_page_t m)
4654 struct md_page *pvh;
4659 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4660 ("pmap_page_exists_quick: page %p is not managed", m));
4662 rw_wlock(&pvh_global_lock);
4663 TAILQ_FOREACH(pv, &m->md.pv_list, pv_next) {
4664 if (PV_PMAP(pv) == pmap) {
4672 if (!rv && loops < 16 && (m->flags & PG_FICTITIOUS) == 0) {
4673 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
4674 TAILQ_FOREACH(pv, &pvh->pv_list, pv_next) {
4675 if (PV_PMAP(pv) == pmap) {
4684 rw_wunlock(&pvh_global_lock);
4689 * pmap_page_wired_mappings:
4691 * Return the number of managed mappings to the given physical page
4695 __CONCAT(PMTYPE, page_wired_mappings)(vm_page_t m)
4700 if ((m->oflags & VPO_UNMANAGED) != 0)
4702 rw_wlock(&pvh_global_lock);
4703 count = pmap_pvh_wired_mappings(&m->md, count);
4704 if ((m->flags & PG_FICTITIOUS) == 0) {
4705 count = pmap_pvh_wired_mappings(pa_to_pvh(VM_PAGE_TO_PHYS(m)),
4708 rw_wunlock(&pvh_global_lock);
4713 * pmap_pvh_wired_mappings:
4715 * Return the updated number "count" of managed mappings that are wired.
4718 pmap_pvh_wired_mappings(struct md_page *pvh, int count)
4724 rw_assert(&pvh_global_lock, RA_WLOCKED);
4726 TAILQ_FOREACH(pv, &pvh->pv_list, pv_next) {
4729 pte = pmap_pte_quick(pmap, pv->pv_va);
4730 if ((*pte & PG_W) != 0)
4739 * Returns TRUE if the given page is mapped individually or as part of
4740 * a 4mpage. Otherwise, returns FALSE.
4743 __CONCAT(PMTYPE, page_is_mapped)(vm_page_t m)
4747 if ((m->oflags & VPO_UNMANAGED) != 0)
4749 rw_wlock(&pvh_global_lock);
4750 rv = !TAILQ_EMPTY(&m->md.pv_list) ||
4751 ((m->flags & PG_FICTITIOUS) == 0 &&
4752 !TAILQ_EMPTY(&pa_to_pvh(VM_PAGE_TO_PHYS(m))->pv_list));
4753 rw_wunlock(&pvh_global_lock);
4758 * Remove all pages from specified address space
4759 * this aids process exit speeds. Also, this code
4760 * is special cased for current process only, but
4761 * can have the more generic (and slightly slower)
4762 * mode enabled. This is much faster than pmap_remove
4763 * in the case of running down an entire address space.
4766 __CONCAT(PMTYPE, remove_pages)(pmap_t pmap)
4768 pt_entry_t *pte, tpte;
4769 vm_page_t m, mpte, mt;
4771 struct md_page *pvh;
4772 struct pv_chunk *pc, *npc;
4773 struct spglist free;
4776 uint32_t inuse, bitmask;
4779 if (pmap != PCPU_GET(curpmap)) {
4780 printf("warning: pmap_remove_pages called with non-current pmap\n");
4784 rw_wlock(&pvh_global_lock);
4787 TAILQ_FOREACH_SAFE(pc, &pmap->pm_pvchunk, pc_list, npc) {
4788 KASSERT(pc->pc_pmap == pmap, ("Wrong pmap %p %p", pmap,
4791 for (field = 0; field < _NPCM; field++) {
4792 inuse = ~pc->pc_map[field] & pc_freemask[field];
4793 while (inuse != 0) {
4795 bitmask = 1UL << bit;
4796 idx = field * 32 + bit;
4797 pv = &pc->pc_pventry[idx];
4800 pte = pmap_pde(pmap, pv->pv_va);
4802 if ((tpte & PG_PS) == 0) {
4803 pte = pmap_pte_quick(pmap, pv->pv_va);
4804 tpte = *pte & ~PG_PTE_PAT;
4809 "TPTE at %p IS ZERO @ VA %08x\n",
4815 * We cannot remove wired pages from a process' mapping at this time
4822 m = PHYS_TO_VM_PAGE(tpte & PG_FRAME);
4823 KASSERT(m->phys_addr == (tpte & PG_FRAME),
4824 ("vm_page_t %p phys_addr mismatch %016jx %016jx",
4825 m, (uintmax_t)m->phys_addr,
4828 KASSERT((m->flags & PG_FICTITIOUS) != 0 ||
4829 m < &vm_page_array[vm_page_array_size],
4830 ("pmap_remove_pages: bad tpte %#jx",
4836 * Update the vm_page_t clean/reference bits.
4838 if ((tpte & (PG_M | PG_RW)) == (PG_M | PG_RW)) {
4839 if ((tpte & PG_PS) != 0) {
4840 for (mt = m; mt < &m[NBPDR / PAGE_SIZE]; mt++)
4847 PV_STAT(pv_entry_frees++);
4848 PV_STAT(pv_entry_spare++);
4850 pc->pc_map[field] |= bitmask;
4851 if ((tpte & PG_PS) != 0) {
4852 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
4853 pvh = pa_to_pvh(tpte & PG_PS_FRAME);
4854 TAILQ_REMOVE(&pvh->pv_list, pv, pv_next);
4855 if (TAILQ_EMPTY(&pvh->pv_list)) {
4856 for (mt = m; mt < &m[NBPDR / PAGE_SIZE]; mt++)
4857 if (TAILQ_EMPTY(&mt->md.pv_list))
4858 vm_page_aflag_clear(mt, PGA_WRITEABLE);
4860 mpte = pmap_remove_pt_page(pmap, pv->pv_va);
4862 KASSERT(mpte->valid == VM_PAGE_BITS_ALL,
4863 ("pmap_remove_pages: pte page not promoted"));
4864 pmap->pm_stats.resident_count--;
4865 KASSERT(mpte->ref_count == NPTEPG,
4866 ("pmap_remove_pages: pte page ref count error"));
4867 mpte->ref_count = 0;
4868 pmap_add_delayed_free_list(mpte, &free, FALSE);
4871 pmap->pm_stats.resident_count--;
4872 TAILQ_REMOVE(&m->md.pv_list, pv, pv_next);
4873 if (TAILQ_EMPTY(&m->md.pv_list) &&
4874 (m->flags & PG_FICTITIOUS) == 0) {
4875 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
4876 if (TAILQ_EMPTY(&pvh->pv_list))
4877 vm_page_aflag_clear(m, PGA_WRITEABLE);
4879 pmap_unuse_pt(pmap, pv->pv_va, &free);
4884 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
4889 pmap_invalidate_all_int(pmap);
4890 rw_wunlock(&pvh_global_lock);
4892 vm_page_free_pages_toq(&free, true);
4898 * Return whether or not the specified physical page was modified
4899 * in any physical maps.
4902 __CONCAT(PMTYPE, is_modified)(vm_page_t m)
4906 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4907 ("pmap_is_modified: page %p is not managed", m));
4910 * If the page is not busied then this check is racy.
4912 if (!pmap_page_is_write_mapped(m))
4914 rw_wlock(&pvh_global_lock);
4915 rv = pmap_is_modified_pvh(&m->md) ||
4916 ((m->flags & PG_FICTITIOUS) == 0 &&
4917 pmap_is_modified_pvh(pa_to_pvh(VM_PAGE_TO_PHYS(m))));
4918 rw_wunlock(&pvh_global_lock);
4923 * Returns TRUE if any of the given mappings were used to modify
4924 * physical memory. Otherwise, returns FALSE. Both page and 2mpage
4925 * mappings are supported.
4928 pmap_is_modified_pvh(struct md_page *pvh)
4935 rw_assert(&pvh_global_lock, RA_WLOCKED);
4938 TAILQ_FOREACH(pv, &pvh->pv_list, pv_next) {
4941 pte = pmap_pte_quick(pmap, pv->pv_va);
4942 rv = (*pte & (PG_M | PG_RW)) == (PG_M | PG_RW);
4952 * pmap_is_prefaultable:
4954 * Return whether or not the specified virtual address is elgible
4958 __CONCAT(PMTYPE, is_prefaultable)(pmap_t pmap, vm_offset_t addr)
4965 pde = *pmap_pde(pmap, addr);
4966 if (pde != 0 && (pde & PG_PS) == 0)
4967 rv = pmap_pte_ufast(pmap, addr, pde) == 0;
4973 * pmap_is_referenced:
4975 * Return whether or not the specified physical page was referenced
4976 * in any physical maps.
4979 __CONCAT(PMTYPE, is_referenced)(vm_page_t m)
4983 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4984 ("pmap_is_referenced: page %p is not managed", m));
4985 rw_wlock(&pvh_global_lock);
4986 rv = pmap_is_referenced_pvh(&m->md) ||
4987 ((m->flags & PG_FICTITIOUS) == 0 &&
4988 pmap_is_referenced_pvh(pa_to_pvh(VM_PAGE_TO_PHYS(m))));
4989 rw_wunlock(&pvh_global_lock);
4994 * Returns TRUE if any of the given mappings were referenced and FALSE
4995 * otherwise. Both page and 4mpage mappings are supported.
4998 pmap_is_referenced_pvh(struct md_page *pvh)
5005 rw_assert(&pvh_global_lock, RA_WLOCKED);
5008 TAILQ_FOREACH(pv, &pvh->pv_list, pv_next) {
5011 pte = pmap_pte_quick(pmap, pv->pv_va);
5012 rv = (*pte & (PG_A | PG_V)) == (PG_A | PG_V);
5022 * Clear the write and modified bits in each of the given page's mappings.
5025 __CONCAT(PMTYPE, remove_write)(vm_page_t m)
5027 struct md_page *pvh;
5028 pv_entry_t next_pv, pv;
5031 pt_entry_t oldpte, *pte;
5034 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
5035 ("pmap_remove_write: page %p is not managed", m));
5036 vm_page_assert_busied(m);
5038 if (!pmap_page_is_write_mapped(m))
5040 rw_wlock(&pvh_global_lock);
5042 if ((m->flags & PG_FICTITIOUS) != 0)
5043 goto small_mappings;
5044 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
5045 TAILQ_FOREACH_SAFE(pv, &pvh->pv_list, pv_next, next_pv) {
5049 pde = pmap_pde(pmap, va);
5050 if ((*pde & PG_RW) != 0)
5051 (void)pmap_demote_pde(pmap, pde, va);
5055 TAILQ_FOREACH(pv, &m->md.pv_list, pv_next) {
5058 pde = pmap_pde(pmap, pv->pv_va);
5059 KASSERT((*pde & PG_PS) == 0, ("pmap_clear_write: found"
5060 " a 4mpage in page %p's pv list", m));
5061 pte = pmap_pte_quick(pmap, pv->pv_va);
5064 if ((oldpte & PG_RW) != 0) {
5066 * Regardless of whether a pte is 32 or 64 bits
5067 * in size, PG_RW and PG_M are among the least
5068 * significant 32 bits.
5070 if (!atomic_cmpset_int((u_int *)pte, oldpte,
5071 oldpte & ~(PG_RW | PG_M)))
5073 if ((oldpte & PG_M) != 0)
5075 pmap_invalidate_page_int(pmap, pv->pv_va);
5079 vm_page_aflag_clear(m, PGA_WRITEABLE);
5081 rw_wunlock(&pvh_global_lock);
5085 * pmap_ts_referenced:
5087 * Return a count of reference bits for a page, clearing those bits.
5088 * It is not necessary for every reference bit to be cleared, but it
5089 * is necessary that 0 only be returned when there are truly no
5090 * reference bits set.
5092 * As an optimization, update the page's dirty field if a modified bit is
5093 * found while counting reference bits. This opportunistic update can be
5094 * performed at low cost and can eliminate the need for some future calls
5095 * to pmap_is_modified(). However, since this function stops after
5096 * finding PMAP_TS_REFERENCED_MAX reference bits, it may not detect some
5097 * dirty pages. Those dirty pages will only be detected by a future call
5098 * to pmap_is_modified().
5101 __CONCAT(PMTYPE, ts_referenced)(vm_page_t m)
5103 struct md_page *pvh;
5111 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
5112 ("pmap_ts_referenced: page %p is not managed", m));
5113 pa = VM_PAGE_TO_PHYS(m);
5114 pvh = pa_to_pvh(pa);
5115 rw_wlock(&pvh_global_lock);
5117 if ((m->flags & PG_FICTITIOUS) != 0 ||
5118 (pvf = TAILQ_FIRST(&pvh->pv_list)) == NULL)
5119 goto small_mappings;
5124 pde = pmap_pde(pmap, pv->pv_va);
5125 if ((*pde & (PG_M | PG_RW)) == (PG_M | PG_RW)) {
5127 * Although "*pde" is mapping a 2/4MB page, because
5128 * this function is called at a 4KB page granularity,
5129 * we only update the 4KB page under test.
5133 if ((*pde & PG_A) != 0) {
5135 * Since this reference bit is shared by either 1024
5136 * or 512 4KB pages, it should not be cleared every
5137 * time it is tested. Apply a simple "hash" function
5138 * on the physical page number, the virtual superpage
5139 * number, and the pmap address to select one 4KB page
5140 * out of the 1024 or 512 on which testing the
5141 * reference bit will result in clearing that bit.
5142 * This function is designed to avoid the selection of
5143 * the same 4KB page for every 2- or 4MB page mapping.
5145 * On demotion, a mapping that hasn't been referenced
5146 * is simply destroyed. To avoid the possibility of a
5147 * subsequent page fault on a demoted wired mapping,
5148 * always leave its reference bit set. Moreover,
5149 * since the superpage is wired, the current state of
5150 * its reference bit won't affect page replacement.
5152 if ((((pa >> PAGE_SHIFT) ^ (pv->pv_va >> PDRSHIFT) ^
5153 (uintptr_t)pmap) & (NPTEPG - 1)) == 0 &&
5154 (*pde & PG_W) == 0) {
5155 atomic_clear_int((u_int *)pde, PG_A);
5156 pmap_invalidate_page_int(pmap, pv->pv_va);
5161 /* Rotate the PV list if it has more than one entry. */
5162 if (TAILQ_NEXT(pv, pv_next) != NULL) {
5163 TAILQ_REMOVE(&pvh->pv_list, pv, pv_next);
5164 TAILQ_INSERT_TAIL(&pvh->pv_list, pv, pv_next);
5166 if (rtval >= PMAP_TS_REFERENCED_MAX)
5168 } while ((pv = TAILQ_FIRST(&pvh->pv_list)) != pvf);
5170 if ((pvf = TAILQ_FIRST(&m->md.pv_list)) == NULL)
5176 pde = pmap_pde(pmap, pv->pv_va);
5177 KASSERT((*pde & PG_PS) == 0,
5178 ("pmap_ts_referenced: found a 4mpage in page %p's pv list",
5180 pte = pmap_pte_quick(pmap, pv->pv_va);
5181 if ((*pte & (PG_M | PG_RW)) == (PG_M | PG_RW))
5183 if ((*pte & PG_A) != 0) {
5184 atomic_clear_int((u_int *)pte, PG_A);
5185 pmap_invalidate_page_int(pmap, pv->pv_va);
5189 /* Rotate the PV list if it has more than one entry. */
5190 if (TAILQ_NEXT(pv, pv_next) != NULL) {
5191 TAILQ_REMOVE(&m->md.pv_list, pv, pv_next);
5192 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_next);
5194 } while ((pv = TAILQ_FIRST(&m->md.pv_list)) != pvf && rtval <
5195 PMAP_TS_REFERENCED_MAX);
5198 rw_wunlock(&pvh_global_lock);
5203 * Apply the given advice to the specified range of addresses within the
5204 * given pmap. Depending on the advice, clear the referenced and/or
5205 * modified flags in each mapping and set the mapped page's dirty field.
5208 __CONCAT(PMTYPE, advise)(pmap_t pmap, vm_offset_t sva, vm_offset_t eva,
5211 pd_entry_t oldpde, *pde;
5213 vm_offset_t va, pdnxt;
5215 bool anychanged, pv_lists_locked;
5217 if (advice != MADV_DONTNEED && advice != MADV_FREE)
5219 if (pmap_is_current(pmap))
5220 pv_lists_locked = false;
5222 pv_lists_locked = true;
5224 rw_wlock(&pvh_global_lock);
5229 for (; sva < eva; sva = pdnxt) {
5230 pdnxt = (sva + NBPDR) & ~PDRMASK;
5233 pde = pmap_pde(pmap, sva);
5235 if ((oldpde & PG_V) == 0)
5237 else if ((oldpde & PG_PS) != 0) {
5238 if ((oldpde & PG_MANAGED) == 0)
5240 if (!pv_lists_locked) {
5241 pv_lists_locked = true;
5242 if (!rw_try_wlock(&pvh_global_lock)) {
5244 pmap_invalidate_all_int(pmap);
5250 if (!pmap_demote_pde(pmap, pde, sva)) {
5252 * The large page mapping was destroyed.
5258 * Unless the page mappings are wired, remove the
5259 * mapping to a single page so that a subsequent
5260 * access may repromote. Choosing the last page
5261 * within the address range [sva, min(pdnxt, eva))
5262 * generally results in more repromotions. Since the
5263 * underlying page table page is fully populated, this
5264 * removal never frees a page table page.
5266 if ((oldpde & PG_W) == 0) {
5272 ("pmap_advise: no address gap"));
5273 pte = pmap_pte_quick(pmap, va);
5274 KASSERT((*pte & PG_V) != 0,
5275 ("pmap_advise: invalid PTE"));
5276 pmap_remove_pte(pmap, pte, va, NULL);
5283 for (pte = pmap_pte_quick(pmap, sva); sva != pdnxt; pte++,
5285 if ((*pte & (PG_MANAGED | PG_V)) != (PG_MANAGED | PG_V))
5287 else if ((*pte & (PG_M | PG_RW)) == (PG_M | PG_RW)) {
5288 if (advice == MADV_DONTNEED) {
5290 * Future calls to pmap_is_modified()
5291 * can be avoided by making the page
5294 m = PHYS_TO_VM_PAGE(*pte & PG_FRAME);
5297 atomic_clear_int((u_int *)pte, PG_M | PG_A);
5298 } else if ((*pte & PG_A) != 0)
5299 atomic_clear_int((u_int *)pte, PG_A);
5302 if ((*pte & PG_G) != 0) {
5310 pmap_invalidate_range_int(pmap, va, sva);
5315 pmap_invalidate_range_int(pmap, va, sva);
5318 pmap_invalidate_all_int(pmap);
5319 if (pv_lists_locked) {
5321 rw_wunlock(&pvh_global_lock);
5327 * Clear the modify bits on the specified physical page.
5330 __CONCAT(PMTYPE, clear_modify)(vm_page_t m)
5332 struct md_page *pvh;
5333 pv_entry_t next_pv, pv;
5335 pd_entry_t oldpde, *pde;
5339 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
5340 ("pmap_clear_modify: page %p is not managed", m));
5341 vm_page_assert_busied(m);
5343 if (!pmap_page_is_write_mapped(m))
5345 rw_wlock(&pvh_global_lock);
5347 if ((m->flags & PG_FICTITIOUS) != 0)
5348 goto small_mappings;
5349 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
5350 TAILQ_FOREACH_SAFE(pv, &pvh->pv_list, pv_next, next_pv) {
5354 pde = pmap_pde(pmap, va);
5356 /* If oldpde has PG_RW set, then it also has PG_M set. */
5357 if ((oldpde & PG_RW) != 0 &&
5358 pmap_demote_pde(pmap, pde, va) &&
5359 (oldpde & PG_W) == 0) {
5361 * Write protect the mapping to a single page so that
5362 * a subsequent write access may repromote.
5364 va += VM_PAGE_TO_PHYS(m) - (oldpde & PG_PS_FRAME);
5365 pte = pmap_pte_quick(pmap, va);
5367 * Regardless of whether a pte is 32 or 64 bits
5368 * in size, PG_RW and PG_M are among the least
5369 * significant 32 bits.
5371 atomic_clear_int((u_int *)pte, PG_M | PG_RW);
5373 pmap_invalidate_page_int(pmap, va);
5378 TAILQ_FOREACH(pv, &m->md.pv_list, pv_next) {
5381 pde = pmap_pde(pmap, pv->pv_va);
5382 KASSERT((*pde & PG_PS) == 0, ("pmap_clear_modify: found"
5383 " a 4mpage in page %p's pv list", m));
5384 pte = pmap_pte_quick(pmap, pv->pv_va);
5385 if ((*pte & (PG_M | PG_RW)) == (PG_M | PG_RW)) {
5387 * Regardless of whether a pte is 32 or 64 bits
5388 * in size, PG_M is among the least significant
5391 atomic_clear_int((u_int *)pte, PG_M);
5392 pmap_invalidate_page_int(pmap, pv->pv_va);
5397 rw_wunlock(&pvh_global_lock);
5401 * Miscellaneous support routines follow
5404 /* Adjust the cache mode for a 4KB page mapped via a PTE. */
5405 static __inline void
5406 pmap_pte_attr(pt_entry_t *pte, int cache_bits)
5411 * The cache mode bits are all in the low 32-bits of the
5412 * PTE, so we can just spin on updating the low 32-bits.
5415 opte = *(u_int *)pte;
5416 npte = opte & ~PG_PTE_CACHE;
5418 } while (npte != opte && !atomic_cmpset_int((u_int *)pte, opte, npte));
5421 /* Adjust the cache mode for a 2/4MB page mapped via a PDE. */
5422 static __inline void
5423 pmap_pde_attr(pd_entry_t *pde, int cache_bits)
5428 * The cache mode bits are all in the low 32-bits of the
5429 * PDE, so we can just spin on updating the low 32-bits.
5432 opde = *(u_int *)pde;
5433 npde = opde & ~PG_PDE_CACHE;
5435 } while (npde != opde && !atomic_cmpset_int((u_int *)pde, opde, npde));
5439 * Map a set of physical memory pages into the kernel virtual
5440 * address space. Return a pointer to where it is mapped. This
5441 * routine is intended to be used for mapping device memory,
5445 __CONCAT(PMTYPE, mapdev_attr)(vm_paddr_t pa, vm_size_t size, int mode,
5448 struct pmap_preinit_mapping *ppim;
5449 vm_offset_t va, offset;
5454 offset = pa & PAGE_MASK;
5455 size = round_page(offset + size);
5458 if (pa < PMAP_MAP_LOW && pa + size <= PMAP_MAP_LOW) {
5459 va = pa + PMAP_MAP_LOW;
5460 if ((flags & MAPDEV_SETATTR) == 0)
5461 return ((void *)(va + offset));
5462 } else if (!pmap_initialized) {
5464 for (i = 0; i < PMAP_PREINIT_MAPPING_COUNT; i++) {
5465 ppim = pmap_preinit_mapping + i;
5466 if (ppim->va == 0) {
5470 ppim->va = virtual_avail;
5471 virtual_avail += size;
5477 panic("%s: too many preinit mappings", __func__);
5480 * If we have a preinit mapping, re-use it.
5482 for (i = 0; i < PMAP_PREINIT_MAPPING_COUNT; i++) {
5483 ppim = pmap_preinit_mapping + i;
5484 if (ppim->pa == pa && ppim->sz == size &&
5485 (ppim->mode == mode ||
5486 (flags & MAPDEV_SETATTR) == 0))
5487 return ((void *)(ppim->va + offset));
5489 va = kva_alloc(size);
5491 panic("%s: Couldn't allocate KVA", __func__);
5493 for (tmpsize = 0; tmpsize < size; tmpsize += PAGE_SIZE) {
5494 if ((flags & MAPDEV_SETATTR) == 0 && pmap_initialized) {
5495 m = PHYS_TO_VM_PAGE(pa);
5496 if (m != NULL && VM_PAGE_TO_PHYS(m) == pa) {
5497 pmap_kenter_attr(va + tmpsize, pa + tmpsize,
5502 pmap_kenter_attr(va + tmpsize, pa + tmpsize, mode);
5504 pmap_invalidate_range_int(kernel_pmap, va, va + tmpsize);
5505 pmap_invalidate_cache_range(va, va + size);
5506 return ((void *)(va + offset));
5510 __CONCAT(PMTYPE, unmapdev)(vm_offset_t va, vm_size_t size)
5512 struct pmap_preinit_mapping *ppim;
5516 if (va >= PMAP_MAP_LOW && va <= KERNBASE && va + size <= KERNBASE)
5518 offset = va & PAGE_MASK;
5519 size = round_page(offset + size);
5520 va = trunc_page(va);
5521 for (i = 0; i < PMAP_PREINIT_MAPPING_COUNT; i++) {
5522 ppim = pmap_preinit_mapping + i;
5523 if (ppim->va == va && ppim->sz == size) {
5524 if (pmap_initialized)
5530 if (va + size == virtual_avail)
5535 if (pmap_initialized) {
5536 pmap_qremove(va, atop(size));
5542 * Sets the memory attribute for the specified page.
5545 __CONCAT(PMTYPE, page_set_memattr)(vm_page_t m, vm_memattr_t ma)
5548 m->md.pat_mode = ma;
5549 if ((m->flags & PG_FICTITIOUS) != 0)
5553 * If "m" is a normal page, flush it from the cache.
5554 * See pmap_invalidate_cache_range().
5556 * First, try to find an existing mapping of the page by sf
5557 * buffer. sf_buf_invalidate_cache() modifies mapping and
5558 * flushes the cache.
5560 if (sf_buf_invalidate_cache(m))
5564 * If page is not mapped by sf buffer, but CPU does not
5565 * support self snoop, map the page transient and do
5566 * invalidation. In the worst case, whole cache is flushed by
5567 * pmap_invalidate_cache_range().
5569 if ((cpu_feature & CPUID_SS) == 0)
5574 __CONCAT(PMTYPE, flush_page)(vm_page_t m)
5576 pt_entry_t *cmap_pte2;
5578 vm_offset_t sva, eva;
5581 useclflushopt = (cpu_stdext_feature & CPUID_STDEXT_CLFLUSHOPT) != 0;
5582 if (useclflushopt || (cpu_feature & CPUID_CLFSH) != 0) {
5585 cmap_pte2 = pc->pc_cmap_pte2;
5586 mtx_lock(&pc->pc_cmap_lock);
5588 panic("pmap_flush_page: CMAP2 busy");
5589 *cmap_pte2 = PG_V | PG_RW | VM_PAGE_TO_PHYS(m) |
5590 PG_A | PG_M | pmap_cache_bits(kernel_pmap, m->md.pat_mode,
5592 invlcaddr(pc->pc_cmap_addr2);
5593 sva = (vm_offset_t)pc->pc_cmap_addr2;
5594 eva = sva + PAGE_SIZE;
5597 * Use mfence or sfence despite the ordering implied by
5598 * mtx_{un,}lock() because clflush on non-Intel CPUs
5599 * and clflushopt are not guaranteed to be ordered by
5600 * any other instruction.
5604 else if (cpu_vendor_id != CPU_VENDOR_INTEL)
5606 for (; sva < eva; sva += cpu_clflush_line_size) {
5614 else if (cpu_vendor_id != CPU_VENDOR_INTEL)
5618 mtx_unlock(&pc->pc_cmap_lock);
5620 pmap_invalidate_cache();
5624 * Changes the specified virtual address range's memory type to that given by
5625 * the parameter "mode". The specified virtual address range must be
5626 * completely contained within either the kernel map.
5628 * Returns zero if the change completed successfully, and either EINVAL or
5629 * ENOMEM if the change failed. Specifically, EINVAL is returned if some part
5630 * of the virtual address range was not mapped, and ENOMEM is returned if
5631 * there was insufficient memory available to complete the change.
5634 __CONCAT(PMTYPE, change_attr)(vm_offset_t va, vm_size_t size, int mode)
5636 vm_offset_t base, offset, tmpva;
5639 int cache_bits_pte, cache_bits_pde;
5642 base = trunc_page(va);
5643 offset = va & PAGE_MASK;
5644 size = round_page(offset + size);
5647 * Only supported on kernel virtual addresses above the recursive map.
5649 if (base < VM_MIN_KERNEL_ADDRESS)
5652 cache_bits_pde = pmap_cache_bits(kernel_pmap, mode, 1);
5653 cache_bits_pte = pmap_cache_bits(kernel_pmap, mode, 0);
5657 * Pages that aren't mapped aren't supported. Also break down
5658 * 2/4MB pages into 4KB pages if required.
5660 PMAP_LOCK(kernel_pmap);
5661 for (tmpva = base; tmpva < base + size; ) {
5662 pde = pmap_pde(kernel_pmap, tmpva);
5664 PMAP_UNLOCK(kernel_pmap);
5669 * If the current 2/4MB page already has
5670 * the required memory type, then we need not
5671 * demote this page. Just increment tmpva to
5672 * the next 2/4MB page frame.
5674 if ((*pde & PG_PDE_CACHE) == cache_bits_pde) {
5675 tmpva = trunc_4mpage(tmpva) + NBPDR;
5680 * If the current offset aligns with a 2/4MB
5681 * page frame and there is at least 2/4MB left
5682 * within the range, then we need not break
5683 * down this page into 4KB pages.
5685 if ((tmpva & PDRMASK) == 0 &&
5686 tmpva + PDRMASK < base + size) {
5690 if (!pmap_demote_pde(kernel_pmap, pde, tmpva)) {
5691 PMAP_UNLOCK(kernel_pmap);
5695 pte = vtopte(tmpva);
5697 PMAP_UNLOCK(kernel_pmap);
5702 PMAP_UNLOCK(kernel_pmap);
5705 * Ok, all the pages exist, so run through them updating their
5706 * cache mode if required.
5708 for (tmpva = base; tmpva < base + size; ) {
5709 pde = pmap_pde(kernel_pmap, tmpva);
5711 if ((*pde & PG_PDE_CACHE) != cache_bits_pde) {
5712 pmap_pde_attr(pde, cache_bits_pde);
5715 tmpva = trunc_4mpage(tmpva) + NBPDR;
5717 pte = vtopte(tmpva);
5718 if ((*pte & PG_PTE_CACHE) != cache_bits_pte) {
5719 pmap_pte_attr(pte, cache_bits_pte);
5727 * Flush CPU caches to make sure any data isn't cached that
5728 * shouldn't be, etc.
5731 pmap_invalidate_range_int(kernel_pmap, base, tmpva);
5732 pmap_invalidate_cache_range(base, tmpva);
5738 * Perform the pmap work for mincore(2). If the page is not both referenced and
5739 * modified by this pmap, returns its physical address so that the caller can
5740 * find other mappings.
5743 __CONCAT(PMTYPE, mincore)(pmap_t pmap, vm_offset_t addr, vm_paddr_t *pap)
5751 pde = *pmap_pde(pmap, addr);
5753 if ((pde & PG_PS) != 0) {
5755 /* Compute the physical address of the 4KB page. */
5756 pa = ((pde & PG_PS_FRAME) | (addr & PDRMASK)) &
5758 val = MINCORE_PSIND(1);
5760 pte = pmap_pte_ufast(pmap, addr, pde);
5761 pa = pte & PG_FRAME;
5769 if ((pte & PG_V) != 0) {
5770 val |= MINCORE_INCORE;
5771 if ((pte & (PG_M | PG_RW)) == (PG_M | PG_RW))
5772 val |= MINCORE_MODIFIED | MINCORE_MODIFIED_OTHER;
5773 if ((pte & PG_A) != 0)
5774 val |= MINCORE_REFERENCED | MINCORE_REFERENCED_OTHER;
5776 if ((val & (MINCORE_MODIFIED_OTHER | MINCORE_REFERENCED_OTHER)) !=
5777 (MINCORE_MODIFIED_OTHER | MINCORE_REFERENCED_OTHER) &&
5778 (pte & (PG_MANAGED | PG_V)) == (PG_MANAGED | PG_V)) {
5786 __CONCAT(PMTYPE, activate)(struct thread *td)
5788 pmap_t pmap, oldpmap;
5793 pmap = vmspace_pmap(td->td_proc->p_vmspace);
5794 oldpmap = PCPU_GET(curpmap);
5795 cpuid = PCPU_GET(cpuid);
5797 CPU_CLR_ATOMIC(cpuid, &oldpmap->pm_active);
5798 CPU_SET_ATOMIC(cpuid, &pmap->pm_active);
5800 CPU_CLR(cpuid, &oldpmap->pm_active);
5801 CPU_SET(cpuid, &pmap->pm_active);
5803 #ifdef PMAP_PAE_COMP
5804 cr3 = vtophys(pmap->pm_pdpt);
5806 cr3 = vtophys(pmap->pm_pdir);
5809 * pmap_activate is for the current thread on the current cpu
5811 td->td_pcb->pcb_cr3 = cr3;
5812 PCPU_SET(curpmap, pmap);
5817 __CONCAT(PMTYPE, activate_boot)(pmap_t pmap)
5821 cpuid = PCPU_GET(cpuid);
5823 CPU_SET_ATOMIC(cpuid, &pmap->pm_active);
5825 CPU_SET(cpuid, &pmap->pm_active);
5827 PCPU_SET(curpmap, pmap);
5831 * Increase the starting virtual address of the given mapping if a
5832 * different alignment might result in more superpage mappings.
5835 __CONCAT(PMTYPE, align_superpage)(vm_object_t object, vm_ooffset_t offset,
5836 vm_offset_t *addr, vm_size_t size)
5838 vm_offset_t superpage_offset;
5842 if (object != NULL && (object->flags & OBJ_COLORED) != 0)
5843 offset += ptoa(object->pg_color);
5844 superpage_offset = offset & PDRMASK;
5845 if (size - ((NBPDR - superpage_offset) & PDRMASK) < NBPDR ||
5846 (*addr & PDRMASK) == superpage_offset)
5848 if ((*addr & PDRMASK) < superpage_offset)
5849 *addr = (*addr & ~PDRMASK) + superpage_offset;
5851 *addr = ((*addr + PDRMASK) & ~PDRMASK) + superpage_offset;
5855 __CONCAT(PMTYPE, quick_enter_page)(vm_page_t m)
5861 qaddr = PCPU_GET(qmap_addr);
5862 pte = vtopte(qaddr);
5865 ("pmap_quick_enter_page: PTE busy %#jx", (uintmax_t)*pte));
5866 *pte = PG_V | PG_RW | VM_PAGE_TO_PHYS(m) | PG_A | PG_M |
5867 pmap_cache_bits(kernel_pmap, pmap_page_get_memattr(m), 0);
5874 __CONCAT(PMTYPE, quick_remove_page)(vm_offset_t addr)
5879 qaddr = PCPU_GET(qmap_addr);
5880 pte = vtopte(qaddr);
5882 KASSERT(*pte != 0, ("pmap_quick_remove_page: PTE not in use"));
5883 KASSERT(addr == qaddr, ("pmap_quick_remove_page: invalid address"));
5889 static vmem_t *pmap_trm_arena;
5890 static vmem_addr_t pmap_trm_arena_last = PMAP_TRM_MIN_ADDRESS;
5891 static int trm_guard = PAGE_SIZE;
5894 pmap_trm_import(void *unused __unused, vmem_size_t size, int flags,
5898 vmem_addr_t af, addr, prev_addr;
5899 pt_entry_t *trm_pte;
5901 prev_addr = atomic_load_long(&pmap_trm_arena_last);
5902 size = round_page(size) + trm_guard;
5904 if (prev_addr + size < prev_addr || prev_addr + size < size ||
5905 prev_addr + size > PMAP_TRM_MAX_ADDRESS)
5907 addr = prev_addr + size;
5908 if (atomic_fcmpset_int(&pmap_trm_arena_last, &prev_addr, addr))
5911 prev_addr += trm_guard;
5912 trm_pte = PTmap + atop(prev_addr);
5913 for (af = prev_addr; af < addr; af += PAGE_SIZE) {
5914 m = vm_page_alloc(NULL, 0, VM_ALLOC_NOOBJ | VM_ALLOC_NOBUSY |
5915 VM_ALLOC_NORMAL | VM_ALLOC_WIRED | VM_ALLOC_WAITOK);
5916 pte_store(&trm_pte[atop(af - prev_addr)], VM_PAGE_TO_PHYS(m) |
5917 PG_M | PG_A | PG_RW | PG_V | pgeflag |
5918 pmap_cache_bits(kernel_pmap, VM_MEMATTR_DEFAULT, FALSE));
5929 TUNABLE_INT_FETCH("machdep.trm_guard", &trm_guard);
5930 if ((trm_guard & PAGE_MASK) != 0)
5932 pmap_trm_arena = vmem_create("i386trampoline", 0, 0, 1, 0, M_WAITOK);
5933 vmem_set_import(pmap_trm_arena, pmap_trm_import, NULL, NULL, PAGE_SIZE);
5934 pd_m = vm_page_alloc(NULL, 0, VM_ALLOC_NOOBJ | VM_ALLOC_NOBUSY |
5935 VM_ALLOC_NORMAL | VM_ALLOC_WIRED | VM_ALLOC_WAITOK | VM_ALLOC_ZERO);
5936 if ((pd_m->flags & PG_ZERO) == 0)
5937 pmap_zero_page(pd_m);
5938 PTD[TRPTDI] = VM_PAGE_TO_PHYS(pd_m) | PG_M | PG_A | PG_RW | PG_V |
5939 pmap_cache_bits(kernel_pmap, VM_MEMATTR_DEFAULT, TRUE);
5943 __CONCAT(PMTYPE, trm_alloc)(size_t size, int flags)
5948 MPASS((flags & ~(M_WAITOK | M_NOWAIT | M_ZERO)) == 0);
5949 error = vmem_xalloc(pmap_trm_arena, roundup2(size, 4), sizeof(int),
5950 0, 0, VMEM_ADDR_MIN, VMEM_ADDR_MAX, flags | M_FIRSTFIT, &res);
5953 if ((flags & M_ZERO) != 0)
5954 bzero((void *)res, size);
5955 return ((void *)res);
5959 __CONCAT(PMTYPE, trm_free)(void *addr, size_t size)
5962 vmem_free(pmap_trm_arena, (uintptr_t)addr, roundup2(size, 4));
5966 __CONCAT(PMTYPE, ksetrw)(vm_offset_t va)
5969 *vtopte(va) |= PG_RW;
5973 __CONCAT(PMTYPE, remap_lowptdi)(bool enable)
5976 PTD[KPTDI] = enable ? PTD[LOWPTDI] : 0;
5981 __CONCAT(PMTYPE, get_map_low)(void)
5984 return (PMAP_MAP_LOW);
5988 __CONCAT(PMTYPE, get_vm_maxuser_address)(void)
5991 return (VM_MAXUSER_ADDRESS);
5995 __CONCAT(PMTYPE, pg_frame)(vm_paddr_t pa)
5998 return (pa & PG_FRAME);
6002 __CONCAT(PMTYPE, sf_buf_map)(struct sf_buf *sf)
6004 pt_entry_t opte, *ptep;
6007 * Update the sf_buf's virtual-to-physical mapping, flushing the
6008 * virtual address from the TLB. Since the reference count for
6009 * the sf_buf's old mapping was zero, that mapping is not
6010 * currently in use. Consequently, there is no need to exchange
6011 * the old and new PTEs atomically, even under PAE.
6013 ptep = vtopte(sf->kva);
6015 *ptep = VM_PAGE_TO_PHYS(sf->m) | PG_RW | PG_V |
6016 pmap_cache_bits(kernel_pmap, sf->m->md.pat_mode, 0);
6019 * Avoid unnecessary TLB invalidations: If the sf_buf's old
6020 * virtual-to-physical mapping was not used, then any processor
6021 * that has invalidated the sf_buf's virtual address from its TLB
6022 * since the last used mapping need not invalidate again.
6025 if ((opte & (PG_V | PG_A)) == (PG_V | PG_A))
6026 CPU_ZERO(&sf->cpumask);
6028 if ((opte & (PG_V | PG_A)) == (PG_V | PG_A))
6029 pmap_invalidate_page_int(kernel_pmap, sf->kva);
6034 __CONCAT(PMTYPE, cp_slow0_map)(vm_offset_t kaddr, int plen, vm_page_t *ma)
6039 for (i = 0, pte = vtopte(kaddr); i < plen; i++, pte++) {
6040 *pte = PG_V | PG_RW | PG_A | PG_M | VM_PAGE_TO_PHYS(ma[i]) |
6041 pmap_cache_bits(kernel_pmap, pmap_page_get_memattr(ma[i]),
6043 invlpg(kaddr + ptoa(i));
6048 __CONCAT(PMTYPE, get_kcr3)(void)
6051 #ifdef PMAP_PAE_COMP
6052 return ((u_int)IdlePDPT);
6054 return ((u_int)IdlePTD);
6059 __CONCAT(PMTYPE, get_cr3)(pmap_t pmap)
6062 #ifdef PMAP_PAE_COMP
6063 return ((u_int)vtophys(pmap->pm_pdpt));
6065 return ((u_int)vtophys(pmap->pm_pdir));
6070 __CONCAT(PMTYPE, cmap3)(vm_paddr_t pa, u_int pte_bits)
6075 *pte = pa | pte_bits;
6081 __CONCAT(PMTYPE, basemem_setup)(u_int basemem)
6087 * Map pages between basemem and ISA_HOLE_START, if any, r/w into
6088 * the vm86 page table so that vm86 can scribble on them using
6089 * the vm86 map too. XXX: why 2 ways for this and only 1 way for
6090 * page 0, at least as initialized here?
6092 pte = (pt_entry_t *)vm86paddr;
6093 for (i = basemem / 4; i < 160; i++)
6094 pte[i] = (i << PAGE_SHIFT) | PG_V | PG_RW | PG_U;
6097 struct bios16_pmap_handle {
6100 pt_entry_t orig_ptd;
6104 __CONCAT(PMTYPE, bios16_enter)(void)
6106 struct bios16_pmap_handle *h;
6109 * no page table, so create one and install it.
6111 h = malloc(sizeof(struct bios16_pmap_handle), M_TEMP, M_WAITOK);
6112 h->pte = (pt_entry_t *)malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
6114 *h->pte = vm86phystk | PG_RW | PG_V;
6115 h->orig_ptd = *h->ptd;
6116 *h->ptd = vtophys(h->pte) | PG_RW | PG_V;
6117 pmap_invalidate_all_int(kernel_pmap); /* XXX insurance for now */
6122 __CONCAT(PMTYPE, bios16_leave)(void *arg)
6124 struct bios16_pmap_handle *h;
6127 *h->ptd = h->orig_ptd; /* remove page table */
6129 * XXX only needs to be invlpg(0) but that doesn't work on the 386
6131 pmap_invalidate_all_int(kernel_pmap);
6132 free(h->pte, M_TEMP); /* ... and free it */
6135 struct pmap_kernel_map_range {
6144 sysctl_kmaps_dump(struct sbuf *sb, struct pmap_kernel_map_range *range,
6150 if (eva <= range->sva)
6153 pat_idx = pmap_pat_index(kernel_pmap, range->attrs, true);
6154 for (i = 0; i < PAT_INDEX_SIZE; i++)
6155 if (pat_index[i] == pat_idx)
6159 case PAT_WRITE_BACK:
6162 case PAT_WRITE_THROUGH:
6165 case PAT_UNCACHEABLE:
6171 case PAT_WRITE_PROTECTED:
6174 case PAT_WRITE_COMBINING:
6178 printf("%s: unknown PAT mode %#x for range 0x%08x-0x%08x\n",
6179 __func__, pat_idx, range->sva, eva);
6184 sbuf_printf(sb, "0x%08x-0x%08x r%c%c%c%c %s %d %d %d\n",
6186 (range->attrs & PG_RW) != 0 ? 'w' : '-',
6187 (range->attrs & pg_nx) != 0 ? '-' : 'x',
6188 (range->attrs & PG_U) != 0 ? 'u' : 's',
6189 (range->attrs & PG_G) != 0 ? 'g' : '-',
6190 mode, range->pdpes, range->pdes, range->ptes);
6192 /* Reset to sentinel value. */
6193 range->sva = 0xffffffff;
6197 * Determine whether the attributes specified by a page table entry match those
6198 * being tracked by the current range. This is not quite as simple as a direct
6199 * flag comparison since some PAT modes have multiple representations.
6202 sysctl_kmaps_match(struct pmap_kernel_map_range *range, pt_entry_t attrs)
6204 pt_entry_t diff, mask;
6206 mask = pg_nx | PG_G | PG_RW | PG_U | PG_PDE_CACHE;
6207 diff = (range->attrs ^ attrs) & mask;
6210 if ((diff & ~PG_PDE_PAT) == 0 &&
6211 pmap_pat_index(kernel_pmap, range->attrs, true) ==
6212 pmap_pat_index(kernel_pmap, attrs, true))
6218 sysctl_kmaps_reinit(struct pmap_kernel_map_range *range, vm_offset_t va,
6222 memset(range, 0, sizeof(*range));
6224 range->attrs = attrs;
6228 * Given a leaf PTE, derive the mapping's attributes. If they do not match
6229 * those of the current run, dump the address range and its attributes, and
6233 sysctl_kmaps_check(struct sbuf *sb, struct pmap_kernel_map_range *range,
6234 vm_offset_t va, pd_entry_t pde, pt_entry_t pte)
6238 attrs = pde & (PG_RW | PG_U | pg_nx);
6240 if ((pde & PG_PS) != 0) {
6241 attrs |= pde & (PG_G | PG_PDE_CACHE);
6242 } else if (pte != 0) {
6243 attrs |= pte & pg_nx;
6244 attrs &= pg_nx | (pte & (PG_RW | PG_U));
6245 attrs |= pte & (PG_G | PG_PTE_CACHE);
6247 /* Canonicalize by always using the PDE PAT bit. */
6248 if ((attrs & PG_PTE_PAT) != 0)
6249 attrs ^= PG_PDE_PAT | PG_PTE_PAT;
6252 if (range->sva > va || !sysctl_kmaps_match(range, attrs)) {
6253 sysctl_kmaps_dump(sb, range, va);
6254 sysctl_kmaps_reinit(range, va, attrs);
6259 __CONCAT(PMTYPE, sysctl_kmaps)(SYSCTL_HANDLER_ARGS)
6261 struct pmap_kernel_map_range range;
6262 struct sbuf sbuf, *sb;
6264 pt_entry_t *pt, pte;
6270 error = sysctl_wire_old_buffer(req, 0);
6274 sbuf_new_for_sysctl(sb, NULL, PAGE_SIZE, req);
6276 /* Sentinel value. */
6277 range.sva = 0xffffffff;
6280 * Iterate over the kernel page tables without holding the
6281 * kernel pmap lock. Kernel page table pages are never freed,
6282 * so at worst we will observe inconsistencies in the output.
6284 for (sva = 0, i = 0; i < NPTEPG * NPGPTD * NPDEPG ;) {
6286 sbuf_printf(sb, "\nLow PDE:\n");
6287 else if (i == LOWPTDI * NPTEPG)
6288 sbuf_printf(sb, "Low PDE dup:\n");
6289 else if (i == PTDPTDI * NPTEPG)
6290 sbuf_printf(sb, "Recursive map:\n");
6291 else if (i == KERNPTDI * NPTEPG)
6292 sbuf_printf(sb, "Kernel base:\n");
6293 else if (i == TRPTDI * NPTEPG)
6294 sbuf_printf(sb, "Trampoline:\n");
6295 pde = IdlePTD[sva >> PDRSHIFT];
6296 if ((pde & PG_V) == 0) {
6297 sva = rounddown2(sva, NBPDR);
6298 sysctl_kmaps_dump(sb, &range, sva);
6303 pa = pde & PG_FRAME;
6304 if ((pde & PG_PS) != 0) {
6305 sysctl_kmaps_check(sb, &range, sva, pde, 0);
6311 for (pt = vtopte(sva), k = 0; k < NPTEPG; i++, k++, pt++,
6314 if ((pte & PG_V) == 0) {
6315 sysctl_kmaps_dump(sb, &range, sva);
6318 sysctl_kmaps_check(sb, &range, sva, pde, pte);
6323 error = sbuf_finish(sb);
6329 .pm_##a = __CONCAT(PMTYPE, a),
6331 struct pmap_methods __CONCAT(PMTYPE, methods) = {
6335 PMM(align_superpage)
6336 PMM(quick_enter_page)
6337 PMM(quick_remove_page)
6341 PMM(get_vm_maxuser_address)
6354 PMM(is_valid_memattr)
6373 PMM(kenter_temporary)
6376 PMM(page_exists_quick)
6377 PMM(page_wired_mappings)
6381 PMM(is_prefaultable)
6387 PMM(page_set_memattr)
6389 PMM(extract_and_hold)
6400 PMM(invalidate_page)
6401 PMM(invalidate_range)
6403 PMM(invalidate_cache)