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/sf_buf.h>
126 #include <sys/vmmeter.h>
127 #include <sys/sched.h>
128 #include <sys/sysctl.h>
130 #include <sys/vmem.h>
133 #include <vm/vm_param.h>
134 #include <vm/vm_kern.h>
135 #include <vm/vm_page.h>
136 #include <vm/vm_map.h>
137 #include <vm/vm_object.h>
138 #include <vm/vm_extern.h>
139 #include <vm/vm_pageout.h>
140 #include <vm/vm_pager.h>
141 #include <vm/vm_phys.h>
142 #include <vm/vm_radix.h>
143 #include <vm/vm_reserv.h>
148 #include <machine/intr_machdep.h>
149 #include <x86/apicvar.h>
151 #include <x86/ifunc.h>
152 #include <machine/bootinfo.h>
153 #include <machine/cpu.h>
154 #include <machine/cputypes.h>
155 #include <machine/md_var.h>
156 #include <machine/pcb.h>
157 #include <machine/specialreg.h>
159 #include <machine/smp.h>
161 #include <machine/pmap_base.h>
163 #if !defined(DIAGNOSTIC)
164 #ifdef __GNUC_GNU_INLINE__
165 #define PMAP_INLINE __attribute__((__gnu_inline__)) inline
167 #define PMAP_INLINE extern inline
174 #define PV_STAT(x) do { x ; } while (0)
176 #define PV_STAT(x) do { } while (0)
179 #define pa_index(pa) ((pa) >> PDRSHIFT)
180 #define pa_to_pvh(pa) (&pv_table[pa_index(pa)])
183 * PTmap is recursive pagemap at top of virtual address space.
184 * Within PTmap, the page directory can be found (third indirection).
186 #define PTmap ((pt_entry_t *)(PTDPTDI << PDRSHIFT))
187 #define PTD ((pd_entry_t *)((PTDPTDI << PDRSHIFT) + (PTDPTDI * PAGE_SIZE)))
188 #define PTDpde ((pd_entry_t *)((PTDPTDI << PDRSHIFT) + (PTDPTDI * PAGE_SIZE) + \
189 (PTDPTDI * PDESIZE)))
192 * Translate a virtual address to the kernel virtual address of its page table
193 * entry (PTE). This can be used recursively. If the address of a PTE as
194 * previously returned by this macro is itself given as the argument, then the
195 * address of the page directory entry (PDE) that maps the PTE will be
198 * This macro may be used before pmap_bootstrap() is called.
200 #define vtopte(va) (PTmap + i386_btop(va))
203 * Get PDEs and PTEs for user/kernel address space
205 #define pmap_pde(m, v) (&((m)->pm_pdir[(vm_offset_t)(v) >> PDRSHIFT]))
206 #define pdir_pde(m, v) (m[(vm_offset_t)(v) >> PDRSHIFT])
208 #define pmap_pde_v(pte) ((*(int *)pte & PG_V) != 0)
209 #define pmap_pte_w(pte) ((*(int *)pte & PG_W) != 0)
210 #define pmap_pte_m(pte) ((*(int *)pte & PG_M) != 0)
211 #define pmap_pte_u(pte) ((*(int *)pte & PG_A) != 0)
212 #define pmap_pte_v(pte) ((*(int *)pte & PG_V) != 0)
214 #define pmap_pte_set_w(pte, v) ((v) ? atomic_set_int((u_int *)(pte), PG_W) : \
215 atomic_clear_int((u_int *)(pte), PG_W))
216 #define pmap_pte_set_prot(pte, v) ((*(int *)pte &= ~PG_PROT), (*(int *)pte |= (v)))
218 _Static_assert(sizeof(struct pmap) <= sizeof(struct pmap_KBI),
221 static int pgeflag = 0; /* PG_G or-in */
222 static int pseflag = 0; /* PG_PS or-in */
224 static int nkpt = NKPT;
228 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 boolean_t pmap_demote_pde(pmap_t pmap, pd_entry_t *pde, vm_offset_t va);
315 static bool pmap_enter_4mpage(pmap_t pmap, vm_offset_t va, vm_page_t m,
317 static int pmap_enter_pde(pmap_t pmap, vm_offset_t va, pd_entry_t newpde,
318 u_int flags, vm_page_t m);
319 static vm_page_t pmap_enter_quick_locked(pmap_t pmap, vm_offset_t va,
320 vm_page_t m, vm_prot_t prot, vm_page_t mpte);
321 static int pmap_insert_pt_page(pmap_t pmap, vm_page_t mpte);
322 static void pmap_invalidate_pde_page(pmap_t pmap, vm_offset_t va,
324 static void pmap_fill_ptp(pt_entry_t *firstpte, pt_entry_t newpte);
325 static boolean_t pmap_is_modified_pvh(struct md_page *pvh);
326 static boolean_t pmap_is_referenced_pvh(struct md_page *pvh);
327 static void pmap_kenter_attr(vm_offset_t va, vm_paddr_t pa, int mode);
328 static void pmap_kenter_pde(vm_offset_t va, pd_entry_t newpde);
329 static void pmap_pde_attr(pd_entry_t *pde, int cache_bits);
330 #if VM_NRESERVLEVEL > 0
331 static void pmap_promote_pde(pmap_t pmap, pd_entry_t *pde, vm_offset_t va);
333 static boolean_t pmap_protect_pde(pmap_t pmap, pd_entry_t *pde, vm_offset_t sva,
335 static void pmap_pte_attr(pt_entry_t *pte, int cache_bits);
336 static void pmap_remove_pde(pmap_t pmap, pd_entry_t *pdq, vm_offset_t sva,
337 struct spglist *free);
338 static int pmap_remove_pte(pmap_t pmap, pt_entry_t *ptq, vm_offset_t sva,
339 struct spglist *free);
340 static vm_page_t pmap_remove_pt_page(pmap_t pmap, vm_offset_t va);
341 static void pmap_remove_page(struct pmap *pmap, vm_offset_t va,
342 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(struct pmap *pmap, vm_page_t m,
347 static void pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t m);
348 static boolean_t pmap_try_insert_pv_entry(pmap_t pmap, vm_offset_t va,
350 static void pmap_update_pde(pmap_t pmap, vm_offset_t va, pd_entry_t *pde,
352 static void pmap_update_pde_invalidate(vm_offset_t va, pd_entry_t newpde);
354 static vm_page_t pmap_allocpte(pmap_t pmap, vm_offset_t va, u_int flags);
356 static vm_page_t _pmap_allocpte(pmap_t pmap, u_int ptepindex, u_int flags);
357 static void _pmap_unwire_ptp(pmap_t pmap, vm_page_t m, struct spglist *free);
358 static pt_entry_t *pmap_pte_quick(pmap_t pmap, vm_offset_t va);
359 static void pmap_pte_release(pt_entry_t *pte);
360 static int pmap_unuse_pt(pmap_t, vm_offset_t, struct spglist *);
362 static void *pmap_pdpt_allocf(uma_zone_t zone, vm_size_t bytes, int domain,
363 uint8_t *flags, int wait);
365 static void pmap_init_trm(void);
366 static void pmap_invalidate_all_int(pmap_t pmap);
368 static __inline void pagezero(void *page);
370 CTASSERT(1 << PDESHIFT == sizeof(pd_entry_t));
371 CTASSERT(1 << PTESHIFT == sizeof(pt_entry_t));
374 extern u_long physfree; /* phys addr of next free page */
375 extern u_long vm86phystk;/* PA of vm86/bios stack */
376 extern u_long vm86paddr;/* address of vm86 region */
377 extern int vm86pa; /* phys addr of vm86 region */
378 extern u_long KERNend; /* phys addr end of kernel (just after bss) */
380 pd_entry_t *IdlePTD_pae; /* phys addr of kernel PTD */
381 pdpt_entry_t *IdlePDPT; /* phys addr of kernel PDPT */
382 pt_entry_t *KPTmap_pae; /* address of kernel page tables */
383 #define IdlePTD IdlePTD_pae
384 #define KPTmap KPTmap_pae
386 pd_entry_t *IdlePTD_nopae;
387 pt_entry_t *KPTmap_nopae;
388 #define IdlePTD IdlePTD_nopae
389 #define KPTmap KPTmap_nopae
391 extern u_long KPTphys; /* phys addr of kernel page tables */
392 extern u_long tramp_idleptd;
395 allocpages(u_int cnt, u_long *physfree)
400 *physfree += PAGE_SIZE * cnt;
401 bzero((void *)res, PAGE_SIZE * cnt);
406 pmap_cold_map(u_long pa, u_long va, u_long cnt)
410 for (pt = (pt_entry_t *)KPTphys + atop(va); cnt > 0;
411 cnt--, pt++, va += PAGE_SIZE, pa += PAGE_SIZE)
412 *pt = pa | PG_V | PG_RW | PG_A | PG_M;
416 pmap_cold_mapident(u_long pa, u_long cnt)
419 pmap_cold_map(pa, pa, cnt);
422 _Static_assert(LOWPTDI * 2 * NBPDR == KERNBASE,
423 "Broken double-map of zero PTD");
426 __CONCAT(PMTYPE, remap_lower)(bool enable)
430 for (i = 0; i < LOWPTDI; i++)
431 IdlePTD[i] = enable ? IdlePTD[LOWPTDI + i] : 0;
432 load_cr3(rcr3()); /* invalidate TLB */
436 * Called from locore.s before paging is enabled. Sets up the first
437 * kernel page table. Since kernel is mapped with PA == VA, this code
438 * does not require relocations.
441 __CONCAT(PMTYPE, cold)(void)
447 physfree = (u_long)&_end;
448 if (bootinfo.bi_esymtab != 0)
449 physfree = bootinfo.bi_esymtab;
450 if (bootinfo.bi_kernend != 0)
451 physfree = bootinfo.bi_kernend;
452 physfree = roundup2(physfree, NBPDR);
455 /* Allocate Kernel Page Tables */
456 KPTphys = allocpages(NKPT, &physfree);
457 KPTmap = (pt_entry_t *)KPTphys;
459 /* Allocate Page Table Directory */
461 /* XXX only need 32 bytes (easier for now) */
462 IdlePDPT = (pdpt_entry_t *)allocpages(1, &physfree);
464 IdlePTD = (pd_entry_t *)allocpages(NPGPTD, &physfree);
467 * Allocate KSTACK. Leave a guard page between IdlePTD and
468 * proc0kstack, to control stack overflow for thread0 and
469 * prevent corruption of the page table. We leak the guard
470 * physical memory due to 1:1 mappings.
472 allocpages(1, &physfree);
473 proc0kstack = allocpages(TD0_KSTACK_PAGES, &physfree);
475 /* vm86/bios stack */
476 vm86phystk = allocpages(1, &physfree);
478 /* pgtable + ext + IOPAGES */
479 vm86paddr = vm86pa = allocpages(3, &physfree);
481 /* Install page tables into PTD. Page table page 1 is wasted. */
482 for (a = 0; a < NKPT; a++)
483 IdlePTD[a] = (KPTphys + ptoa(a)) | PG_V | PG_RW | PG_A | PG_M;
486 /* PAE install PTD pointers into PDPT */
487 for (a = 0; a < NPGPTD; a++)
488 IdlePDPT[a] = ((u_int)IdlePTD + ptoa(a)) | PG_V;
492 * Install recursive mapping for kernel page tables into
495 for (a = 0; a < NPGPTD; a++)
496 IdlePTD[PTDPTDI + a] = ((u_int)IdlePTD + ptoa(a)) | PG_V |
500 * Initialize page table pages mapping physical address zero
501 * through the (physical) end of the kernel. Many of these
502 * pages must be reserved, and we reserve them all and map
503 * them linearly for convenience. We do this even if we've
504 * enabled PSE above; we'll just switch the corresponding
505 * kernel PDEs before we turn on paging.
507 * This and all other page table entries allow read and write
508 * access for various reasons. Kernel mappings never have any
509 * access restrictions.
511 pmap_cold_mapident(0, atop(NBPDR) * LOWPTDI);
512 pmap_cold_map(0, NBPDR * LOWPTDI, atop(NBPDR) * LOWPTDI);
513 pmap_cold_mapident(KERNBASE, atop(KERNend - KERNBASE));
515 /* Map page table directory */
517 pmap_cold_mapident((u_long)IdlePDPT, 1);
519 pmap_cold_mapident((u_long)IdlePTD, NPGPTD);
521 /* Map early KPTmap. It is really pmap_cold_mapident. */
522 pmap_cold_map(KPTphys, (u_long)KPTmap, NKPT);
524 /* Map proc0kstack */
525 pmap_cold_mapident(proc0kstack, TD0_KSTACK_PAGES);
526 /* ISA hole already mapped */
528 pmap_cold_mapident(vm86phystk, 1);
529 pmap_cold_mapident(vm86pa, 3);
531 /* Map page 0 into the vm86 page table */
532 *(pt_entry_t *)vm86pa = 0 | PG_RW | PG_U | PG_A | PG_M | PG_V;
534 /* ...likewise for the ISA hole for vm86 */
535 for (pt = (pt_entry_t *)vm86pa + atop(ISA_HOLE_START), a = 0;
536 a < atop(ISA_HOLE_LENGTH); a++, pt++)
537 *pt = (ISA_HOLE_START + ptoa(a)) | PG_RW | PG_U | PG_A |
540 /* Enable PSE, PGE, VME, and PAE if configured. */
542 if ((cpu_feature & CPUID_PSE) != 0) {
546 * Superpage mapping of the kernel text. Existing 4k
547 * page table pages are wasted.
549 for (a = KERNBASE; a < KERNend; a += NBPDR)
550 IdlePTD[a >> PDRSHIFT] = a | PG_PS | PG_A | PG_M |
553 if ((cpu_feature & CPUID_PGE) != 0) {
557 ncr4 |= (cpu_feature & CPUID_VME) != 0 ? CR4_VME : 0;
562 load_cr4(rcr4() | ncr4);
564 /* Now enable paging */
566 cr3 = (u_int)IdlePDPT;
567 if ((cpu_feature & CPUID_PAT) == 0)
570 cr3 = (u_int)IdlePTD;
574 load_cr0(rcr0() | CR0_PG);
577 * Now running relocated at KERNBASE where the system is
582 * Remove the lowest part of the double mapping of low memory
583 * to get some null pointer checks.
585 __CONCAT(PMTYPE, remap_lower)(false);
587 kernel_vm_end = /* 0 + */ NKPT * NBPDR;
589 i386_pmap_VM_NFREEORDER = VM_NFREEORDER_PAE;
590 i386_pmap_VM_LEVEL_0_ORDER = VM_LEVEL_0_ORDER_PAE;
591 i386_pmap_PDRSHIFT = PDRSHIFT_PAE;
593 i386_pmap_VM_NFREEORDER = VM_NFREEORDER_NOPAE;
594 i386_pmap_VM_LEVEL_0_ORDER = VM_LEVEL_0_ORDER_NOPAE;
595 i386_pmap_PDRSHIFT = PDRSHIFT_NOPAE;
600 __CONCAT(PMTYPE, set_nx)(void)
604 if ((amd_feature & AMDID_NX) == 0)
608 /* EFER.EFER_NXE is set in initializecpu(). */
613 * Bootstrap the system enough to run with virtual memory.
615 * On the i386 this is called after pmap_cold() created initial
616 * kernel page table and enabled paging, and just syncs the pmap
617 * module with what has already been done.
620 __CONCAT(PMTYPE, bootstrap)(vm_paddr_t firstaddr)
623 pt_entry_t *pte, *unused;
628 res = atop(firstaddr - (vm_paddr_t)KERNLOAD);
631 * Add a physical memory segment (vm_phys_seg) corresponding to the
632 * preallocated kernel page table pages so that vm_page structures
633 * representing these pages will be created. The vm_page structures
634 * are required for promotion of the corresponding kernel virtual
635 * addresses to superpage mappings.
637 vm_phys_add_seg(KPTphys, KPTphys + ptoa(nkpt));
640 * Initialize the first available kernel virtual address.
641 * However, using "firstaddr" may waste a few pages of the
642 * kernel virtual address space, because pmap_cold() may not
643 * have mapped every physical page that it allocated.
644 * Preferably, pmap_cold() would provide a first unused
645 * virtual address in addition to "firstaddr".
647 virtual_avail = (vm_offset_t)firstaddr;
648 virtual_end = VM_MAX_KERNEL_ADDRESS;
651 * Initialize the kernel pmap (which is statically allocated).
652 * Count bootstrap data as being resident in case any of this data is
653 * later unmapped (using pmap_remove()) and freed.
655 PMAP_LOCK_INIT(kernel_pmap);
656 kernel_pmap->pm_pdir = IdlePTD;
658 kernel_pmap->pm_pdpt = IdlePDPT;
660 CPU_FILL(&kernel_pmap->pm_active); /* don't allow deactivation */
661 kernel_pmap->pm_stats.resident_count = res;
662 TAILQ_INIT(&kernel_pmap->pm_pvchunk);
665 * Initialize the global pv list lock.
667 rw_init(&pvh_global_lock, "pmap pv global");
670 * Reserve some special page table entries/VA space for temporary
673 #define SYSMAP(c, p, v, n) \
674 v = (c)va; va += ((n)*PAGE_SIZE); p = pte; pte += (n);
681 * Initialize temporary map objects on the current CPU for use
683 * CMAP1/CMAP2 are used for zeroing and copying pages.
684 * CMAP3 is used for the boot-time memory test.
687 mtx_init(&pc->pc_cmap_lock, "SYSMAPS", NULL, MTX_DEF);
688 SYSMAP(caddr_t, pc->pc_cmap_pte1, pc->pc_cmap_addr1, 1)
689 SYSMAP(caddr_t, pc->pc_cmap_pte2, pc->pc_cmap_addr2, 1)
690 SYSMAP(vm_offset_t, pte, pc->pc_qmap_addr, 1)
692 SYSMAP(caddr_t, CMAP3, CADDR3, 1);
697 SYSMAP(caddr_t, unused, crashdumpmap, MAXDUMPPGS)
700 * ptvmmap is used for reading arbitrary physical pages via /dev/mem.
702 SYSMAP(caddr_t, unused, ptvmmap, 1)
705 * msgbufp is used to map the system message buffer.
707 SYSMAP(struct msgbuf *, unused, msgbufp, atop(round_page(msgbufsize)))
710 * KPTmap is used by pmap_kextract().
712 * KPTmap is first initialized by pmap_cold(). However, that initial
713 * KPTmap can only support NKPT page table pages. Here, a larger
714 * KPTmap is created that can support KVA_PAGES page table pages.
716 SYSMAP(pt_entry_t *, KPTD, KPTmap, KVA_PAGES)
718 for (i = 0; i < NKPT; i++)
719 KPTD[i] = (KPTphys + ptoa(i)) | PG_RW | PG_V;
722 * PADDR1 and PADDR2 are used by pmap_pte_quick() and pmap_pte(),
725 SYSMAP(pt_entry_t *, PMAP1, PADDR1, 1)
726 SYSMAP(pt_entry_t *, PMAP2, PADDR2, 1)
727 SYSMAP(pt_entry_t *, PMAP3, PADDR3, 1)
729 mtx_init(&PMAP2mutex, "PMAP2", NULL, MTX_DEF);
734 * Initialize the PAT MSR if present.
735 * pmap_init_pat() clears and sets CR4_PGE, which, as a
736 * side-effect, invalidates stale PG_G TLB entries that might
737 * have been created in our pre-boot environment. We assume
738 * that PAT support implies PGE and in reverse, PGE presence
739 * comes with PAT. Both features were added for Pentium Pro.
745 pmap_init_reserved_pages(void)
760 mtx_init(&pc->pc_copyout_mlock, "cpmlk", NULL, MTX_DEF |
762 pc->pc_copyout_maddr = kva_alloc(ptoa(2));
763 if (pc->pc_copyout_maddr == 0)
764 panic("unable to allocate non-sleepable copyout KVA");
765 sx_init(&pc->pc_copyout_slock, "cpslk");
766 pc->pc_copyout_saddr = kva_alloc(ptoa(2));
767 if (pc->pc_copyout_saddr == 0)
768 panic("unable to allocate sleepable copyout KVA");
769 pc->pc_pmap_eh_va = kva_alloc(ptoa(1));
770 if (pc->pc_pmap_eh_va == 0)
771 panic("unable to allocate pmap_extract_and_hold KVA");
772 pc->pc_pmap_eh_ptep = (char *)vtopte(pc->pc_pmap_eh_va);
775 * Skip if the mappings have already been initialized,
776 * i.e. this is the BSP.
778 if (pc->pc_cmap_addr1 != 0)
781 mtx_init(&pc->pc_cmap_lock, "SYSMAPS", NULL, MTX_DEF);
782 pages = kva_alloc(PAGE_SIZE * 3);
784 panic("unable to allocate CMAP KVA");
785 pc->pc_cmap_pte1 = vtopte(pages);
786 pc->pc_cmap_pte2 = vtopte(pages + PAGE_SIZE);
787 pc->pc_cmap_addr1 = (caddr_t)pages;
788 pc->pc_cmap_addr2 = (caddr_t)(pages + PAGE_SIZE);
789 pc->pc_qmap_addr = pages + ptoa(2);
793 SYSINIT(rpages_init, SI_SUB_CPU, SI_ORDER_ANY, pmap_init_reserved_pages, NULL);
799 __CONCAT(PMTYPE, init_pat)(void)
801 int pat_table[PAT_INDEX_SIZE];
806 /* Set default PAT index table. */
807 for (i = 0; i < PAT_INDEX_SIZE; i++)
809 pat_table[PAT_WRITE_BACK] = 0;
810 pat_table[PAT_WRITE_THROUGH] = 1;
811 pat_table[PAT_UNCACHEABLE] = 3;
812 pat_table[PAT_WRITE_COMBINING] = 3;
813 pat_table[PAT_WRITE_PROTECTED] = 3;
814 pat_table[PAT_UNCACHED] = 3;
817 * Bail if this CPU doesn't implement PAT.
818 * We assume that PAT support implies PGE.
820 if ((cpu_feature & CPUID_PAT) == 0) {
821 for (i = 0; i < PAT_INDEX_SIZE; i++)
822 pat_index[i] = pat_table[i];
828 * Due to some Intel errata, we can only safely use the lower 4
831 * Intel Pentium III Processor Specification Update
832 * Errata E.27 (Upper Four PAT Entries Not Usable With Mode B
835 * Intel Pentium IV Processor Specification Update
836 * Errata N46 (PAT Index MSB May Be Calculated Incorrectly)
838 if (cpu_vendor_id == CPU_VENDOR_INTEL &&
839 !(CPUID_TO_FAMILY(cpu_id) == 6 && CPUID_TO_MODEL(cpu_id) >= 0xe))
842 /* Initialize default PAT entries. */
843 pat_msr = PAT_VALUE(0, PAT_WRITE_BACK) |
844 PAT_VALUE(1, PAT_WRITE_THROUGH) |
845 PAT_VALUE(2, PAT_UNCACHED) |
846 PAT_VALUE(3, PAT_UNCACHEABLE) |
847 PAT_VALUE(4, PAT_WRITE_BACK) |
848 PAT_VALUE(5, PAT_WRITE_THROUGH) |
849 PAT_VALUE(6, PAT_UNCACHED) |
850 PAT_VALUE(7, PAT_UNCACHEABLE);
854 * Leave the indices 0-3 at the default of WB, WT, UC-, and UC.
855 * Program 5 and 6 as WP and WC.
856 * Leave 4 and 7 as WB and UC.
858 pat_msr &= ~(PAT_MASK(5) | PAT_MASK(6));
859 pat_msr |= PAT_VALUE(5, PAT_WRITE_PROTECTED) |
860 PAT_VALUE(6, PAT_WRITE_COMBINING);
861 pat_table[PAT_UNCACHED] = 2;
862 pat_table[PAT_WRITE_PROTECTED] = 5;
863 pat_table[PAT_WRITE_COMBINING] = 6;
866 * Just replace PAT Index 2 with WC instead of UC-.
868 pat_msr &= ~PAT_MASK(2);
869 pat_msr |= PAT_VALUE(2, PAT_WRITE_COMBINING);
870 pat_table[PAT_WRITE_COMBINING] = 2;
875 load_cr4(cr4 & ~CR4_PGE);
877 /* Disable caches (CD = 1, NW = 0). */
879 load_cr0((cr0 & ~CR0_NW) | CR0_CD);
881 /* Flushes caches and TLBs. */
885 /* Update PAT and index table. */
886 wrmsr(MSR_PAT, pat_msr);
887 for (i = 0; i < PAT_INDEX_SIZE; i++)
888 pat_index[i] = pat_table[i];
890 /* Flush caches and TLBs again. */
894 /* Restore caches and PGE. */
901 pmap_pdpt_allocf(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *flags,
905 /* Inform UMA that this allocator uses kernel_map/object. */
906 *flags = UMA_SLAB_KERNEL;
907 return ((void *)kmem_alloc_contig_domainset(DOMAINSET_FIXED(domain),
908 bytes, wait, 0x0ULL, 0xffffffffULL, 1, 0, VM_MEMATTR_DEFAULT));
913 * Abuse the pte nodes for unmapped kva to thread a kva freelist through.
915 * - Must deal with pages in order to ensure that none of the PG_* bits
916 * are ever set, PG_V in particular.
917 * - Assumes we can write to ptes without pte_store() atomic ops, even
918 * on PAE systems. This should be ok.
919 * - Assumes nothing will ever test these addresses for 0 to indicate
920 * no mapping instead of correctly checking PG_V.
921 * - Assumes a vm_offset_t will fit in a pte (true for i386).
922 * Because PG_V is never set, there can be no mappings to invalidate.
925 pmap_ptelist_alloc(vm_offset_t *head)
932 panic("pmap_ptelist_alloc: exhausted ptelist KVA");
936 panic("pmap_ptelist_alloc: va with PG_V set!");
942 pmap_ptelist_free(vm_offset_t *head, vm_offset_t va)
947 panic("pmap_ptelist_free: freeing va with PG_V set!");
949 *pte = *head; /* virtual! PG_V is 0 though */
954 pmap_ptelist_init(vm_offset_t *head, void *base, int npages)
960 for (i = npages - 1; i >= 0; i--) {
961 va = (vm_offset_t)base + i * PAGE_SIZE;
962 pmap_ptelist_free(head, va);
968 * Initialize the pmap module.
969 * Called by vm_init, to initialize any structures that the pmap
970 * system needs to map virtual memory.
973 __CONCAT(PMTYPE, init)(void)
975 struct pmap_preinit_mapping *ppim;
981 * Initialize the vm page array entries for the kernel pmap's
984 PMAP_LOCK(kernel_pmap);
985 for (i = 0; i < NKPT; i++) {
986 mpte = PHYS_TO_VM_PAGE(KPTphys + ptoa(i));
987 KASSERT(mpte >= vm_page_array &&
988 mpte < &vm_page_array[vm_page_array_size],
989 ("pmap_init: page table page is out of range"));
990 mpte->pindex = i + KPTDI;
991 mpte->phys_addr = KPTphys + ptoa(i);
992 mpte->wire_count = 1;
994 KERNBASE <= i << PDRSHIFT && i << PDRSHIFT < KERNend &&
995 pmap_insert_pt_page(kernel_pmap, mpte))
996 panic("pmap_init: pmap_insert_pt_page failed");
998 PMAP_UNLOCK(kernel_pmap);
1002 * Initialize the address space (zone) for the pv entries. Set a
1003 * high water mark so that the system can recover from excessive
1004 * numbers of pv entries.
1006 TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
1007 pv_entry_max = shpgperproc * maxproc + vm_cnt.v_page_count;
1008 TUNABLE_INT_FETCH("vm.pmap.pv_entries", &pv_entry_max);
1009 pv_entry_max = roundup(pv_entry_max, _NPCPV);
1010 pv_entry_high_water = 9 * (pv_entry_max / 10);
1013 * If the kernel is running on a virtual machine, then it must assume
1014 * that MCA is enabled by the hypervisor. Moreover, the kernel must
1015 * be prepared for the hypervisor changing the vendor and family that
1016 * are reported by CPUID. Consequently, the workaround for AMD Family
1017 * 10h Erratum 383 is enabled if the processor's feature set does not
1018 * include at least one feature that is only supported by older Intel
1019 * or newer AMD processors.
1021 if (vm_guest != VM_GUEST_NO && (cpu_feature & CPUID_SS) == 0 &&
1022 (cpu_feature2 & (CPUID2_SSSE3 | CPUID2_SSE41 | CPUID2_AESNI |
1023 CPUID2_AVX | CPUID2_XSAVE)) == 0 && (amd_feature2 & (AMDID2_XOP |
1025 workaround_erratum383 = 1;
1028 * Are large page mappings supported and enabled?
1030 TUNABLE_INT_FETCH("vm.pmap.pg_ps_enabled", &pg_ps_enabled);
1033 else if (pg_ps_enabled) {
1034 KASSERT(MAXPAGESIZES > 1 && pagesizes[1] == 0,
1035 ("pmap_init: can't assign to pagesizes[1]"));
1036 pagesizes[1] = NBPDR;
1040 * Calculate the size of the pv head table for superpages.
1041 * Handle the possibility that "vm_phys_segs[...].end" is zero.
1043 pv_npg = trunc_4mpage(vm_phys_segs[vm_phys_nsegs - 1].end -
1044 PAGE_SIZE) / NBPDR + 1;
1047 * Allocate memory for the pv head table for superpages.
1049 s = (vm_size_t)(pv_npg * sizeof(struct md_page));
1051 pv_table = (struct md_page *)kmem_malloc(s, M_WAITOK | M_ZERO);
1052 for (i = 0; i < pv_npg; i++)
1053 TAILQ_INIT(&pv_table[i].pv_list);
1055 pv_maxchunks = MAX(pv_entry_max / _NPCPV, maxproc);
1056 pv_chunkbase = (struct pv_chunk *)kva_alloc(PAGE_SIZE * pv_maxchunks);
1057 if (pv_chunkbase == NULL)
1058 panic("pmap_init: not enough kvm for pv chunks");
1059 pmap_ptelist_init(&pv_vafree, pv_chunkbase, pv_maxchunks);
1060 #ifdef PMAP_PAE_COMP
1061 pdptzone = uma_zcreate("PDPT", NPGPTD * sizeof(pdpt_entry_t), NULL,
1062 NULL, NULL, NULL, (NPGPTD * sizeof(pdpt_entry_t)) - 1,
1063 UMA_ZONE_VM | UMA_ZONE_NOFREE);
1064 uma_zone_set_allocf(pdptzone, pmap_pdpt_allocf);
1067 pmap_initialized = 1;
1072 for (i = 0; i < PMAP_PREINIT_MAPPING_COUNT; i++) {
1073 ppim = pmap_preinit_mapping + i;
1076 printf("PPIM %u: PA=%#jx, VA=%#x, size=%#x, mode=%#x\n", i,
1077 (uintmax_t)ppim->pa, ppim->va, ppim->sz, ppim->mode);
1082 extern u_long pmap_pde_demotions;
1083 extern u_long pmap_pde_mappings;
1084 extern u_long pmap_pde_p_failures;
1085 extern u_long pmap_pde_promotions;
1087 /***************************************************
1088 * Low level helper routines.....
1089 ***************************************************/
1092 __CONCAT(PMTYPE, is_valid_memattr)(pmap_t pmap __unused, vm_memattr_t mode)
1095 return (mode >= 0 && mode < PAT_INDEX_SIZE &&
1096 pat_index[(int)mode] >= 0);
1100 * Determine the appropriate bits to set in a PTE or PDE for a specified
1104 __CONCAT(PMTYPE, cache_bits)(pmap_t pmap, int mode, boolean_t is_pde)
1106 int cache_bits, pat_flag, pat_idx;
1108 if (!pmap_is_valid_memattr(pmap, mode))
1109 panic("Unknown caching mode %d\n", mode);
1111 /* The PAT bit is different for PTE's and PDE's. */
1112 pat_flag = is_pde ? PG_PDE_PAT : PG_PTE_PAT;
1114 /* Map the caching mode to a PAT index. */
1115 pat_idx = pat_index[mode];
1117 /* Map the 3-bit index value into the PAT, PCD, and PWT bits. */
1120 cache_bits |= pat_flag;
1122 cache_bits |= PG_NC_PCD;
1124 cache_bits |= PG_NC_PWT;
1125 return (cache_bits);
1129 __CONCAT(PMTYPE, ps_enabled)(pmap_t pmap __unused)
1132 return (pg_ps_enabled);
1136 * The caller is responsible for maintaining TLB consistency.
1139 pmap_kenter_pde(vm_offset_t va, pd_entry_t newpde)
1143 pde = pmap_pde(kernel_pmap, va);
1144 pde_store(pde, newpde);
1148 * After changing the page size for the specified virtual address in the page
1149 * table, flush the corresponding entries from the processor's TLB. Only the
1150 * calling processor's TLB is affected.
1152 * The calling thread must be pinned to a processor.
1155 pmap_update_pde_invalidate(vm_offset_t va, pd_entry_t newpde)
1158 if ((newpde & PG_PS) == 0)
1159 /* Demotion: flush a specific 2MB page mapping. */
1161 else /* if ((newpde & PG_G) == 0) */
1163 * Promotion: flush every 4KB page mapping from the TLB
1164 * because there are too many to flush individually.
1171 * For SMP, these functions have to use the IPI mechanism for coherence.
1173 * N.B.: Before calling any of the following TLB invalidation functions,
1174 * the calling processor must ensure that all stores updating a non-
1175 * kernel page table are globally performed. Otherwise, another
1176 * processor could cache an old, pre-update entry without being
1177 * invalidated. This can happen one of two ways: (1) The pmap becomes
1178 * active on another processor after its pm_active field is checked by
1179 * one of the following functions but before a store updating the page
1180 * table is globally performed. (2) The pmap becomes active on another
1181 * processor before its pm_active field is checked but due to
1182 * speculative loads one of the following functions stills reads the
1183 * pmap as inactive on the other processor.
1185 * The kernel page table is exempt because its pm_active field is
1186 * immutable. The kernel page table is always active on every
1190 pmap_invalidate_page_int(pmap_t pmap, vm_offset_t va)
1192 cpuset_t *mask, other_cpus;
1196 if (pmap == kernel_pmap) {
1199 } else if (!CPU_CMP(&pmap->pm_active, &all_cpus)) {
1202 cpuid = PCPU_GET(cpuid);
1203 other_cpus = all_cpus;
1204 CPU_CLR(cpuid, &other_cpus);
1205 CPU_AND(&other_cpus, &pmap->pm_active);
1208 smp_masked_invlpg(*mask, va, pmap);
1212 /* 4k PTEs -- Chosen to exceed the total size of Broadwell L2 TLB */
1213 #define PMAP_INVLPG_THRESHOLD (4 * 1024 * PAGE_SIZE)
1216 pmap_invalidate_range_int(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
1218 cpuset_t *mask, other_cpus;
1222 if (eva - sva >= PMAP_INVLPG_THRESHOLD) {
1223 pmap_invalidate_all_int(pmap);
1228 if (pmap == kernel_pmap) {
1229 for (addr = sva; addr < eva; addr += PAGE_SIZE)
1232 } else if (!CPU_CMP(&pmap->pm_active, &all_cpus)) {
1235 cpuid = PCPU_GET(cpuid);
1236 other_cpus = all_cpus;
1237 CPU_CLR(cpuid, &other_cpus);
1238 CPU_AND(&other_cpus, &pmap->pm_active);
1241 smp_masked_invlpg_range(*mask, sva, eva, pmap);
1246 pmap_invalidate_all_int(pmap_t pmap)
1248 cpuset_t *mask, other_cpus;
1252 if (pmap == kernel_pmap) {
1255 } else if (!CPU_CMP(&pmap->pm_active, &all_cpus)) {
1258 cpuid = PCPU_GET(cpuid);
1259 other_cpus = all_cpus;
1260 CPU_CLR(cpuid, &other_cpus);
1261 CPU_AND(&other_cpus, &pmap->pm_active);
1264 smp_masked_invltlb(*mask, pmap);
1269 __CONCAT(PMTYPE, invalidate_cache)(void)
1279 cpuset_t invalidate; /* processors that invalidate their TLB */
1283 u_int store; /* processor that updates the PDE */
1287 pmap_update_pde_kernel(void *arg)
1289 struct pde_action *act = arg;
1292 if (act->store == PCPU_GET(cpuid)) {
1293 pde = pmap_pde(kernel_pmap, act->va);
1294 pde_store(pde, act->newpde);
1299 pmap_update_pde_user(void *arg)
1301 struct pde_action *act = arg;
1303 if (act->store == PCPU_GET(cpuid))
1304 pde_store(act->pde, act->newpde);
1308 pmap_update_pde_teardown(void *arg)
1310 struct pde_action *act = arg;
1312 if (CPU_ISSET(PCPU_GET(cpuid), &act->invalidate))
1313 pmap_update_pde_invalidate(act->va, act->newpde);
1317 * Change the page size for the specified virtual address in a way that
1318 * prevents any possibility of the TLB ever having two entries that map the
1319 * same virtual address using different page sizes. This is the recommended
1320 * workaround for Erratum 383 on AMD Family 10h processors. It prevents a
1321 * machine check exception for a TLB state that is improperly diagnosed as a
1325 pmap_update_pde(pmap_t pmap, vm_offset_t va, pd_entry_t *pde, pd_entry_t newpde)
1327 struct pde_action act;
1328 cpuset_t active, other_cpus;
1332 cpuid = PCPU_GET(cpuid);
1333 other_cpus = all_cpus;
1334 CPU_CLR(cpuid, &other_cpus);
1335 if (pmap == kernel_pmap)
1338 active = pmap->pm_active;
1339 if (CPU_OVERLAP(&active, &other_cpus)) {
1341 act.invalidate = active;
1344 act.newpde = newpde;
1345 CPU_SET(cpuid, &active);
1346 smp_rendezvous_cpus(active,
1347 smp_no_rendezvous_barrier, pmap == kernel_pmap ?
1348 pmap_update_pde_kernel : pmap_update_pde_user,
1349 pmap_update_pde_teardown, &act);
1351 if (pmap == kernel_pmap)
1352 pmap_kenter_pde(va, newpde);
1354 pde_store(pde, newpde);
1355 if (CPU_ISSET(cpuid, &active))
1356 pmap_update_pde_invalidate(va, newpde);
1362 * Normal, non-SMP, 486+ invalidation functions.
1363 * We inline these within pmap.c for speed.
1366 pmap_invalidate_page_int(pmap_t pmap, vm_offset_t va)
1369 if (pmap == kernel_pmap)
1374 pmap_invalidate_range_int(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
1378 if (pmap == kernel_pmap)
1379 for (addr = sva; addr < eva; addr += PAGE_SIZE)
1384 pmap_invalidate_all_int(pmap_t pmap)
1387 if (pmap == kernel_pmap)
1392 __CONCAT(PMTYPE, invalidate_cache)(void)
1399 pmap_update_pde(pmap_t pmap, vm_offset_t va, pd_entry_t *pde, pd_entry_t newpde)
1402 if (pmap == kernel_pmap)
1403 pmap_kenter_pde(va, newpde);
1405 pde_store(pde, newpde);
1406 if (pmap == kernel_pmap || !CPU_EMPTY(&pmap->pm_active))
1407 pmap_update_pde_invalidate(va, newpde);
1412 __CONCAT(PMTYPE, invalidate_page)(pmap_t pmap, vm_offset_t va)
1415 pmap_invalidate_page_int(pmap, va);
1419 __CONCAT(PMTYPE, invalidate_range)(pmap_t pmap, vm_offset_t sva,
1423 pmap_invalidate_range_int(pmap, sva, eva);
1427 __CONCAT(PMTYPE, invalidate_all)(pmap_t pmap)
1430 pmap_invalidate_all_int(pmap);
1434 pmap_invalidate_pde_page(pmap_t pmap, vm_offset_t va, pd_entry_t pde)
1438 * When the PDE has PG_PROMOTED set, the 2- or 4MB page mapping was
1439 * created by a promotion that did not invalidate the 512 or 1024 4KB
1440 * page mappings that might exist in the TLB. Consequently, at this
1441 * point, the TLB may hold both 4KB and 2- or 4MB page mappings for
1442 * the address range [va, va + NBPDR). Therefore, the entire range
1443 * must be invalidated here. In contrast, when PG_PROMOTED is clear,
1444 * the TLB will not hold any 4KB page mappings for the address range
1445 * [va, va + NBPDR), and so a single INVLPG suffices to invalidate the
1446 * 2- or 4MB page mapping from the TLB.
1448 if ((pde & PG_PROMOTED) != 0)
1449 pmap_invalidate_range_int(pmap, va, va + NBPDR - 1);
1451 pmap_invalidate_page_int(pmap, va);
1455 * Are we current address space or kernel?
1458 pmap_is_current(pmap_t pmap)
1461 return (pmap == kernel_pmap);
1465 * If the given pmap is not the current or kernel pmap, the returned pte must
1466 * be released by passing it to pmap_pte_release().
1469 __CONCAT(PMTYPE, pte)(pmap_t pmap, vm_offset_t va)
1474 pde = pmap_pde(pmap, va);
1478 /* are we current address space or kernel? */
1479 if (pmap_is_current(pmap))
1480 return (vtopte(va));
1481 mtx_lock(&PMAP2mutex);
1482 newpf = *pde & PG_FRAME;
1483 if ((*PMAP2 & PG_FRAME) != newpf) {
1484 *PMAP2 = newpf | PG_RW | PG_V | PG_A | PG_M;
1485 pmap_invalidate_page_int(kernel_pmap,
1486 (vm_offset_t)PADDR2);
1488 return (PADDR2 + (i386_btop(va) & (NPTEPG - 1)));
1494 * Releases a pte that was obtained from pmap_pte(). Be prepared for the pte
1497 static __inline void
1498 pmap_pte_release(pt_entry_t *pte)
1501 if ((pt_entry_t *)((vm_offset_t)pte & ~PAGE_MASK) == PADDR2)
1502 mtx_unlock(&PMAP2mutex);
1506 * NB: The sequence of updating a page table followed by accesses to the
1507 * corresponding pages is subject to the situation described in the "AMD64
1508 * Architecture Programmer's Manual Volume 2: System Programming" rev. 3.23,
1509 * "7.3.1 Special Coherency Considerations". Therefore, issuing the INVLPG
1510 * right after modifying the PTE bits is crucial.
1512 static __inline void
1513 invlcaddr(void *caddr)
1516 invlpg((u_int)caddr);
1520 * Super fast pmap_pte routine best used when scanning
1521 * the pv lists. This eliminates many coarse-grained
1522 * invltlb calls. Note that many of the pv list
1523 * scans are across different pmaps. It is very wasteful
1524 * to do an entire invltlb for checking a single mapping.
1526 * If the given pmap is not the current pmap, pvh_global_lock
1527 * must be held and curthread pinned to a CPU.
1530 pmap_pte_quick(pmap_t pmap, vm_offset_t va)
1535 pde = pmap_pde(pmap, va);
1539 /* are we current address space or kernel? */
1540 if (pmap_is_current(pmap))
1541 return (vtopte(va));
1542 rw_assert(&pvh_global_lock, RA_WLOCKED);
1543 KASSERT(curthread->td_pinned > 0, ("curthread not pinned"));
1544 newpf = *pde & PG_FRAME;
1545 if ((*PMAP1 & PG_FRAME) != newpf) {
1546 *PMAP1 = newpf | PG_RW | PG_V | PG_A | PG_M;
1548 PMAP1cpu = PCPU_GET(cpuid);
1554 if (PMAP1cpu != PCPU_GET(cpuid)) {
1555 PMAP1cpu = PCPU_GET(cpuid);
1561 return (PADDR1 + (i386_btop(va) & (NPTEPG - 1)));
1567 pmap_pte_quick3(pmap_t pmap, vm_offset_t va)
1572 pde = pmap_pde(pmap, va);
1576 rw_assert(&pvh_global_lock, RA_WLOCKED);
1577 KASSERT(curthread->td_pinned > 0, ("curthread not pinned"));
1578 newpf = *pde & PG_FRAME;
1579 if ((*PMAP3 & PG_FRAME) != newpf) {
1580 *PMAP3 = newpf | PG_RW | PG_V | PG_A | PG_M;
1582 PMAP3cpu = PCPU_GET(cpuid);
1588 if (PMAP3cpu != PCPU_GET(cpuid)) {
1589 PMAP3cpu = PCPU_GET(cpuid);
1595 return (PADDR3 + (i386_btop(va) & (NPTEPG - 1)));
1601 pmap_pte_ufast(pmap_t pmap, vm_offset_t va, pd_entry_t pde)
1603 pt_entry_t *eh_ptep, pte, *ptep;
1605 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1608 eh_ptep = (pt_entry_t *)PCPU_GET(pmap_eh_ptep);
1609 if ((*eh_ptep & PG_FRAME) != pde) {
1610 *eh_ptep = pde | PG_RW | PG_V | PG_A | PG_M;
1611 invlcaddr((void *)PCPU_GET(pmap_eh_va));
1613 ptep = (pt_entry_t *)PCPU_GET(pmap_eh_va) + (i386_btop(va) &
1621 * Extract from the kernel page table the physical address that is mapped by
1622 * the given virtual address "va".
1624 * This function may be used before pmap_bootstrap() is called.
1627 __CONCAT(PMTYPE, kextract)(vm_offset_t va)
1631 if ((pa = pte_load(&PTD[va >> PDRSHIFT])) & PG_PS) {
1632 pa = (pa & PG_PS_FRAME) | (va & PDRMASK);
1635 * Beware of a concurrent promotion that changes the PDE at
1636 * this point! For example, vtopte() must not be used to
1637 * access the PTE because it would use the new PDE. It is,
1638 * however, safe to use the old PDE because the page table
1639 * page is preserved by the promotion.
1641 pa = KPTmap[i386_btop(va)];
1642 pa = (pa & PG_FRAME) | (va & PAGE_MASK);
1648 * Routine: pmap_extract
1650 * Extract the physical page address associated
1651 * with the given map/virtual_address pair.
1654 __CONCAT(PMTYPE, extract)(pmap_t pmap, vm_offset_t va)
1662 pde = pmap->pm_pdir[va >> PDRSHIFT];
1664 if ((pde & PG_PS) != 0)
1665 rtval = (pde & PG_PS_FRAME) | (va & PDRMASK);
1667 pte = pmap_pte_ufast(pmap, va, pde);
1668 rtval = (pte & PG_FRAME) | (va & PAGE_MASK);
1676 * Routine: pmap_extract_and_hold
1678 * Atomically extract and hold the physical page
1679 * with the given pmap and virtual address pair
1680 * if that mapping permits the given protection.
1683 __CONCAT(PMTYPE, extract_and_hold)(pmap_t pmap, vm_offset_t va, vm_prot_t prot)
1694 pde = *pmap_pde(pmap, va);
1697 if ((pde & PG_RW) || (prot & VM_PROT_WRITE) == 0) {
1698 if (vm_page_pa_tryrelock(pmap, (pde &
1699 PG_PS_FRAME) | (va & PDRMASK), &pa))
1701 m = PHYS_TO_VM_PAGE(pa);
1704 pte = pmap_pte_ufast(pmap, va, pde);
1706 ((pte & PG_RW) || (prot & VM_PROT_WRITE) == 0)) {
1707 if (vm_page_pa_tryrelock(pmap, pte & PG_FRAME,
1710 m = PHYS_TO_VM_PAGE(pa);
1721 /***************************************************
1722 * Low level mapping routines.....
1723 ***************************************************/
1726 * Add a wired page to the kva.
1727 * Note: not SMP coherent.
1729 * This function may be used before pmap_bootstrap() is called.
1732 __CONCAT(PMTYPE, kenter)(vm_offset_t va, vm_paddr_t pa)
1737 pte_store(pte, pa | PG_RW | PG_V);
1740 static __inline void
1741 pmap_kenter_attr(vm_offset_t va, vm_paddr_t pa, int mode)
1746 pte_store(pte, pa | PG_RW | PG_V | pmap_cache_bits(kernel_pmap,
1751 * Remove a page from the kernel pagetables.
1752 * Note: not SMP coherent.
1754 * This function may be used before pmap_bootstrap() is called.
1757 __CONCAT(PMTYPE, kremove)(vm_offset_t va)
1766 * Used to map a range of physical addresses into kernel
1767 * virtual address space.
1769 * The value passed in '*virt' is a suggested virtual address for
1770 * the mapping. Architectures which can support a direct-mapped
1771 * physical to virtual region can return the appropriate address
1772 * within that region, leaving '*virt' unchanged. Other
1773 * architectures should map the pages starting at '*virt' and
1774 * update '*virt' with the first usable address after the mapped
1778 __CONCAT(PMTYPE, map)(vm_offset_t *virt, vm_paddr_t start, vm_paddr_t end,
1781 vm_offset_t va, sva;
1782 vm_paddr_t superpage_offset;
1787 * Does the physical address range's size and alignment permit at
1788 * least one superpage mapping to be created?
1790 superpage_offset = start & PDRMASK;
1791 if ((end - start) - ((NBPDR - superpage_offset) & PDRMASK) >= NBPDR) {
1793 * Increase the starting virtual address so that its alignment
1794 * does not preclude the use of superpage mappings.
1796 if ((va & PDRMASK) < superpage_offset)
1797 va = (va & ~PDRMASK) + superpage_offset;
1798 else if ((va & PDRMASK) > superpage_offset)
1799 va = ((va + PDRMASK) & ~PDRMASK) + superpage_offset;
1802 while (start < end) {
1803 if ((start & PDRMASK) == 0 && end - start >= NBPDR &&
1805 KASSERT((va & PDRMASK) == 0,
1806 ("pmap_map: misaligned va %#x", va));
1807 newpde = start | PG_PS | PG_RW | PG_V;
1808 pmap_kenter_pde(va, newpde);
1812 pmap_kenter(va, start);
1817 pmap_invalidate_range_int(kernel_pmap, sva, va);
1824 * Add a list of wired pages to the kva
1825 * this routine is only used for temporary
1826 * kernel mappings that do not need to have
1827 * page modification or references recorded.
1828 * Note that old mappings are simply written
1829 * over. The page *must* be wired.
1830 * Note: SMP coherent. Uses a ranged shootdown IPI.
1833 __CONCAT(PMTYPE, qenter)(vm_offset_t sva, vm_page_t *ma, int count)
1835 pt_entry_t *endpte, oldpte, pa, *pte;
1840 endpte = pte + count;
1841 while (pte < endpte) {
1843 pa = VM_PAGE_TO_PHYS(m) | pmap_cache_bits(kernel_pmap,
1845 if ((*pte & (PG_FRAME | PG_PTE_CACHE)) != pa) {
1847 #ifdef PMAP_PAE_COMP
1848 pte_store(pte, pa | pg_nx | PG_RW | PG_V);
1850 pte_store(pte, pa | PG_RW | PG_V);
1855 if (__predict_false((oldpte & PG_V) != 0))
1856 pmap_invalidate_range_int(kernel_pmap, sva, sva + count *
1861 * This routine tears out page mappings from the
1862 * kernel -- it is meant only for temporary mappings.
1863 * Note: SMP coherent. Uses a ranged shootdown IPI.
1866 __CONCAT(PMTYPE, qremove)(vm_offset_t sva, int count)
1871 while (count-- > 0) {
1875 pmap_invalidate_range_int(kernel_pmap, sva, va);
1878 /***************************************************
1879 * Page table page management routines.....
1880 ***************************************************/
1882 * Schedule the specified unused page table page to be freed. Specifically,
1883 * add the page to the specified list of pages that will be released to the
1884 * physical memory manager after the TLB has been updated.
1886 static __inline void
1887 pmap_add_delayed_free_list(vm_page_t m, struct spglist *free,
1888 boolean_t set_PG_ZERO)
1892 m->flags |= PG_ZERO;
1894 m->flags &= ~PG_ZERO;
1895 SLIST_INSERT_HEAD(free, m, plinks.s.ss);
1899 * Inserts the specified page table page into the specified pmap's collection
1900 * of idle page table pages. Each of a pmap's page table pages is responsible
1901 * for mapping a distinct range of virtual addresses. The pmap's collection is
1902 * ordered by this virtual address range.
1905 pmap_insert_pt_page(pmap_t pmap, vm_page_t mpte)
1908 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1909 return (vm_radix_insert(&pmap->pm_root, mpte));
1913 * Removes the page table page mapping the specified virtual address from the
1914 * specified pmap's collection of idle page table pages, and returns it.
1915 * Otherwise, returns NULL if there is no page table page corresponding to the
1916 * specified virtual address.
1918 static __inline vm_page_t
1919 pmap_remove_pt_page(pmap_t pmap, vm_offset_t va)
1922 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1923 return (vm_radix_remove(&pmap->pm_root, va >> PDRSHIFT));
1927 * Decrements a page table page's wire count, which is used to record the
1928 * number of valid page table entries within the page. If the wire count
1929 * drops to zero, then the page table page is unmapped. Returns TRUE if the
1930 * page table page was unmapped and FALSE otherwise.
1932 static inline boolean_t
1933 pmap_unwire_ptp(pmap_t pmap, vm_page_t m, struct spglist *free)
1937 if (m->wire_count == 0) {
1938 _pmap_unwire_ptp(pmap, m, free);
1945 _pmap_unwire_ptp(pmap_t pmap, vm_page_t m, struct spglist *free)
1949 * unmap the page table page
1951 pmap->pm_pdir[m->pindex] = 0;
1952 --pmap->pm_stats.resident_count;
1955 * There is not need to invalidate the recursive mapping since
1956 * we never instantiate such mapping for the usermode pmaps,
1957 * and never remove page table pages from the kernel pmap.
1958 * Put page on a list so that it is released since all TLB
1959 * shootdown is done.
1961 MPASS(pmap != kernel_pmap);
1962 pmap_add_delayed_free_list(m, free, TRUE);
1966 * After removing a page table entry, this routine is used to
1967 * conditionally free the page, and manage the hold/wire counts.
1970 pmap_unuse_pt(pmap_t pmap, vm_offset_t va, struct spglist *free)
1975 if (pmap == kernel_pmap)
1977 ptepde = *pmap_pde(pmap, va);
1978 mpte = PHYS_TO_VM_PAGE(ptepde & PG_FRAME);
1979 return (pmap_unwire_ptp(pmap, mpte, free));
1983 * Initialize the pmap for the swapper process.
1986 __CONCAT(PMTYPE, pinit0)(pmap_t pmap)
1989 PMAP_LOCK_INIT(pmap);
1990 pmap->pm_pdir = IdlePTD;
1991 #ifdef PMAP_PAE_COMP
1992 pmap->pm_pdpt = IdlePDPT;
1994 pmap->pm_root.rt_root = 0;
1995 CPU_ZERO(&pmap->pm_active);
1996 TAILQ_INIT(&pmap->pm_pvchunk);
1997 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1998 pmap_activate_boot(pmap);
2002 * Initialize a preallocated and zeroed pmap structure,
2003 * such as one in a vmspace structure.
2006 __CONCAT(PMTYPE, pinit)(pmap_t pmap)
2012 * No need to allocate page table space yet but we do need a valid
2013 * page directory table.
2015 if (pmap->pm_pdir == NULL) {
2016 pmap->pm_pdir = (pd_entry_t *)kva_alloc(NBPTD);
2017 if (pmap->pm_pdir == NULL)
2019 #ifdef PMAP_PAE_COMP
2020 pmap->pm_pdpt = uma_zalloc(pdptzone, M_WAITOK | M_ZERO);
2021 KASSERT(((vm_offset_t)pmap->pm_pdpt &
2022 ((NPGPTD * sizeof(pdpt_entry_t)) - 1)) == 0,
2023 ("pmap_pinit: pdpt misaligned"));
2024 KASSERT(pmap_kextract((vm_offset_t)pmap->pm_pdpt) < (4ULL<<30),
2025 ("pmap_pinit: pdpt above 4g"));
2027 pmap->pm_root.rt_root = 0;
2029 KASSERT(vm_radix_is_empty(&pmap->pm_root),
2030 ("pmap_pinit: pmap has reserved page table page(s)"));
2033 * allocate the page directory page(s)
2035 for (i = 0; i < NPGPTD; i++) {
2036 m = vm_page_alloc(NULL, 0, VM_ALLOC_NORMAL | VM_ALLOC_NOOBJ |
2037 VM_ALLOC_WIRED | VM_ALLOC_ZERO | VM_ALLOC_WAITOK);
2038 pmap->pm_ptdpg[i] = m;
2039 #ifdef PMAP_PAE_COMP
2040 pmap->pm_pdpt[i] = VM_PAGE_TO_PHYS(m) | PG_V;
2044 pmap_qenter((vm_offset_t)pmap->pm_pdir, pmap->pm_ptdpg, NPGPTD);
2045 #ifdef PMAP_PAE_COMP
2046 if ((cpu_feature & CPUID_PAT) == 0) {
2047 pmap_invalidate_cache_range(
2048 trunc_page((vm_offset_t)pmap->pm_pdpt),
2049 round_page((vm_offset_t)pmap->pm_pdpt +
2050 NPGPTD * sizeof(pdpt_entry_t)));
2054 for (i = 0; i < NPGPTD; i++)
2055 if ((pmap->pm_ptdpg[i]->flags & PG_ZERO) == 0)
2056 pagezero(pmap->pm_pdir + (i * NPDEPG));
2058 /* Install the trampoline mapping. */
2059 pmap->pm_pdir[TRPTDI] = PTD[TRPTDI];
2061 CPU_ZERO(&pmap->pm_active);
2062 TAILQ_INIT(&pmap->pm_pvchunk);
2063 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
2069 * this routine is called if the page table page is not
2073 _pmap_allocpte(pmap_t pmap, u_int ptepindex, u_int flags)
2079 * Allocate a page table page.
2081 if ((m = vm_page_alloc(NULL, ptepindex, VM_ALLOC_NOOBJ |
2082 VM_ALLOC_WIRED | VM_ALLOC_ZERO)) == NULL) {
2083 if ((flags & PMAP_ENTER_NOSLEEP) == 0) {
2085 rw_wunlock(&pvh_global_lock);
2087 rw_wlock(&pvh_global_lock);
2092 * Indicate the need to retry. While waiting, the page table
2093 * page may have been allocated.
2097 if ((m->flags & PG_ZERO) == 0)
2101 * Map the pagetable page into the process address space, if
2102 * it isn't already there.
2105 pmap->pm_stats.resident_count++;
2107 ptepa = VM_PAGE_TO_PHYS(m);
2108 pmap->pm_pdir[ptepindex] =
2109 (pd_entry_t) (ptepa | PG_U | PG_RW | PG_V | PG_A | PG_M);
2115 pmap_allocpte(pmap_t pmap, vm_offset_t va, u_int flags)
2122 * Calculate pagetable page index
2124 ptepindex = va >> PDRSHIFT;
2127 * Get the page directory entry
2129 ptepa = pmap->pm_pdir[ptepindex];
2132 * This supports switching from a 4MB page to a
2135 if (ptepa & PG_PS) {
2136 (void)pmap_demote_pde(pmap, &pmap->pm_pdir[ptepindex], va);
2137 ptepa = pmap->pm_pdir[ptepindex];
2141 * If the page table page is mapped, we just increment the
2142 * hold count, and activate it.
2145 m = PHYS_TO_VM_PAGE(ptepa & PG_FRAME);
2149 * Here if the pte page isn't mapped, or if it has
2152 m = _pmap_allocpte(pmap, ptepindex, flags);
2153 if (m == NULL && (flags & PMAP_ENTER_NOSLEEP) == 0)
2160 /***************************************************
2161 * Pmap allocation/deallocation routines.
2162 ***************************************************/
2165 * Release any resources held by the given physical map.
2166 * Called when a pmap initialized by pmap_pinit is being released.
2167 * Should only be called if the map contains no valid mappings.
2170 __CONCAT(PMTYPE, release)(pmap_t pmap)
2175 KASSERT(pmap->pm_stats.resident_count == 0,
2176 ("pmap_release: pmap resident count %ld != 0",
2177 pmap->pm_stats.resident_count));
2178 KASSERT(vm_radix_is_empty(&pmap->pm_root),
2179 ("pmap_release: pmap has reserved page table page(s)"));
2180 KASSERT(CPU_EMPTY(&pmap->pm_active),
2181 ("releasing active pmap %p", pmap));
2183 pmap_qremove((vm_offset_t)pmap->pm_pdir, NPGPTD);
2185 for (i = 0; i < NPGPTD; i++) {
2186 m = pmap->pm_ptdpg[i];
2187 #ifdef PMAP_PAE_COMP
2188 KASSERT(VM_PAGE_TO_PHYS(m) == (pmap->pm_pdpt[i] & PG_FRAME),
2189 ("pmap_release: got wrong ptd page"));
2191 vm_page_unwire_noq(m);
2197 * grow the number of kernel page table entries, if needed
2200 __CONCAT(PMTYPE, growkernel)(vm_offset_t addr)
2202 vm_paddr_t ptppaddr;
2206 mtx_assert(&kernel_map->system_mtx, MA_OWNED);
2207 addr = roundup2(addr, NBPDR);
2208 if (addr - 1 >= vm_map_max(kernel_map))
2209 addr = vm_map_max(kernel_map);
2210 while (kernel_vm_end < addr) {
2211 if (pdir_pde(PTD, kernel_vm_end)) {
2212 kernel_vm_end = (kernel_vm_end + NBPDR) & ~PDRMASK;
2213 if (kernel_vm_end - 1 >= vm_map_max(kernel_map)) {
2214 kernel_vm_end = vm_map_max(kernel_map);
2220 nkpg = vm_page_alloc(NULL, kernel_vm_end >> PDRSHIFT,
2221 VM_ALLOC_INTERRUPT | VM_ALLOC_NOOBJ | VM_ALLOC_WIRED |
2224 panic("pmap_growkernel: no memory to grow kernel");
2228 if ((nkpg->flags & PG_ZERO) == 0)
2229 pmap_zero_page(nkpg);
2230 ptppaddr = VM_PAGE_TO_PHYS(nkpg);
2231 newpdir = (pd_entry_t) (ptppaddr | PG_V | PG_RW | PG_A | PG_M);
2232 pdir_pde(KPTD, kernel_vm_end) = newpdir;
2234 pmap_kenter_pde(kernel_vm_end, newpdir);
2235 kernel_vm_end = (kernel_vm_end + NBPDR) & ~PDRMASK;
2236 if (kernel_vm_end - 1 >= vm_map_max(kernel_map)) {
2237 kernel_vm_end = vm_map_max(kernel_map);
2244 /***************************************************
2245 * page management routines.
2246 ***************************************************/
2248 CTASSERT(sizeof(struct pv_chunk) == PAGE_SIZE);
2249 CTASSERT(_NPCM == 11);
2250 CTASSERT(_NPCPV == 336);
2252 static __inline struct pv_chunk *
2253 pv_to_chunk(pv_entry_t pv)
2256 return ((struct pv_chunk *)((uintptr_t)pv & ~(uintptr_t)PAGE_MASK));
2259 #define PV_PMAP(pv) (pv_to_chunk(pv)->pc_pmap)
2261 #define PC_FREE0_9 0xfffffffful /* Free values for index 0 through 9 */
2262 #define PC_FREE10 0x0000fffful /* Free values for index 10 */
2264 static const uint32_t pc_freemask[_NPCM] = {
2265 PC_FREE0_9, PC_FREE0_9, PC_FREE0_9,
2266 PC_FREE0_9, PC_FREE0_9, PC_FREE0_9,
2267 PC_FREE0_9, PC_FREE0_9, PC_FREE0_9,
2268 PC_FREE0_9, PC_FREE10
2272 extern int pc_chunk_count, pc_chunk_allocs, pc_chunk_frees, pc_chunk_tryfail;
2273 extern long pv_entry_frees, pv_entry_allocs;
2274 extern int pv_entry_spare;
2278 * We are in a serious low memory condition. Resort to
2279 * drastic measures to free some pages so we can allocate
2280 * another pv entry chunk.
2283 pmap_pv_reclaim(pmap_t locked_pmap)
2286 struct pv_chunk *pc;
2287 struct md_page *pvh;
2290 pt_entry_t *pte, tpte;
2294 struct spglist free;
2296 int bit, field, freed;
2298 PMAP_LOCK_ASSERT(locked_pmap, MA_OWNED);
2302 TAILQ_INIT(&newtail);
2303 while ((pc = TAILQ_FIRST(&pv_chunks)) != NULL && (pv_vafree == 0 ||
2304 SLIST_EMPTY(&free))) {
2305 TAILQ_REMOVE(&pv_chunks, pc, pc_lru);
2306 if (pmap != pc->pc_pmap) {
2308 pmap_invalidate_all_int(pmap);
2309 if (pmap != locked_pmap)
2313 /* Avoid deadlock and lock recursion. */
2314 if (pmap > locked_pmap)
2316 else if (pmap != locked_pmap && !PMAP_TRYLOCK(pmap)) {
2318 TAILQ_INSERT_TAIL(&newtail, pc, pc_lru);
2324 * Destroy every non-wired, 4 KB page mapping in the chunk.
2327 for (field = 0; field < _NPCM; field++) {
2328 for (inuse = ~pc->pc_map[field] & pc_freemask[field];
2329 inuse != 0; inuse &= ~(1UL << bit)) {
2331 pv = &pc->pc_pventry[field * 32 + bit];
2333 pde = pmap_pde(pmap, va);
2334 if ((*pde & PG_PS) != 0)
2336 pte = __CONCAT(PMTYPE, pte)(pmap, va);
2338 if ((tpte & PG_W) == 0)
2339 tpte = pte_load_clear(pte);
2340 pmap_pte_release(pte);
2341 if ((tpte & PG_W) != 0)
2344 ("pmap_pv_reclaim: pmap %p va %x zero pte",
2346 if ((tpte & PG_G) != 0)
2347 pmap_invalidate_page_int(pmap, va);
2348 m = PHYS_TO_VM_PAGE(tpte & PG_FRAME);
2349 if ((tpte & (PG_M | PG_RW)) == (PG_M | PG_RW))
2351 if ((tpte & PG_A) != 0)
2352 vm_page_aflag_set(m, PGA_REFERENCED);
2353 TAILQ_REMOVE(&m->md.pv_list, pv, pv_next);
2354 if (TAILQ_EMPTY(&m->md.pv_list) &&
2355 (m->flags & PG_FICTITIOUS) == 0) {
2356 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
2357 if (TAILQ_EMPTY(&pvh->pv_list)) {
2358 vm_page_aflag_clear(m,
2362 pc->pc_map[field] |= 1UL << bit;
2363 pmap_unuse_pt(pmap, va, &free);
2368 TAILQ_INSERT_TAIL(&newtail, pc, pc_lru);
2371 /* Every freed mapping is for a 4 KB page. */
2372 pmap->pm_stats.resident_count -= freed;
2373 PV_STAT(pv_entry_frees += freed);
2374 PV_STAT(pv_entry_spare += freed);
2375 pv_entry_count -= freed;
2376 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
2377 for (field = 0; field < _NPCM; field++)
2378 if (pc->pc_map[field] != pc_freemask[field]) {
2379 TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc,
2381 TAILQ_INSERT_TAIL(&newtail, pc, pc_lru);
2384 * One freed pv entry in locked_pmap is
2387 if (pmap == locked_pmap)
2391 if (field == _NPCM) {
2392 PV_STAT(pv_entry_spare -= _NPCPV);
2393 PV_STAT(pc_chunk_count--);
2394 PV_STAT(pc_chunk_frees++);
2395 /* Entire chunk is free; return it. */
2396 m_pc = PHYS_TO_VM_PAGE(pmap_kextract((vm_offset_t)pc));
2397 pmap_qremove((vm_offset_t)pc, 1);
2398 pmap_ptelist_free(&pv_vafree, (vm_offset_t)pc);
2403 TAILQ_CONCAT(&pv_chunks, &newtail, pc_lru);
2405 pmap_invalidate_all_int(pmap);
2406 if (pmap != locked_pmap)
2409 if (m_pc == NULL && pv_vafree != 0 && SLIST_EMPTY(&free)) {
2410 m_pc = SLIST_FIRST(&free);
2411 SLIST_REMOVE_HEAD(&free, plinks.s.ss);
2412 /* Recycle a freed page table page. */
2413 m_pc->wire_count = 1;
2415 vm_page_free_pages_toq(&free, true);
2420 * free the pv_entry back to the free list
2423 free_pv_entry(pmap_t pmap, pv_entry_t pv)
2425 struct pv_chunk *pc;
2426 int idx, field, bit;
2428 rw_assert(&pvh_global_lock, RA_WLOCKED);
2429 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2430 PV_STAT(pv_entry_frees++);
2431 PV_STAT(pv_entry_spare++);
2433 pc = pv_to_chunk(pv);
2434 idx = pv - &pc->pc_pventry[0];
2437 pc->pc_map[field] |= 1ul << bit;
2438 for (idx = 0; idx < _NPCM; idx++)
2439 if (pc->pc_map[idx] != pc_freemask[idx]) {
2441 * 98% of the time, pc is already at the head of the
2442 * list. If it isn't already, move it to the head.
2444 if (__predict_false(TAILQ_FIRST(&pmap->pm_pvchunk) !=
2446 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
2447 TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc,
2452 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
2457 free_pv_chunk(struct pv_chunk *pc)
2461 TAILQ_REMOVE(&pv_chunks, pc, pc_lru);
2462 PV_STAT(pv_entry_spare -= _NPCPV);
2463 PV_STAT(pc_chunk_count--);
2464 PV_STAT(pc_chunk_frees++);
2465 /* entire chunk is free, return it */
2466 m = PHYS_TO_VM_PAGE(pmap_kextract((vm_offset_t)pc));
2467 pmap_qremove((vm_offset_t)pc, 1);
2468 vm_page_unwire(m, PQ_NONE);
2470 pmap_ptelist_free(&pv_vafree, (vm_offset_t)pc);
2474 * get a new pv_entry, allocating a block from the system
2478 get_pv_entry(pmap_t pmap, boolean_t try)
2480 static const struct timeval printinterval = { 60, 0 };
2481 static struct timeval lastprint;
2484 struct pv_chunk *pc;
2487 rw_assert(&pvh_global_lock, RA_WLOCKED);
2488 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2489 PV_STAT(pv_entry_allocs++);
2491 if (pv_entry_count > pv_entry_high_water)
2492 if (ratecheck(&lastprint, &printinterval))
2493 printf("Approaching the limit on PV entries, consider "
2494 "increasing either the vm.pmap.shpgperproc or the "
2495 "vm.pmap.pv_entries tunable.\n");
2497 pc = TAILQ_FIRST(&pmap->pm_pvchunk);
2499 for (field = 0; field < _NPCM; field++) {
2500 if (pc->pc_map[field]) {
2501 bit = bsfl(pc->pc_map[field]);
2505 if (field < _NPCM) {
2506 pv = &pc->pc_pventry[field * 32 + bit];
2507 pc->pc_map[field] &= ~(1ul << bit);
2508 /* If this was the last item, move it to tail */
2509 for (field = 0; field < _NPCM; field++)
2510 if (pc->pc_map[field] != 0) {
2511 PV_STAT(pv_entry_spare--);
2512 return (pv); /* not full, return */
2514 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
2515 TAILQ_INSERT_TAIL(&pmap->pm_pvchunk, pc, pc_list);
2516 PV_STAT(pv_entry_spare--);
2521 * Access to the ptelist "pv_vafree" is synchronized by the pvh
2522 * global lock. If "pv_vafree" is currently non-empty, it will
2523 * remain non-empty until pmap_ptelist_alloc() completes.
2525 if (pv_vafree == 0 || (m = vm_page_alloc(NULL, 0, VM_ALLOC_NORMAL |
2526 VM_ALLOC_NOOBJ | VM_ALLOC_WIRED)) == NULL) {
2529 PV_STAT(pc_chunk_tryfail++);
2532 m = pmap_pv_reclaim(pmap);
2536 PV_STAT(pc_chunk_count++);
2537 PV_STAT(pc_chunk_allocs++);
2538 pc = (struct pv_chunk *)pmap_ptelist_alloc(&pv_vafree);
2539 pmap_qenter((vm_offset_t)pc, &m, 1);
2541 pc->pc_map[0] = pc_freemask[0] & ~1ul; /* preallocated bit 0 */
2542 for (field = 1; field < _NPCM; field++)
2543 pc->pc_map[field] = pc_freemask[field];
2544 TAILQ_INSERT_TAIL(&pv_chunks, pc, pc_lru);
2545 pv = &pc->pc_pventry[0];
2546 TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc, pc_list);
2547 PV_STAT(pv_entry_spare += _NPCPV - 1);
2551 static __inline pv_entry_t
2552 pmap_pvh_remove(struct md_page *pvh, pmap_t pmap, vm_offset_t va)
2556 rw_assert(&pvh_global_lock, RA_WLOCKED);
2557 TAILQ_FOREACH(pv, &pvh->pv_list, pv_next) {
2558 if (pmap == PV_PMAP(pv) && va == pv->pv_va) {
2559 TAILQ_REMOVE(&pvh->pv_list, pv, pv_next);
2567 pmap_pv_demote_pde(pmap_t pmap, vm_offset_t va, vm_paddr_t pa)
2569 struct md_page *pvh;
2571 vm_offset_t va_last;
2574 rw_assert(&pvh_global_lock, RA_WLOCKED);
2575 KASSERT((pa & PDRMASK) == 0,
2576 ("pmap_pv_demote_pde: pa is not 4mpage aligned"));
2579 * Transfer the 4mpage's pv entry for this mapping to the first
2582 pvh = pa_to_pvh(pa);
2583 va = trunc_4mpage(va);
2584 pv = pmap_pvh_remove(pvh, pmap, va);
2585 KASSERT(pv != NULL, ("pmap_pv_demote_pde: pv not found"));
2586 m = PHYS_TO_VM_PAGE(pa);
2587 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_next);
2588 /* Instantiate the remaining NPTEPG - 1 pv entries. */
2589 va_last = va + NBPDR - PAGE_SIZE;
2592 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2593 ("pmap_pv_demote_pde: page %p is not managed", m));
2595 pmap_insert_entry(pmap, va, m);
2596 } while (va < va_last);
2599 #if VM_NRESERVLEVEL > 0
2601 pmap_pv_promote_pde(pmap_t pmap, vm_offset_t va, vm_paddr_t pa)
2603 struct md_page *pvh;
2605 vm_offset_t va_last;
2608 rw_assert(&pvh_global_lock, RA_WLOCKED);
2609 KASSERT((pa & PDRMASK) == 0,
2610 ("pmap_pv_promote_pde: pa is not 4mpage aligned"));
2613 * Transfer the first page's pv entry for this mapping to the
2614 * 4mpage's pv list. Aside from avoiding the cost of a call
2615 * to get_pv_entry(), a transfer avoids the possibility that
2616 * get_pv_entry() calls pmap_collect() and that pmap_collect()
2617 * removes one of the mappings that is being promoted.
2619 m = PHYS_TO_VM_PAGE(pa);
2620 va = trunc_4mpage(va);
2621 pv = pmap_pvh_remove(&m->md, pmap, va);
2622 KASSERT(pv != NULL, ("pmap_pv_promote_pde: pv not found"));
2623 pvh = pa_to_pvh(pa);
2624 TAILQ_INSERT_TAIL(&pvh->pv_list, pv, pv_next);
2625 /* Free the remaining NPTEPG - 1 pv entries. */
2626 va_last = va + NBPDR - PAGE_SIZE;
2630 pmap_pvh_free(&m->md, pmap, va);
2631 } while (va < va_last);
2633 #endif /* VM_NRESERVLEVEL > 0 */
2636 pmap_pvh_free(struct md_page *pvh, pmap_t pmap, vm_offset_t va)
2640 pv = pmap_pvh_remove(pvh, pmap, va);
2641 KASSERT(pv != NULL, ("pmap_pvh_free: pv not found"));
2642 free_pv_entry(pmap, pv);
2646 pmap_remove_entry(pmap_t pmap, vm_page_t m, vm_offset_t va)
2648 struct md_page *pvh;
2650 rw_assert(&pvh_global_lock, RA_WLOCKED);
2651 pmap_pvh_free(&m->md, pmap, va);
2652 if (TAILQ_EMPTY(&m->md.pv_list) && (m->flags & PG_FICTITIOUS) == 0) {
2653 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
2654 if (TAILQ_EMPTY(&pvh->pv_list))
2655 vm_page_aflag_clear(m, PGA_WRITEABLE);
2660 * Create a pv entry for page at pa for
2664 pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t m)
2668 rw_assert(&pvh_global_lock, RA_WLOCKED);
2669 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2670 pv = get_pv_entry(pmap, FALSE);
2672 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_next);
2676 * Conditionally create a pv entry.
2679 pmap_try_insert_pv_entry(pmap_t pmap, vm_offset_t va, vm_page_t m)
2683 rw_assert(&pvh_global_lock, RA_WLOCKED);
2684 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2685 if (pv_entry_count < pv_entry_high_water &&
2686 (pv = get_pv_entry(pmap, TRUE)) != NULL) {
2688 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_next);
2695 * Create the pv entries for each of the pages within a superpage.
2698 pmap_pv_insert_pde(pmap_t pmap, vm_offset_t va, pd_entry_t pde, u_int flags)
2700 struct md_page *pvh;
2704 rw_assert(&pvh_global_lock, RA_WLOCKED);
2705 noreclaim = (flags & PMAP_ENTER_NORECLAIM) != 0;
2706 if ((noreclaim && pv_entry_count >= pv_entry_high_water) ||
2707 (pv = get_pv_entry(pmap, noreclaim)) == NULL)
2710 pvh = pa_to_pvh(pde & PG_PS_FRAME);
2711 TAILQ_INSERT_TAIL(&pvh->pv_list, pv, pv_next);
2716 * Fills a page table page with mappings to consecutive physical pages.
2719 pmap_fill_ptp(pt_entry_t *firstpte, pt_entry_t newpte)
2723 for (pte = firstpte; pte < firstpte + NPTEPG; pte++) {
2725 newpte += PAGE_SIZE;
2730 * Tries to demote a 2- or 4MB page mapping. If demotion fails, the
2731 * 2- or 4MB page mapping is invalidated.
2734 pmap_demote_pde(pmap_t pmap, pd_entry_t *pde, vm_offset_t va)
2736 pd_entry_t newpde, oldpde;
2737 pt_entry_t *firstpte, newpte;
2740 struct spglist free;
2743 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2745 KASSERT((oldpde & (PG_PS | PG_V)) == (PG_PS | PG_V),
2746 ("pmap_demote_pde: oldpde is missing PG_PS and/or PG_V"));
2747 if ((oldpde & PG_A) == 0 || (mpte = pmap_remove_pt_page(pmap, va)) ==
2749 KASSERT((oldpde & PG_W) == 0,
2750 ("pmap_demote_pde: page table page for a wired mapping"
2754 * Invalidate the 2- or 4MB page mapping and return
2755 * "failure" if the mapping was never accessed or the
2756 * allocation of the new page table page fails.
2758 if ((oldpde & PG_A) == 0 || (mpte = vm_page_alloc(NULL,
2759 va >> PDRSHIFT, VM_ALLOC_NOOBJ | VM_ALLOC_NORMAL |
2760 VM_ALLOC_WIRED)) == NULL) {
2762 sva = trunc_4mpage(va);
2763 pmap_remove_pde(pmap, pde, sva, &free);
2764 if ((oldpde & PG_G) == 0)
2765 pmap_invalidate_pde_page(pmap, sva, oldpde);
2766 vm_page_free_pages_toq(&free, true);
2767 CTR2(KTR_PMAP, "pmap_demote_pde: failure for va %#x"
2768 " in pmap %p", va, pmap);
2771 if (pmap != kernel_pmap)
2772 pmap->pm_stats.resident_count++;
2774 mptepa = VM_PAGE_TO_PHYS(mpte);
2777 * If the page mapping is in the kernel's address space, then the
2778 * KPTmap can provide access to the page table page. Otherwise,
2779 * temporarily map the page table page (mpte) into the kernel's
2780 * address space at either PADDR1 or PADDR2.
2782 if (pmap == kernel_pmap)
2783 firstpte = &KPTmap[i386_btop(trunc_4mpage(va))];
2784 else if (curthread->td_pinned > 0 && rw_wowned(&pvh_global_lock)) {
2785 if ((*PMAP1 & PG_FRAME) != mptepa) {
2786 *PMAP1 = mptepa | PG_RW | PG_V | PG_A | PG_M;
2788 PMAP1cpu = PCPU_GET(cpuid);
2794 if (PMAP1cpu != PCPU_GET(cpuid)) {
2795 PMAP1cpu = PCPU_GET(cpuid);
2803 mtx_lock(&PMAP2mutex);
2804 if ((*PMAP2 & PG_FRAME) != mptepa) {
2805 *PMAP2 = mptepa | PG_RW | PG_V | PG_A | PG_M;
2806 pmap_invalidate_page_int(kernel_pmap,
2807 (vm_offset_t)PADDR2);
2811 newpde = mptepa | PG_M | PG_A | (oldpde & PG_U) | PG_RW | PG_V;
2812 KASSERT((oldpde & PG_A) != 0,
2813 ("pmap_demote_pde: oldpde is missing PG_A"));
2814 KASSERT((oldpde & (PG_M | PG_RW)) != PG_RW,
2815 ("pmap_demote_pde: oldpde is missing PG_M"));
2816 newpte = oldpde & ~PG_PS;
2817 if ((newpte & PG_PDE_PAT) != 0)
2818 newpte ^= PG_PDE_PAT | PG_PTE_PAT;
2821 * If the page table page is new, initialize it.
2823 if (mpte->wire_count == 1) {
2824 mpte->wire_count = NPTEPG;
2825 pmap_fill_ptp(firstpte, newpte);
2827 KASSERT((*firstpte & PG_FRAME) == (newpte & PG_FRAME),
2828 ("pmap_demote_pde: firstpte and newpte map different physical"
2832 * If the mapping has changed attributes, update the page table
2835 if ((*firstpte & PG_PTE_PROMOTE) != (newpte & PG_PTE_PROMOTE))
2836 pmap_fill_ptp(firstpte, newpte);
2839 * Demote the mapping. This pmap is locked. The old PDE has
2840 * PG_A set. If the old PDE has PG_RW set, it also has PG_M
2841 * set. Thus, there is no danger of a race with another
2842 * processor changing the setting of PG_A and/or PG_M between
2843 * the read above and the store below.
2845 if (workaround_erratum383)
2846 pmap_update_pde(pmap, va, pde, newpde);
2847 else if (pmap == kernel_pmap)
2848 pmap_kenter_pde(va, newpde);
2850 pde_store(pde, newpde);
2851 if (firstpte == PADDR2)
2852 mtx_unlock(&PMAP2mutex);
2855 * Invalidate the recursive mapping of the page table page.
2857 pmap_invalidate_page_int(pmap, (vm_offset_t)vtopte(va));
2860 * Demote the pv entry. This depends on the earlier demotion
2861 * of the mapping. Specifically, the (re)creation of a per-
2862 * page pv entry might trigger the execution of pmap_collect(),
2863 * which might reclaim a newly (re)created per-page pv entry
2864 * and destroy the associated mapping. In order to destroy
2865 * the mapping, the PDE must have already changed from mapping
2866 * the 2mpage to referencing the page table page.
2868 if ((oldpde & PG_MANAGED) != 0)
2869 pmap_pv_demote_pde(pmap, va, oldpde & PG_PS_FRAME);
2871 pmap_pde_demotions++;
2872 CTR2(KTR_PMAP, "pmap_demote_pde: success for va %#x"
2873 " in pmap %p", va, pmap);
2878 * Removes a 2- or 4MB page mapping from the kernel pmap.
2881 pmap_remove_kernel_pde(pmap_t pmap, pd_entry_t *pde, vm_offset_t va)
2887 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2888 mpte = pmap_remove_pt_page(pmap, va);
2890 panic("pmap_remove_kernel_pde: Missing pt page.");
2892 mptepa = VM_PAGE_TO_PHYS(mpte);
2893 newpde = mptepa | PG_M | PG_A | PG_RW | PG_V;
2896 * Initialize the page table page.
2898 pagezero((void *)&KPTmap[i386_btop(trunc_4mpage(va))]);
2901 * Remove the mapping.
2903 if (workaround_erratum383)
2904 pmap_update_pde(pmap, va, pde, newpde);
2906 pmap_kenter_pde(va, newpde);
2909 * Invalidate the recursive mapping of the page table page.
2911 pmap_invalidate_page_int(pmap, (vm_offset_t)vtopte(va));
2915 * pmap_remove_pde: do the things to unmap a superpage in a process
2918 pmap_remove_pde(pmap_t pmap, pd_entry_t *pdq, vm_offset_t sva,
2919 struct spglist *free)
2921 struct md_page *pvh;
2923 vm_offset_t eva, va;
2926 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2927 KASSERT((sva & PDRMASK) == 0,
2928 ("pmap_remove_pde: sva is not 4mpage aligned"));
2929 oldpde = pte_load_clear(pdq);
2931 pmap->pm_stats.wired_count -= NBPDR / PAGE_SIZE;
2934 * Machines that don't support invlpg, also don't support
2937 if ((oldpde & PG_G) != 0)
2938 pmap_invalidate_pde_page(kernel_pmap, sva, oldpde);
2940 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
2941 if (oldpde & PG_MANAGED) {
2942 pvh = pa_to_pvh(oldpde & PG_PS_FRAME);
2943 pmap_pvh_free(pvh, pmap, sva);
2945 for (va = sva, m = PHYS_TO_VM_PAGE(oldpde & PG_PS_FRAME);
2946 va < eva; va += PAGE_SIZE, m++) {
2947 if ((oldpde & (PG_M | PG_RW)) == (PG_M | PG_RW))
2950 vm_page_aflag_set(m, PGA_REFERENCED);
2951 if (TAILQ_EMPTY(&m->md.pv_list) &&
2952 TAILQ_EMPTY(&pvh->pv_list))
2953 vm_page_aflag_clear(m, PGA_WRITEABLE);
2956 if (pmap == kernel_pmap) {
2957 pmap_remove_kernel_pde(pmap, pdq, sva);
2959 mpte = pmap_remove_pt_page(pmap, sva);
2961 pmap->pm_stats.resident_count--;
2962 KASSERT(mpte->wire_count == NPTEPG,
2963 ("pmap_remove_pde: pte page wire count error"));
2964 mpte->wire_count = 0;
2965 pmap_add_delayed_free_list(mpte, free, FALSE);
2971 * pmap_remove_pte: do the things to unmap a page in a process
2974 pmap_remove_pte(pmap_t pmap, pt_entry_t *ptq, vm_offset_t va,
2975 struct spglist *free)
2980 rw_assert(&pvh_global_lock, RA_WLOCKED);
2981 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2982 oldpte = pte_load_clear(ptq);
2983 KASSERT(oldpte != 0,
2984 ("pmap_remove_pte: pmap %p va %x zero pte", pmap, va));
2986 pmap->pm_stats.wired_count -= 1;
2988 * Machines that don't support invlpg, also don't support
2992 pmap_invalidate_page_int(kernel_pmap, va);
2993 pmap->pm_stats.resident_count -= 1;
2994 if (oldpte & PG_MANAGED) {
2995 m = PHYS_TO_VM_PAGE(oldpte & PG_FRAME);
2996 if ((oldpte & (PG_M | PG_RW)) == (PG_M | PG_RW))
2999 vm_page_aflag_set(m, PGA_REFERENCED);
3000 pmap_remove_entry(pmap, m, va);
3002 return (pmap_unuse_pt(pmap, va, free));
3006 * Remove a single page from a process address space
3009 pmap_remove_page(pmap_t pmap, vm_offset_t va, struct spglist *free)
3013 rw_assert(&pvh_global_lock, RA_WLOCKED);
3014 KASSERT(curthread->td_pinned > 0, ("curthread not pinned"));
3015 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3016 if ((pte = pmap_pte_quick(pmap, va)) == NULL || *pte == 0)
3018 pmap_remove_pte(pmap, pte, va, free);
3019 pmap_invalidate_page_int(pmap, va);
3023 * Removes the specified range of addresses from the page table page.
3026 pmap_remove_ptes(pmap_t pmap, vm_offset_t sva, vm_offset_t eva,
3027 struct spglist *free)
3032 rw_assert(&pvh_global_lock, RA_WLOCKED);
3033 KASSERT(curthread->td_pinned > 0, ("curthread not pinned"));
3034 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3036 for (pte = pmap_pte_quick(pmap, sva); sva != eva; pte++,
3042 * The TLB entry for a PG_G mapping is invalidated by
3043 * pmap_remove_pte().
3045 if ((*pte & PG_G) == 0)
3048 if (pmap_remove_pte(pmap, pte, sva, free))
3055 * Remove the given range of addresses from the specified map.
3057 * It is assumed that the start and end are properly
3058 * rounded to the page size.
3061 __CONCAT(PMTYPE, remove)(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
3065 struct spglist free;
3069 * Perform an unsynchronized read. This is, however, safe.
3071 if (pmap->pm_stats.resident_count == 0)
3077 rw_wlock(&pvh_global_lock);
3082 * special handling of removing one page. a very
3083 * common operation and easy to short circuit some
3086 if ((sva + PAGE_SIZE == eva) &&
3087 ((pmap->pm_pdir[(sva >> PDRSHIFT)] & PG_PS) == 0)) {
3088 pmap_remove_page(pmap, sva, &free);
3092 for (; sva < eva; sva = pdnxt) {
3096 * Calculate index for next page table.
3098 pdnxt = (sva + NBPDR) & ~PDRMASK;
3101 if (pmap->pm_stats.resident_count == 0)
3104 pdirindex = sva >> PDRSHIFT;
3105 ptpaddr = pmap->pm_pdir[pdirindex];
3108 * Weed out invalid mappings. Note: we assume that the page
3109 * directory table is always allocated, and in kernel virtual.
3115 * Check for large page.
3117 if ((ptpaddr & PG_PS) != 0) {
3119 * Are we removing the entire large page? If not,
3120 * demote the mapping and fall through.
3122 if (sva + NBPDR == pdnxt && eva >= pdnxt) {
3124 * The TLB entry for a PG_G mapping is
3125 * invalidated by pmap_remove_pde().
3127 if ((ptpaddr & PG_G) == 0)
3129 pmap_remove_pde(pmap,
3130 &pmap->pm_pdir[pdirindex], sva, &free);
3132 } else if (!pmap_demote_pde(pmap,
3133 &pmap->pm_pdir[pdirindex], sva)) {
3134 /* The large page mapping was destroyed. */
3140 * Limit our scan to either the end of the va represented
3141 * by the current page table page, or to the end of the
3142 * range being removed.
3147 if (pmap_remove_ptes(pmap, sva, pdnxt, &free))
3153 pmap_invalidate_all_int(pmap);
3154 rw_wunlock(&pvh_global_lock);
3156 vm_page_free_pages_toq(&free, true);
3160 * Routine: pmap_remove_all
3162 * Removes this physical page from
3163 * all physical maps in which it resides.
3164 * Reflects back modify bits to the pager.
3167 * Original versions of this routine were very
3168 * inefficient because they iteratively called
3169 * pmap_remove (slow...)
3173 __CONCAT(PMTYPE, remove_all)(vm_page_t m)
3175 struct md_page *pvh;
3178 pt_entry_t *pte, tpte;
3181 struct spglist free;
3183 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
3184 ("pmap_remove_all: page %p is not managed", m));
3186 rw_wlock(&pvh_global_lock);
3188 if ((m->flags & PG_FICTITIOUS) != 0)
3189 goto small_mappings;
3190 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
3191 while ((pv = TAILQ_FIRST(&pvh->pv_list)) != NULL) {
3195 pde = pmap_pde(pmap, va);
3196 (void)pmap_demote_pde(pmap, pde, va);
3200 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
3203 pmap->pm_stats.resident_count--;
3204 pde = pmap_pde(pmap, pv->pv_va);
3205 KASSERT((*pde & PG_PS) == 0, ("pmap_remove_all: found"
3206 " a 4mpage in page %p's pv list", m));
3207 pte = pmap_pte_quick(pmap, pv->pv_va);
3208 tpte = pte_load_clear(pte);
3209 KASSERT(tpte != 0, ("pmap_remove_all: pmap %p va %x zero pte",
3212 pmap->pm_stats.wired_count--;
3214 vm_page_aflag_set(m, PGA_REFERENCED);
3217 * Update the vm_page_t clean and reference bits.
3219 if ((tpte & (PG_M | PG_RW)) == (PG_M | PG_RW))
3221 pmap_unuse_pt(pmap, pv->pv_va, &free);
3222 pmap_invalidate_page_int(pmap, pv->pv_va);
3223 TAILQ_REMOVE(&m->md.pv_list, pv, pv_next);
3224 free_pv_entry(pmap, pv);
3227 vm_page_aflag_clear(m, PGA_WRITEABLE);
3229 rw_wunlock(&pvh_global_lock);
3230 vm_page_free_pages_toq(&free, true);
3234 * pmap_protect_pde: do the things to protect a 4mpage in a process
3237 pmap_protect_pde(pmap_t pmap, pd_entry_t *pde, vm_offset_t sva, vm_prot_t prot)
3239 pd_entry_t newpde, oldpde;
3240 vm_offset_t eva, va;
3242 boolean_t anychanged;
3244 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3245 KASSERT((sva & PDRMASK) == 0,
3246 ("pmap_protect_pde: sva is not 4mpage aligned"));
3249 oldpde = newpde = *pde;
3250 if ((oldpde & (PG_MANAGED | PG_M | PG_RW)) ==
3251 (PG_MANAGED | PG_M | PG_RW)) {
3253 for (va = sva, m = PHYS_TO_VM_PAGE(oldpde & PG_PS_FRAME);
3254 va < eva; va += PAGE_SIZE, m++)
3257 if ((prot & VM_PROT_WRITE) == 0)
3258 newpde &= ~(PG_RW | PG_M);
3259 #ifdef PMAP_PAE_COMP
3260 if ((prot & VM_PROT_EXECUTE) == 0 && !i386_read_exec)
3263 if (newpde != oldpde) {
3265 * As an optimization to future operations on this PDE, clear
3266 * PG_PROMOTED. The impending invalidation will remove any
3267 * lingering 4KB page mappings from the TLB.
3269 if (!pde_cmpset(pde, oldpde, newpde & ~PG_PROMOTED))
3271 if ((oldpde & PG_G) != 0)
3272 pmap_invalidate_pde_page(kernel_pmap, sva, oldpde);
3276 return (anychanged);
3280 * Set the physical protection on the
3281 * specified range of this map as requested.
3284 __CONCAT(PMTYPE, protect)(pmap_t pmap, vm_offset_t sva, vm_offset_t eva,
3290 boolean_t anychanged, pv_lists_locked;
3292 KASSERT((prot & ~VM_PROT_ALL) == 0, ("invalid prot %x", prot));
3293 if (prot == VM_PROT_NONE) {
3294 pmap_remove(pmap, sva, eva);
3298 #ifdef PMAP_PAE_COMP
3299 if ((prot & (VM_PROT_WRITE | VM_PROT_EXECUTE)) ==
3300 (VM_PROT_WRITE | VM_PROT_EXECUTE))
3303 if (prot & VM_PROT_WRITE)
3307 if (pmap_is_current(pmap))
3308 pv_lists_locked = FALSE;
3310 pv_lists_locked = TRUE;
3312 rw_wlock(&pvh_global_lock);
3318 for (; sva < eva; sva = pdnxt) {
3319 pt_entry_t obits, pbits;
3322 pdnxt = (sva + NBPDR) & ~PDRMASK;
3326 pdirindex = sva >> PDRSHIFT;
3327 ptpaddr = pmap->pm_pdir[pdirindex];
3330 * Weed out invalid mappings. Note: we assume that the page
3331 * directory table is always allocated, and in kernel virtual.
3337 * Check for large page.
3339 if ((ptpaddr & PG_PS) != 0) {
3341 * Are we protecting the entire large page? If not,
3342 * demote the mapping and fall through.
3344 if (sva + NBPDR == pdnxt && eva >= pdnxt) {
3346 * The TLB entry for a PG_G mapping is
3347 * invalidated by pmap_protect_pde().
3349 if (pmap_protect_pde(pmap,
3350 &pmap->pm_pdir[pdirindex], sva, prot))
3354 if (!pv_lists_locked) {
3355 pv_lists_locked = TRUE;
3356 if (!rw_try_wlock(&pvh_global_lock)) {
3358 pmap_invalidate_all_int(
3365 if (!pmap_demote_pde(pmap,
3366 &pmap->pm_pdir[pdirindex], sva)) {
3368 * The large page mapping was
3379 for (pte = pmap_pte_quick(pmap, sva); sva != pdnxt; pte++,
3385 * Regardless of whether a pte is 32 or 64 bits in
3386 * size, PG_RW, PG_A, and PG_M are among the least
3387 * significant 32 bits.
3389 obits = pbits = *pte;
3390 if ((pbits & PG_V) == 0)
3393 if ((prot & VM_PROT_WRITE) == 0) {
3394 if ((pbits & (PG_MANAGED | PG_M | PG_RW)) ==
3395 (PG_MANAGED | PG_M | PG_RW)) {
3396 m = PHYS_TO_VM_PAGE(pbits & PG_FRAME);
3399 pbits &= ~(PG_RW | PG_M);
3401 #ifdef PMAP_PAE_COMP
3402 if ((prot & VM_PROT_EXECUTE) == 0 && !i386_read_exec)
3406 if (pbits != obits) {
3407 #ifdef PMAP_PAE_COMP
3408 if (!atomic_cmpset_64(pte, obits, pbits))
3411 if (!atomic_cmpset_int((u_int *)pte, obits,
3416 pmap_invalidate_page_int(pmap, sva);
3423 pmap_invalidate_all_int(pmap);
3424 if (pv_lists_locked) {
3426 rw_wunlock(&pvh_global_lock);
3431 #if VM_NRESERVLEVEL > 0
3433 * Tries to promote the 512 or 1024, contiguous 4KB page mappings that are
3434 * within a single page table page (PTP) to a single 2- or 4MB page mapping.
3435 * For promotion to occur, two conditions must be met: (1) the 4KB page
3436 * mappings must map aligned, contiguous physical memory and (2) the 4KB page
3437 * mappings must have identical characteristics.
3439 * Managed (PG_MANAGED) mappings within the kernel address space are not
3440 * promoted. The reason is that kernel PDEs are replicated in each pmap but
3441 * pmap_clear_ptes() and pmap_ts_referenced() only read the PDE from the kernel
3445 pmap_promote_pde(pmap_t pmap, pd_entry_t *pde, vm_offset_t va)
3448 pt_entry_t *firstpte, oldpte, pa, *pte;
3449 vm_offset_t oldpteva;
3452 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3455 * Examine the first PTE in the specified PTP. Abort if this PTE is
3456 * either invalid, unused, or does not map the first 4KB physical page
3457 * within a 2- or 4MB page.
3459 firstpte = pmap_pte_quick(pmap, trunc_4mpage(va));
3462 if ((newpde & ((PG_FRAME & PDRMASK) | PG_A | PG_V)) != (PG_A | PG_V)) {
3463 pmap_pde_p_failures++;
3464 CTR2(KTR_PMAP, "pmap_promote_pde: failure for va %#x"
3465 " in pmap %p", va, pmap);
3468 if ((*firstpte & PG_MANAGED) != 0 && pmap == kernel_pmap) {
3469 pmap_pde_p_failures++;
3470 CTR2(KTR_PMAP, "pmap_promote_pde: failure for va %#x"
3471 " in pmap %p", va, pmap);
3474 if ((newpde & (PG_M | PG_RW)) == PG_RW) {
3476 * When PG_M is already clear, PG_RW can be cleared without
3477 * a TLB invalidation.
3479 if (!atomic_cmpset_int((u_int *)firstpte, newpde, newpde &
3486 * Examine each of the other PTEs in the specified PTP. Abort if this
3487 * PTE maps an unexpected 4KB physical page or does not have identical
3488 * characteristics to the first PTE.
3490 pa = (newpde & (PG_PS_FRAME | PG_A | PG_V)) + NBPDR - PAGE_SIZE;
3491 for (pte = firstpte + NPTEPG - 1; pte > firstpte; pte--) {
3494 if ((oldpte & (PG_FRAME | PG_A | PG_V)) != pa) {
3495 pmap_pde_p_failures++;
3496 CTR2(KTR_PMAP, "pmap_promote_pde: failure for va %#x"
3497 " in pmap %p", va, pmap);
3500 if ((oldpte & (PG_M | PG_RW)) == PG_RW) {
3502 * When PG_M is already clear, PG_RW can be cleared
3503 * without a TLB invalidation.
3505 if (!atomic_cmpset_int((u_int *)pte, oldpte,
3509 oldpteva = (oldpte & PG_FRAME & PDRMASK) |
3511 CTR2(KTR_PMAP, "pmap_promote_pde: protect for va %#x"
3512 " in pmap %p", oldpteva, pmap);
3514 if ((oldpte & PG_PTE_PROMOTE) != (newpde & PG_PTE_PROMOTE)) {
3515 pmap_pde_p_failures++;
3516 CTR2(KTR_PMAP, "pmap_promote_pde: failure for va %#x"
3517 " in pmap %p", va, pmap);
3524 * Save the page table page in its current state until the PDE
3525 * mapping the superpage is demoted by pmap_demote_pde() or
3526 * destroyed by pmap_remove_pde().
3528 mpte = PHYS_TO_VM_PAGE(*pde & PG_FRAME);
3529 KASSERT(mpte >= vm_page_array &&
3530 mpte < &vm_page_array[vm_page_array_size],
3531 ("pmap_promote_pde: page table page is out of range"));
3532 KASSERT(mpte->pindex == va >> PDRSHIFT,
3533 ("pmap_promote_pde: page table page's pindex is wrong"));
3534 if (pmap_insert_pt_page(pmap, mpte)) {
3535 pmap_pde_p_failures++;
3537 "pmap_promote_pde: failure for va %#x in pmap %p", va,
3543 * Promote the pv entries.
3545 if ((newpde & PG_MANAGED) != 0)
3546 pmap_pv_promote_pde(pmap, va, newpde & PG_PS_FRAME);
3549 * Propagate the PAT index to its proper position.
3551 if ((newpde & PG_PTE_PAT) != 0)
3552 newpde ^= PG_PDE_PAT | PG_PTE_PAT;
3555 * Map the superpage.
3557 if (workaround_erratum383)
3558 pmap_update_pde(pmap, va, pde, PG_PS | newpde);
3559 else if (pmap == kernel_pmap)
3560 pmap_kenter_pde(va, PG_PROMOTED | PG_PS | newpde);
3562 pde_store(pde, PG_PROMOTED | PG_PS | newpde);
3564 pmap_pde_promotions++;
3565 CTR2(KTR_PMAP, "pmap_promote_pde: success for va %#x"
3566 " in pmap %p", va, pmap);
3568 #endif /* VM_NRESERVLEVEL > 0 */
3571 * Insert the given physical page (p) at
3572 * the specified virtual address (v) in the
3573 * target physical map with the protection requested.
3575 * If specified, the page will be wired down, meaning
3576 * that the related pte can not be reclaimed.
3578 * NB: This is the only routine which MAY NOT lazy-evaluate
3579 * or lose information. That is, this routine must actually
3580 * insert this page into the given map NOW.
3583 __CONCAT(PMTYPE, enter)(pmap_t pmap, vm_offset_t va, vm_page_t m,
3584 vm_prot_t prot, u_int flags, int8_t psind)
3588 pt_entry_t newpte, origpte;
3594 va = trunc_page(va);
3595 KASSERT((pmap == kernel_pmap && va < VM_MAX_KERNEL_ADDRESS) ||
3596 (pmap != kernel_pmap && va < VM_MAXUSER_ADDRESS),
3597 ("pmap_enter: toobig k%d %#x", pmap == kernel_pmap, va));
3598 KASSERT(va < PMAP_TRM_MIN_ADDRESS,
3599 ("pmap_enter: invalid to pmap_enter into trampoline (va: 0x%x)",
3601 KASSERT(pmap != kernel_pmap || (m->oflags & VPO_UNMANAGED) != 0 ||
3602 va < kmi.clean_sva || va >= kmi.clean_eva,
3603 ("pmap_enter: managed mapping within the clean submap"));
3604 if ((m->oflags & VPO_UNMANAGED) == 0 && !vm_page_xbusied(m))
3605 VM_OBJECT_ASSERT_LOCKED(m->object);
3606 KASSERT((flags & PMAP_ENTER_RESERVED) == 0,
3607 ("pmap_enter: flags %u has reserved bits set", flags));
3608 pa = VM_PAGE_TO_PHYS(m);
3609 newpte = (pt_entry_t)(pa | PG_A | PG_V);
3610 if ((flags & VM_PROT_WRITE) != 0)
3612 if ((prot & VM_PROT_WRITE) != 0)
3614 KASSERT((newpte & (PG_M | PG_RW)) != PG_M,
3615 ("pmap_enter: flags includes VM_PROT_WRITE but prot doesn't"));
3616 #ifdef PMAP_PAE_COMP
3617 if ((prot & VM_PROT_EXECUTE) == 0 && !i386_read_exec)
3620 if ((flags & PMAP_ENTER_WIRED) != 0)
3622 if (pmap != kernel_pmap)
3624 newpte |= pmap_cache_bits(pmap, m->md.pat_mode, psind > 0);
3625 if ((m->oflags & VPO_UNMANAGED) == 0)
3626 newpte |= PG_MANAGED;
3628 rw_wlock(&pvh_global_lock);
3632 /* Assert the required virtual and physical alignment. */
3633 KASSERT((va & PDRMASK) == 0, ("pmap_enter: va unaligned"));
3634 KASSERT(m->psind > 0, ("pmap_enter: m->psind < psind"));
3635 rv = pmap_enter_pde(pmap, va, newpte | PG_PS, flags, m);
3639 pde = pmap_pde(pmap, va);
3640 if (pmap != kernel_pmap) {
3643 * In the case that a page table page is not resident,
3644 * we are creating it here. pmap_allocpte() handles
3647 mpte = pmap_allocpte(pmap, va, flags);
3649 KASSERT((flags & PMAP_ENTER_NOSLEEP) != 0,
3650 ("pmap_allocpte failed with sleep allowed"));
3651 rv = KERN_RESOURCE_SHORTAGE;
3656 * va is for KVA, so pmap_demote_pde() will never fail
3657 * to install a page table page. PG_V is also
3658 * asserted by pmap_demote_pde().
3661 KASSERT(pde != NULL && (*pde & PG_V) != 0,
3662 ("KVA %#x invalid pde pdir %#jx", va,
3663 (uintmax_t)pmap->pm_pdir[PTDPTDI]));
3664 if ((*pde & PG_PS) != 0)
3665 pmap_demote_pde(pmap, pde, va);
3667 pte = pmap_pte_quick(pmap, va);
3670 * Page Directory table entry is not valid, which should not
3671 * happen. We should have either allocated the page table
3672 * page or demoted the existing mapping above.
3675 panic("pmap_enter: invalid page directory pdir=%#jx, va=%#x",
3676 (uintmax_t)pmap->pm_pdir[PTDPTDI], va);
3683 * Is the specified virtual address already mapped?
3685 if ((origpte & PG_V) != 0) {
3687 * Wiring change, just update stats. We don't worry about
3688 * wiring PT pages as they remain resident as long as there
3689 * are valid mappings in them. Hence, if a user page is wired,
3690 * the PT page will be also.
3692 if ((newpte & PG_W) != 0 && (origpte & PG_W) == 0)
3693 pmap->pm_stats.wired_count++;
3694 else if ((newpte & PG_W) == 0 && (origpte & PG_W) != 0)
3695 pmap->pm_stats.wired_count--;
3698 * Remove the extra PT page reference.
3702 KASSERT(mpte->wire_count > 0,
3703 ("pmap_enter: missing reference to page table page,"
3708 * Has the physical page changed?
3710 opa = origpte & PG_FRAME;
3713 * No, might be a protection or wiring change.
3715 if ((origpte & PG_MANAGED) != 0 &&
3716 (newpte & PG_RW) != 0)
3717 vm_page_aflag_set(m, PGA_WRITEABLE);
3718 if (((origpte ^ newpte) & ~(PG_M | PG_A)) == 0)
3724 * The physical page has changed. Temporarily invalidate
3725 * the mapping. This ensures that all threads sharing the
3726 * pmap keep a consistent view of the mapping, which is
3727 * necessary for the correct handling of COW faults. It
3728 * also permits reuse of the old mapping's PV entry,
3729 * avoiding an allocation.
3731 * For consistency, handle unmanaged mappings the same way.
3733 origpte = pte_load_clear(pte);
3734 KASSERT((origpte & PG_FRAME) == opa,
3735 ("pmap_enter: unexpected pa update for %#x", va));
3736 if ((origpte & PG_MANAGED) != 0) {
3737 om = PHYS_TO_VM_PAGE(opa);
3740 * The pmap lock is sufficient to synchronize with
3741 * concurrent calls to pmap_page_test_mappings() and
3742 * pmap_ts_referenced().
3744 if ((origpte & (PG_M | PG_RW)) == (PG_M | PG_RW))
3746 if ((origpte & PG_A) != 0)
3747 vm_page_aflag_set(om, PGA_REFERENCED);
3748 pv = pmap_pvh_remove(&om->md, pmap, va);
3750 ("pmap_enter: no PV entry for %#x", va));
3751 if ((newpte & PG_MANAGED) == 0)
3752 free_pv_entry(pmap, pv);
3753 if ((om->aflags & PGA_WRITEABLE) != 0 &&
3754 TAILQ_EMPTY(&om->md.pv_list) &&
3755 ((om->flags & PG_FICTITIOUS) != 0 ||
3756 TAILQ_EMPTY(&pa_to_pvh(opa)->pv_list)))
3757 vm_page_aflag_clear(om, PGA_WRITEABLE);
3759 if ((origpte & PG_A) != 0)
3760 pmap_invalidate_page_int(pmap, va);
3764 * Increment the counters.
3766 if ((newpte & PG_W) != 0)
3767 pmap->pm_stats.wired_count++;
3768 pmap->pm_stats.resident_count++;
3772 * Enter on the PV list if part of our managed memory.
3774 if ((newpte & PG_MANAGED) != 0) {
3776 pv = get_pv_entry(pmap, FALSE);
3779 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_next);
3780 if ((newpte & PG_RW) != 0)
3781 vm_page_aflag_set(m, PGA_WRITEABLE);
3787 if ((origpte & PG_V) != 0) {
3789 origpte = pte_load_store(pte, newpte);
3790 KASSERT((origpte & PG_FRAME) == pa,
3791 ("pmap_enter: unexpected pa update for %#x", va));
3792 if ((newpte & PG_M) == 0 && (origpte & (PG_M | PG_RW)) ==
3794 if ((origpte & PG_MANAGED) != 0)
3798 * Although the PTE may still have PG_RW set, TLB
3799 * invalidation may nonetheless be required because
3800 * the PTE no longer has PG_M set.
3803 #ifdef PMAP_PAE_COMP
3804 else if ((origpte & PG_NX) != 0 || (newpte & PG_NX) == 0) {
3806 * This PTE change does not require TLB invalidation.
3811 if ((origpte & PG_A) != 0)
3812 pmap_invalidate_page_int(pmap, va);
3814 pte_store_zero(pte, newpte);
3818 #if VM_NRESERVLEVEL > 0
3820 * If both the page table page and the reservation are fully
3821 * populated, then attempt promotion.
3823 if ((mpte == NULL || mpte->wire_count == NPTEPG) &&
3824 pg_ps_enabled && (m->flags & PG_FICTITIOUS) == 0 &&
3825 vm_reserv_level_iffullpop(m) == 0)
3826 pmap_promote_pde(pmap, pde, va);
3832 rw_wunlock(&pvh_global_lock);
3838 * Tries to create a read- and/or execute-only 2 or 4 MB page mapping. Returns
3839 * true if successful. Returns false if (1) a mapping already exists at the
3840 * specified virtual address or (2) a PV entry cannot be allocated without
3841 * reclaiming another PV entry.
3844 pmap_enter_4mpage(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot)
3848 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3849 newpde = VM_PAGE_TO_PHYS(m) | pmap_cache_bits(pmap, m->md.pat_mode, 1) |
3851 if ((m->oflags & VPO_UNMANAGED) == 0)
3852 newpde |= PG_MANAGED;
3853 #ifdef PMAP_PAE_COMP
3854 if ((prot & VM_PROT_EXECUTE) == 0 && !i386_read_exec)
3857 if (pmap != kernel_pmap)
3859 return (pmap_enter_pde(pmap, va, newpde, PMAP_ENTER_NOSLEEP |
3860 PMAP_ENTER_NOREPLACE | PMAP_ENTER_NORECLAIM, NULL) ==
3865 * Tries to create the specified 2 or 4 MB page mapping. Returns KERN_SUCCESS
3866 * if the mapping was created, and either KERN_FAILURE or
3867 * KERN_RESOURCE_SHORTAGE otherwise. Returns KERN_FAILURE if
3868 * PMAP_ENTER_NOREPLACE was specified and a mapping already exists at the
3869 * specified virtual address. Returns KERN_RESOURCE_SHORTAGE if
3870 * PMAP_ENTER_NORECLAIM was specified and a PV entry allocation failed.
3872 * The parameter "m" is only used when creating a managed, writeable mapping.
3875 pmap_enter_pde(pmap_t pmap, vm_offset_t va, pd_entry_t newpde, u_int flags,
3878 struct spglist free;
3879 pd_entry_t oldpde, *pde;
3882 rw_assert(&pvh_global_lock, RA_WLOCKED);
3883 KASSERT((newpde & (PG_M | PG_RW)) != PG_RW,
3884 ("pmap_enter_pde: newpde is missing PG_M"));
3885 KASSERT(pmap == kernel_pmap || (newpde & PG_W) == 0,
3886 ("pmap_enter_pde: cannot create wired user mapping"));
3887 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3888 pde = pmap_pde(pmap, va);
3890 if ((oldpde & PG_V) != 0) {
3891 if ((flags & PMAP_ENTER_NOREPLACE) != 0) {
3892 CTR2(KTR_PMAP, "pmap_enter_pde: failure for va %#lx"
3893 " in pmap %p", va, pmap);
3894 return (KERN_FAILURE);
3896 /* Break the existing mapping(s). */
3898 if ((oldpde & PG_PS) != 0) {
3900 * If the PDE resulted from a promotion, then a
3901 * reserved PT page could be freed.
3903 (void)pmap_remove_pde(pmap, pde, va, &free);
3904 if ((oldpde & PG_G) == 0)
3905 pmap_invalidate_pde_page(pmap, va, oldpde);
3907 if (pmap_remove_ptes(pmap, va, va + NBPDR, &free))
3908 pmap_invalidate_all_int(pmap);
3910 vm_page_free_pages_toq(&free, true);
3911 if (pmap == kernel_pmap) {
3912 mt = PHYS_TO_VM_PAGE(*pde & PG_FRAME);
3913 if (pmap_insert_pt_page(pmap, mt)) {
3915 * XXX Currently, this can't happen because
3916 * we do not perform pmap_enter(psind == 1)
3917 * on the kernel pmap.
3919 panic("pmap_enter_pde: trie insert failed");
3922 KASSERT(*pde == 0, ("pmap_enter_pde: non-zero pde %p",
3925 if ((newpde & PG_MANAGED) != 0) {
3927 * Abort this mapping if its PV entry could not be created.
3929 if (!pmap_pv_insert_pde(pmap, va, newpde, flags)) {
3930 CTR2(KTR_PMAP, "pmap_enter_pde: failure for va %#lx"
3931 " in pmap %p", va, pmap);
3932 return (KERN_RESOURCE_SHORTAGE);
3934 if ((newpde & PG_RW) != 0) {
3935 for (mt = m; mt < &m[NBPDR / PAGE_SIZE]; mt++)
3936 vm_page_aflag_set(mt, PGA_WRITEABLE);
3941 * Increment counters.
3943 if ((newpde & PG_W) != 0)
3944 pmap->pm_stats.wired_count += NBPDR / PAGE_SIZE;
3945 pmap->pm_stats.resident_count += NBPDR / PAGE_SIZE;
3948 * Map the superpage. (This is not a promoted mapping; there will not
3949 * be any lingering 4KB page mappings in the TLB.)
3951 pde_store(pde, newpde);
3953 pmap_pde_mappings++;
3954 CTR2(KTR_PMAP, "pmap_enter_pde: success for va %#lx"
3955 " in pmap %p", va, pmap);
3956 return (KERN_SUCCESS);
3960 * Maps a sequence of resident pages belonging to the same object.
3961 * The sequence begins with the given page m_start. This page is
3962 * mapped at the given virtual address start. Each subsequent page is
3963 * mapped at a virtual address that is offset from start by the same
3964 * amount as the page is offset from m_start within the object. The
3965 * last page in the sequence is the page with the largest offset from
3966 * m_start that can be mapped at a virtual address less than the given
3967 * virtual address end. Not every virtual page between start and end
3968 * is mapped; only those for which a resident page exists with the
3969 * corresponding offset from m_start are mapped.
3972 __CONCAT(PMTYPE, enter_object)(pmap_t pmap, vm_offset_t start, vm_offset_t end,
3973 vm_page_t m_start, vm_prot_t prot)
3977 vm_pindex_t diff, psize;
3979 VM_OBJECT_ASSERT_LOCKED(m_start->object);
3981 psize = atop(end - start);
3984 rw_wlock(&pvh_global_lock);
3986 while (m != NULL && (diff = m->pindex - m_start->pindex) < psize) {
3987 va = start + ptoa(diff);
3988 if ((va & PDRMASK) == 0 && va + NBPDR <= end &&
3989 m->psind == 1 && pg_ps_enabled &&
3990 pmap_enter_4mpage(pmap, va, m, prot))
3991 m = &m[NBPDR / PAGE_SIZE - 1];
3993 mpte = pmap_enter_quick_locked(pmap, va, m, prot,
3995 m = TAILQ_NEXT(m, listq);
3997 rw_wunlock(&pvh_global_lock);
4002 * this code makes some *MAJOR* assumptions:
4003 * 1. Current pmap & pmap exists.
4006 * 4. No page table pages.
4007 * but is *MUCH* faster than pmap_enter...
4011 __CONCAT(PMTYPE, enter_quick)(pmap_t pmap, vm_offset_t va, vm_page_t m,
4015 rw_wlock(&pvh_global_lock);
4017 (void)pmap_enter_quick_locked(pmap, va, m, prot, NULL);
4018 rw_wunlock(&pvh_global_lock);
4023 pmap_enter_quick_locked(pmap_t pmap, vm_offset_t va, vm_page_t m,
4024 vm_prot_t prot, vm_page_t mpte)
4026 pt_entry_t newpte, *pte;
4027 struct spglist free;
4029 KASSERT(pmap != kernel_pmap || va < kmi.clean_sva ||
4030 va >= kmi.clean_eva || (m->oflags & VPO_UNMANAGED) != 0,
4031 ("pmap_enter_quick_locked: managed mapping within the clean submap"));
4032 rw_assert(&pvh_global_lock, RA_WLOCKED);
4033 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
4036 * In the case that a page table page is not
4037 * resident, we are creating it here.
4039 if (pmap != kernel_pmap) {
4044 * Calculate pagetable page index
4046 ptepindex = va >> PDRSHIFT;
4047 if (mpte && (mpte->pindex == ptepindex)) {
4051 * Get the page directory entry
4053 ptepa = pmap->pm_pdir[ptepindex];
4056 * If the page table page is mapped, we just increment
4057 * the hold count, and activate it.
4062 mpte = PHYS_TO_VM_PAGE(ptepa & PG_FRAME);
4065 mpte = _pmap_allocpte(pmap, ptepindex,
4066 PMAP_ENTER_NOSLEEP);
4076 pte = pmap_pte_quick(pmap, va);
4087 * Enter on the PV list if part of our managed memory.
4089 if ((m->oflags & VPO_UNMANAGED) == 0 &&
4090 !pmap_try_insert_pv_entry(pmap, va, m)) {
4093 if (pmap_unwire_ptp(pmap, mpte, &free)) {
4094 pmap_invalidate_page_int(pmap, va);
4095 vm_page_free_pages_toq(&free, true);
4105 * Increment counters
4107 pmap->pm_stats.resident_count++;
4109 newpte = VM_PAGE_TO_PHYS(m) | PG_V |
4110 pmap_cache_bits(pmap, m->md.pat_mode, 0);
4111 if ((m->oflags & VPO_UNMANAGED) == 0)
4112 newpte |= PG_MANAGED;
4113 #ifdef PMAP_PAE_COMP
4114 if ((prot & VM_PROT_EXECUTE) == 0 && !i386_read_exec)
4117 if (pmap != kernel_pmap)
4119 pte_store_zero(pte, newpte);
4125 * Make a temporary mapping for a physical address. This is only intended
4126 * to be used for panic dumps.
4129 __CONCAT(PMTYPE, kenter_temporary)(vm_paddr_t pa, int i)
4133 va = (vm_offset_t)crashdumpmap + (i * PAGE_SIZE);
4134 pmap_kenter(va, pa);
4136 return ((void *)crashdumpmap);
4140 * This code maps large physical mmap regions into the
4141 * processor address space. Note that some shortcuts
4142 * are taken, but the code works.
4145 __CONCAT(PMTYPE, object_init_pt)(pmap_t pmap, vm_offset_t addr,
4146 vm_object_t object, vm_pindex_t pindex, vm_size_t size)
4149 vm_paddr_t pa, ptepa;
4153 VM_OBJECT_ASSERT_WLOCKED(object);
4154 KASSERT(object->type == OBJT_DEVICE || object->type == OBJT_SG,
4155 ("pmap_object_init_pt: non-device object"));
4156 if (pg_ps_enabled &&
4157 (addr & (NBPDR - 1)) == 0 && (size & (NBPDR - 1)) == 0) {
4158 if (!vm_object_populate(object, pindex, pindex + atop(size)))
4160 p = vm_page_lookup(object, pindex);
4161 KASSERT(p->valid == VM_PAGE_BITS_ALL,
4162 ("pmap_object_init_pt: invalid page %p", p));
4163 pat_mode = p->md.pat_mode;
4166 * Abort the mapping if the first page is not physically
4167 * aligned to a 2/4MB page boundary.
4169 ptepa = VM_PAGE_TO_PHYS(p);
4170 if (ptepa & (NBPDR - 1))
4174 * Skip the first page. Abort the mapping if the rest of
4175 * the pages are not physically contiguous or have differing
4176 * memory attributes.
4178 p = TAILQ_NEXT(p, listq);
4179 for (pa = ptepa + PAGE_SIZE; pa < ptepa + size;
4181 KASSERT(p->valid == VM_PAGE_BITS_ALL,
4182 ("pmap_object_init_pt: invalid page %p", p));
4183 if (pa != VM_PAGE_TO_PHYS(p) ||
4184 pat_mode != p->md.pat_mode)
4186 p = TAILQ_NEXT(p, listq);
4190 * Map using 2/4MB pages. Since "ptepa" is 2/4M aligned and
4191 * "size" is a multiple of 2/4M, adding the PAT setting to
4192 * "pa" will not affect the termination of this loop.
4195 for (pa = ptepa | pmap_cache_bits(pmap, pat_mode, 1);
4196 pa < ptepa + size; pa += NBPDR) {
4197 pde = pmap_pde(pmap, addr);
4199 pde_store(pde, pa | PG_PS | PG_M | PG_A |
4200 PG_U | PG_RW | PG_V);
4201 pmap->pm_stats.resident_count += NBPDR /
4203 pmap_pde_mappings++;
4205 /* Else continue on if the PDE is already valid. */
4213 * Clear the wired attribute from the mappings for the specified range of
4214 * addresses in the given pmap. Every valid mapping within that range
4215 * must have the wired attribute set. In contrast, invalid mappings
4216 * cannot have the wired attribute set, so they are ignored.
4218 * The wired attribute of the page table entry is not a hardware feature,
4219 * so there is no need to invalidate any TLB entries.
4222 __CONCAT(PMTYPE, unwire)(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
4227 boolean_t pv_lists_locked;
4229 if (pmap_is_current(pmap))
4230 pv_lists_locked = FALSE;
4232 pv_lists_locked = TRUE;
4234 rw_wlock(&pvh_global_lock);
4238 for (; sva < eva; sva = pdnxt) {
4239 pdnxt = (sva + NBPDR) & ~PDRMASK;
4242 pde = pmap_pde(pmap, sva);
4243 if ((*pde & PG_V) == 0)
4245 if ((*pde & PG_PS) != 0) {
4246 if ((*pde & PG_W) == 0)
4247 panic("pmap_unwire: pde %#jx is missing PG_W",
4251 * Are we unwiring the entire large page? If not,
4252 * demote the mapping and fall through.
4254 if (sva + NBPDR == pdnxt && eva >= pdnxt) {
4256 * Regardless of whether a pde (or pte) is 32
4257 * or 64 bits in size, PG_W is among the least
4258 * significant 32 bits.
4260 atomic_clear_int((u_int *)pde, PG_W);
4261 pmap->pm_stats.wired_count -= NBPDR /
4265 if (!pv_lists_locked) {
4266 pv_lists_locked = TRUE;
4267 if (!rw_try_wlock(&pvh_global_lock)) {
4274 if (!pmap_demote_pde(pmap, pde, sva))
4275 panic("pmap_unwire: demotion failed");
4280 for (pte = pmap_pte_quick(pmap, sva); sva != pdnxt; pte++,
4282 if ((*pte & PG_V) == 0)
4284 if ((*pte & PG_W) == 0)
4285 panic("pmap_unwire: pte %#jx is missing PG_W",
4289 * PG_W must be cleared atomically. Although the pmap
4290 * lock synchronizes access to PG_W, another processor
4291 * could be setting PG_M and/or PG_A concurrently.
4293 * PG_W is among the least significant 32 bits.
4295 atomic_clear_int((u_int *)pte, PG_W);
4296 pmap->pm_stats.wired_count--;
4299 if (pv_lists_locked) {
4301 rw_wunlock(&pvh_global_lock);
4308 * Copy the range specified by src_addr/len
4309 * from the source map to the range dst_addr/len
4310 * in the destination map.
4312 * This routine is only advisory and need not do anything. Since
4313 * current pmap is always the kernel pmap when executing in
4314 * kernel, and we do not copy from the kernel pmap to a user
4315 * pmap, this optimization is not usable in 4/4G full split i386
4320 __CONCAT(PMTYPE, copy)(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr,
4321 vm_size_t len, vm_offset_t src_addr)
4323 struct spglist free;
4324 pt_entry_t *src_pte, *dst_pte, ptetemp;
4325 pd_entry_t srcptepaddr;
4326 vm_page_t dstmpte, srcmpte;
4327 vm_offset_t addr, end_addr, pdnxt;
4330 if (dst_addr != src_addr)
4333 end_addr = src_addr + len;
4335 rw_wlock(&pvh_global_lock);
4336 if (dst_pmap < src_pmap) {
4337 PMAP_LOCK(dst_pmap);
4338 PMAP_LOCK(src_pmap);
4340 PMAP_LOCK(src_pmap);
4341 PMAP_LOCK(dst_pmap);
4344 for (addr = src_addr; addr < end_addr; addr = pdnxt) {
4345 KASSERT(addr < PMAP_TRM_MIN_ADDRESS,
4346 ("pmap_copy: invalid to pmap_copy the trampoline"));
4348 pdnxt = (addr + NBPDR) & ~PDRMASK;
4351 ptepindex = addr >> PDRSHIFT;
4353 srcptepaddr = src_pmap->pm_pdir[ptepindex];
4354 if (srcptepaddr == 0)
4357 if (srcptepaddr & PG_PS) {
4358 if ((addr & PDRMASK) != 0 || addr + NBPDR > end_addr)
4360 if (dst_pmap->pm_pdir[ptepindex] == 0 &&
4361 ((srcptepaddr & PG_MANAGED) == 0 ||
4362 pmap_pv_insert_pde(dst_pmap, addr, srcptepaddr,
4363 PMAP_ENTER_NORECLAIM))) {
4364 dst_pmap->pm_pdir[ptepindex] = srcptepaddr &
4366 dst_pmap->pm_stats.resident_count +=
4368 pmap_pde_mappings++;
4373 srcmpte = PHYS_TO_VM_PAGE(srcptepaddr & PG_FRAME);
4374 KASSERT(srcmpte->wire_count > 0,
4375 ("pmap_copy: source page table page is unused"));
4377 if (pdnxt > end_addr)
4380 src_pte = pmap_pte_quick3(src_pmap, addr);
4381 while (addr < pdnxt) {
4384 * we only virtual copy managed pages
4386 if ((ptetemp & PG_MANAGED) != 0) {
4387 dstmpte = pmap_allocpte(dst_pmap, addr,
4388 PMAP_ENTER_NOSLEEP);
4389 if (dstmpte == NULL)
4391 dst_pte = pmap_pte_quick(dst_pmap, addr);
4392 if (*dst_pte == 0 &&
4393 pmap_try_insert_pv_entry(dst_pmap, addr,
4394 PHYS_TO_VM_PAGE(ptetemp & PG_FRAME))) {
4396 * Clear the wired, modified, and
4397 * accessed (referenced) bits
4400 *dst_pte = ptetemp & ~(PG_W | PG_M |
4402 dst_pmap->pm_stats.resident_count++;
4405 if (pmap_unwire_ptp(dst_pmap, dstmpte,
4407 pmap_invalidate_page_int(
4409 vm_page_free_pages_toq(&free,
4414 if (dstmpte->wire_count >= srcmpte->wire_count)
4423 rw_wunlock(&pvh_global_lock);
4424 PMAP_UNLOCK(src_pmap);
4425 PMAP_UNLOCK(dst_pmap);
4429 * Zero 1 page of virtual memory mapped from a hardware page by the caller.
4431 static __inline void
4432 pagezero(void *page)
4434 #if defined(I686_CPU)
4435 if (cpu_class == CPUCLASS_686) {
4436 if (cpu_feature & CPUID_SSE2)
4437 sse2_pagezero(page);
4439 i686_pagezero(page);
4442 bzero(page, PAGE_SIZE);
4446 * Zero the specified hardware page.
4449 __CONCAT(PMTYPE, zero_page)(vm_page_t m)
4451 pt_entry_t *cmap_pte2;
4456 cmap_pte2 = pc->pc_cmap_pte2;
4457 mtx_lock(&pc->pc_cmap_lock);
4459 panic("pmap_zero_page: CMAP2 busy");
4460 *cmap_pte2 = PG_V | PG_RW | VM_PAGE_TO_PHYS(m) | PG_A | PG_M |
4461 pmap_cache_bits(kernel_pmap, m->md.pat_mode, 0);
4462 invlcaddr(pc->pc_cmap_addr2);
4463 pagezero(pc->pc_cmap_addr2);
4467 * Unpin the thread before releasing the lock. Otherwise the thread
4468 * could be rescheduled while still bound to the current CPU, only
4469 * to unpin itself immediately upon resuming execution.
4472 mtx_unlock(&pc->pc_cmap_lock);
4476 * Zero an an area within a single hardware page. off and size must not
4477 * cover an area beyond a single hardware page.
4480 __CONCAT(PMTYPE, zero_page_area)(vm_page_t m, int off, int size)
4482 pt_entry_t *cmap_pte2;
4487 cmap_pte2 = pc->pc_cmap_pte2;
4488 mtx_lock(&pc->pc_cmap_lock);
4490 panic("pmap_zero_page_area: CMAP2 busy");
4491 *cmap_pte2 = PG_V | PG_RW | VM_PAGE_TO_PHYS(m) | PG_A | PG_M |
4492 pmap_cache_bits(kernel_pmap, m->md.pat_mode, 0);
4493 invlcaddr(pc->pc_cmap_addr2);
4494 if (off == 0 && size == PAGE_SIZE)
4495 pagezero(pc->pc_cmap_addr2);
4497 bzero(pc->pc_cmap_addr2 + off, size);
4500 mtx_unlock(&pc->pc_cmap_lock);
4504 * Copy 1 specified hardware page to another.
4507 __CONCAT(PMTYPE, copy_page)(vm_page_t src, vm_page_t dst)
4509 pt_entry_t *cmap_pte1, *cmap_pte2;
4514 cmap_pte1 = pc->pc_cmap_pte1;
4515 cmap_pte2 = pc->pc_cmap_pte2;
4516 mtx_lock(&pc->pc_cmap_lock);
4518 panic("pmap_copy_page: CMAP1 busy");
4520 panic("pmap_copy_page: CMAP2 busy");
4521 *cmap_pte1 = PG_V | VM_PAGE_TO_PHYS(src) | PG_A |
4522 pmap_cache_bits(kernel_pmap, src->md.pat_mode, 0);
4523 invlcaddr(pc->pc_cmap_addr1);
4524 *cmap_pte2 = PG_V | PG_RW | VM_PAGE_TO_PHYS(dst) | PG_A | PG_M |
4525 pmap_cache_bits(kernel_pmap, dst->md.pat_mode, 0);
4526 invlcaddr(pc->pc_cmap_addr2);
4527 bcopy(pc->pc_cmap_addr1, pc->pc_cmap_addr2, PAGE_SIZE);
4531 mtx_unlock(&pc->pc_cmap_lock);
4535 __CONCAT(PMTYPE, copy_pages)(vm_page_t ma[], vm_offset_t a_offset,
4536 vm_page_t mb[], vm_offset_t b_offset, int xfersize)
4538 vm_page_t a_pg, b_pg;
4540 vm_offset_t a_pg_offset, b_pg_offset;
4541 pt_entry_t *cmap_pte1, *cmap_pte2;
4547 cmap_pte1 = pc->pc_cmap_pte1;
4548 cmap_pte2 = pc->pc_cmap_pte2;
4549 mtx_lock(&pc->pc_cmap_lock);
4550 if (*cmap_pte1 != 0)
4551 panic("pmap_copy_pages: CMAP1 busy");
4552 if (*cmap_pte2 != 0)
4553 panic("pmap_copy_pages: CMAP2 busy");
4554 while (xfersize > 0) {
4555 a_pg = ma[a_offset >> PAGE_SHIFT];
4556 a_pg_offset = a_offset & PAGE_MASK;
4557 cnt = min(xfersize, PAGE_SIZE - a_pg_offset);
4558 b_pg = mb[b_offset >> PAGE_SHIFT];
4559 b_pg_offset = b_offset & PAGE_MASK;
4560 cnt = min(cnt, PAGE_SIZE - b_pg_offset);
4561 *cmap_pte1 = PG_V | VM_PAGE_TO_PHYS(a_pg) | PG_A |
4562 pmap_cache_bits(kernel_pmap, a_pg->md.pat_mode, 0);
4563 invlcaddr(pc->pc_cmap_addr1);
4564 *cmap_pte2 = PG_V | PG_RW | VM_PAGE_TO_PHYS(b_pg) | PG_A |
4565 PG_M | pmap_cache_bits(kernel_pmap, b_pg->md.pat_mode, 0);
4566 invlcaddr(pc->pc_cmap_addr2);
4567 a_cp = pc->pc_cmap_addr1 + a_pg_offset;
4568 b_cp = pc->pc_cmap_addr2 + b_pg_offset;
4569 bcopy(a_cp, b_cp, cnt);
4577 mtx_unlock(&pc->pc_cmap_lock);
4581 * Returns true if the pmap's pv is one of the first
4582 * 16 pvs linked to from this page. This count may
4583 * be changed upwards or downwards in the future; it
4584 * is only necessary that true be returned for a small
4585 * subset of pmaps for proper page aging.
4588 __CONCAT(PMTYPE, page_exists_quick)(pmap_t pmap, vm_page_t m)
4590 struct md_page *pvh;
4595 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4596 ("pmap_page_exists_quick: page %p is not managed", m));
4598 rw_wlock(&pvh_global_lock);
4599 TAILQ_FOREACH(pv, &m->md.pv_list, pv_next) {
4600 if (PV_PMAP(pv) == pmap) {
4608 if (!rv && loops < 16 && (m->flags & PG_FICTITIOUS) == 0) {
4609 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
4610 TAILQ_FOREACH(pv, &pvh->pv_list, pv_next) {
4611 if (PV_PMAP(pv) == pmap) {
4620 rw_wunlock(&pvh_global_lock);
4625 * pmap_page_wired_mappings:
4627 * Return the number of managed mappings to the given physical page
4631 __CONCAT(PMTYPE, page_wired_mappings)(vm_page_t m)
4636 if ((m->oflags & VPO_UNMANAGED) != 0)
4638 rw_wlock(&pvh_global_lock);
4639 count = pmap_pvh_wired_mappings(&m->md, count);
4640 if ((m->flags & PG_FICTITIOUS) == 0) {
4641 count = pmap_pvh_wired_mappings(pa_to_pvh(VM_PAGE_TO_PHYS(m)),
4644 rw_wunlock(&pvh_global_lock);
4649 * pmap_pvh_wired_mappings:
4651 * Return the updated number "count" of managed mappings that are wired.
4654 pmap_pvh_wired_mappings(struct md_page *pvh, int count)
4660 rw_assert(&pvh_global_lock, RA_WLOCKED);
4662 TAILQ_FOREACH(pv, &pvh->pv_list, pv_next) {
4665 pte = pmap_pte_quick(pmap, pv->pv_va);
4666 if ((*pte & PG_W) != 0)
4675 * Returns TRUE if the given page is mapped individually or as part of
4676 * a 4mpage. Otherwise, returns FALSE.
4679 __CONCAT(PMTYPE, page_is_mapped)(vm_page_t m)
4683 if ((m->oflags & VPO_UNMANAGED) != 0)
4685 rw_wlock(&pvh_global_lock);
4686 rv = !TAILQ_EMPTY(&m->md.pv_list) ||
4687 ((m->flags & PG_FICTITIOUS) == 0 &&
4688 !TAILQ_EMPTY(&pa_to_pvh(VM_PAGE_TO_PHYS(m))->pv_list));
4689 rw_wunlock(&pvh_global_lock);
4694 * Remove all pages from specified address space
4695 * this aids process exit speeds. Also, this code
4696 * is special cased for current process only, but
4697 * can have the more generic (and slightly slower)
4698 * mode enabled. This is much faster than pmap_remove
4699 * in the case of running down an entire address space.
4702 __CONCAT(PMTYPE, remove_pages)(pmap_t pmap)
4704 pt_entry_t *pte, tpte;
4705 vm_page_t m, mpte, mt;
4707 struct md_page *pvh;
4708 struct pv_chunk *pc, *npc;
4709 struct spglist free;
4712 uint32_t inuse, bitmask;
4715 if (pmap != PCPU_GET(curpmap)) {
4716 printf("warning: pmap_remove_pages called with non-current pmap\n");
4720 rw_wlock(&pvh_global_lock);
4723 TAILQ_FOREACH_SAFE(pc, &pmap->pm_pvchunk, pc_list, npc) {
4724 KASSERT(pc->pc_pmap == pmap, ("Wrong pmap %p %p", pmap,
4727 for (field = 0; field < _NPCM; field++) {
4728 inuse = ~pc->pc_map[field] & pc_freemask[field];
4729 while (inuse != 0) {
4731 bitmask = 1UL << bit;
4732 idx = field * 32 + bit;
4733 pv = &pc->pc_pventry[idx];
4736 pte = pmap_pde(pmap, pv->pv_va);
4738 if ((tpte & PG_PS) == 0) {
4739 pte = pmap_pte_quick(pmap, pv->pv_va);
4740 tpte = *pte & ~PG_PTE_PAT;
4745 "TPTE at %p IS ZERO @ VA %08x\n",
4751 * We cannot remove wired pages from a process' mapping at this time
4758 m = PHYS_TO_VM_PAGE(tpte & PG_FRAME);
4759 KASSERT(m->phys_addr == (tpte & PG_FRAME),
4760 ("vm_page_t %p phys_addr mismatch %016jx %016jx",
4761 m, (uintmax_t)m->phys_addr,
4764 KASSERT((m->flags & PG_FICTITIOUS) != 0 ||
4765 m < &vm_page_array[vm_page_array_size],
4766 ("pmap_remove_pages: bad tpte %#jx",
4772 * Update the vm_page_t clean/reference bits.
4774 if ((tpte & (PG_M | PG_RW)) == (PG_M | PG_RW)) {
4775 if ((tpte & PG_PS) != 0) {
4776 for (mt = m; mt < &m[NBPDR / PAGE_SIZE]; mt++)
4783 PV_STAT(pv_entry_frees++);
4784 PV_STAT(pv_entry_spare++);
4786 pc->pc_map[field] |= bitmask;
4787 if ((tpte & PG_PS) != 0) {
4788 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
4789 pvh = pa_to_pvh(tpte & PG_PS_FRAME);
4790 TAILQ_REMOVE(&pvh->pv_list, pv, pv_next);
4791 if (TAILQ_EMPTY(&pvh->pv_list)) {
4792 for (mt = m; mt < &m[NBPDR / PAGE_SIZE]; mt++)
4793 if (TAILQ_EMPTY(&mt->md.pv_list))
4794 vm_page_aflag_clear(mt, PGA_WRITEABLE);
4796 mpte = pmap_remove_pt_page(pmap, pv->pv_va);
4798 pmap->pm_stats.resident_count--;
4799 KASSERT(mpte->wire_count == NPTEPG,
4800 ("pmap_remove_pages: pte page wire count error"));
4801 mpte->wire_count = 0;
4802 pmap_add_delayed_free_list(mpte, &free, FALSE);
4805 pmap->pm_stats.resident_count--;
4806 TAILQ_REMOVE(&m->md.pv_list, pv, pv_next);
4807 if (TAILQ_EMPTY(&m->md.pv_list) &&
4808 (m->flags & PG_FICTITIOUS) == 0) {
4809 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
4810 if (TAILQ_EMPTY(&pvh->pv_list))
4811 vm_page_aflag_clear(m, PGA_WRITEABLE);
4813 pmap_unuse_pt(pmap, pv->pv_va, &free);
4818 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
4823 pmap_invalidate_all_int(pmap);
4824 rw_wunlock(&pvh_global_lock);
4826 vm_page_free_pages_toq(&free, true);
4832 * Return whether or not the specified physical page was modified
4833 * in any physical maps.
4836 __CONCAT(PMTYPE, is_modified)(vm_page_t m)
4840 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4841 ("pmap_is_modified: page %p is not managed", m));
4844 * If the page is not exclusive busied, then PGA_WRITEABLE cannot be
4845 * concurrently set while the object is locked. Thus, if PGA_WRITEABLE
4846 * is clear, no PTEs can have PG_M set.
4848 VM_OBJECT_ASSERT_WLOCKED(m->object);
4849 if (!vm_page_xbusied(m) && (m->aflags & PGA_WRITEABLE) == 0)
4851 rw_wlock(&pvh_global_lock);
4852 rv = pmap_is_modified_pvh(&m->md) ||
4853 ((m->flags & PG_FICTITIOUS) == 0 &&
4854 pmap_is_modified_pvh(pa_to_pvh(VM_PAGE_TO_PHYS(m))));
4855 rw_wunlock(&pvh_global_lock);
4860 * Returns TRUE if any of the given mappings were used to modify
4861 * physical memory. Otherwise, returns FALSE. Both page and 2mpage
4862 * mappings are supported.
4865 pmap_is_modified_pvh(struct md_page *pvh)
4872 rw_assert(&pvh_global_lock, RA_WLOCKED);
4875 TAILQ_FOREACH(pv, &pvh->pv_list, pv_next) {
4878 pte = pmap_pte_quick(pmap, pv->pv_va);
4879 rv = (*pte & (PG_M | PG_RW)) == (PG_M | PG_RW);
4889 * pmap_is_prefaultable:
4891 * Return whether or not the specified virtual address is elgible
4895 __CONCAT(PMTYPE, is_prefaultable)(pmap_t pmap, vm_offset_t addr)
4902 pde = *pmap_pde(pmap, addr);
4903 if (pde != 0 && (pde & PG_PS) == 0)
4904 rv = pmap_pte_ufast(pmap, addr, pde) == 0;
4910 * pmap_is_referenced:
4912 * Return whether or not the specified physical page was referenced
4913 * in any physical maps.
4916 __CONCAT(PMTYPE, is_referenced)(vm_page_t m)
4920 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4921 ("pmap_is_referenced: page %p is not managed", m));
4922 rw_wlock(&pvh_global_lock);
4923 rv = pmap_is_referenced_pvh(&m->md) ||
4924 ((m->flags & PG_FICTITIOUS) == 0 &&
4925 pmap_is_referenced_pvh(pa_to_pvh(VM_PAGE_TO_PHYS(m))));
4926 rw_wunlock(&pvh_global_lock);
4931 * Returns TRUE if any of the given mappings were referenced and FALSE
4932 * otherwise. Both page and 4mpage mappings are supported.
4935 pmap_is_referenced_pvh(struct md_page *pvh)
4942 rw_assert(&pvh_global_lock, RA_WLOCKED);
4945 TAILQ_FOREACH(pv, &pvh->pv_list, pv_next) {
4948 pte = pmap_pte_quick(pmap, pv->pv_va);
4949 rv = (*pte & (PG_A | PG_V)) == (PG_A | PG_V);
4959 * Clear the write and modified bits in each of the given page's mappings.
4962 __CONCAT(PMTYPE, remove_write)(vm_page_t m)
4964 struct md_page *pvh;
4965 pv_entry_t next_pv, pv;
4968 pt_entry_t oldpte, *pte;
4971 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4972 ("pmap_remove_write: page %p is not managed", m));
4975 * If the page is not exclusive busied, then PGA_WRITEABLE cannot be
4976 * set by another thread while the object is locked. Thus,
4977 * if PGA_WRITEABLE is clear, no page table entries need updating.
4979 VM_OBJECT_ASSERT_WLOCKED(m->object);
4980 if (!vm_page_xbusied(m) && (m->aflags & PGA_WRITEABLE) == 0)
4982 rw_wlock(&pvh_global_lock);
4984 if ((m->flags & PG_FICTITIOUS) != 0)
4985 goto small_mappings;
4986 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
4987 TAILQ_FOREACH_SAFE(pv, &pvh->pv_list, pv_next, next_pv) {
4991 pde = pmap_pde(pmap, va);
4992 if ((*pde & PG_RW) != 0)
4993 (void)pmap_demote_pde(pmap, pde, va);
4997 TAILQ_FOREACH(pv, &m->md.pv_list, pv_next) {
5000 pde = pmap_pde(pmap, pv->pv_va);
5001 KASSERT((*pde & PG_PS) == 0, ("pmap_clear_write: found"
5002 " a 4mpage in page %p's pv list", m));
5003 pte = pmap_pte_quick(pmap, pv->pv_va);
5006 if ((oldpte & PG_RW) != 0) {
5008 * Regardless of whether a pte is 32 or 64 bits
5009 * in size, PG_RW and PG_M are among the least
5010 * significant 32 bits.
5012 if (!atomic_cmpset_int((u_int *)pte, oldpte,
5013 oldpte & ~(PG_RW | PG_M)))
5015 if ((oldpte & PG_M) != 0)
5017 pmap_invalidate_page_int(pmap, pv->pv_va);
5021 vm_page_aflag_clear(m, PGA_WRITEABLE);
5023 rw_wunlock(&pvh_global_lock);
5027 * pmap_ts_referenced:
5029 * Return a count of reference bits for a page, clearing those bits.
5030 * It is not necessary for every reference bit to be cleared, but it
5031 * is necessary that 0 only be returned when there are truly no
5032 * reference bits set.
5034 * As an optimization, update the page's dirty field if a modified bit is
5035 * found while counting reference bits. This opportunistic update can be
5036 * performed at low cost and can eliminate the need for some future calls
5037 * to pmap_is_modified(). However, since this function stops after
5038 * finding PMAP_TS_REFERENCED_MAX reference bits, it may not detect some
5039 * dirty pages. Those dirty pages will only be detected by a future call
5040 * to pmap_is_modified().
5043 __CONCAT(PMTYPE, ts_referenced)(vm_page_t m)
5045 struct md_page *pvh;
5053 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
5054 ("pmap_ts_referenced: page %p is not managed", m));
5055 pa = VM_PAGE_TO_PHYS(m);
5056 pvh = pa_to_pvh(pa);
5057 rw_wlock(&pvh_global_lock);
5059 if ((m->flags & PG_FICTITIOUS) != 0 ||
5060 (pvf = TAILQ_FIRST(&pvh->pv_list)) == NULL)
5061 goto small_mappings;
5066 pde = pmap_pde(pmap, pv->pv_va);
5067 if ((*pde & (PG_M | PG_RW)) == (PG_M | PG_RW)) {
5069 * Although "*pde" is mapping a 2/4MB page, because
5070 * this function is called at a 4KB page granularity,
5071 * we only update the 4KB page under test.
5075 if ((*pde & PG_A) != 0) {
5077 * Since this reference bit is shared by either 1024
5078 * or 512 4KB pages, it should not be cleared every
5079 * time it is tested. Apply a simple "hash" function
5080 * on the physical page number, the virtual superpage
5081 * number, and the pmap address to select one 4KB page
5082 * out of the 1024 or 512 on which testing the
5083 * reference bit will result in clearing that bit.
5084 * This function is designed to avoid the selection of
5085 * the same 4KB page for every 2- or 4MB page mapping.
5087 * On demotion, a mapping that hasn't been referenced
5088 * is simply destroyed. To avoid the possibility of a
5089 * subsequent page fault on a demoted wired mapping,
5090 * always leave its reference bit set. Moreover,
5091 * since the superpage is wired, the current state of
5092 * its reference bit won't affect page replacement.
5094 if ((((pa >> PAGE_SHIFT) ^ (pv->pv_va >> PDRSHIFT) ^
5095 (uintptr_t)pmap) & (NPTEPG - 1)) == 0 &&
5096 (*pde & PG_W) == 0) {
5097 atomic_clear_int((u_int *)pde, PG_A);
5098 pmap_invalidate_page_int(pmap, pv->pv_va);
5103 /* Rotate the PV list if it has more than one entry. */
5104 if (TAILQ_NEXT(pv, pv_next) != NULL) {
5105 TAILQ_REMOVE(&pvh->pv_list, pv, pv_next);
5106 TAILQ_INSERT_TAIL(&pvh->pv_list, pv, pv_next);
5108 if (rtval >= PMAP_TS_REFERENCED_MAX)
5110 } while ((pv = TAILQ_FIRST(&pvh->pv_list)) != pvf);
5112 if ((pvf = TAILQ_FIRST(&m->md.pv_list)) == NULL)
5118 pde = pmap_pde(pmap, pv->pv_va);
5119 KASSERT((*pde & PG_PS) == 0,
5120 ("pmap_ts_referenced: found a 4mpage in page %p's pv list",
5122 pte = pmap_pte_quick(pmap, pv->pv_va);
5123 if ((*pte & (PG_M | PG_RW)) == (PG_M | PG_RW))
5125 if ((*pte & PG_A) != 0) {
5126 atomic_clear_int((u_int *)pte, PG_A);
5127 pmap_invalidate_page_int(pmap, pv->pv_va);
5131 /* Rotate the PV list if it has more than one entry. */
5132 if (TAILQ_NEXT(pv, pv_next) != NULL) {
5133 TAILQ_REMOVE(&m->md.pv_list, pv, pv_next);
5134 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_next);
5136 } while ((pv = TAILQ_FIRST(&m->md.pv_list)) != pvf && rtval <
5137 PMAP_TS_REFERENCED_MAX);
5140 rw_wunlock(&pvh_global_lock);
5145 * Apply the given advice to the specified range of addresses within the
5146 * given pmap. Depending on the advice, clear the referenced and/or
5147 * modified flags in each mapping and set the mapped page's dirty field.
5150 __CONCAT(PMTYPE, advise)(pmap_t pmap, vm_offset_t sva, vm_offset_t eva,
5153 pd_entry_t oldpde, *pde;
5155 vm_offset_t va, pdnxt;
5157 boolean_t anychanged, pv_lists_locked;
5159 if (advice != MADV_DONTNEED && advice != MADV_FREE)
5161 if (pmap_is_current(pmap))
5162 pv_lists_locked = FALSE;
5164 pv_lists_locked = TRUE;
5166 rw_wlock(&pvh_global_lock);
5171 for (; sva < eva; sva = pdnxt) {
5172 pdnxt = (sva + NBPDR) & ~PDRMASK;
5175 pde = pmap_pde(pmap, sva);
5177 if ((oldpde & PG_V) == 0)
5179 else if ((oldpde & PG_PS) != 0) {
5180 if ((oldpde & PG_MANAGED) == 0)
5182 if (!pv_lists_locked) {
5183 pv_lists_locked = TRUE;
5184 if (!rw_try_wlock(&pvh_global_lock)) {
5186 pmap_invalidate_all_int(pmap);
5192 if (!pmap_demote_pde(pmap, pde, sva)) {
5194 * The large page mapping was destroyed.
5200 * Unless the page mappings are wired, remove the
5201 * mapping to a single page so that a subsequent
5202 * access may repromote. Since the underlying page
5203 * table page is fully populated, this removal never
5204 * frees a page table page.
5206 if ((oldpde & PG_W) == 0) {
5207 pte = pmap_pte_quick(pmap, sva);
5208 KASSERT((*pte & PG_V) != 0,
5209 ("pmap_advise: invalid PTE"));
5210 pmap_remove_pte(pmap, pte, sva, NULL);
5217 for (pte = pmap_pte_quick(pmap, sva); sva != pdnxt; pte++,
5219 if ((*pte & (PG_MANAGED | PG_V)) != (PG_MANAGED | PG_V))
5221 else if ((*pte & (PG_M | PG_RW)) == (PG_M | PG_RW)) {
5222 if (advice == MADV_DONTNEED) {
5224 * Future calls to pmap_is_modified()
5225 * can be avoided by making the page
5228 m = PHYS_TO_VM_PAGE(*pte & PG_FRAME);
5231 atomic_clear_int((u_int *)pte, PG_M | PG_A);
5232 } else if ((*pte & PG_A) != 0)
5233 atomic_clear_int((u_int *)pte, PG_A);
5236 if ((*pte & PG_G) != 0) {
5244 pmap_invalidate_range_int(pmap, va, sva);
5249 pmap_invalidate_range_int(pmap, va, sva);
5252 pmap_invalidate_all_int(pmap);
5253 if (pv_lists_locked) {
5255 rw_wunlock(&pvh_global_lock);
5261 * Clear the modify bits on the specified physical page.
5264 __CONCAT(PMTYPE, clear_modify)(vm_page_t m)
5266 struct md_page *pvh;
5267 pv_entry_t next_pv, pv;
5269 pd_entry_t oldpde, *pde;
5270 pt_entry_t oldpte, *pte;
5273 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
5274 ("pmap_clear_modify: page %p is not managed", m));
5275 VM_OBJECT_ASSERT_WLOCKED(m->object);
5276 KASSERT(!vm_page_xbusied(m),
5277 ("pmap_clear_modify: page %p is exclusive busied", m));
5280 * If the page is not PGA_WRITEABLE, then no PTEs can have PG_M set.
5281 * If the object containing the page is locked and the page is not
5282 * exclusive busied, then PGA_WRITEABLE cannot be concurrently set.
5284 if ((m->aflags & PGA_WRITEABLE) == 0)
5286 rw_wlock(&pvh_global_lock);
5288 if ((m->flags & PG_FICTITIOUS) != 0)
5289 goto small_mappings;
5290 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
5291 TAILQ_FOREACH_SAFE(pv, &pvh->pv_list, pv_next, next_pv) {
5295 pde = pmap_pde(pmap, va);
5297 if ((oldpde & PG_RW) != 0) {
5298 if (pmap_demote_pde(pmap, pde, va)) {
5299 if ((oldpde & PG_W) == 0) {
5301 * Write protect the mapping to a
5302 * single page so that a subsequent
5303 * write access may repromote.
5305 va += VM_PAGE_TO_PHYS(m) - (oldpde &
5307 pte = pmap_pte_quick(pmap, va);
5309 if ((oldpte & PG_V) != 0) {
5311 * Regardless of whether a pte is 32 or 64 bits
5312 * in size, PG_RW and PG_M are among the least
5313 * significant 32 bits.
5315 while (!atomic_cmpset_int((u_int *)pte,
5317 oldpte & ~(PG_M | PG_RW)))
5320 pmap_invalidate_page_int(pmap,
5329 TAILQ_FOREACH(pv, &m->md.pv_list, pv_next) {
5332 pde = pmap_pde(pmap, pv->pv_va);
5333 KASSERT((*pde & PG_PS) == 0, ("pmap_clear_modify: found"
5334 " a 4mpage in page %p's pv list", m));
5335 pte = pmap_pte_quick(pmap, pv->pv_va);
5336 if ((*pte & (PG_M | PG_RW)) == (PG_M | PG_RW)) {
5338 * Regardless of whether a pte is 32 or 64 bits
5339 * in size, PG_M is among the least significant
5342 atomic_clear_int((u_int *)pte, PG_M);
5343 pmap_invalidate_page_int(pmap, pv->pv_va);
5348 rw_wunlock(&pvh_global_lock);
5352 * Miscellaneous support routines follow
5355 /* Adjust the cache mode for a 4KB page mapped via a PTE. */
5356 static __inline void
5357 pmap_pte_attr(pt_entry_t *pte, int cache_bits)
5362 * The cache mode bits are all in the low 32-bits of the
5363 * PTE, so we can just spin on updating the low 32-bits.
5366 opte = *(u_int *)pte;
5367 npte = opte & ~PG_PTE_CACHE;
5369 } while (npte != opte && !atomic_cmpset_int((u_int *)pte, opte, npte));
5372 /* Adjust the cache mode for a 2/4MB page mapped via a PDE. */
5373 static __inline void
5374 pmap_pde_attr(pd_entry_t *pde, int cache_bits)
5379 * The cache mode bits are all in the low 32-bits of the
5380 * PDE, so we can just spin on updating the low 32-bits.
5383 opde = *(u_int *)pde;
5384 npde = opde & ~PG_PDE_CACHE;
5386 } while (npde != opde && !atomic_cmpset_int((u_int *)pde, opde, npde));
5390 * Map a set of physical memory pages into the kernel virtual
5391 * address space. Return a pointer to where it is mapped. This
5392 * routine is intended to be used for mapping device memory,
5396 __CONCAT(PMTYPE, mapdev_attr)(vm_paddr_t pa, vm_size_t size, int mode)
5398 struct pmap_preinit_mapping *ppim;
5399 vm_offset_t va, offset;
5403 offset = pa & PAGE_MASK;
5404 size = round_page(offset + size);
5407 if (pa < PMAP_MAP_LOW && pa + size <= PMAP_MAP_LOW)
5408 va = pa + PMAP_MAP_LOW;
5409 else if (!pmap_initialized) {
5411 for (i = 0; i < PMAP_PREINIT_MAPPING_COUNT; i++) {
5412 ppim = pmap_preinit_mapping + i;
5413 if (ppim->va == 0) {
5417 ppim->va = virtual_avail;
5418 virtual_avail += size;
5424 panic("%s: too many preinit mappings", __func__);
5427 * If we have a preinit mapping, re-use it.
5429 for (i = 0; i < PMAP_PREINIT_MAPPING_COUNT; i++) {
5430 ppim = pmap_preinit_mapping + i;
5431 if (ppim->pa == pa && ppim->sz == size &&
5433 return ((void *)(ppim->va + offset));
5435 va = kva_alloc(size);
5437 panic("%s: Couldn't allocate KVA", __func__);
5439 for (tmpsize = 0; tmpsize < size; tmpsize += PAGE_SIZE)
5440 pmap_kenter_attr(va + tmpsize, pa + tmpsize, mode);
5441 pmap_invalidate_range_int(kernel_pmap, va, va + tmpsize);
5442 pmap_invalidate_cache_range(va, va + size);
5443 return ((void *)(va + offset));
5447 __CONCAT(PMTYPE, unmapdev)(vm_offset_t va, vm_size_t size)
5449 struct pmap_preinit_mapping *ppim;
5453 if (va >= PMAP_MAP_LOW && va <= KERNBASE && va + size <= KERNBASE)
5455 offset = va & PAGE_MASK;
5456 size = round_page(offset + size);
5457 va = trunc_page(va);
5458 for (i = 0; i < PMAP_PREINIT_MAPPING_COUNT; i++) {
5459 ppim = pmap_preinit_mapping + i;
5460 if (ppim->va == va && ppim->sz == size) {
5461 if (pmap_initialized)
5467 if (va + size == virtual_avail)
5472 if (pmap_initialized)
5477 * Sets the memory attribute for the specified page.
5480 __CONCAT(PMTYPE, page_set_memattr)(vm_page_t m, vm_memattr_t ma)
5483 m->md.pat_mode = ma;
5484 if ((m->flags & PG_FICTITIOUS) != 0)
5488 * If "m" is a normal page, flush it from the cache.
5489 * See pmap_invalidate_cache_range().
5491 * First, try to find an existing mapping of the page by sf
5492 * buffer. sf_buf_invalidate_cache() modifies mapping and
5493 * flushes the cache.
5495 if (sf_buf_invalidate_cache(m))
5499 * If page is not mapped by sf buffer, but CPU does not
5500 * support self snoop, map the page transient and do
5501 * invalidation. In the worst case, whole cache is flushed by
5502 * pmap_invalidate_cache_range().
5504 if ((cpu_feature & CPUID_SS) == 0)
5509 __CONCAT(PMTYPE, flush_page)(vm_page_t m)
5511 pt_entry_t *cmap_pte2;
5513 vm_offset_t sva, eva;
5516 useclflushopt = (cpu_stdext_feature & CPUID_STDEXT_CLFLUSHOPT) != 0;
5517 if (useclflushopt || (cpu_feature & CPUID_CLFSH) != 0) {
5520 cmap_pte2 = pc->pc_cmap_pte2;
5521 mtx_lock(&pc->pc_cmap_lock);
5523 panic("pmap_flush_page: CMAP2 busy");
5524 *cmap_pte2 = PG_V | PG_RW | VM_PAGE_TO_PHYS(m) |
5525 PG_A | PG_M | pmap_cache_bits(kernel_pmap, m->md.pat_mode,
5527 invlcaddr(pc->pc_cmap_addr2);
5528 sva = (vm_offset_t)pc->pc_cmap_addr2;
5529 eva = sva + PAGE_SIZE;
5532 * Use mfence or sfence despite the ordering implied by
5533 * mtx_{un,}lock() because clflush on non-Intel CPUs
5534 * and clflushopt are not guaranteed to be ordered by
5535 * any other instruction.
5539 else if (cpu_vendor_id != CPU_VENDOR_INTEL)
5541 for (; sva < eva; sva += cpu_clflush_line_size) {
5549 else if (cpu_vendor_id != CPU_VENDOR_INTEL)
5553 mtx_unlock(&pc->pc_cmap_lock);
5555 pmap_invalidate_cache();
5559 * Changes the specified virtual address range's memory type to that given by
5560 * the parameter "mode". The specified virtual address range must be
5561 * completely contained within either the kernel map.
5563 * Returns zero if the change completed successfully, and either EINVAL or
5564 * ENOMEM if the change failed. Specifically, EINVAL is returned if some part
5565 * of the virtual address range was not mapped, and ENOMEM is returned if
5566 * there was insufficient memory available to complete the change.
5569 __CONCAT(PMTYPE, change_attr)(vm_offset_t va, vm_size_t size, int mode)
5571 vm_offset_t base, offset, tmpva;
5574 int cache_bits_pte, cache_bits_pde;
5577 base = trunc_page(va);
5578 offset = va & PAGE_MASK;
5579 size = round_page(offset + size);
5582 * Only supported on kernel virtual addresses above the recursive map.
5584 if (base < VM_MIN_KERNEL_ADDRESS)
5587 cache_bits_pde = pmap_cache_bits(kernel_pmap, mode, 1);
5588 cache_bits_pte = pmap_cache_bits(kernel_pmap, mode, 0);
5592 * Pages that aren't mapped aren't supported. Also break down
5593 * 2/4MB pages into 4KB pages if required.
5595 PMAP_LOCK(kernel_pmap);
5596 for (tmpva = base; tmpva < base + size; ) {
5597 pde = pmap_pde(kernel_pmap, tmpva);
5599 PMAP_UNLOCK(kernel_pmap);
5604 * If the current 2/4MB page already has
5605 * the required memory type, then we need not
5606 * demote this page. Just increment tmpva to
5607 * the next 2/4MB page frame.
5609 if ((*pde & PG_PDE_CACHE) == cache_bits_pde) {
5610 tmpva = trunc_4mpage(tmpva) + NBPDR;
5615 * If the current offset aligns with a 2/4MB
5616 * page frame and there is at least 2/4MB left
5617 * within the range, then we need not break
5618 * down this page into 4KB pages.
5620 if ((tmpva & PDRMASK) == 0 &&
5621 tmpva + PDRMASK < base + size) {
5625 if (!pmap_demote_pde(kernel_pmap, pde, tmpva)) {
5626 PMAP_UNLOCK(kernel_pmap);
5630 pte = vtopte(tmpva);
5632 PMAP_UNLOCK(kernel_pmap);
5637 PMAP_UNLOCK(kernel_pmap);
5640 * Ok, all the pages exist, so run through them updating their
5641 * cache mode if required.
5643 for (tmpva = base; tmpva < base + size; ) {
5644 pde = pmap_pde(kernel_pmap, tmpva);
5646 if ((*pde & PG_PDE_CACHE) != cache_bits_pde) {
5647 pmap_pde_attr(pde, cache_bits_pde);
5650 tmpva = trunc_4mpage(tmpva) + NBPDR;
5652 pte = vtopte(tmpva);
5653 if ((*pte & PG_PTE_CACHE) != cache_bits_pte) {
5654 pmap_pte_attr(pte, cache_bits_pte);
5662 * Flush CPU caches to make sure any data isn't cached that
5663 * shouldn't be, etc.
5666 pmap_invalidate_range_int(kernel_pmap, base, tmpva);
5667 pmap_invalidate_cache_range(base, tmpva);
5673 * perform the pmap work for mincore
5676 __CONCAT(PMTYPE, mincore)(pmap_t pmap, vm_offset_t addr, vm_paddr_t *locked_pa)
5685 pde = *pmap_pde(pmap, addr);
5687 if ((pde & PG_PS) != 0) {
5689 /* Compute the physical address of the 4KB page. */
5690 pa = ((pde & PG_PS_FRAME) | (addr & PDRMASK)) &
5692 val = MINCORE_SUPER;
5694 pte = pmap_pte_ufast(pmap, addr, pde);
5695 pa = pte & PG_FRAME;
5703 if ((pte & PG_V) != 0) {
5704 val |= MINCORE_INCORE;
5705 if ((pte & (PG_M | PG_RW)) == (PG_M | PG_RW))
5706 val |= MINCORE_MODIFIED | MINCORE_MODIFIED_OTHER;
5707 if ((pte & PG_A) != 0)
5708 val |= MINCORE_REFERENCED | MINCORE_REFERENCED_OTHER;
5710 if ((val & (MINCORE_MODIFIED_OTHER | MINCORE_REFERENCED_OTHER)) !=
5711 (MINCORE_MODIFIED_OTHER | MINCORE_REFERENCED_OTHER) &&
5712 (pte & (PG_MANAGED | PG_V)) == (PG_MANAGED | PG_V)) {
5713 /* Ensure that "PHYS_TO_VM_PAGE(pa)->object" doesn't change. */
5714 if (vm_page_pa_tryrelock(pmap, pa, locked_pa))
5717 PA_UNLOCK_COND(*locked_pa);
5723 __CONCAT(PMTYPE, activate)(struct thread *td)
5725 pmap_t pmap, oldpmap;
5730 pmap = vmspace_pmap(td->td_proc->p_vmspace);
5731 oldpmap = PCPU_GET(curpmap);
5732 cpuid = PCPU_GET(cpuid);
5734 CPU_CLR_ATOMIC(cpuid, &oldpmap->pm_active);
5735 CPU_SET_ATOMIC(cpuid, &pmap->pm_active);
5737 CPU_CLR(cpuid, &oldpmap->pm_active);
5738 CPU_SET(cpuid, &pmap->pm_active);
5740 #ifdef PMAP_PAE_COMP
5741 cr3 = vtophys(pmap->pm_pdpt);
5743 cr3 = vtophys(pmap->pm_pdir);
5746 * pmap_activate is for the current thread on the current cpu
5748 td->td_pcb->pcb_cr3 = cr3;
5749 PCPU_SET(curpmap, pmap);
5754 __CONCAT(PMTYPE, activate_boot)(pmap_t pmap)
5758 cpuid = PCPU_GET(cpuid);
5760 CPU_SET_ATOMIC(cpuid, &pmap->pm_active);
5762 CPU_SET(cpuid, &pmap->pm_active);
5764 PCPU_SET(curpmap, pmap);
5768 * Increase the starting virtual address of the given mapping if a
5769 * different alignment might result in more superpage mappings.
5772 __CONCAT(PMTYPE, align_superpage)(vm_object_t object, vm_ooffset_t offset,
5773 vm_offset_t *addr, vm_size_t size)
5775 vm_offset_t superpage_offset;
5779 if (object != NULL && (object->flags & OBJ_COLORED) != 0)
5780 offset += ptoa(object->pg_color);
5781 superpage_offset = offset & PDRMASK;
5782 if (size - ((NBPDR - superpage_offset) & PDRMASK) < NBPDR ||
5783 (*addr & PDRMASK) == superpage_offset)
5785 if ((*addr & PDRMASK) < superpage_offset)
5786 *addr = (*addr & ~PDRMASK) + superpage_offset;
5788 *addr = ((*addr + PDRMASK) & ~PDRMASK) + superpage_offset;
5792 __CONCAT(PMTYPE, quick_enter_page)(vm_page_t m)
5798 qaddr = PCPU_GET(qmap_addr);
5799 pte = vtopte(qaddr);
5802 ("pmap_quick_enter_page: PTE busy %#jx", (uintmax_t)*pte));
5803 *pte = PG_V | PG_RW | VM_PAGE_TO_PHYS(m) | PG_A | PG_M |
5804 pmap_cache_bits(kernel_pmap, pmap_page_get_memattr(m), 0);
5811 __CONCAT(PMTYPE, quick_remove_page)(vm_offset_t addr)
5816 qaddr = PCPU_GET(qmap_addr);
5817 pte = vtopte(qaddr);
5819 KASSERT(*pte != 0, ("pmap_quick_remove_page: PTE not in use"));
5820 KASSERT(addr == qaddr, ("pmap_quick_remove_page: invalid address"));
5826 static vmem_t *pmap_trm_arena;
5827 static vmem_addr_t pmap_trm_arena_last = PMAP_TRM_MIN_ADDRESS;
5828 static int trm_guard = PAGE_SIZE;
5831 pmap_trm_import(void *unused __unused, vmem_size_t size, int flags,
5835 vmem_addr_t af, addr, prev_addr;
5836 pt_entry_t *trm_pte;
5838 prev_addr = atomic_load_long(&pmap_trm_arena_last);
5839 size = round_page(size) + trm_guard;
5841 if (prev_addr + size < prev_addr || prev_addr + size < size ||
5842 prev_addr + size > PMAP_TRM_MAX_ADDRESS)
5844 addr = prev_addr + size;
5845 if (atomic_fcmpset_int(&pmap_trm_arena_last, &prev_addr, addr))
5848 prev_addr += trm_guard;
5849 trm_pte = PTmap + atop(prev_addr);
5850 for (af = prev_addr; af < addr; af += PAGE_SIZE) {
5851 m = vm_page_alloc(NULL, 0, VM_ALLOC_NOOBJ | VM_ALLOC_NOBUSY |
5852 VM_ALLOC_NORMAL | VM_ALLOC_WIRED | VM_ALLOC_WAITOK);
5853 pte_store(&trm_pte[atop(af - prev_addr)], VM_PAGE_TO_PHYS(m) |
5854 PG_M | PG_A | PG_RW | PG_V | pgeflag |
5855 pmap_cache_bits(kernel_pmap, VM_MEMATTR_DEFAULT, FALSE));
5866 TUNABLE_INT_FETCH("machdep.trm_guard", &trm_guard);
5867 if ((trm_guard & PAGE_MASK) != 0)
5869 pmap_trm_arena = vmem_create("i386trampoline", 0, 0, 1, 0, M_WAITOK);
5870 vmem_set_import(pmap_trm_arena, pmap_trm_import, NULL, NULL, PAGE_SIZE);
5871 pd_m = vm_page_alloc(NULL, 0, VM_ALLOC_NOOBJ | VM_ALLOC_NOBUSY |
5872 VM_ALLOC_NORMAL | VM_ALLOC_WIRED | VM_ALLOC_WAITOK | VM_ALLOC_ZERO);
5873 if ((pd_m->flags & PG_ZERO) == 0)
5874 pmap_zero_page(pd_m);
5875 PTD[TRPTDI] = VM_PAGE_TO_PHYS(pd_m) | PG_M | PG_A | PG_RW | PG_V |
5876 pmap_cache_bits(kernel_pmap, VM_MEMATTR_DEFAULT, TRUE);
5880 __CONCAT(PMTYPE, trm_alloc)(size_t size, int flags)
5885 MPASS((flags & ~(M_WAITOK | M_NOWAIT | M_ZERO)) == 0);
5886 error = vmem_xalloc(pmap_trm_arena, roundup2(size, 4), sizeof(int),
5887 0, 0, VMEM_ADDR_MIN, VMEM_ADDR_MAX, flags | M_FIRSTFIT, &res);
5890 if ((flags & M_ZERO) != 0)
5891 bzero((void *)res, size);
5892 return ((void *)res);
5896 __CONCAT(PMTYPE, trm_free)(void *addr, size_t size)
5899 vmem_free(pmap_trm_arena, (uintptr_t)addr, roundup2(size, 4));
5902 #if defined(PMAP_DEBUG)
5903 pmap_pid_dump(int pid)
5910 sx_slock(&allproc_lock);
5911 FOREACH_PROC_IN_SYSTEM(p) {
5912 if (p->p_pid != pid)
5918 pmap = vmspace_pmap(p->p_vmspace);
5919 for (i = 0; i < NPDEPTD; i++) {
5922 vm_offset_t base = i << PDRSHIFT;
5924 pde = &pmap->pm_pdir[i];
5925 if (pde && pmap_pde_v(pde)) {
5926 for (j = 0; j < NPTEPG; j++) {
5927 vm_offset_t va = base + (j << PAGE_SHIFT);
5928 if (va >= (vm_offset_t) VM_MIN_KERNEL_ADDRESS) {
5933 sx_sunlock(&allproc_lock);
5936 pte = pmap_pte(pmap, va);
5937 if (pte && pmap_pte_v(pte)) {
5941 m = PHYS_TO_VM_PAGE(pa & PG_FRAME);
5942 printf("va: 0x%x, pt: 0x%x, h: %d, w: %d, f: 0x%x",
5943 va, pa, m->hold_count, m->wire_count, m->flags);
5958 sx_sunlock(&allproc_lock);
5964 __CONCAT(PMTYPE, ksetrw)(vm_offset_t va)
5967 *vtopte(va) |= PG_RW;
5971 __CONCAT(PMTYPE, remap_lowptdi)(bool enable)
5974 PTD[KPTDI] = enable ? PTD[LOWPTDI] : 0;
5979 __CONCAT(PMTYPE, get_map_low)(void)
5982 return (PMAP_MAP_LOW);
5986 __CONCAT(PMTYPE, get_vm_maxuser_address)(void)
5989 return (VM_MAXUSER_ADDRESS);
5993 __CONCAT(PMTYPE, pg_frame)(vm_paddr_t pa)
5996 return (pa & PG_FRAME);
6000 __CONCAT(PMTYPE, sf_buf_map)(struct sf_buf *sf)
6002 pt_entry_t opte, *ptep;
6005 * Update the sf_buf's virtual-to-physical mapping, flushing the
6006 * virtual address from the TLB. Since the reference count for
6007 * the sf_buf's old mapping was zero, that mapping is not
6008 * currently in use. Consequently, there is no need to exchange
6009 * the old and new PTEs atomically, even under PAE.
6011 ptep = vtopte(sf->kva);
6013 *ptep = VM_PAGE_TO_PHYS(sf->m) | PG_RW | PG_V |
6014 pmap_cache_bits(kernel_pmap, sf->m->md.pat_mode, 0);
6017 * Avoid unnecessary TLB invalidations: If the sf_buf's old
6018 * virtual-to-physical mapping was not used, then any processor
6019 * that has invalidated the sf_buf's virtual address from its TLB
6020 * since the last used mapping need not invalidate again.
6023 if ((opte & (PG_V | PG_A)) == (PG_V | PG_A))
6024 CPU_ZERO(&sf->cpumask);
6026 if ((opte & (PG_V | PG_A)) == (PG_V | PG_A))
6027 pmap_invalidate_page_int(kernel_pmap, sf->kva);
6032 __CONCAT(PMTYPE, cp_slow0_map)(vm_offset_t kaddr, int plen, vm_page_t *ma)
6037 for (i = 0, pte = vtopte(kaddr); i < plen; i++, pte++) {
6038 *pte = PG_V | PG_RW | PG_A | PG_M | VM_PAGE_TO_PHYS(ma[i]) |
6039 pmap_cache_bits(kernel_pmap, pmap_page_get_memattr(ma[i]),
6041 invlpg(kaddr + ptoa(i));
6046 __CONCAT(PMTYPE, get_kcr3)(void)
6049 #ifdef PMAP_PAE_COMP
6050 return ((u_int)IdlePDPT);
6052 return ((u_int)IdlePTD);
6057 __CONCAT(PMTYPE, get_cr3)(pmap_t pmap)
6060 #ifdef PMAP_PAE_COMP
6061 return ((u_int)vtophys(pmap->pm_pdpt));
6063 return ((u_int)vtophys(pmap->pm_pdir));
6068 __CONCAT(PMTYPE, cmap3)(vm_paddr_t pa, u_int pte_bits)
6073 *pte = pa | pte_bits;
6079 __CONCAT(PMTYPE, basemem_setup)(u_int basemem)
6085 * Map pages between basemem and ISA_HOLE_START, if any, r/w into
6086 * the vm86 page table so that vm86 can scribble on them using
6087 * the vm86 map too. XXX: why 2 ways for this and only 1 way for
6088 * page 0, at least as initialized here?
6090 pte = (pt_entry_t *)vm86paddr;
6091 for (i = basemem / 4; i < 160; i++)
6092 pte[i] = (i << PAGE_SHIFT) | PG_V | PG_RW | PG_U;
6095 struct bios16_pmap_handle {
6098 pt_entry_t orig_ptd;
6102 __CONCAT(PMTYPE, bios16_enter)(void)
6104 struct bios16_pmap_handle *h;
6107 * no page table, so create one and install it.
6109 h = malloc(sizeof(struct bios16_pmap_handle), M_TEMP, M_WAITOK);
6110 h->pte = (pt_entry_t *)malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
6112 *h->pte = vm86phystk | PG_RW | PG_V;
6113 h->orig_ptd = *h->ptd;
6114 *h->ptd = vtophys(h->pte) | PG_RW | PG_V;
6115 pmap_invalidate_all_int(kernel_pmap); /* XXX insurance for now */
6120 __CONCAT(PMTYPE, bios16_leave)(void *arg)
6122 struct bios16_pmap_handle *h;
6125 *h->ptd = h->orig_ptd; /* remove page table */
6127 * XXX only needs to be invlpg(0) but that doesn't work on the 386
6129 pmap_invalidate_all_int(kernel_pmap);
6130 free(h->pte, M_TEMP); /* ... and free it */
6134 .pm_##a = __CONCAT(PMTYPE, a),
6136 struct pmap_methods __CONCAT(PMTYPE, methods) = {
6140 PMM(align_superpage)
6141 PMM(quick_enter_page)
6142 PMM(quick_remove_page)
6146 PMM(get_vm_maxuser_address)
6159 PMM(is_valid_memattr)
6178 PMM(kenter_temporary)
6181 PMM(page_exists_quick)
6182 PMM(page_wired_mappings)
6186 PMM(is_prefaultable)
6192 PMM(page_set_memattr)
6194 PMM(extract_and_hold)
6205 PMM(invalidate_page)
6206 PMM(invalidate_range)
6208 PMM(invalidate_cache)