2 * Copyright (c) 1991 Regents of the University of California.
3 * Copyright (c) 1994 John S. Dyson
4 * Copyright (c) 1994 David Greenman
5 * Copyright (c) 2003 Peter Wemm
6 * Copyright (c) 2005-2008 Alan L. Cox <alc@cs.rice.edu>
7 * Copyright (c) 2008, 2009 The DragonFly Project.
8 * Copyright (c) 2008, 2009 Jordan Gordeev.
9 * Copyright (c) 2011-2017 Matthew Dillon
10 * All rights reserved.
12 * This code is derived from software contributed to Berkeley by
13 * the Systems Programming Group of the University of Utah Computer
14 * Science Department and William Jolitz of UUNET Technologies Inc.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 3. All advertising materials mentioning features or use of this software
25 * must display the following acknowledgement:
26 * This product includes software developed by the University of
27 * California, Berkeley and its contributors.
28 * 4. Neither the name of the University nor the names of its contributors
29 * may be used to endorse or promote products derived from this software
30 * without specific prior written permission.
32 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 * Manage physical address maps for x86-64 systems.
48 * - The 'M'odified bit is only applicable to terminal PTEs.
50 * - The 'U'ser access bit can be set for higher-level PTEs as
51 * long as it isn't set for terminal PTEs for pages we don't
52 * want user access to.
58 #include "opt_msgbuf.h"
60 #include <sys/param.h>
61 #include <sys/kernel.h>
63 #include <sys/msgbuf.h>
64 #include <sys/vmmeter.h>
66 #include <sys/systm.h>
69 #include <vm/vm_param.h>
70 #include <sys/sysctl.h>
72 #include <vm/vm_kern.h>
73 #include <vm/vm_page.h>
74 #include <vm/vm_map.h>
75 #include <vm/vm_object.h>
76 #include <vm/vm_extern.h>
77 #include <vm/vm_pageout.h>
78 #include <vm/vm_pager.h>
79 #include <vm/vm_zone.h>
82 #include <sys/thread2.h>
83 #include <sys/spinlock2.h>
84 #include <vm/vm_page2.h>
86 #include <machine/cputypes.h>
87 #include <machine/cpu.h>
88 #include <machine/md_var.h>
89 #include <machine/specialreg.h>
90 #include <machine/smp.h>
91 #include <machine_base/apic/apicreg.h>
92 #include <machine/globaldata.h>
93 #include <machine/pmap.h>
94 #include <machine/pmap_inval.h>
95 #include <machine/inttypes.h>
99 #define PMAP_KEEP_PDIRS
100 #ifndef PMAP_SHPGPERPROC
101 #define PMAP_SHPGPERPROC 2000
104 #if defined(DIAGNOSTIC)
105 #define PMAP_DIAGNOSTIC
111 * pmap debugging will report who owns a pv lock when blocking.
115 #define PMAP_DEBUG_DECL ,const char *func, int lineno
116 #define PMAP_DEBUG_ARGS , __func__, __LINE__
117 #define PMAP_DEBUG_COPY , func, lineno
119 #define pv_get(pmap, pindex, pmarkp) _pv_get(pmap, pindex, pmarkp \
121 #define pv_lock(pv) _pv_lock(pv \
123 #define pv_hold_try(pv) _pv_hold_try(pv \
125 #define pv_alloc(pmap, pindex, isnewp) _pv_alloc(pmap, pindex, isnewp \
128 #define pv_free(pv, pvp) _pv_free(pv, pvp PMAP_DEBUG_ARGS)
132 #define PMAP_DEBUG_DECL
133 #define PMAP_DEBUG_ARGS
134 #define PMAP_DEBUG_COPY
136 #define pv_get(pmap, pindex, pmarkp) _pv_get(pmap, pindex, pmarkp)
137 #define pv_lock(pv) _pv_lock(pv)
138 #define pv_hold_try(pv) _pv_hold_try(pv)
139 #define pv_alloc(pmap, pindex, isnewp) _pv_alloc(pmap, pindex, isnewp)
140 #define pv_free(pv, pvp) _pv_free(pv, pvp)
145 * Get PDEs and PTEs for user/kernel address space
147 #define pdir_pde(m, v) (m[(vm_offset_t)(v) >> PDRSHIFT])
149 #define pmap_pde_v(pmap, pte) ((*(pd_entry_t *)pte & pmap->pmap_bits[PG_V_IDX]) != 0)
150 #define pmap_pte_w(pmap, pte) ((*(pt_entry_t *)pte & pmap->pmap_bits[PG_W_IDX]) != 0)
151 #define pmap_pte_m(pmap, pte) ((*(pt_entry_t *)pte & pmap->pmap_bits[PG_M_IDX]) != 0)
152 #define pmap_pte_u(pmap, pte) ((*(pt_entry_t *)pte & pmap->pmap_bits[PG_U_IDX]) != 0)
153 #define pmap_pte_v(pmap, pte) ((*(pt_entry_t *)pte & pmap->pmap_bits[PG_V_IDX]) != 0)
156 * Given a map and a machine independent protection code,
157 * convert to a vax protection code.
159 #define pte_prot(m, p) \
160 (m->protection_codes[p & (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE)])
161 static uint64_t protection_codes[PROTECTION_CODES_SIZE];
163 struct pmap kernel_pmap;
164 struct pmap iso_pmap;
166 MALLOC_DEFINE(M_OBJPMAP, "objpmap", "pmaps associated with VM objects");
168 vm_paddr_t avail_start; /* PA of first available physical page */
169 vm_paddr_t avail_end; /* PA of last available physical page */
170 vm_offset_t virtual2_start; /* cutout free area prior to kernel start */
171 vm_offset_t virtual2_end;
172 vm_offset_t virtual_start; /* VA of first avail page (after kernel bss) */
173 vm_offset_t virtual_end; /* VA of last avail page (end of kernel AS) */
174 vm_offset_t KvaStart; /* VA start of KVA space */
175 vm_offset_t KvaEnd; /* VA end of KVA space (non-inclusive) */
176 vm_offset_t KvaSize; /* max size of kernel virtual address space */
177 static boolean_t pmap_initialized = FALSE; /* Has pmap_init completed? */
178 //static int pgeflag; /* PG_G or-in */
182 static vm_paddr_t dmaplimit;
183 vm_offset_t kernel_vm_end = VM_MIN_KERNEL_ADDRESS;
185 static pt_entry_t pat_pte_index[PAT_INDEX_SIZE]; /* PAT -> PG_ bits */
186 /*static pt_entry_t pat_pde_index[PAT_INDEX_SIZE];*/ /* PAT -> PG_ bits */
188 static uint64_t KPTbase;
189 static uint64_t KPTphys;
190 static uint64_t KPDphys; /* phys addr of kernel level 2 */
191 static uint64_t KPDbase; /* phys addr of kernel level 2 @ KERNBASE */
192 uint64_t KPDPphys; /* phys addr of kernel level 3 */
193 uint64_t KPML4phys; /* phys addr of kernel level 4 */
195 static uint64_t DMPDphys; /* phys addr of direct mapped level 2 */
196 static uint64_t DMPDPphys; /* phys addr of direct mapped level 3 */
199 * Data for the pv entry allocation mechanism
201 static vm_zone_t pvzone;
202 static struct vm_zone pvzone_store;
203 static vm_pindex_t pv_entry_max=0, pv_entry_high_water=0;
204 static int pmap_pagedaemon_waken = 0;
205 static struct pv_entry *pvinit;
208 * All those kernel PT submaps that BSD is so fond of
210 pt_entry_t *CMAP1 = NULL, *ptmmap;
211 caddr_t CADDR1 = NULL, ptvmmap = NULL;
212 static pt_entry_t *msgbufmap;
213 struct msgbuf *msgbufp=NULL;
216 * PMAP default PG_* bits. Needed to be able to add
217 * EPT/NPT pagetable pmap_bits for the VMM module
219 uint64_t pmap_bits_default[] = {
220 REGULAR_PMAP, /* TYPE_IDX 0 */
221 X86_PG_V, /* PG_V_IDX 1 */
222 X86_PG_RW, /* PG_RW_IDX 2 */
223 X86_PG_U, /* PG_U_IDX 3 */
224 X86_PG_A, /* PG_A_IDX 4 */
225 X86_PG_M, /* PG_M_IDX 5 */
226 X86_PG_PS, /* PG_PS_IDX3 6 */
227 X86_PG_G, /* PG_G_IDX 7 */
228 X86_PG_AVAIL1, /* PG_AVAIL1_IDX 8 */
229 X86_PG_AVAIL2, /* PG_AVAIL2_IDX 9 */
230 X86_PG_AVAIL3, /* PG_AVAIL3_IDX 10 */
231 X86_PG_NC_PWT | X86_PG_NC_PCD, /* PG_N_IDX 11 */
232 X86_PG_NX, /* PG_NX_IDX 12 */
237 static pt_entry_t *pt_crashdumpmap;
238 static caddr_t crashdumpmap;
240 static int pmap_debug = 0;
241 SYSCTL_INT(_machdep, OID_AUTO, pmap_debug, CTLFLAG_RW,
242 &pmap_debug, 0, "Debug pmap's");
244 static int pmap_enter_debug = 0;
245 SYSCTL_INT(_machdep, OID_AUTO, pmap_enter_debug, CTLFLAG_RW,
246 &pmap_enter_debug, 0, "Debug pmap_enter's");
248 static int pmap_yield_count = 64;
249 SYSCTL_INT(_machdep, OID_AUTO, pmap_yield_count, CTLFLAG_RW,
250 &pmap_yield_count, 0, "Yield during init_pt/release");
251 static int pmap_mmu_optimize = 0;
252 SYSCTL_INT(_machdep, OID_AUTO, pmap_mmu_optimize, CTLFLAG_RW,
253 &pmap_mmu_optimize, 0, "Share page table pages when possible");
254 int pmap_fast_kernel_cpusync = 0;
255 SYSCTL_INT(_machdep, OID_AUTO, pmap_fast_kernel_cpusync, CTLFLAG_RW,
256 &pmap_fast_kernel_cpusync, 0, "Share page table pages when possible");
257 int pmap_dynamic_delete = 0;
258 SYSCTL_INT(_machdep, OID_AUTO, pmap_dynamic_delete, CTLFLAG_RW,
259 &pmap_dynamic_delete, 0, "Dynamically delete PT/PD/PDPs");
260 int pmap_lock_delay = 100;
261 SYSCTL_INT(_machdep, OID_AUTO, pmap_lock_delay, CTLFLAG_RW,
262 &pmap_lock_delay, 0, "Spin loops");
263 static int meltdown_mitigation = -1;
264 TUNABLE_INT("machdep.meltdown_mitigation", &meltdown_mitigation);
265 SYSCTL_INT(_machdep, OID_AUTO, meltdown_mitigation, CTLFLAG_RW,
266 &meltdown_mitigation, 0, "Userland pmap isolation");
268 static int pmap_nx_enable = -1; /* -1 = auto */
269 /* needs manual TUNABLE in early probe, see below */
270 SYSCTL_INT(_machdep, OID_AUTO, pmap_nx_enable, CTLFLAG_RD,
272 "no-execute support (0=disabled, 1=w/READ, 2=w/READ & WRITE)");
274 static int pmap_pv_debug = 50;
275 SYSCTL_INT(_machdep, OID_AUTO, pmap_pv_debug, CTLFLAG_RW,
276 &pmap_pv_debug, 0, "");
278 /* Standard user access funtions */
279 extern int std_copyinstr (const void *udaddr, void *kaddr, size_t len,
281 extern int std_copyin (const void *udaddr, void *kaddr, size_t len);
282 extern int std_copyout (const void *kaddr, void *udaddr, size_t len);
283 extern int std_fubyte (const uint8_t *base);
284 extern int std_subyte (uint8_t *base, uint8_t byte);
285 extern int32_t std_fuword32 (const uint32_t *base);
286 extern int64_t std_fuword64 (const uint64_t *base);
287 extern int std_suword64 (uint64_t *base, uint64_t word);
288 extern int std_suword32 (uint32_t *base, int word);
289 extern uint32_t std_swapu32 (volatile uint32_t *base, uint32_t v);
290 extern uint64_t std_swapu64 (volatile uint64_t *base, uint64_t v);
291 extern uint32_t std_fuwordadd32 (volatile uint32_t *base, uint32_t v);
292 extern uint64_t std_fuwordadd64 (volatile uint64_t *base, uint64_t v);
294 static void pv_hold(pv_entry_t pv);
295 static int _pv_hold_try(pv_entry_t pv
297 static void pv_drop(pv_entry_t pv);
298 static void _pv_lock(pv_entry_t pv
300 static void pv_unlock(pv_entry_t pv);
301 static pv_entry_t _pv_alloc(pmap_t pmap, vm_pindex_t pindex, int *isnew
303 static pv_entry_t _pv_get(pmap_t pmap, vm_pindex_t pindex, vm_pindex_t **pmarkp
305 static void _pv_free(pv_entry_t pv, pv_entry_t pvp PMAP_DEBUG_DECL);
306 static pv_entry_t pv_get_try(pmap_t pmap, vm_pindex_t pindex,
307 vm_pindex_t **pmarkp, int *errorp);
308 static void pv_put(pv_entry_t pv);
309 static void *pv_pte_lookup(pv_entry_t pv, vm_pindex_t pindex);
310 static pv_entry_t pmap_allocpte(pmap_t pmap, vm_pindex_t ptepindex,
312 static pv_entry_t pmap_allocpte_seg(pmap_t pmap, vm_pindex_t ptepindex,
313 pv_entry_t *pvpp, vm_map_entry_t entry, vm_offset_t va);
314 static void pmap_remove_pv_pte(pv_entry_t pv, pv_entry_t pvp,
315 pmap_inval_bulk_t *bulk, int destroy);
316 static vm_page_t pmap_remove_pv_page(pv_entry_t pv);
317 static int pmap_release_pv(pv_entry_t pv, pv_entry_t pvp,
318 pmap_inval_bulk_t *bulk);
320 struct pmap_scan_info;
321 static void pmap_remove_callback(pmap_t pmap, struct pmap_scan_info *info,
322 pv_entry_t pte_pv, vm_pindex_t *pte_placemark,
323 pv_entry_t pt_pv, int sharept,
324 vm_offset_t va, pt_entry_t *ptep, void *arg __unused);
325 static void pmap_protect_callback(pmap_t pmap, struct pmap_scan_info *info,
326 pv_entry_t pte_pv, vm_pindex_t *pte_placemark,
327 pv_entry_t pt_pv, int sharept,
328 vm_offset_t va, pt_entry_t *ptep, void *arg __unused);
330 static void x86_64_protection_init (void);
331 static void create_pagetables(vm_paddr_t *firstaddr);
332 static void pmap_remove_all (vm_page_t m);
333 static boolean_t pmap_testbit (vm_page_t m, int bit);
335 static pt_entry_t * pmap_pte_quick (pmap_t pmap, vm_offset_t va);
336 static vm_offset_t pmap_kmem_choose(vm_offset_t addr);
338 static void pmap_pinit_defaults(struct pmap *pmap);
339 static void pv_placemarker_wait(pmap_t pmap, vm_pindex_t *pmark);
340 static void pv_placemarker_wakeup(pmap_t pmap, vm_pindex_t *pmark);
343 pv_entry_compare(pv_entry_t pv1, pv_entry_t pv2)
345 if (pv1->pv_pindex < pv2->pv_pindex)
347 if (pv1->pv_pindex > pv2->pv_pindex)
352 RB_GENERATE2(pv_entry_rb_tree, pv_entry, pv_entry,
353 pv_entry_compare, vm_pindex_t, pv_pindex);
357 pmap_page_stats_adding(vm_page_t m)
359 globaldata_t gd = mycpu;
361 if (TAILQ_EMPTY(&m->md.pv_list)) {
362 ++gd->gd_vmtotal.t_arm;
363 } else if (TAILQ_FIRST(&m->md.pv_list) ==
364 TAILQ_LAST(&m->md.pv_list, md_page_pv_list)) {
365 ++gd->gd_vmtotal.t_armshr;
366 ++gd->gd_vmtotal.t_avmshr;
368 ++gd->gd_vmtotal.t_avmshr;
374 pmap_page_stats_deleting(vm_page_t m)
376 globaldata_t gd = mycpu;
378 if (TAILQ_EMPTY(&m->md.pv_list)) {
379 --gd->gd_vmtotal.t_arm;
380 } else if (TAILQ_FIRST(&m->md.pv_list) ==
381 TAILQ_LAST(&m->md.pv_list, md_page_pv_list)) {
382 --gd->gd_vmtotal.t_armshr;
383 --gd->gd_vmtotal.t_avmshr;
385 --gd->gd_vmtotal.t_avmshr;
390 * This is an ineligent crowbar to prevent heavily threaded programs
391 * from creating long live-locks in the pmap code when pmap_mmu_optimize
392 * is enabled. Without it a pmap-local page table page can wind up being
393 * constantly created and destroyed (without injury, but also without
394 * progress) as the optimization tries to switch to the object's shared page
398 pmap_softwait(pmap_t pmap)
400 while (pmap->pm_softhold) {
401 tsleep_interlock(&pmap->pm_softhold, 0);
402 if (pmap->pm_softhold)
403 tsleep(&pmap->pm_softhold, PINTERLOCKED, "mmopt", 0);
408 pmap_softhold(pmap_t pmap)
410 while (atomic_swap_int(&pmap->pm_softhold, 1) == 1) {
411 tsleep_interlock(&pmap->pm_softhold, 0);
412 if (atomic_swap_int(&pmap->pm_softhold, 1) == 1)
413 tsleep(&pmap->pm_softhold, PINTERLOCKED, "mmopt", 0);
418 pmap_softdone(pmap_t pmap)
420 atomic_swap_int(&pmap->pm_softhold, 0);
421 wakeup(&pmap->pm_softhold);
425 * Move the kernel virtual free pointer to the next
426 * 2MB. This is used to help improve performance
427 * by using a large (2MB) page for much of the kernel
428 * (.text, .data, .bss)
432 pmap_kmem_choose(vm_offset_t addr)
434 vm_offset_t newaddr = addr;
436 newaddr = roundup2(addr, NBPDR);
441 * Returns the pindex of a page table entry (representing a terminal page).
442 * There are NUPTE_TOTAL page table entries possible (a huge number)
444 * x86-64 has a 48-bit address space, where bit 47 is sign-extended out.
445 * We want to properly translate negative KVAs.
449 pmap_pte_pindex(vm_offset_t va)
451 return ((va >> PAGE_SHIFT) & (NUPTE_TOTAL - 1));
455 * Returns the pindex of a page table.
459 pmap_pt_pindex(vm_offset_t va)
461 return (NUPTE_TOTAL + ((va >> PDRSHIFT) & (NUPT_TOTAL - 1)));
465 * Returns the pindex of a page directory.
469 pmap_pd_pindex(vm_offset_t va)
471 return (NUPTE_TOTAL + NUPT_TOTAL +
472 ((va >> PDPSHIFT) & (NUPD_TOTAL - 1)));
477 pmap_pdp_pindex(vm_offset_t va)
479 return (NUPTE_TOTAL + NUPT_TOTAL + NUPD_TOTAL +
480 ((va >> PML4SHIFT) & (NUPDP_TOTAL - 1)));
485 pmap_pml4_pindex(void)
487 return (NUPTE_TOTAL + NUPT_TOTAL + NUPD_TOTAL + NUPDP_TOTAL);
491 * Return various clipped indexes for a given VA
493 * Returns the index of a pt in a page directory, representing a page
498 pmap_pt_index(vm_offset_t va)
500 return ((va >> PDRSHIFT) & ((1ul << NPDEPGSHIFT) - 1));
504 * Returns the index of a pd in a page directory page, representing a page
509 pmap_pd_index(vm_offset_t va)
511 return ((va >> PDPSHIFT) & ((1ul << NPDPEPGSHIFT) - 1));
515 * Returns the index of a pdp in the pml4 table, representing a page
520 pmap_pdp_index(vm_offset_t va)
522 return ((va >> PML4SHIFT) & ((1ul << NPML4EPGSHIFT) - 1));
526 * Of all the layers (PTE, PT, PD, PDP, PML4) the best one to cache is
527 * the PT layer. This will speed up core pmap operations considerably.
528 * We also cache the PTE layer to (hopefully) improve relative lookup
531 * NOTE: The pmap spinlock does not need to be held but the passed-in pv
532 * must be in a known associated state (typically by being locked when
533 * the pmap spinlock isn't held). We allow the race for that case.
535 * NOTE: pm_pvhint* is only accessed (read) with the spin-lock held, using
536 * cpu_ccfence() to prevent compiler optimizations from reloading the
541 pv_cache(pmap_t pmap, pv_entry_t pv, vm_pindex_t pindex)
543 if (pindex < pmap_pt_pindex(0)) {
544 pmap->pm_pvhint_pte = pv;
545 } else if (pindex < pmap_pd_pindex(0)) {
546 pmap->pm_pvhint_pt = pv;
551 * Locate the requested pt_entry
555 pv_entry_lookup(pmap_t pmap, vm_pindex_t pindex)
560 if (pindex < pmap_pt_pindex(0))
561 pv = pmap->pm_pvhint_pte;
562 else if (pindex < pmap_pd_pindex(0))
563 pv = pmap->pm_pvhint_pt;
567 if (pv == NULL || pv->pv_pmap != pmap) {
568 pv = pv_entry_rb_tree_RB_LOOKUP(&pmap->pm_pvroot, pindex);
570 pv_cache(pmap, pv, pindex);
571 } else if (pv->pv_pindex != pindex) {
572 pv = pv_entry_rb_tree_RB_LOOKUP_REL(&pmap->pm_pvroot,
575 pv_cache(pmap, pv, pindex);
578 pv = pv_entry_rb_tree_RB_LOOKUP(&pmap->pm_pvroot, pindex);
586 * Super fast pmap_pte routine best used when scanning the pv lists.
587 * This eliminates many course-grained invltlb calls. Note that many of
588 * the pv list scans are across different pmaps and it is very wasteful
589 * to do an entire invltlb when checking a single mapping.
591 static __inline pt_entry_t *pmap_pte(pmap_t pmap, vm_offset_t va);
595 pmap_pte_quick(pmap_t pmap, vm_offset_t va)
597 return pmap_pte(pmap, va);
601 * The placemarker hash must be broken up into four zones so lock
602 * ordering semantics continue to work (e.g. pte, pt, pd, then pdp).
604 * Placemarkers are used to 'lock' page table indices that do not have
605 * a pv_entry. This allows the pmap to support managed and unmanaged
606 * pages and shared page tables.
608 #define PM_PLACE_BASE (PM_PLACEMARKS >> 2)
612 pmap_placemarker_hash(pmap_t pmap, vm_pindex_t pindex)
616 if (pindex < pmap_pt_pindex(0)) /* zone 0 - PTE */
618 else if (pindex < pmap_pd_pindex(0)) /* zone 1 - PT */
620 else if (pindex < pmap_pdp_pindex(0)) /* zone 2 - PD */
621 hi = PM_PLACE_BASE << 1;
622 else /* zone 3 - PDP (and PML4E) */
623 hi = PM_PLACE_BASE | (PM_PLACE_BASE << 1);
624 hi += pindex & (PM_PLACE_BASE - 1);
626 return (&pmap->pm_placemarks[hi]);
631 * Generic procedure to index a pte from a pt, pd, or pdp.
633 * NOTE: Normally passed pindex as pmap_xx_index(). pmap_xx_pindex() is NOT
634 * a page table page index but is instead of PV lookup index.
638 pv_pte_lookup(pv_entry_t pv, vm_pindex_t pindex)
642 pte = (pt_entry_t *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(pv->pv_m));
643 return(&pte[pindex]);
647 * Return pointer to PDP slot in the PML4
651 pmap_pdp(pmap_t pmap, vm_offset_t va)
653 return (&pmap->pm_pml4[pmap_pdp_index(va)]);
657 * Return pointer to PD slot in the PDP given a pointer to the PDP
661 pmap_pdp_to_pd(pml4_entry_t pdp_pte, vm_offset_t va)
665 pd = (pdp_entry_t *)PHYS_TO_DMAP(pdp_pte & PG_FRAME);
666 return (&pd[pmap_pd_index(va)]);
670 * Return pointer to PD slot in the PDP.
674 pmap_pd(pmap_t pmap, vm_offset_t va)
678 pdp = pmap_pdp(pmap, va);
679 if ((*pdp & pmap->pmap_bits[PG_V_IDX]) == 0)
681 return (pmap_pdp_to_pd(*pdp, va));
685 * Return pointer to PT slot in the PD given a pointer to the PD
689 pmap_pd_to_pt(pdp_entry_t pd_pte, vm_offset_t va)
693 pt = (pd_entry_t *)PHYS_TO_DMAP(pd_pte & PG_FRAME);
694 return (&pt[pmap_pt_index(va)]);
698 * Return pointer to PT slot in the PD
700 * SIMPLE PMAP NOTE: Simple pmaps (embedded in objects) do not have PDPs,
701 * so we cannot lookup the PD via the PDP. Instead we
702 * must look it up via the pmap.
706 pmap_pt(pmap_t pmap, vm_offset_t va)
710 vm_pindex_t pd_pindex;
713 if (pmap->pm_flags & PMAP_FLAG_SIMPLE) {
714 pd_pindex = pmap_pd_pindex(va);
715 spin_lock_shared(&pmap->pm_spin);
716 pv = pv_entry_rb_tree_RB_LOOKUP(&pmap->pm_pvroot, pd_pindex);
717 if (pv == NULL || pv->pv_m == NULL) {
718 spin_unlock_shared(&pmap->pm_spin);
721 phys = VM_PAGE_TO_PHYS(pv->pv_m);
722 spin_unlock_shared(&pmap->pm_spin);
723 return (pmap_pd_to_pt(phys, va));
725 pd = pmap_pd(pmap, va);
726 if (pd == NULL || (*pd & pmap->pmap_bits[PG_V_IDX]) == 0)
728 return (pmap_pd_to_pt(*pd, va));
733 * Return pointer to PTE slot in the PT given a pointer to the PT
737 pmap_pt_to_pte(pd_entry_t pt_pte, vm_offset_t va)
741 pte = (pt_entry_t *)PHYS_TO_DMAP(pt_pte & PG_FRAME);
742 return (&pte[pmap_pte_index(va)]);
746 * Return pointer to PTE slot in the PT
750 pmap_pte(pmap_t pmap, vm_offset_t va)
754 pt = pmap_pt(pmap, va);
755 if (pt == NULL || (*pt & pmap->pmap_bits[PG_V_IDX]) == 0)
757 if ((*pt & pmap->pmap_bits[PG_PS_IDX]) != 0)
758 return ((pt_entry_t *)pt);
759 return (pmap_pt_to_pte(*pt, va));
763 * Return address of PT slot in PD (KVM only)
765 * Cannot be used for user page tables because it might interfere with
766 * the shared page-table-page optimization (pmap_mmu_optimize).
770 vtopt(vm_offset_t va)
772 uint64_t mask = ((1ul << (NPDEPGSHIFT + NPDPEPGSHIFT +
773 NPML4EPGSHIFT)) - 1);
775 return (PDmap + ((va >> PDRSHIFT) & mask));
779 * KVM - return address of PTE slot in PT
783 vtopte(vm_offset_t va)
785 uint64_t mask = ((1ul << (NPTEPGSHIFT + NPDEPGSHIFT +
786 NPDPEPGSHIFT + NPML4EPGSHIFT)) - 1);
788 return (PTmap + ((va >> PAGE_SHIFT) & mask));
792 * Returns the physical address translation from va for a user address.
793 * (vm_paddr_t)-1 is returned on failure.
796 uservtophys(vm_offset_t va)
798 uint64_t mask = ((1ul << (NPTEPGSHIFT + NPDEPGSHIFT +
799 NPDPEPGSHIFT + NPML4EPGSHIFT)) - 1);
804 pmap = vmspace_pmap(mycpu->gd_curthread->td_lwp->lwp_vmspace);
806 if (va < VM_MAX_USER_ADDRESS) {
807 pte = kreadmem64(PTmap + ((va >> PAGE_SHIFT) & mask));
808 if (pte & pmap->pmap_bits[PG_V_IDX])
809 pa = (pte & PG_FRAME) | (va & PAGE_MASK);
815 allocpages(vm_paddr_t *firstaddr, long n)
820 bzero((void *)ret, n * PAGE_SIZE);
821 *firstaddr += n * PAGE_SIZE;
827 create_pagetables(vm_paddr_t *firstaddr)
829 long i; /* must be 64 bits */
836 * We are running (mostly) V=P at this point
838 * Calculate how many 1GB PD entries in our PDP pages are needed
839 * for the DMAP. This is only allocated if the system does not
840 * support 1GB pages. Otherwise ndmpdp is simply a count of
841 * the number of 1G terminal entries in our PDP pages are needed.
843 * NOTE: Maxmem is in pages
845 ndmpdp = (ptoa(Maxmem) + NBPDP - 1) >> PDPSHIFT;
846 if (ndmpdp < 4) /* Minimum 4GB of dirmap */
848 KKASSERT(ndmpdp <= NDMPML4E * NPML4EPG);
851 * Starting at KERNBASE - map all 2G worth of page table pages.
852 * KERNBASE is offset -2G from the end of kvm. This will accomodate
853 * all KVM allocations above KERNBASE, including the SYSMAPs below.
855 * We do this by allocating 2*512 PT pages. Each PT page can map
856 * 2MB, for 2GB total.
858 nkpt_base = (NPDPEPG - KPDPI) * NPTEPG; /* typically 2 x 512 */
861 * Starting at the beginning of kvm (VM_MIN_KERNEL_ADDRESS),
862 * Calculate how many page table pages we need to preallocate
863 * for early vm_map allocations.
865 * A few extra won't hurt, they will get used up in the running
871 nkpt_phys = (Maxmem * sizeof(struct vm_page) + NBPDR - 1) / NBPDR;
872 nkpt_phys += (Maxmem * sizeof(struct pv_entry) + NBPDR - 1) / NBPDR;
873 nkpt_phys += 128; /* a few extra */
876 * The highest value nkpd_phys can be set to is
877 * NKPDPE - (NPDPEPG - KPDPI) (i.e. NKPDPE - 2).
879 * Doing so would cause all PD pages to be pre-populated for
880 * a maximal KVM space (approximately 16*512 pages, or 32MB.
881 * We can save memory by not doing this.
883 nkpd_phys = (nkpt_phys + NPDPEPG - 1) / NPDPEPG;
888 * Normally NKPML4E=1-16 (1-16 kernel PDP page)
889 * Normally NKPDPE= NKPML4E*512-1 (511 min kernel PD pages)
891 * Only allocate enough PD pages
892 * NOTE: We allocate all kernel PD pages up-front, typically
893 * ~511G of KVM, requiring 511 PD pages.
895 KPTbase = allocpages(firstaddr, nkpt_base); /* KERNBASE to end */
896 KPTphys = allocpages(firstaddr, nkpt_phys); /* KVA start */
897 KPML4phys = allocpages(firstaddr, 1); /* recursive PML4 map */
898 KPDPphys = allocpages(firstaddr, NKPML4E); /* kernel PDP pages */
899 KPDphys = allocpages(firstaddr, nkpd_phys); /* kernel PD pages */
902 * Alloc PD pages for the area starting at KERNBASE.
904 KPDbase = allocpages(firstaddr, NPDPEPG - KPDPI);
909 DMPDPphys = allocpages(firstaddr, NDMPML4E);
910 if ((amd_feature & AMDID_PAGE1GB) == 0)
911 DMPDphys = allocpages(firstaddr, ndmpdp);
912 dmaplimit = (vm_paddr_t)ndmpdp << PDPSHIFT;
915 * Fill in the underlying page table pages for the area around
916 * KERNBASE. This remaps low physical memory to KERNBASE.
918 * Read-only from zero to physfree
919 * XXX not fully used, underneath 2M pages
921 for (i = 0; (i << PAGE_SHIFT) < *firstaddr; i++) {
922 ((pt_entry_t *)KPTbase)[i] = i << PAGE_SHIFT;
923 ((pt_entry_t *)KPTbase)[i] |=
924 pmap_bits_default[PG_RW_IDX] |
925 pmap_bits_default[PG_V_IDX] |
926 pmap_bits_default[PG_G_IDX];
930 * Now map the initial kernel page tables. One block of page
931 * tables is placed at the beginning of kernel virtual memory,
932 * and another block is placed at KERNBASE to map the kernel binary,
933 * data, bss, and initial pre-allocations.
935 for (i = 0; i < nkpt_base; i++) {
936 ((pd_entry_t *)KPDbase)[i] = KPTbase + (i << PAGE_SHIFT);
937 ((pd_entry_t *)KPDbase)[i] |=
938 pmap_bits_default[PG_RW_IDX] |
939 pmap_bits_default[PG_V_IDX];
941 for (i = 0; i < nkpt_phys; i++) {
942 ((pd_entry_t *)KPDphys)[i] = KPTphys + (i << PAGE_SHIFT);
943 ((pd_entry_t *)KPDphys)[i] |=
944 pmap_bits_default[PG_RW_IDX] |
945 pmap_bits_default[PG_V_IDX];
949 * Map from zero to end of allocations using 2M pages as an
950 * optimization. This will bypass some of the KPTBase pages
951 * above in the KERNBASE area.
953 for (i = 0; (i << PDRSHIFT) < *firstaddr; i++) {
954 ((pd_entry_t *)KPDbase)[i] = i << PDRSHIFT;
955 ((pd_entry_t *)KPDbase)[i] |=
956 pmap_bits_default[PG_RW_IDX] |
957 pmap_bits_default[PG_V_IDX] |
958 pmap_bits_default[PG_PS_IDX] |
959 pmap_bits_default[PG_G_IDX];
963 * Load PD addresses into the PDP pages for primary KVA space to
964 * cover existing page tables. PD's for KERNBASE are handled in
967 * expected to pre-populate all of its PDs. See NKPDPE in vmparam.h.
969 for (i = 0; i < nkpd_phys; i++) {
970 ((pdp_entry_t *)KPDPphys)[NKPML4E * NPDPEPG - NKPDPE + i] =
971 KPDphys + (i << PAGE_SHIFT);
972 ((pdp_entry_t *)KPDPphys)[NKPML4E * NPDPEPG - NKPDPE + i] |=
973 pmap_bits_default[PG_RW_IDX] |
974 pmap_bits_default[PG_V_IDX] |
975 pmap_bits_default[PG_A_IDX];
979 * Load PDs for KERNBASE to the end
981 i = (NKPML4E - 1) * NPDPEPG + KPDPI;
982 for (j = 0; j < NPDPEPG - KPDPI; ++j) {
983 ((pdp_entry_t *)KPDPphys)[i + j] =
984 KPDbase + (j << PAGE_SHIFT);
985 ((pdp_entry_t *)KPDPphys)[i + j] |=
986 pmap_bits_default[PG_RW_IDX] |
987 pmap_bits_default[PG_V_IDX] |
988 pmap_bits_default[PG_A_IDX];
992 * Now set up the direct map space using either 2MB or 1GB pages
993 * Preset PG_M and PG_A because demotion expects it.
995 * When filling in entries in the PD pages make sure any excess
996 * entries are set to zero as we allocated enough PD pages
998 if ((amd_feature & AMDID_PAGE1GB) == 0) {
1002 for (i = 0; i < NPDEPG * ndmpdp; i++) {
1003 ((pd_entry_t *)DMPDphys)[i] = i << PDRSHIFT;
1004 ((pd_entry_t *)DMPDphys)[i] |=
1005 pmap_bits_default[PG_RW_IDX] |
1006 pmap_bits_default[PG_V_IDX] |
1007 pmap_bits_default[PG_PS_IDX] |
1008 pmap_bits_default[PG_G_IDX] |
1009 pmap_bits_default[PG_M_IDX] |
1010 pmap_bits_default[PG_A_IDX];
1014 * And the direct map space's PDP
1016 for (i = 0; i < ndmpdp; i++) {
1017 ((pdp_entry_t *)DMPDPphys)[i] = DMPDphys +
1019 ((pdp_entry_t *)DMPDPphys)[i] |=
1020 pmap_bits_default[PG_RW_IDX] |
1021 pmap_bits_default[PG_V_IDX];
1027 for (i = 0; i < ndmpdp; i++) {
1028 ((pdp_entry_t *)DMPDPphys)[i] =
1029 (vm_paddr_t)i << PDPSHIFT;
1030 ((pdp_entry_t *)DMPDPphys)[i] |=
1031 pmap_bits_default[PG_RW_IDX] |
1032 pmap_bits_default[PG_V_IDX] |
1033 pmap_bits_default[PG_PS_IDX] |
1034 pmap_bits_default[PG_G_IDX] |
1035 pmap_bits_default[PG_M_IDX] |
1036 pmap_bits_default[PG_A_IDX];
1040 /* And recursively map PML4 to itself in order to get PTmap */
1041 ((pdp_entry_t *)KPML4phys)[PML4PML4I] = KPML4phys;
1042 ((pdp_entry_t *)KPML4phys)[PML4PML4I] |=
1043 pmap_bits_default[PG_RW_IDX] |
1044 pmap_bits_default[PG_V_IDX] |
1045 pmap_bits_default[PG_A_IDX];
1048 * Connect the Direct Map slots up to the PML4
1050 for (j = 0; j < NDMPML4E; ++j) {
1051 ((pdp_entry_t *)KPML4phys)[DMPML4I + j] =
1052 (DMPDPphys + ((vm_paddr_t)j << PAGE_SHIFT)) |
1053 pmap_bits_default[PG_RW_IDX] |
1054 pmap_bits_default[PG_V_IDX] |
1055 pmap_bits_default[PG_A_IDX];
1059 * Connect the KVA slot up to the PML4
1061 for (j = 0; j < NKPML4E; ++j) {
1062 ((pdp_entry_t *)KPML4phys)[KPML4I + j] =
1063 KPDPphys + ((vm_paddr_t)j << PAGE_SHIFT);
1064 ((pdp_entry_t *)KPML4phys)[KPML4I + j] |=
1065 pmap_bits_default[PG_RW_IDX] |
1066 pmap_bits_default[PG_V_IDX] |
1067 pmap_bits_default[PG_A_IDX];
1074 * Bootstrap the system enough to run with virtual memory.
1076 * On x86_64 this is called after mapping has already been enabled
1077 * and just syncs the pmap module with what has already been done.
1078 * [We can't call it easily with mapping off since the kernel is not
1079 * mapped with PA == VA, hence we would have to relocate every address
1080 * from the linked base (virtual) address "KERNBASE" to the actual
1081 * (physical) address starting relative to 0]
1084 pmap_bootstrap(vm_paddr_t *firstaddr)
1090 KvaStart = VM_MIN_KERNEL_ADDRESS;
1091 KvaEnd = VM_MAX_KERNEL_ADDRESS;
1092 KvaSize = KvaEnd - KvaStart;
1094 avail_start = *firstaddr;
1097 * Create an initial set of page tables to run the kernel in.
1099 create_pagetables(firstaddr);
1101 virtual2_start = KvaStart;
1102 virtual2_end = PTOV_OFFSET;
1104 virtual_start = (vm_offset_t) PTOV_OFFSET + *firstaddr;
1105 virtual_start = pmap_kmem_choose(virtual_start);
1107 virtual_end = VM_MAX_KERNEL_ADDRESS;
1109 /* XXX do %cr0 as well */
1110 load_cr4(rcr4() | CR4_PGE | CR4_PSE);
1111 load_cr3(KPML4phys);
1114 * Initialize protection array.
1116 x86_64_protection_init();
1119 * The kernel's pmap is statically allocated so we don't have to use
1120 * pmap_create, which is unlikely to work correctly at this part of
1121 * the boot sequence (XXX and which no longer exists).
1123 kernel_pmap.pm_pml4 = (pdp_entry_t *) (PTOV_OFFSET + KPML4phys);
1124 kernel_pmap.pm_count = 1;
1125 CPUMASK_ASSALLONES(kernel_pmap.pm_active);
1126 RB_INIT(&kernel_pmap.pm_pvroot);
1127 spin_init(&kernel_pmap.pm_spin, "pmapbootstrap");
1128 for (i = 0; i < PM_PLACEMARKS; ++i)
1129 kernel_pmap.pm_placemarks[i] = PM_NOPLACEMARK;
1132 * Reserve some special page table entries/VA space for temporary
1135 #define SYSMAP(c, p, v, n) \
1136 v = (c)va; va += ((n)*PAGE_SIZE); p = pte; pte += (n);
1142 * CMAP1/CMAP2 are used for zeroing and copying pages.
1144 SYSMAP(caddr_t, CMAP1, CADDR1, 1)
1149 SYSMAP(caddr_t, pt_crashdumpmap, crashdumpmap, MAXDUMPPGS);
1152 * ptvmmap is used for reading arbitrary physical pages via
1155 SYSMAP(caddr_t, ptmmap, ptvmmap, 1)
1158 * msgbufp is used to map the system message buffer.
1159 * XXX msgbufmap is not used.
1161 SYSMAP(struct msgbuf *, msgbufmap, msgbufp,
1162 atop(round_page(MSGBUF_SIZE)))
1165 virtual_start = pmap_kmem_choose(virtual_start);
1170 * PG_G is terribly broken on SMP because we IPI invltlb's in some
1171 * cases rather then invl1pg. Actually, I don't even know why it
1172 * works under UP because self-referential page table mappings
1178 /* Initialize the PAT MSR */
1180 pmap_pinit_defaults(&kernel_pmap);
1182 TUNABLE_INT_FETCH("machdep.pmap_fast_kernel_cpusync",
1183 &pmap_fast_kernel_cpusync);
1188 * Setup the PAT MSR.
1197 * Default values mapping PATi,PCD,PWT bits at system reset.
1198 * The default values effectively ignore the PATi bit by
1199 * repeating the encodings for 0-3 in 4-7, and map the PCD
1200 * and PWT bit combinations to the expected PAT types.
1202 pat_msr = PAT_VALUE(0, PAT_WRITE_BACK) | /* 000 */
1203 PAT_VALUE(1, PAT_WRITE_THROUGH) | /* 001 */
1204 PAT_VALUE(2, PAT_UNCACHED) | /* 010 */
1205 PAT_VALUE(3, PAT_UNCACHEABLE) | /* 011 */
1206 PAT_VALUE(4, PAT_WRITE_BACK) | /* 100 */
1207 PAT_VALUE(5, PAT_WRITE_THROUGH) | /* 101 */
1208 PAT_VALUE(6, PAT_UNCACHED) | /* 110 */
1209 PAT_VALUE(7, PAT_UNCACHEABLE); /* 111 */
1210 pat_pte_index[PAT_WRITE_BACK] = 0;
1211 pat_pte_index[PAT_WRITE_THROUGH]= 0 | X86_PG_NC_PWT;
1212 pat_pte_index[PAT_UNCACHED] = X86_PG_NC_PCD;
1213 pat_pte_index[PAT_UNCACHEABLE] = X86_PG_NC_PCD | X86_PG_NC_PWT;
1214 pat_pte_index[PAT_WRITE_PROTECTED] = pat_pte_index[PAT_UNCACHEABLE];
1215 pat_pte_index[PAT_WRITE_COMBINING] = pat_pte_index[PAT_UNCACHEABLE];
1217 if (cpu_feature & CPUID_PAT) {
1219 * If we support the PAT then set-up entries for
1220 * WRITE_PROTECTED and WRITE_COMBINING using bit patterns
1223 pat_msr = (pat_msr & ~PAT_MASK(5)) |
1224 PAT_VALUE(5, PAT_WRITE_PROTECTED);
1225 pat_msr = (pat_msr & ~PAT_MASK(6)) |
1226 PAT_VALUE(6, PAT_WRITE_COMBINING);
1227 pat_pte_index[PAT_WRITE_PROTECTED] = X86_PG_PTE_PAT | X86_PG_NC_PWT;
1228 pat_pte_index[PAT_WRITE_COMBINING] = X86_PG_PTE_PAT | X86_PG_NC_PCD;
1231 * Then enable the PAT
1236 load_cr4(cr4 & ~CR4_PGE);
1238 /* Disable caches (CD = 1, NW = 0). */
1240 load_cr0((cr0 & ~CR0_NW) | CR0_CD);
1242 /* Flushes caches and TLBs. */
1246 /* Update PAT and index table. */
1247 wrmsr(MSR_PAT, pat_msr);
1249 /* Flush caches and TLBs again. */
1253 /* Restore caches and PGE. */
1261 * Set 4mb pdir for mp startup
1266 if (cpu_feature & CPUID_PSE) {
1267 load_cr4(rcr4() | CR4_PSE);
1268 if (mycpu->gd_cpuid == 0) /* only on BSP */
1274 * Early initialization of the pmap module.
1276 * Called by vm_init, to initialize any structures that the pmap
1277 * system needs to map virtual memory. pmap_init has been enhanced to
1278 * support in a fairly consistant way, discontiguous physical memory.
1283 vm_pindex_t initial_pvs;
1287 * Allocate memory for random pmap data structures. Includes the
1290 for (i = 0; i < vm_page_array_size; i++) {
1293 m = &vm_page_array[i];
1294 TAILQ_INIT(&m->md.pv_list);
1298 * init the pv free list
1300 initial_pvs = vm_page_array_size;
1301 if (initial_pvs < MINPV)
1302 initial_pvs = MINPV;
1303 pvzone = &pvzone_store;
1304 pvinit = (void *)kmem_alloc(&kernel_map,
1305 initial_pvs * sizeof (struct pv_entry),
1307 zbootinit(pvzone, "PV ENTRY", sizeof (struct pv_entry),
1308 pvinit, initial_pvs);
1311 * Now it is safe to enable pv_table recording.
1313 pmap_initialized = TRUE;
1317 * Initialize the address space (zone) for the pv_entries. Set a
1318 * high water mark so that the system can recover from excessive
1319 * numbers of pv entries.
1321 * Also create the kernel page table template for isolated user
1324 static void pmap_init_iso_range(vm_offset_t base, size_t bytes);
1325 static void pmap_init2_iso_pmap(void);
1327 static void dump_pmap(pmap_t pmap, pt_entry_t pte, int level, vm_offset_t base);
1333 vm_pindex_t shpgperproc = PMAP_SHPGPERPROC;
1334 vm_pindex_t entry_max;
1336 TUNABLE_LONG_FETCH("vm.pmap.shpgperproc", &shpgperproc);
1337 pv_entry_max = shpgperproc * maxproc + vm_page_array_size;
1338 TUNABLE_LONG_FETCH("vm.pmap.pv_entries", &pv_entry_max);
1339 pv_entry_high_water = 9 * (pv_entry_max / 10);
1342 * Subtract out pages already installed in the zone (hack)
1344 entry_max = pv_entry_max - vm_page_array_size;
1348 zinitna(pvzone, NULL, 0, entry_max, ZONE_INTERRUPT);
1351 * Enable dynamic deletion of empty higher-level page table pages
1352 * by default only if system memory is < 8GB (use 7GB for slop).
1353 * This can save a little memory, but imposes significant
1354 * performance overhead for things like bulk builds, and for programs
1355 * which do a lot of memory mapping and memory unmapping.
1357 if (pmap_dynamic_delete < 0) {
1358 if (vmstats.v_page_count < 7LL * 1024 * 1024 * 1024 / PAGE_SIZE)
1359 pmap_dynamic_delete = 1;
1361 pmap_dynamic_delete = 0;
1365 * Automatic detection of Intel meltdown bug requiring user/kernel
1368 * Currently there are so many Intel cpu's impacted that its better
1369 * to whitelist future Intel CPUs. Most? AMD cpus are not impacted
1370 * so the default is off for AMD.
1372 if (meltdown_mitigation < 0) {
1373 if (cpu_vendor_id == CPU_VENDOR_INTEL)
1374 meltdown_mitigation = 1;
1376 meltdown_mitigation = 0;
1378 if (meltdown_mitigation) {
1379 kprintf("machdep.meltdown_mitigation enabled to "
1380 "protect against (mostly Intel) meltdown bug\n");
1381 kprintf("system call performance will be impacted\n");
1384 pmap_init2_iso_pmap();
1388 * Create the isolation pmap template. Once created, the template
1389 * is static and its PML4e entries are used to populate the
1390 * kernel portion of any isolated user pmaps.
1392 * Our isolation pmap must contain:
1393 * (1) trampoline area for all cpus
1394 * (2) common_tss area for all cpus (its part of the trampoline area now)
1395 * (3) IDT for all cpus
1396 * (4) GDT for all cpus
1399 pmap_init2_iso_pmap(void)
1404 kprintf("Initialize isolation pmap\n");
1407 * Try to use our normal API calls to make this easier. We have
1408 * to scrap the shadowed kernel PDPs pmap_pinit() creates for our
1411 pmap_pinit(&iso_pmap);
1412 bzero(iso_pmap.pm_pml4, PAGE_SIZE);
1415 * Install areas needed by the cpu and trampoline.
1417 for (n = 0; n < ncpus; ++n) {
1418 struct privatespace *ps;
1420 ps = CPU_prvspace[n];
1421 pmap_init_iso_range((vm_offset_t)&ps->trampoline,
1422 sizeof(ps->trampoline));
1423 pmap_init_iso_range((vm_offset_t)&ps->dblstack,
1424 sizeof(ps->dblstack));
1425 pmap_init_iso_range((vm_offset_t)&ps->dbgstack,
1426 sizeof(ps->dbgstack));
1427 pmap_init_iso_range((vm_offset_t)&ps->common_tss,
1428 sizeof(ps->common_tss));
1429 pmap_init_iso_range(r_idt_arr[n].rd_base,
1430 r_idt_arr[n].rd_limit + 1);
1432 pmap_init_iso_range((register_t)gdt, sizeof(gdt));
1433 pmap_init_iso_range((vm_offset_t)(int *)btext,
1434 (vm_offset_t)(int *)etext -
1435 (vm_offset_t)(int *)btext);
1438 kprintf("Dump iso_pmap:\n");
1439 dump_pmap(&iso_pmap, vtophys(iso_pmap.pm_pml4), 0, 0);
1440 kprintf("\nDump kernel_pmap:\n");
1441 dump_pmap(&kernel_pmap, vtophys(kernel_pmap.pm_pml4), 0, 0);
1446 * This adds a kernel virtual address range to the isolation pmap.
1449 pmap_init_iso_range(vm_offset_t base, size_t bytes)
1458 kprintf("isolate %016jx-%016jx (%zd)\n",
1459 base, base + bytes, bytes);
1461 va = base & ~(vm_offset_t)PAGE_MASK;
1462 while (va < base + bytes) {
1463 if ((va & PDRMASK) == 0 && va + NBPDR <= base + bytes &&
1464 (ptep = pmap_pt(&kernel_pmap, va)) != NULL &&
1465 (*ptep & kernel_pmap.pmap_bits[PG_V_IDX]) &&
1466 (*ptep & kernel_pmap.pmap_bits[PG_PS_IDX])) {
1468 * Use 2MB pages if possible
1471 pv = pmap_allocpte(&iso_pmap, pmap_pd_pindex(va), &pvp);
1472 ptep = pv_pte_lookup(pv, (va >> PDRSHIFT) & 511);
1477 * Otherwise use 4KB pages
1479 pv = pmap_allocpte(&iso_pmap, pmap_pt_pindex(va), &pvp);
1480 ptep = pv_pte_lookup(pv, (va >> PAGE_SHIFT) & 511);
1481 *ptep = vtophys(va) | kernel_pmap.pmap_bits[PG_RW_IDX] |
1482 kernel_pmap.pmap_bits[PG_V_IDX] |
1483 kernel_pmap.pmap_bits[PG_A_IDX] |
1484 kernel_pmap.pmap_bits[PG_M_IDX];
1495 * Useful debugging pmap dumper, do not remove (#if 0 when not in use)
1499 dump_pmap(pmap_t pmap, pt_entry_t pte, int level, vm_offset_t base)
1506 case 0: /* PML4e page, 512G entries */
1507 incr = (1LL << 48) / 512;
1509 case 1: /* PDP page, 1G entries */
1510 incr = (1LL << 39) / 512;
1512 case 2: /* PD page, 2MB entries */
1513 incr = (1LL << 30) / 512;
1515 case 3: /* PT page, 4KB entries */
1516 incr = (1LL << 21) / 512;
1524 kprintf("cr3 %016jx @ va=%016jx\n", pte, base);
1525 ptp = (void *)PHYS_TO_DMAP(pte & ~(pt_entry_t)PAGE_MASK);
1526 for (i = 0; i < 512; ++i) {
1527 if (level == 0 && i == 128)
1528 base += 0xFFFF000000000000LLU;
1530 kprintf("%*.*s ", level * 4, level * 4, "");
1531 if (level == 1 && (ptp[i] & 0x180) == 0x180) {
1532 kprintf("va=%016jx %3d term %016jx (1GB)\n",
1534 } else if (level == 2 && (ptp[i] & 0x180) == 0x180) {
1535 kprintf("va=%016jx %3d term %016jx (2MB)\n",
1537 } else if (level == 3) {
1538 kprintf("va=%016jx %3d term %016jx\n",
1541 kprintf("va=%016jx %3d deep %016jx\n",
1543 dump_pmap(pmap, ptp[i], level + 1, base);
1553 * Typically used to initialize a fictitious page by vm/device_pager.c
1556 pmap_page_init(struct vm_page *m)
1559 TAILQ_INIT(&m->md.pv_list);
1562 /***************************************************
1563 * Low level helper routines.....
1564 ***************************************************/
1567 * this routine defines the region(s) of memory that should
1568 * not be tested for the modified bit.
1572 pmap_track_modified(vm_pindex_t pindex)
1574 vm_offset_t va = (vm_offset_t)pindex << PAGE_SHIFT;
1575 if ((va < clean_sva) || (va >= clean_eva))
1582 * Extract the physical page address associated with the map/VA pair.
1583 * The page must be wired for this to work reliably.
1586 pmap_extract(pmap_t pmap, vm_offset_t va, void **handlep)
1593 if (va >= VM_MAX_USER_ADDRESS) {
1595 * Kernel page directories might be direct-mapped and
1596 * there is typically no PV tracking of pte's
1600 pt = pmap_pt(pmap, va);
1601 if (pt && (*pt & pmap->pmap_bits[PG_V_IDX])) {
1602 if (*pt & pmap->pmap_bits[PG_PS_IDX]) {
1603 rtval = *pt & PG_PS_FRAME;
1604 rtval |= va & PDRMASK;
1606 ptep = pmap_pt_to_pte(*pt, va);
1607 if (*pt & pmap->pmap_bits[PG_V_IDX]) {
1608 rtval = *ptep & PG_FRAME;
1609 rtval |= va & PAGE_MASK;
1617 * User pages currently do not direct-map the page directory
1618 * and some pages might not used managed PVs. But all PT's
1621 pt_pv = pv_get(pmap, pmap_pt_pindex(va), NULL);
1623 ptep = pv_pte_lookup(pt_pv, pmap_pte_index(va));
1624 if (*ptep & pmap->pmap_bits[PG_V_IDX]) {
1625 rtval = *ptep & PG_FRAME;
1626 rtval |= va & PAGE_MASK;
1629 *handlep = pt_pv; /* locked until done */
1632 } else if (handlep) {
1640 pmap_extract_done(void *handle)
1643 pv_put((pv_entry_t)handle);
1647 * Similar to extract but checks protections, SMP-friendly short-cut for
1648 * vm_fault_page[_quick](). Can return NULL to cause the caller to
1649 * fall-through to the real fault code. Does not work with HVM page
1652 * if busyp is NULL the returned page, if not NULL, is held (and not busied).
1654 * If busyp is not NULL and this function sets *busyp non-zero, the returned
1655 * page is busied (and not held).
1657 * If busyp is not NULL and this function sets *busyp to zero, the returned
1658 * page is held (and not busied).
1660 * If VM_PROT_WRITE is set in prot, and the pte is already writable, the
1661 * returned page will be dirtied. If the pte is not already writable NULL
1662 * is returned. In otherwords, if the bit is set and a vm_page_t is returned,
1663 * any COW will already have happened and that page can be written by the
1666 * WARNING! THE RETURNED PAGE IS ONLY HELD AND NOT SUITABLE FOR READING
1670 pmap_fault_page_quick(pmap_t pmap, vm_offset_t va, vm_prot_t prot, int *busyp)
1673 va < VM_MAX_USER_ADDRESS &&
1674 (pmap->pm_flags & PMAP_HVM) == 0) {
1682 req = pmap->pmap_bits[PG_V_IDX] |
1683 pmap->pmap_bits[PG_U_IDX];
1684 if (prot & VM_PROT_WRITE)
1685 req |= pmap->pmap_bits[PG_RW_IDX];
1687 pt_pv = pv_get(pmap, pmap_pt_pindex(va), NULL);
1690 ptep = pv_pte_lookup(pt_pv, pmap_pte_index(va));
1691 if ((*ptep & req) != req) {
1695 pte_pv = pv_get_try(pmap, pmap_pte_pindex(va), NULL, &error);
1696 if (pte_pv && error == 0) {
1698 if (prot & VM_PROT_WRITE) {
1699 /* interlocked by presence of pv_entry */
1703 if (prot & VM_PROT_WRITE) {
1704 if (vm_page_busy_try(m, TRUE))
1715 } else if (pte_pv) {
1719 /* error, since we didn't request a placemarker */
1730 * Extract the physical page address associated kernel virtual address.
1733 pmap_kextract(vm_offset_t va)
1735 pd_entry_t pt; /* pt entry in pd */
1738 if (va >= DMAP_MIN_ADDRESS && va < DMAP_MAX_ADDRESS) {
1739 pa = DMAP_TO_PHYS(va);
1742 if (pt & kernel_pmap.pmap_bits[PG_PS_IDX]) {
1743 pa = (pt & PG_PS_FRAME) | (va & PDRMASK);
1746 * Beware of a concurrent promotion that changes the
1747 * PDE at this point! For example, vtopte() must not
1748 * be used to access the PTE because it would use the
1749 * new PDE. It is, however, safe to use the old PDE
1750 * because the page table page is preserved by the
1753 pa = *pmap_pt_to_pte(pt, va);
1754 pa = (pa & PG_FRAME) | (va & PAGE_MASK);
1760 /***************************************************
1761 * Low level mapping routines.....
1762 ***************************************************/
1765 * Routine: pmap_kenter
1767 * Add a wired page to the KVA
1768 * NOTE! note that in order for the mapping to take effect -- you
1769 * should do an invltlb after doing the pmap_kenter().
1772 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
1778 kernel_pmap.pmap_bits[PG_RW_IDX] |
1779 kernel_pmap.pmap_bits[PG_V_IDX];
1783 pmap_inval_smp(&kernel_pmap, va, 1, ptep, npte);
1787 pmap_inval_smp(&kernel_pmap, va, ptep, npte);
1794 * Similar to pmap_kenter(), except we only invalidate the mapping on the
1795 * current CPU. Returns 0 if the previous pte was 0, 1 if it wasn't
1796 * (caller can conditionalize calling smp_invltlb()).
1799 pmap_kenter_quick(vm_offset_t va, vm_paddr_t pa)
1805 npte = pa | kernel_pmap.pmap_bits[PG_RW_IDX] |
1806 kernel_pmap.pmap_bits[PG_V_IDX];
1815 atomic_swap_long(ptep, npte);
1816 cpu_invlpg((void *)va);
1822 * Enter addresses into the kernel pmap but don't bother
1823 * doing any tlb invalidations. Caller will do a rollup
1824 * invalidation via pmap_rollup_inval().
1827 pmap_kenter_noinval(vm_offset_t va, vm_paddr_t pa)
1834 kernel_pmap.pmap_bits[PG_RW_IDX] |
1835 kernel_pmap.pmap_bits[PG_V_IDX];
1844 atomic_swap_long(ptep, npte);
1845 cpu_invlpg((void *)va);
1851 * remove a page from the kernel pagetables
1854 pmap_kremove(vm_offset_t va)
1859 pmap_inval_smp(&kernel_pmap, va, 1, ptep, 0);
1863 pmap_kremove_quick(vm_offset_t va)
1868 (void)pte_load_clear(ptep);
1869 cpu_invlpg((void *)va);
1873 * Remove addresses from the kernel pmap but don't bother
1874 * doing any tlb invalidations. Caller will do a rollup
1875 * invalidation via pmap_rollup_inval().
1878 pmap_kremove_noinval(vm_offset_t va)
1883 (void)pte_load_clear(ptep);
1887 * XXX these need to be recoded. They are not used in any critical path.
1890 pmap_kmodify_rw(vm_offset_t va)
1892 atomic_set_long(vtopte(va), kernel_pmap.pmap_bits[PG_RW_IDX]);
1893 cpu_invlpg((void *)va);
1898 pmap_kmodify_nc(vm_offset_t va)
1900 atomic_set_long(vtopte(va), PG_N);
1901 cpu_invlpg((void *)va);
1906 * Used to map a range of physical addresses into kernel virtual
1907 * address space during the low level boot, typically to map the
1908 * dump bitmap, message buffer, and vm_page_array.
1910 * These mappings are typically made at some pointer after the end of the
1913 * We could return PHYS_TO_DMAP(start) here and not allocate any
1914 * via (*virtp), but then kmem from userland and kernel dumps won't
1915 * have access to the related pointers.
1918 pmap_map(vm_offset_t *virtp, vm_paddr_t start, vm_paddr_t end, int prot)
1921 vm_offset_t va_start;
1923 /*return PHYS_TO_DMAP(start);*/
1928 while (start < end) {
1929 pmap_kenter_quick(va, start);
1937 #define PMAP_CLFLUSH_THRESHOLD (2 * 1024 * 1024)
1940 * Remove the specified set of pages from the data and instruction caches.
1942 * In contrast to pmap_invalidate_cache_range(), this function does not
1943 * rely on the CPU's self-snoop feature, because it is intended for use
1944 * when moving pages into a different cache domain.
1947 pmap_invalidate_cache_pages(vm_page_t *pages, int count)
1949 vm_offset_t daddr, eva;
1952 if (count >= PMAP_CLFLUSH_THRESHOLD / PAGE_SIZE ||
1953 (cpu_feature & CPUID_CLFSH) == 0)
1957 for (i = 0; i < count; i++) {
1958 daddr = PHYS_TO_DMAP(VM_PAGE_TO_PHYS(pages[i]));
1959 eva = daddr + PAGE_SIZE;
1960 for (; daddr < eva; daddr += cpu_clflush_line_size)
1968 pmap_invalidate_cache_range(vm_offset_t sva, vm_offset_t eva)
1970 KASSERT((sva & PAGE_MASK) == 0,
1971 ("pmap_invalidate_cache_range: sva not page-aligned"));
1972 KASSERT((eva & PAGE_MASK) == 0,
1973 ("pmap_invalidate_cache_range: eva not page-aligned"));
1975 if (cpu_feature & CPUID_SS) {
1976 ; /* If "Self Snoop" is supported, do nothing. */
1978 /* Globally invalidate caches */
1979 cpu_wbinvd_on_all_cpus();
1984 * Invalidate the specified range of virtual memory on all cpus associated
1988 pmap_invalidate_range(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
1990 pmap_inval_smp(pmap, sva, (eva - sva) >> PAGE_SHIFT, NULL, 0);
1994 * Add a list of wired pages to the kva. This routine is used for temporary
1995 * kernel mappings such as those found in buffer cache buffer. Page
1996 * modifications and accesses are not tracked or recorded.
1998 * NOTE! Old mappings are simply overwritten, and we cannot assume relaxed
1999 * semantics as previous mappings may have been zerod without any
2002 * The page *must* be wired.
2004 static __inline void
2005 _pmap_qenter(vm_offset_t beg_va, vm_page_t *m, int count, int doinval)
2010 end_va = beg_va + count * PAGE_SIZE;
2012 for (va = beg_va; va < end_va; va += PAGE_SIZE) {
2017 pte = VM_PAGE_TO_PHYS(*m) |
2018 kernel_pmap.pmap_bits[PG_RW_IDX] |
2019 kernel_pmap.pmap_bits[PG_V_IDX] |
2020 kernel_pmap.pmap_cache_bits[(*m)->pat_mode];
2022 atomic_swap_long(ptep, pte);
2026 pmap_invalidate_range(&kernel_pmap, beg_va, end_va);
2030 pmap_qenter(vm_offset_t beg_va, vm_page_t *m, int count)
2032 _pmap_qenter(beg_va, m, count, 1);
2036 pmap_qenter_noinval(vm_offset_t beg_va, vm_page_t *m, int count)
2038 _pmap_qenter(beg_va, m, count, 0);
2042 * This routine jerks page mappings from the kernel -- it is meant only
2043 * for temporary mappings such as those found in buffer cache buffers.
2044 * No recording modified or access status occurs.
2046 * MPSAFE, INTERRUPT SAFE (cluster callback)
2049 pmap_qremove(vm_offset_t beg_va, int count)
2054 end_va = beg_va + count * PAGE_SIZE;
2056 for (va = beg_va; va < end_va; va += PAGE_SIZE) {
2060 (void)pte_load_clear(pte);
2061 cpu_invlpg((void *)va);
2063 pmap_invalidate_range(&kernel_pmap, beg_va, end_va);
2067 * This routine removes temporary kernel mappings, only invalidating them
2068 * on the current cpu. It should only be used under carefully controlled
2072 pmap_qremove_quick(vm_offset_t beg_va, int count)
2077 end_va = beg_va + count * PAGE_SIZE;
2079 for (va = beg_va; va < end_va; va += PAGE_SIZE) {
2083 (void)pte_load_clear(pte);
2084 cpu_invlpg((void *)va);
2089 * This routine removes temporary kernel mappings *without* invalidating
2090 * the TLB. It can only be used on permanent kva reservations such as those
2091 * found in buffer cache buffers, under carefully controlled circumstances.
2093 * NOTE: Repopulating these KVAs requires unconditional invalidation.
2094 * (pmap_qenter() does unconditional invalidation).
2097 pmap_qremove_noinval(vm_offset_t beg_va, int count)
2102 end_va = beg_va + count * PAGE_SIZE;
2104 for (va = beg_va; va < end_va; va += PAGE_SIZE) {
2108 (void)pte_load_clear(pte);
2113 * Create a new thread and optionally associate it with a (new) process.
2114 * NOTE! the new thread's cpu may not equal the current cpu.
2117 pmap_init_thread(thread_t td)
2119 /* enforce pcb placement & alignment */
2120 td->td_pcb = (struct pcb *)(td->td_kstack + td->td_kstack_size) - 1;
2121 td->td_pcb = (struct pcb *)((intptr_t)td->td_pcb & ~(intptr_t)0xF);
2122 td->td_savefpu = &td->td_pcb->pcb_save;
2123 td->td_sp = (char *)td->td_pcb; /* no -16 */
2127 * This routine directly affects the fork perf for a process.
2130 pmap_init_proc(struct proc *p)
2135 pmap_pinit_defaults(struct pmap *pmap)
2137 bcopy(pmap_bits_default, pmap->pmap_bits,
2138 sizeof(pmap_bits_default));
2139 bcopy(protection_codes, pmap->protection_codes,
2140 sizeof(protection_codes));
2141 bcopy(pat_pte_index, pmap->pmap_cache_bits,
2142 sizeof(pat_pte_index));
2143 pmap->pmap_cache_mask = X86_PG_NC_PWT | X86_PG_NC_PCD | X86_PG_PTE_PAT;
2144 pmap->copyinstr = std_copyinstr;
2145 pmap->copyin = std_copyin;
2146 pmap->copyout = std_copyout;
2147 pmap->fubyte = std_fubyte;
2148 pmap->subyte = std_subyte;
2149 pmap->fuword32 = std_fuword32;
2150 pmap->fuword64 = std_fuword64;
2151 pmap->suword32 = std_suword32;
2152 pmap->suword64 = std_suword64;
2153 pmap->swapu32 = std_swapu32;
2154 pmap->swapu64 = std_swapu64;
2155 pmap->fuwordadd32 = std_fuwordadd32;
2156 pmap->fuwordadd64 = std_fuwordadd64;
2159 * Initialize pmap0/vmspace0.
2161 * On architectures where the kernel pmap is not integrated into the user
2162 * process pmap, this pmap represents the process pmap, not the kernel pmap.
2163 * kernel_pmap should be used to directly access the kernel_pmap.
2166 pmap_pinit0(struct pmap *pmap)
2170 pmap->pm_pml4 = (pml4_entry_t *)(PTOV_OFFSET + KPML4phys);
2172 CPUMASK_ASSZERO(pmap->pm_active);
2173 pmap->pm_pvhint_pt = NULL;
2174 pmap->pm_pvhint_pte = NULL;
2175 RB_INIT(&pmap->pm_pvroot);
2176 spin_init(&pmap->pm_spin, "pmapinit0");
2177 for (i = 0; i < PM_PLACEMARKS; ++i)
2178 pmap->pm_placemarks[i] = PM_NOPLACEMARK;
2179 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
2180 pmap_pinit_defaults(pmap);
2184 * Initialize a preallocated and zeroed pmap structure,
2185 * such as one in a vmspace structure.
2188 pmap_pinit_simple(struct pmap *pmap)
2193 * Misc initialization
2196 CPUMASK_ASSZERO(pmap->pm_active);
2197 pmap->pm_pvhint_pt = NULL;
2198 pmap->pm_pvhint_pte = NULL;
2199 pmap->pm_flags = PMAP_FLAG_SIMPLE;
2201 pmap_pinit_defaults(pmap);
2204 * Don't blow up locks/tokens on re-use (XXX fix/use drop code
2207 if (pmap->pm_pmlpv == NULL) {
2208 RB_INIT(&pmap->pm_pvroot);
2209 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
2210 spin_init(&pmap->pm_spin, "pmapinitsimple");
2211 for (i = 0; i < PM_PLACEMARKS; ++i)
2212 pmap->pm_placemarks[i] = PM_NOPLACEMARK;
2217 pmap_pinit(struct pmap *pmap)
2222 if (pmap->pm_pmlpv) {
2223 if (pmap->pmap_bits[TYPE_IDX] != REGULAR_PMAP) {
2228 pmap_pinit_simple(pmap);
2229 pmap->pm_flags &= ~PMAP_FLAG_SIMPLE;
2232 * No need to allocate page table space yet but we do need a valid
2233 * page directory table.
2235 if (pmap->pm_pml4 == NULL) {
2237 (pml4_entry_t *)kmem_alloc_pageable(&kernel_map,
2240 pmap->pm_pml4_iso = (void *)((char *)pmap->pm_pml4 + PAGE_SIZE);
2244 * Allocate the PML4e table, which wires it even though it isn't
2245 * being entered into some higher level page table (it being the
2246 * highest level). If one is already cached we don't have to do
2249 if ((pv = pmap->pm_pmlpv) == NULL) {
2250 pv = pmap_allocpte(pmap, pmap_pml4_pindex(), NULL);
2251 pmap->pm_pmlpv = pv;
2252 pmap_kenter((vm_offset_t)pmap->pm_pml4,
2253 VM_PAGE_TO_PHYS(pv->pv_m));
2257 * Install DMAP and KMAP.
2259 for (j = 0; j < NDMPML4E; ++j) {
2260 pmap->pm_pml4[DMPML4I + j] =
2261 (DMPDPphys + ((vm_paddr_t)j << PAGE_SHIFT)) |
2262 pmap->pmap_bits[PG_RW_IDX] |
2263 pmap->pmap_bits[PG_V_IDX] |
2264 pmap->pmap_bits[PG_A_IDX];
2266 for (j = 0; j < NKPML4E; ++j) {
2267 pmap->pm_pml4[KPML4I + j] =
2268 (KPDPphys + ((vm_paddr_t)j << PAGE_SHIFT)) |
2269 pmap->pmap_bits[PG_RW_IDX] |
2270 pmap->pmap_bits[PG_V_IDX] |
2271 pmap->pmap_bits[PG_A_IDX];
2275 * install self-referential address mapping entry
2277 pmap->pm_pml4[PML4PML4I] = VM_PAGE_TO_PHYS(pv->pv_m) |
2278 pmap->pmap_bits[PG_V_IDX] |
2279 pmap->pmap_bits[PG_RW_IDX] |
2280 pmap->pmap_bits[PG_A_IDX];
2282 KKASSERT(pv->pv_m->flags & PG_MAPPED);
2283 KKASSERT(pv->pv_m->flags & PG_WRITEABLE);
2285 KKASSERT(pmap->pm_pml4[255] == 0);
2288 * When implementing an isolated userland pmap, a second PML4e table
2289 * is needed. We use pmap_pml4_pindex() + 1 for convenience, but
2290 * note that we do not operate on this table using our API functions
2291 * so handling of the + 1 case is mostly just to prevent implosions.
2293 * We install an isolated version of the kernel PDPs into this
2294 * second PML4e table. The pmap code will mirror all user PDPs
2295 * between the primary and secondary PML4e table.
2297 if ((pv = pmap->pm_pmlpv_iso) == NULL && meltdown_mitigation &&
2298 pmap != &iso_pmap) {
2299 pv = pmap_allocpte(pmap, pmap_pml4_pindex() + 1, NULL);
2300 pmap->pm_pmlpv_iso = pv;
2301 pmap_kenter((vm_offset_t)pmap->pm_pml4_iso,
2302 VM_PAGE_TO_PHYS(pv->pv_m));
2306 * Install an isolated version of the kernel pmap for
2307 * user consumption, using PDPs constructed in iso_pmap.
2309 for (j = 0; j < NKPML4E; ++j) {
2310 pmap->pm_pml4_iso[KPML4I + j] =
2311 iso_pmap.pm_pml4[KPML4I + j];
2314 KKASSERT(pv->pv_m->flags & PG_MAPPED);
2315 KKASSERT(pv->pv_m->flags & PG_WRITEABLE);
2320 * Clean up a pmap structure so it can be physically freed. This routine
2321 * is called by the vmspace dtor function. A great deal of pmap data is
2322 * left passively mapped to improve vmspace management so we have a bit
2323 * of cleanup work to do here.
2326 pmap_puninit(pmap_t pmap)
2331 KKASSERT(CPUMASK_TESTZERO(pmap->pm_active));
2332 if ((pv = pmap->pm_pmlpv) != NULL) {
2333 if (pv_hold_try(pv) == 0)
2335 KKASSERT(pv == pmap->pm_pmlpv);
2336 p = pmap_remove_pv_page(pv);
2338 pv = NULL; /* safety */
2339 pmap_kremove((vm_offset_t)pmap->pm_pml4);
2340 vm_page_busy_wait(p, FALSE, "pgpun");
2341 KKASSERT(p->flags & (PG_FICTITIOUS|PG_UNMANAGED));
2342 vm_page_unwire(p, 0);
2343 vm_page_flag_clear(p, PG_MAPPED | PG_WRITEABLE);
2345 pmap->pm_pmlpv = NULL;
2347 if ((pv = pmap->pm_pmlpv_iso) != NULL) {
2348 if (pv_hold_try(pv) == 0)
2350 KKASSERT(pv == pmap->pm_pmlpv_iso);
2351 p = pmap_remove_pv_page(pv);
2353 pv = NULL; /* safety */
2354 pmap_kremove((vm_offset_t)pmap->pm_pml4_iso);
2355 vm_page_busy_wait(p, FALSE, "pgpun");
2356 KKASSERT(p->flags & (PG_FICTITIOUS|PG_UNMANAGED));
2357 vm_page_unwire(p, 0);
2358 vm_page_flag_clear(p, PG_MAPPED | PG_WRITEABLE);
2360 pmap->pm_pmlpv_iso = NULL;
2362 if (pmap->pm_pml4) {
2363 KKASSERT(pmap->pm_pml4 != (void *)(PTOV_OFFSET + KPML4phys));
2364 kmem_free(&kernel_map,
2365 (vm_offset_t)pmap->pm_pml4, PAGE_SIZE * 2);
2366 pmap->pm_pml4 = NULL;
2367 pmap->pm_pml4_iso = NULL;
2369 KKASSERT(pmap->pm_stats.resident_count == 0);
2370 KKASSERT(pmap->pm_stats.wired_count == 0);
2374 * This function is now unused (used to add the pmap to the pmap_list)
2377 pmap_pinit2(struct pmap *pmap)
2382 * This routine is called when various levels in the page table need to
2383 * be populated. This routine cannot fail.
2385 * This function returns two locked pv_entry's, one representing the
2386 * requested pv and one representing the requested pv's parent pv. If
2387 * an intermediate page table does not exist it will be created, mapped,
2388 * wired, and the parent page table will be given an additional hold
2389 * count representing the presence of the child pv_entry.
2393 pmap_allocpte(pmap_t pmap, vm_pindex_t ptepindex, pv_entry_t *pvpp)
2396 pt_entry_t *ptep_iso;
2400 vm_pindex_t pt_pindex;
2406 * If the pv already exists and we aren't being asked for the
2407 * parent page table page we can just return it. A locked+held pv
2408 * is returned. The pv will also have a second hold related to the
2409 * pmap association that we don't have to worry about.
2412 pv = pv_alloc(pmap, ptepindex, &isnew);
2413 if (isnew == 0 && pvpp == NULL)
2417 * Special case terminal PVs. These are not page table pages so
2418 * no vm_page is allocated (the caller supplied the vm_page). If
2419 * pvpp is non-NULL we are being asked to also removed the pt_pv
2422 * Note that pt_pv's are only returned for user VAs. We assert that
2423 * a pt_pv is not being requested for kernel VAs. The kernel
2424 * pre-wires all higher-level page tables so don't overload managed
2425 * higher-level page tables on top of it!
2427 * However, its convenient for us to allow the case when creating
2428 * iso_pmap. This is a bit of a hack but it simplifies iso_pmap
2431 if (ptepindex < pmap_pt_pindex(0)) {
2432 if (ptepindex >= NUPTE_USER && pmap != &iso_pmap) {
2433 /* kernel manages this manually for KVM */
2434 KKASSERT(pvpp == NULL);
2436 KKASSERT(pvpp != NULL);
2437 pt_pindex = NUPTE_TOTAL + (ptepindex >> NPTEPGSHIFT);
2438 pvp = pmap_allocpte(pmap, pt_pindex, NULL);
2440 vm_page_wire_quick(pvp->pv_m);
2447 * The kernel never uses managed PT/PD/PDP pages.
2449 KKASSERT(pmap != &kernel_pmap);
2452 * Non-terminal PVs allocate a VM page to represent the page table,
2453 * so we have to resolve pvp and calculate ptepindex for the pvp
2454 * and then for the page table entry index in the pvp for
2457 if (ptepindex < pmap_pd_pindex(0)) {
2459 * pv is PT, pvp is PD
2461 ptepindex = (ptepindex - pmap_pt_pindex(0)) >> NPDEPGSHIFT;
2462 ptepindex += NUPTE_TOTAL + NUPT_TOTAL;
2463 pvp = pmap_allocpte(pmap, ptepindex, NULL);
2468 ptepindex = pv->pv_pindex - pmap_pt_pindex(0);
2469 ptepindex &= ((1ul << NPDEPGSHIFT) - 1);
2471 } else if (ptepindex < pmap_pdp_pindex(0)) {
2473 * pv is PD, pvp is PDP
2475 * SIMPLE PMAP NOTE: Simple pmaps do not allocate above
2478 ptepindex = (ptepindex - pmap_pd_pindex(0)) >> NPDPEPGSHIFT;
2479 ptepindex += NUPTE_TOTAL + NUPT_TOTAL + NUPD_TOTAL;
2481 if (pmap->pm_flags & PMAP_FLAG_SIMPLE) {
2482 KKASSERT(pvpp == NULL);
2485 pvp = pmap_allocpte(pmap, ptepindex, NULL);
2491 ptepindex = pv->pv_pindex - pmap_pd_pindex(0);
2492 ptepindex &= ((1ul << NPDPEPGSHIFT) - 1);
2493 } else if (ptepindex < pmap_pml4_pindex()) {
2495 * pv is PDP, pvp is the root pml4 table
2497 pvp = pmap_allocpte(pmap, pmap_pml4_pindex(), NULL);
2502 ptepindex = pv->pv_pindex - pmap_pdp_pindex(0);
2503 ptepindex &= ((1ul << NPML4EPGSHIFT) - 1);
2506 * pv represents the top-level PML4, there is no parent.
2515 * (isnew) is TRUE, pv is not terminal.
2517 * (1) Add a wire count to the parent page table (pvp).
2518 * (2) Allocate a VM page for the page table.
2519 * (3) Enter the VM page into the parent page table.
2521 * page table pages are marked PG_WRITEABLE and PG_MAPPED.
2524 vm_page_wire_quick(pvp->pv_m);
2527 m = vm_page_alloc(NULL, pv->pv_pindex,
2528 VM_ALLOC_NORMAL | VM_ALLOC_SYSTEM |
2529 VM_ALLOC_INTERRUPT);
2534 vm_page_wire(m); /* wire for mapping in parent */
2535 vm_page_unmanage(m); /* m must be spinunlocked */
2536 pmap_zero_page(VM_PAGE_TO_PHYS(m));
2537 m->valid = VM_PAGE_BITS_ALL;
2539 vm_page_spin_lock(m);
2540 pmap_page_stats_adding(m);
2543 * PGTABLE pv's only exist in the context of the pmap RB tree
2544 * (pmap->pm_pvroot).
2547 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
2549 pv->pv_flags |= PV_FLAG_PGTABLE;
2551 vm_page_flag_set(m, PG_MAPPED | PG_WRITEABLE);
2552 vm_page_spin_unlock(m);
2555 * (isnew) is TRUE, pv is not terminal.
2557 * Wire the page into pvp. Bump the resident_count for the pmap.
2558 * There is no pvp for the top level, address the pm_pml4[] array
2561 * If the caller wants the parent we return it, otherwise
2562 * we just put it away.
2564 * No interlock is needed for pte 0 -> non-zero.
2566 * In the situation where *ptep is valid we might have an unmanaged
2567 * page table page shared from another page table which we need to
2568 * unshare before installing our private page table page.
2571 v = VM_PAGE_TO_PHYS(m) |
2572 (pmap->pmap_bits[PG_RW_IDX] |
2573 pmap->pmap_bits[PG_V_IDX] |
2574 pmap->pmap_bits[PG_A_IDX]);
2575 if (ptepindex < NUPTE_USER)
2576 v |= pmap->pmap_bits[PG_U_IDX];
2577 if (ptepindex < pmap_pt_pindex(0))
2578 v |= pmap->pmap_bits[PG_M_IDX];
2580 ptep = pv_pte_lookup(pvp, ptepindex);
2581 if (pvp == pmap->pm_pmlpv && pmap->pm_pmlpv_iso)
2582 ptep_iso = pv_pte_lookup(pmap->pm_pmlpv_iso, ptepindex);
2585 if (*ptep & pmap->pmap_bits[PG_V_IDX]) {
2589 panic("pmap_allocpte: unexpected pte %p/%d",
2590 pvp, (int)ptepindex);
2592 pte = pmap_inval_smp(pmap, (vm_offset_t)-1, 1,
2595 pmap_inval_smp(pmap, (vm_offset_t)-1, 1,
2598 if (vm_page_unwire_quick(
2599 PHYS_TO_VM_PAGE(pte & PG_FRAME))) {
2600 panic("pmap_allocpte: shared pgtable "
2601 "pg bad wirecount");
2606 pte = atomic_swap_long(ptep, v);
2608 atomic_swap_long(ptep_iso, v);
2610 kprintf("install pgtbl mixup 0x%016jx "
2611 "old/new 0x%016jx/0x%016jx\n",
2612 (intmax_t)ptepindex, pte, v);
2619 * (isnew) may be TRUE or FALSE, pv may or may not be terminal.
2623 KKASSERT(pvp->pv_m != NULL);
2624 ptep = pv_pte_lookup(pvp, ptepindex);
2625 v = VM_PAGE_TO_PHYS(pv->pv_m) |
2626 (pmap->pmap_bits[PG_RW_IDX] |
2627 pmap->pmap_bits[PG_V_IDX] |
2628 pmap->pmap_bits[PG_A_IDX]);
2629 if (ptepindex < NUPTE_USER)
2630 v |= pmap->pmap_bits[PG_U_IDX];
2631 if (ptepindex < pmap_pt_pindex(0))
2632 v |= pmap->pmap_bits[PG_M_IDX];
2634 kprintf("mismatched upper level pt %016jx/%016jx\n",
2646 * This version of pmap_allocpte() checks for possible segment optimizations
2647 * that would allow page-table sharing. It can be called for terminal
2648 * page or page table page ptepindex's.
2650 * The function is called with page table page ptepindex's for fictitious
2651 * and unmanaged terminal pages. That is, we don't want to allocate a
2652 * terminal pv, we just want the pt_pv. pvpp is usually passed as NULL
2655 * This function can return a pv and *pvpp associated with the passed in pmap
2656 * OR a pv and *pvpp associated with the shared pmap. In the latter case
2657 * an unmanaged page table page will be entered into the pass in pmap.
2661 pmap_allocpte_seg(pmap_t pmap, vm_pindex_t ptepindex, pv_entry_t *pvpp,
2662 vm_map_entry_t entry, vm_offset_t va)
2667 vm_pindex_t *pt_placemark;
2669 pv_entry_t pte_pv; /* in original or shared pmap */
2670 pv_entry_t pt_pv; /* in original or shared pmap */
2671 pv_entry_t proc_pd_pv; /* in original pmap */
2672 pv_entry_t proc_pt_pv; /* in original pmap */
2673 pv_entry_t xpv; /* PT in shared pmap */
2674 pd_entry_t *pt; /* PT entry in PD of original pmap */
2675 pd_entry_t opte; /* contents of *pt */
2676 pd_entry_t npte; /* contents of *pt */
2681 * Basic tests, require a non-NULL vm_map_entry, require proper
2682 * alignment and type for the vm_map_entry, require that the
2683 * underlying object already be allocated.
2685 * We allow almost any type of object to use this optimization.
2686 * The object itself does NOT have to be sized to a multiple of the
2687 * segment size, but the memory mapping does.
2689 * XXX don't handle devices currently, because VM_PAGE_TO_PHYS()
2690 * won't work as expected.
2692 if (entry == NULL ||
2693 pmap_mmu_optimize == 0 || /* not enabled */
2694 (pmap->pm_flags & PMAP_HVM) || /* special pmap */
2695 ptepindex >= pmap_pd_pindex(0) || /* not terminal or pt */
2696 entry->inheritance != VM_INHERIT_SHARE || /* not shared */
2697 entry->maptype != VM_MAPTYPE_NORMAL || /* weird map type */
2698 entry->object.vm_object == NULL || /* needs VM object */
2699 entry->object.vm_object->type == OBJT_DEVICE || /* ick */
2700 entry->object.vm_object->type == OBJT_MGTDEVICE || /* ick */
2701 (entry->offset & SEG_MASK) || /* must be aligned */
2702 (entry->start & SEG_MASK)) {
2703 return(pmap_allocpte(pmap, ptepindex, pvpp));
2707 * Make sure the full segment can be represented.
2709 b = va & ~(vm_offset_t)SEG_MASK;
2710 if (b < entry->start || b + SEG_SIZE > entry->end)
2711 return(pmap_allocpte(pmap, ptepindex, pvpp));
2714 * If the full segment can be represented dive the VM object's
2715 * shared pmap, allocating as required.
2717 object = entry->object.vm_object;
2719 if (entry->protection & VM_PROT_WRITE)
2720 obpmapp = &object->md.pmap_rw;
2722 obpmapp = &object->md.pmap_ro;
2725 if (pmap_enter_debug > 0) {
2727 kprintf("pmap_allocpte_seg: va=%jx prot %08x o=%p "
2729 va, entry->protection, object,
2731 kprintf("pmap_allocpte_seg: entry %p %jx-%jx\n",
2732 entry, entry->start, entry->end);
2737 * We allocate what appears to be a normal pmap but because portions
2738 * of this pmap are shared with other unrelated pmaps we have to
2739 * set pm_active to point to all cpus.
2741 * XXX Currently using pmap_spin to interlock the update, can't use
2742 * vm_object_hold/drop because the token might already be held
2743 * shared OR exclusive and we don't know.
2745 while ((obpmap = *obpmapp) == NULL) {
2746 obpmap = kmalloc(sizeof(*obpmap), M_OBJPMAP, M_WAITOK|M_ZERO);
2747 pmap_pinit_simple(obpmap);
2748 pmap_pinit2(obpmap);
2749 spin_lock(&pmap_spin);
2750 if (*obpmapp != NULL) {
2754 spin_unlock(&pmap_spin);
2755 pmap_release(obpmap);
2756 pmap_puninit(obpmap);
2757 kfree(obpmap, M_OBJPMAP);
2758 obpmap = *obpmapp; /* safety */
2760 obpmap->pm_active = smp_active_mask;
2761 obpmap->pm_flags |= PMAP_SEGSHARED;
2763 spin_unlock(&pmap_spin);
2768 * Layering is: PTE, PT, PD, PDP, PML4. We have to return the
2769 * pte/pt using the shared pmap from the object but also adjust
2770 * the process pmap's page table page as a side effect.
2774 * Resolve the terminal PTE and PT in the shared pmap. This is what
2775 * we will return. This is true if ptepindex represents a terminal
2776 * page, otherwise pte_pv is actually the PT and pt_pv is actually
2780 pte_pv = pmap_allocpte(obpmap, ptepindex, &pt_pv);
2783 if (ptepindex >= pmap_pt_pindex(0))
2789 * Resolve the PD in the process pmap so we can properly share the
2790 * page table page. Lock order is bottom-up (leaf first)!
2792 * NOTE: proc_pt_pv can be NULL.
2794 proc_pt_pv = pv_get(pmap, pmap_pt_pindex(b), &pt_placemark);
2795 proc_pd_pv = pmap_allocpte(pmap, pmap_pd_pindex(b), NULL);
2797 if (pmap_enter_debug > 0) {
2799 kprintf("proc_pt_pv %p (wc %d) pd_pv %p va=%jx\n",
2801 (proc_pt_pv ? proc_pt_pv->pv_m->wire_count : -1),
2808 * xpv is the page table page pv from the shared object
2809 * (for convenience), from above.
2811 * Calculate the pte value for the PT to load into the process PD.
2812 * If we have to change it we must properly dispose of the previous
2815 pt = pv_pte_lookup(proc_pd_pv, pmap_pt_index(b));
2816 npte = VM_PAGE_TO_PHYS(xpv->pv_m) |
2817 (pmap->pmap_bits[PG_U_IDX] |
2818 pmap->pmap_bits[PG_RW_IDX] |
2819 pmap->pmap_bits[PG_V_IDX] |
2820 pmap->pmap_bits[PG_A_IDX] |
2821 pmap->pmap_bits[PG_M_IDX]);
2824 * Dispose of previous page table page if it was local to the
2825 * process pmap. If the old pt is not empty we cannot dispose of it
2826 * until we clean it out. This case should not arise very often so
2827 * it is not optimized.
2829 * Leave pt_pv and pte_pv (in our object pmap) locked and intact
2833 pmap_inval_bulk_t bulk;
2835 if (proc_pt_pv->pv_m->wire_count != 1) {
2837 * The page table has a bunch of stuff in it
2838 * which we have to scrap.
2840 if (softhold == 0) {
2842 pmap_softhold(pmap);
2847 va & ~(vm_offset_t)SEG_MASK,
2848 (va + SEG_SIZE) & ~(vm_offset_t)SEG_MASK);
2851 * The page table is empty and can be destroyed.
2852 * However, doing so leaves the pt slot unlocked,
2853 * so we have to loop-up to handle any races until
2854 * we get a NULL proc_pt_pv and a proper pt_placemark.
2856 pmap_inval_bulk_init(&bulk, proc_pt_pv->pv_pmap);
2857 pmap_release_pv(proc_pt_pv, proc_pd_pv, &bulk);
2858 pmap_inval_bulk_flush(&bulk);
2865 * Handle remaining cases. We are holding pt_placemark to lock
2866 * the page table page in the primary pmap while we manipulate
2870 atomic_swap_long(pt, npte);
2871 vm_page_wire_quick(xpv->pv_m); /* shared pt -> proc */
2872 vm_page_wire_quick(proc_pd_pv->pv_m); /* proc pd for sh pt */
2873 atomic_add_long(&pmap->pm_stats.resident_count, 1);
2874 } else if (*pt != npte) {
2875 opte = pmap_inval_smp(pmap, (vm_offset_t)-1, 1, pt, npte);
2878 opte = pte_load_clear(pt);
2879 KKASSERT(opte && opte != npte);
2883 vm_page_wire_quick(xpv->pv_m); /* shared pt -> proc */
2886 * Clean up opte, bump the wire_count for the process
2887 * PD page representing the new entry if it was
2890 * If the entry was not previously empty and we have
2891 * a PT in the proc pmap then opte must match that
2892 * pt. The proc pt must be retired (this is done
2893 * later on in this procedure).
2895 * NOTE: replacing valid pte, wire_count on proc_pd_pv
2898 KKASSERT(opte & pmap->pmap_bits[PG_V_IDX]);
2899 m = PHYS_TO_VM_PAGE(opte & PG_FRAME);
2900 if (vm_page_unwire_quick(m)) {
2901 panic("pmap_allocpte_seg: "
2902 "bad wire count %p",
2908 pmap_softdone(pmap);
2911 * Remove our earmark on the page table page.
2913 pv_placemarker_wakeup(pmap, pt_placemark);
2916 * The existing process page table was replaced and must be destroyed
2929 * Release any resources held by the given physical map.
2931 * Called when a pmap initialized by pmap_pinit is being released. Should
2932 * only be called if the map contains no valid mappings.
2934 struct pmap_release_info {
2940 static int pmap_release_callback(pv_entry_t pv, void *data);
2943 pmap_release(struct pmap *pmap)
2945 struct pmap_release_info info;
2947 KASSERT(CPUMASK_TESTZERO(pmap->pm_active),
2948 ("pmap still active! %016jx",
2949 (uintmax_t)CPUMASK_LOWMASK(pmap->pm_active)));
2952 * There is no longer a pmap_list, if there were we would remove the
2953 * pmap from it here.
2957 * Pull pv's off the RB tree in order from low to high and release
2965 spin_lock(&pmap->pm_spin);
2966 RB_SCAN(pv_entry_rb_tree, &pmap->pm_pvroot, NULL,
2967 pmap_release_callback, &info);
2968 spin_unlock(&pmap->pm_spin);
2972 } while (info.retry);
2976 * One resident page (the pml4 page) should remain. Two if
2977 * the pmap has implemented an isolated userland PML4E table.
2978 * No wired pages should remain.
2980 int expected_res = 0;
2982 if ((pmap->pm_flags & PMAP_FLAG_SIMPLE) == 0)
2984 if (pmap->pm_pmlpv_iso)
2988 if (pmap->pm_stats.resident_count != expected_res ||
2989 pmap->pm_stats.wired_count != 0) {
2990 kprintf("fatal pmap problem - pmap %p flags %08x "
2991 "rescnt=%jd wirecnt=%jd\n",
2994 pmap->pm_stats.resident_count,
2995 pmap->pm_stats.wired_count);
2996 tsleep(pmap, 0, "DEAD", 0);
2999 KKASSERT(pmap->pm_stats.resident_count == expected_res);
3000 KKASSERT(pmap->pm_stats.wired_count == 0);
3005 * Called from low to high. We must cache the proper parent pv so we
3006 * can adjust its wired count.
3009 pmap_release_callback(pv_entry_t pv, void *data)
3011 struct pmap_release_info *info = data;
3012 pmap_t pmap = info->pmap;
3017 * Acquire a held and locked pv, check for release race
3019 pindex = pv->pv_pindex;
3020 if (info->pvp == pv) {
3021 spin_unlock(&pmap->pm_spin);
3023 } else if (pv_hold_try(pv)) {
3024 spin_unlock(&pmap->pm_spin);
3026 spin_unlock(&pmap->pm_spin);
3030 spin_lock(&pmap->pm_spin);
3034 KKASSERT(pv->pv_pmap == pmap && pindex == pv->pv_pindex);
3036 if (pv->pv_pindex < pmap_pt_pindex(0)) {
3038 * I am PTE, parent is PT
3040 pindex = pv->pv_pindex >> NPTEPGSHIFT;
3041 pindex += NUPTE_TOTAL;
3042 } else if (pv->pv_pindex < pmap_pd_pindex(0)) {
3044 * I am PT, parent is PD
3046 pindex = (pv->pv_pindex - NUPTE_TOTAL) >> NPDEPGSHIFT;
3047 pindex += NUPTE_TOTAL + NUPT_TOTAL;
3048 } else if (pv->pv_pindex < pmap_pdp_pindex(0)) {
3050 * I am PD, parent is PDP
3052 pindex = (pv->pv_pindex - NUPTE_TOTAL - NUPT_TOTAL) >>
3054 pindex += NUPTE_TOTAL + NUPT_TOTAL + NUPD_TOTAL;
3055 } else if (pv->pv_pindex < pmap_pml4_pindex()) {
3057 * I am PDP, parent is PML4. We always calculate the
3058 * normal PML4 here, not the isolated PML4.
3060 pindex = pmap_pml4_pindex();
3072 if (info->pvp && info->pvp->pv_pindex != pindex) {
3076 if (info->pvp == NULL)
3077 info->pvp = pv_get(pmap, pindex, NULL);
3084 r = pmap_release_pv(pv, info->pvp, NULL);
3085 spin_lock(&pmap->pm_spin);
3091 * Called with held (i.e. also locked) pv. This function will dispose of
3092 * the lock along with the pv.
3094 * If the caller already holds the locked parent page table for pv it
3095 * must pass it as pvp, allowing us to avoid a deadlock, else it can
3096 * pass NULL for pvp.
3099 pmap_release_pv(pv_entry_t pv, pv_entry_t pvp, pmap_inval_bulk_t *bulk)
3104 * The pmap is currently not spinlocked, pv is held+locked.
3105 * Remove the pv's page from its parent's page table. The
3106 * parent's page table page's wire_count will be decremented.
3108 * This will clean out the pte at any level of the page table.
3109 * If smp != 0 all cpus are affected.
3111 * Do not tear-down recursively, its faster to just let the
3112 * release run its course.
3114 pmap_remove_pv_pte(pv, pvp, bulk, 0);
3117 * Terminal pvs are unhooked from their vm_pages. Because
3118 * terminal pages aren't page table pages they aren't wired
3119 * by us, so we have to be sure not to unwire them either.
3121 if (pv->pv_pindex < pmap_pt_pindex(0)) {
3122 pmap_remove_pv_page(pv);
3127 * We leave the top-level page table page cached, wired, and
3128 * mapped in the pmap until the dtor function (pmap_puninit())
3131 * Since we are leaving the top-level pv intact we need
3132 * to break out of what would otherwise be an infinite loop.
3134 * This covers both the normal and the isolated PML4 page.
3136 if (pv->pv_pindex >= pmap_pml4_pindex()) {
3142 * For page table pages (other than the top-level page),
3143 * remove and free the vm_page. The representitive mapping
3144 * removed above by pmap_remove_pv_pte() did not undo the
3145 * last wire_count so we have to do that as well.
3147 p = pmap_remove_pv_page(pv);
3148 vm_page_busy_wait(p, FALSE, "pmaprl");
3149 if (p->wire_count != 1) {
3150 kprintf("p->wire_count was %016lx %d\n",
3151 pv->pv_pindex, p->wire_count);
3153 KKASSERT(p->wire_count == 1);
3154 KKASSERT(p->flags & PG_UNMANAGED);
3156 vm_page_unwire(p, 0);
3157 KKASSERT(p->wire_count == 0);
3167 * This function will remove the pte associated with a pv from its parent.
3168 * Terminal pv's are supported. All cpus specified by (bulk) are properly
3171 * The wire count will be dropped on the parent page table. The wire
3172 * count on the page being removed (pv->pv_m) from the parent page table
3173 * is NOT touched. Note that terminal pages will not have any additional
3174 * wire counts while page table pages will have at least one representing
3175 * the mapping, plus others representing sub-mappings.
3177 * NOTE: Cannot be called on kernel page table pages, only KVM terminal
3178 * pages and user page table and terminal pages.
3180 * NOTE: The pte being removed might be unmanaged, and the pv supplied might
3181 * be freshly allocated and not imply that the pte is managed. In this
3182 * case pv->pv_m should be NULL.
3184 * The pv must be locked. The pvp, if supplied, must be locked. All
3185 * supplied pv's will remain locked on return.
3187 * XXX must lock parent pv's if they exist to remove pte XXX
3191 pmap_remove_pv_pte(pv_entry_t pv, pv_entry_t pvp, pmap_inval_bulk_t *bulk,
3194 vm_pindex_t ptepindex = pv->pv_pindex;
3195 pmap_t pmap = pv->pv_pmap;
3201 if (ptepindex >= pmap_pml4_pindex()) {
3203 * We are the top level PML4E table, there is no parent.
3205 * This is either the normal or isolated PML4E table.
3206 * Only the normal is used in regular operation, the isolated
3207 * is only passed in when breaking down the whole pmap.
3209 p = pmap->pm_pmlpv->pv_m;
3210 KKASSERT(pv->pv_m == p); /* debugging */
3211 } else if (ptepindex >= pmap_pdp_pindex(0)) {
3213 * Remove a PDP page from the PML4E. This can only occur
3214 * with user page tables. We do not have to lock the
3215 * pml4 PV so just ignore pvp.
3217 vm_pindex_t pml4_pindex;
3218 vm_pindex_t pdp_index;
3220 pml4_entry_t *pdp_iso;
3222 pdp_index = ptepindex - pmap_pdp_pindex(0);
3224 pml4_pindex = pmap_pml4_pindex();
3225 pvp = pv_get(pv->pv_pmap, pml4_pindex, NULL);
3230 pdp = &pmap->pm_pml4[pdp_index & ((1ul << NPML4EPGSHIFT) - 1)];
3231 KKASSERT((*pdp & pmap->pmap_bits[PG_V_IDX]) != 0);
3232 p = PHYS_TO_VM_PAGE(*pdp & PG_FRAME);
3233 pmap_inval_bulk(bulk, (vm_offset_t)-1, pdp, 0);
3236 * Also remove the PDP from the isolated PML4E if the
3239 if (pvp == pmap->pm_pmlpv && pmap->pm_pmlpv_iso) {
3240 pdp_iso = &pmap->pm_pml4_iso[pdp_index &
3241 ((1ul << NPML4EPGSHIFT) - 1)];
3242 pmap_inval_bulk(bulk, (vm_offset_t)-1, pdp_iso, 0);
3244 KKASSERT(pv->pv_m == p); /* debugging */
3245 } else if (ptepindex >= pmap_pd_pindex(0)) {
3247 * Remove a PD page from the PDP
3249 * SIMPLE PMAP NOTE: Non-existant pvp's are ok in the case
3250 * of a simple pmap because it stops at
3253 vm_pindex_t pdp_pindex;
3254 vm_pindex_t pd_index;
3257 pd_index = ptepindex - pmap_pd_pindex(0);
3260 pdp_pindex = NUPTE_TOTAL + NUPT_TOTAL + NUPD_TOTAL +
3261 (pd_index >> NPML4EPGSHIFT);
3262 pvp = pv_get(pv->pv_pmap, pdp_pindex, NULL);
3267 pd = pv_pte_lookup(pvp, pd_index &
3268 ((1ul << NPDPEPGSHIFT) - 1));
3269 KKASSERT((*pd & pmap->pmap_bits[PG_V_IDX]) != 0);
3270 p = PHYS_TO_VM_PAGE(*pd & PG_FRAME);
3271 pmap_inval_bulk(bulk, (vm_offset_t)-1, pd, 0);
3273 KKASSERT(pmap->pm_flags & PMAP_FLAG_SIMPLE);
3274 p = pv->pv_m; /* degenerate test later */
3276 KKASSERT(pv->pv_m == p); /* debugging */
3277 } else if (ptepindex >= pmap_pt_pindex(0)) {
3279 * Remove a PT page from the PD
3281 vm_pindex_t pd_pindex;
3282 vm_pindex_t pt_index;
3285 pt_index = ptepindex - pmap_pt_pindex(0);
3288 pd_pindex = NUPTE_TOTAL + NUPT_TOTAL +
3289 (pt_index >> NPDPEPGSHIFT);
3290 pvp = pv_get(pv->pv_pmap, pd_pindex, NULL);
3295 pt = pv_pte_lookup(pvp, pt_index & ((1ul << NPDPEPGSHIFT) - 1));
3297 KASSERT((*pt & pmap->pmap_bits[PG_V_IDX]) != 0,
3298 ("*pt unexpectedly invalid %016jx "
3299 "gotpvp=%d ptepindex=%ld ptindex=%ld pv=%p pvp=%p",
3300 *pt, gotpvp, ptepindex, pt_index, pv, pvp));
3301 p = PHYS_TO_VM_PAGE(*pt & PG_FRAME);
3303 if ((*pt & pmap->pmap_bits[PG_V_IDX]) == 0) {
3304 kprintf("*pt unexpectedly invalid %016jx "
3305 "gotpvp=%d ptepindex=%ld ptindex=%ld "
3307 *pt, gotpvp, ptepindex, pt_index, pv, pvp);
3308 tsleep(pt, 0, "DEAD", 0);
3311 p = PHYS_TO_VM_PAGE(*pt & PG_FRAME);
3314 pmap_inval_bulk(bulk, (vm_offset_t)-1, pt, 0);
3315 KKASSERT(pv->pv_m == p); /* debugging */
3318 * Remove a PTE from the PT page. The PV might exist even if
3319 * the PTE is not managed, in whichcase pv->pv_m should be
3322 * NOTE: Userland pmaps manage the parent PT/PD/PDP page
3323 * table pages but the kernel_pmap does not.
3325 * NOTE: pv's must be locked bottom-up to avoid deadlocking.
3326 * pv is a pte_pv so we can safely lock pt_pv.
3328 * NOTE: FICTITIOUS pages may have multiple physical mappings
3329 * so PHYS_TO_VM_PAGE() will not necessarily work for
3332 vm_pindex_t pt_pindex;
3337 pt_pindex = ptepindex >> NPTEPGSHIFT;
3338 va = (vm_offset_t)ptepindex << PAGE_SHIFT;
3340 if (ptepindex >= NUPTE_USER) {
3341 ptep = vtopte(ptepindex << PAGE_SHIFT);
3342 KKASSERT(pvp == NULL);
3343 /* pvp remains NULL */
3346 pt_pindex = NUPTE_TOTAL +
3347 (ptepindex >> NPDPEPGSHIFT);
3348 pvp = pv_get(pv->pv_pmap, pt_pindex, NULL);
3352 ptep = pv_pte_lookup(pvp, ptepindex &
3353 ((1ul << NPDPEPGSHIFT) - 1));
3355 pte = pmap_inval_bulk(bulk, va, ptep, 0);
3356 if (bulk == NULL) /* XXX */
3357 cpu_invlpg((void *)va); /* XXX */
3360 * Now update the vm_page_t
3362 if ((pte & pmap->pmap_bits[PG_MANAGED_IDX]) &&
3363 (pte & pmap->pmap_bits[PG_V_IDX])) {
3365 * Valid managed page, adjust (p).
3367 if (pte & pmap->pmap_bits[PG_DEVICE_IDX]) {
3370 p = PHYS_TO_VM_PAGE(pte & PG_FRAME);
3371 KKASSERT(pv->pv_m == p);
3373 if (pte & pmap->pmap_bits[PG_M_IDX]) {
3374 if (pmap_track_modified(ptepindex))
3377 if (pte & pmap->pmap_bits[PG_A_IDX]) {
3378 vm_page_flag_set(p, PG_REFERENCED);
3382 * Unmanaged page, do not try to adjust the vm_page_t.
3383 * pv could be freshly allocated for a pmap_enter(),
3384 * replacing an unmanaged page with a managed one.
3386 * pv->pv_m might reflect the new page and not the
3389 * We could extract p from the physical address and
3390 * adjust it but we explicitly do not for unmanaged
3395 if (pte & pmap->pmap_bits[PG_W_IDX])
3396 atomic_add_long(&pmap->pm_stats.wired_count, -1);
3397 if (pte & pmap->pmap_bits[PG_G_IDX])
3398 cpu_invlpg((void *)va);
3402 * If requested, scrap the underlying pv->pv_m and the underlying
3403 * pv. If this is a page-table-page we must also free the page.
3405 * pvp must be returned locked.
3409 * page table page (PT, PD, PDP, PML4), caller was responsible
3410 * for testing wired_count.
3412 KKASSERT(pv->pv_m->wire_count == 1);
3413 p = pmap_remove_pv_page(pv);
3417 vm_page_busy_wait(p, FALSE, "pgpun");
3418 vm_page_unwire(p, 0);
3419 vm_page_flag_clear(p, PG_MAPPED | PG_WRITEABLE);
3421 } else if (destroy == 2) {
3423 * Normal page, remove from pmap and leave the underlying
3426 pmap_remove_pv_page(pv);
3428 pv = NULL; /* safety */
3432 * If we acquired pvp ourselves then we are responsible for
3433 * recursively deleting it.
3435 if (pvp && gotpvp) {
3437 * Recursively destroy higher-level page tables.
3439 * This is optional. If we do not, they will still
3440 * be destroyed when the process exits.
3442 * NOTE: Do not destroy pv_entry's with extra hold refs,
3443 * a caller may have unlocked it and intends to
3444 * continue to use it.
3446 if (pmap_dynamic_delete &&
3448 pvp->pv_m->wire_count == 1 &&
3449 (pvp->pv_hold & PV_HOLD_MASK) == 2 &&
3450 pvp->pv_pindex < pmap_pml4_pindex()) {
3451 if (pmap_dynamic_delete == 2)
3452 kprintf("A %jd %08x\n", pvp->pv_pindex, pvp->pv_hold);
3453 if (pmap != &kernel_pmap) {
3454 pmap_remove_pv_pte(pvp, NULL, bulk, 1);
3455 pvp = NULL; /* safety */
3457 kprintf("Attempt to remove kernel_pmap pindex "
3458 "%jd\n", pvp->pv_pindex);
3468 * Remove the vm_page association to a pv. The pv must be locked.
3472 pmap_remove_pv_page(pv_entry_t pv)
3477 vm_page_spin_lock(m);
3478 KKASSERT(m && m == pv->pv_m);
3480 if (pv->pv_flags & PV_FLAG_PGTABLE) {
3481 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
3482 KKASSERT(TAILQ_EMPTY(&m->md.pv_list));
3484 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
3485 if (TAILQ_EMPTY(&m->md.pv_list))
3486 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
3488 pmap_page_stats_deleting(m);
3489 vm_page_spin_unlock(m);
3495 * Grow the number of kernel page table entries, if needed.
3497 * This routine is always called to validate any address space
3498 * beyond KERNBASE (for kldloads). kernel_vm_end only governs the address
3499 * space below KERNBASE.
3501 * kernel_map must be locked exclusively by the caller.
3504 pmap_growkernel(vm_offset_t kstart, vm_offset_t kend)
3507 vm_offset_t ptppaddr;
3509 pd_entry_t *pt, newpt;
3510 pdp_entry_t *pd, newpd;
3511 int update_kernel_vm_end;
3514 * bootstrap kernel_vm_end on first real VM use
3516 if (kernel_vm_end == 0) {
3517 kernel_vm_end = VM_MIN_KERNEL_ADDRESS;
3520 pt = pmap_pt(&kernel_pmap, kernel_vm_end);
3523 if ((*pt & kernel_pmap.pmap_bits[PG_V_IDX]) == 0)
3525 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) &
3526 ~(vm_offset_t)(PAGE_SIZE * NPTEPG - 1);
3527 if (kernel_vm_end - 1 >= vm_map_max(&kernel_map)) {
3528 kernel_vm_end = vm_map_max(&kernel_map);
3535 * Fill in the gaps. kernel_vm_end is only adjusted for ranges
3536 * below KERNBASE. Ranges above KERNBASE are kldloaded and we
3537 * do not want to force-fill 128G worth of page tables.
3539 if (kstart < KERNBASE) {
3540 if (kstart > kernel_vm_end)
3541 kstart = kernel_vm_end;
3542 KKASSERT(kend <= KERNBASE);
3543 update_kernel_vm_end = 1;
3545 update_kernel_vm_end = 0;
3548 kstart = rounddown2(kstart, (vm_offset_t)(PAGE_SIZE * NPTEPG));
3549 kend = roundup2(kend, (vm_offset_t)(PAGE_SIZE * NPTEPG));
3551 if (kend - 1 >= vm_map_max(&kernel_map))
3552 kend = vm_map_max(&kernel_map);
3554 while (kstart < kend) {
3555 pt = pmap_pt(&kernel_pmap, kstart);
3558 * We need a new PD entry
3560 nkpg = vm_page_alloc(NULL, mycpu->gd_rand_incr++,
3563 VM_ALLOC_INTERRUPT);
3565 panic("pmap_growkernel: no memory to grow "
3568 paddr = VM_PAGE_TO_PHYS(nkpg);
3569 pmap_zero_page(paddr);
3570 pd = pmap_pd(&kernel_pmap, kstart);
3572 newpd = (pdp_entry_t)
3574 kernel_pmap.pmap_bits[PG_V_IDX] |
3575 kernel_pmap.pmap_bits[PG_RW_IDX] |
3576 kernel_pmap.pmap_bits[PG_A_IDX]);
3577 atomic_swap_long(pd, newpd);
3580 kprintf("NEWPD pd=%p pde=%016jx phys=%016jx\n",
3584 continue; /* try again */
3587 if ((*pt & kernel_pmap.pmap_bits[PG_V_IDX]) != 0) {
3588 kstart = (kstart + PAGE_SIZE * NPTEPG) &
3589 ~(vm_offset_t)(PAGE_SIZE * NPTEPG - 1);
3590 if (kstart - 1 >= vm_map_max(&kernel_map)) {
3591 kstart = vm_map_max(&kernel_map);
3600 * This index is bogus, but out of the way
3602 nkpg = vm_page_alloc(NULL, mycpu->gd_rand_incr++,
3605 VM_ALLOC_INTERRUPT);
3607 panic("pmap_growkernel: no memory to grow kernel");
3610 ptppaddr = VM_PAGE_TO_PHYS(nkpg);
3611 pmap_zero_page(ptppaddr);
3612 newpt = (pd_entry_t)(ptppaddr |
3613 kernel_pmap.pmap_bits[PG_V_IDX] |
3614 kernel_pmap.pmap_bits[PG_RW_IDX] |
3615 kernel_pmap.pmap_bits[PG_A_IDX]);
3616 atomic_swap_long(pt, newpt);
3618 kstart = (kstart + PAGE_SIZE * NPTEPG) &
3619 ~(vm_offset_t)(PAGE_SIZE * NPTEPG - 1);
3621 if (kstart - 1 >= vm_map_max(&kernel_map)) {
3622 kstart = vm_map_max(&kernel_map);
3628 * Only update kernel_vm_end for areas below KERNBASE.
3630 if (update_kernel_vm_end && kernel_vm_end < kstart)
3631 kernel_vm_end = kstart;
3635 * Add a reference to the specified pmap.
3638 pmap_reference(pmap_t pmap)
3641 atomic_add_int(&pmap->pm_count, 1);
3644 /***************************************************
3645 * page management routines.
3646 ***************************************************/
3649 * Hold a pv without locking it
3652 pv_hold(pv_entry_t pv)
3654 atomic_add_int(&pv->pv_hold, 1);
3658 * Hold a pv_entry, preventing its destruction. TRUE is returned if the pv
3659 * was successfully locked, FALSE if it wasn't. The caller must dispose of
3662 * Either the pmap->pm_spin or the related vm_page_spin (if traversing a
3663 * pv list via its page) must be held by the caller in order to stabilize
3667 _pv_hold_try(pv_entry_t pv PMAP_DEBUG_DECL)
3672 * Critical path shortcut expects pv to already have one ref
3673 * (for the pv->pv_pmap).
3675 count = pv->pv_hold;
3678 if ((count & PV_HOLD_LOCKED) == 0) {
3679 if (atomic_fcmpset_int(&pv->pv_hold, &count,
3680 (count + 1) | PV_HOLD_LOCKED)) {
3683 pv->pv_line = lineno;
3688 if (atomic_fcmpset_int(&pv->pv_hold, &count, count + 1))
3696 * Drop a previously held pv_entry which could not be locked, allowing its
3699 * Must not be called with a spinlock held as we might zfree() the pv if it
3700 * is no longer associated with a pmap and this was the last hold count.
3703 pv_drop(pv_entry_t pv)
3708 count = pv->pv_hold;
3710 KKASSERT((count & PV_HOLD_MASK) > 0);
3711 KKASSERT((count & (PV_HOLD_LOCKED | PV_HOLD_MASK)) !=
3712 (PV_HOLD_LOCKED | 1));
3713 if (atomic_cmpset_int(&pv->pv_hold, count, count - 1)) {
3714 if ((count & PV_HOLD_MASK) == 1) {
3716 if (pmap_enter_debug > 0) {
3718 kprintf("pv_drop: free pv %p\n", pv);
3721 KKASSERT(count == 1);
3722 KKASSERT(pv->pv_pmap == NULL);
3732 * Find or allocate the requested PV entry, returning a locked, held pv.
3734 * If (*isnew) is non-zero, the returned pv will have two hold counts, one
3735 * for the caller and one representing the pmap and vm_page association.
3737 * If (*isnew) is zero, the returned pv will have only one hold count.
3739 * Since both associations can only be adjusted while the pv is locked,
3740 * together they represent just one additional hold.
3744 _pv_alloc(pmap_t pmap, vm_pindex_t pindex, int *isnew PMAP_DEBUG_DECL)
3746 struct mdglobaldata *md = mdcpu;
3754 pnew = atomic_swap_ptr((void *)&md->gd_newpv, NULL);
3757 pnew = md->gd_newpv; /* might race NULL */
3758 md->gd_newpv = NULL;
3763 pnew = zalloc(pvzone);
3765 spin_lock_shared(&pmap->pm_spin);
3770 pv = pv_entry_lookup(pmap, pindex);
3775 * Requires exclusive pmap spinlock
3777 if (pmap_excl == 0) {
3779 if (!spin_lock_upgrade_try(&pmap->pm_spin)) {
3780 spin_unlock_shared(&pmap->pm_spin);
3781 spin_lock(&pmap->pm_spin);
3787 * We need to block if someone is holding our
3788 * placemarker. As long as we determine the
3789 * placemarker has not been aquired we do not
3790 * need to get it as acquision also requires
3791 * the pmap spin lock.
3793 * However, we can race the wakeup.
3795 pmark = pmap_placemarker_hash(pmap, pindex);
3797 if (((*pmark ^ pindex) & ~PM_PLACEMARK_WAKEUP) == 0) {
3798 atomic_set_long(pmark, PM_PLACEMARK_WAKEUP);
3799 tsleep_interlock(pmark, 0);
3800 if (((*pmark ^ pindex) &
3801 ~PM_PLACEMARK_WAKEUP) == 0) {
3802 spin_unlock(&pmap->pm_spin);
3803 tsleep(pmark, PINTERLOCKED, "pvplc", 0);
3804 spin_lock(&pmap->pm_spin);
3810 * Setup the new entry
3812 pnew->pv_pmap = pmap;
3813 pnew->pv_pindex = pindex;
3814 pnew->pv_hold = PV_HOLD_LOCKED | 2;
3817 pnew->pv_func = func;
3818 pnew->pv_line = lineno;
3819 if (pnew->pv_line_lastfree > 0) {
3820 pnew->pv_line_lastfree =
3821 -pnew->pv_line_lastfree;
3824 pv = pv_entry_rb_tree_RB_INSERT(&pmap->pm_pvroot, pnew);
3825 atomic_add_long(&pmap->pm_stats.resident_count, 1);
3826 spin_unlock(&pmap->pm_spin);
3829 KASSERT(pv == NULL, ("pv insert failed %p->%p", pnew, pv));
3834 * We already have an entry, cleanup the staged pnew if
3835 * we can get the lock, otherwise block and retry.
3837 if (__predict_true(_pv_hold_try(pv PMAP_DEBUG_COPY))) {
3839 spin_unlock(&pmap->pm_spin);
3841 spin_unlock_shared(&pmap->pm_spin);
3843 pnew = atomic_swap_ptr((void *)&md->gd_newpv, pnew);
3845 zfree(pvzone, pnew);
3848 if (md->gd_newpv == NULL)
3849 md->gd_newpv = pnew;
3851 zfree(pvzone, pnew);
3854 KKASSERT(pv->pv_pmap == pmap &&
3855 pv->pv_pindex == pindex);
3860 spin_unlock(&pmap->pm_spin);
3861 _pv_lock(pv PMAP_DEBUG_COPY);
3863 spin_lock(&pmap->pm_spin);
3865 spin_unlock_shared(&pmap->pm_spin);
3866 _pv_lock(pv PMAP_DEBUG_COPY);
3868 spin_lock_shared(&pmap->pm_spin);
3875 * Find the requested PV entry, returning a locked+held pv or NULL
3879 _pv_get(pmap_t pmap, vm_pindex_t pindex, vm_pindex_t **pmarkp PMAP_DEBUG_DECL)
3884 spin_lock_shared(&pmap->pm_spin);
3889 pv = pv_entry_lookup(pmap, pindex);
3892 * Block if there is ANY placemarker. If we are to
3893 * return it, we must also aquire the spot, so we
3894 * have to block even if the placemarker is held on
3895 * a different address.
3897 * OPTIMIZATION: If pmarkp is passed as NULL the
3898 * caller is just probing (or looking for a real
3899 * pv_entry), and in this case we only need to check
3900 * to see if the placemarker matches pindex.
3905 * Requires exclusive pmap spinlock
3907 if (pmap_excl == 0) {
3909 if (!spin_lock_upgrade_try(&pmap->pm_spin)) {
3910 spin_unlock_shared(&pmap->pm_spin);
3911 spin_lock(&pmap->pm_spin);
3916 pmark = pmap_placemarker_hash(pmap, pindex);
3918 if ((pmarkp && *pmark != PM_NOPLACEMARK) ||
3919 ((*pmark ^ pindex) & ~PM_PLACEMARK_WAKEUP) == 0) {
3920 atomic_set_long(pmark, PM_PLACEMARK_WAKEUP);
3921 tsleep_interlock(pmark, 0);
3922 if ((pmarkp && *pmark != PM_NOPLACEMARK) ||
3923 ((*pmark ^ pindex) &
3924 ~PM_PLACEMARK_WAKEUP) == 0) {
3925 spin_unlock(&pmap->pm_spin);
3926 tsleep(pmark, PINTERLOCKED, "pvpld", 0);
3927 spin_lock(&pmap->pm_spin);
3932 if (atomic_swap_long(pmark, pindex) !=
3934 panic("_pv_get: pmark race");
3938 spin_unlock(&pmap->pm_spin);
3941 if (_pv_hold_try(pv PMAP_DEBUG_COPY)) {
3943 spin_unlock(&pmap->pm_spin);
3945 spin_unlock_shared(&pmap->pm_spin);
3946 KKASSERT(pv->pv_pmap == pmap &&
3947 pv->pv_pindex == pindex);
3951 spin_unlock(&pmap->pm_spin);
3952 _pv_lock(pv PMAP_DEBUG_COPY);
3954 spin_lock(&pmap->pm_spin);
3956 spin_unlock_shared(&pmap->pm_spin);
3957 _pv_lock(pv PMAP_DEBUG_COPY);
3959 spin_lock_shared(&pmap->pm_spin);
3965 * Lookup, hold, and attempt to lock (pmap,pindex).
3967 * If the entry does not exist NULL is returned and *errorp is set to 0
3969 * If the entry exists and could be successfully locked it is returned and
3970 * errorp is set to 0.
3972 * If the entry exists but could NOT be successfully locked it is returned
3973 * held and *errorp is set to 1.
3975 * If the entry is placemarked by someone else NULL is returned and *errorp
3980 pv_get_try(pmap_t pmap, vm_pindex_t pindex, vm_pindex_t **pmarkp, int *errorp)
3984 spin_lock_shared(&pmap->pm_spin);
3986 pv = pv_entry_lookup(pmap, pindex);
3990 pmark = pmap_placemarker_hash(pmap, pindex);
3992 if (((*pmark ^ pindex) & ~PM_PLACEMARK_WAKEUP) == 0) {
3994 } else if (pmarkp &&
3995 atomic_cmpset_long(pmark, PM_NOPLACEMARK, pindex)) {
3999 * Can't set a placemark with a NULL pmarkp, or if
4000 * pmarkp is non-NULL but we failed to set our
4007 spin_unlock_shared(&pmap->pm_spin);
4013 * XXX This has problems if the lock is shared, why?
4015 if (pv_hold_try(pv)) {
4016 spin_unlock_shared(&pmap->pm_spin);
4018 KKASSERT(pv->pv_pmap == pmap && pv->pv_pindex == pindex);
4019 return(pv); /* lock succeeded */
4021 spin_unlock_shared(&pmap->pm_spin);
4024 return (pv); /* lock failed */
4028 * Lock a held pv, keeping the hold count
4032 _pv_lock(pv_entry_t pv PMAP_DEBUG_DECL)
4037 count = pv->pv_hold;
4039 if ((count & PV_HOLD_LOCKED) == 0) {
4040 if (atomic_cmpset_int(&pv->pv_hold, count,
4041 count | PV_HOLD_LOCKED)) {
4044 pv->pv_line = lineno;
4050 tsleep_interlock(pv, 0);
4051 if (atomic_cmpset_int(&pv->pv_hold, count,
4052 count | PV_HOLD_WAITING)) {
4054 if (pmap_enter_debug > 0) {
4056 kprintf("pv waiting on %s:%d\n",
4057 pv->pv_func, pv->pv_line);
4060 tsleep(pv, PINTERLOCKED, "pvwait", hz);
4067 * Unlock a held and locked pv, keeping the hold count.
4071 pv_unlock(pv_entry_t pv)
4076 count = pv->pv_hold;
4078 KKASSERT((count & (PV_HOLD_LOCKED | PV_HOLD_MASK)) >=
4079 (PV_HOLD_LOCKED | 1));
4080 if (atomic_cmpset_int(&pv->pv_hold, count,
4082 ~(PV_HOLD_LOCKED | PV_HOLD_WAITING))) {
4083 if (count & PV_HOLD_WAITING)
4091 * Unlock and drop a pv. If the pv is no longer associated with a pmap
4092 * and the hold count drops to zero we will free it.
4094 * Caller should not hold any spin locks. We are protected from hold races
4095 * by virtue of holds only occuring only with a pmap_spin or vm_page_spin
4096 * lock held. A pv cannot be located otherwise.
4100 pv_put(pv_entry_t pv)
4103 if (pmap_enter_debug > 0) {
4105 kprintf("pv_put pv=%p hold=%08x\n", pv, pv->pv_hold);
4110 * Normal put-aways must have a pv_m associated with the pv,
4111 * but allow the case where the pv has been destructed due
4112 * to pmap_dynamic_delete.
4114 KKASSERT(pv->pv_pmap == NULL || pv->pv_m != NULL);
4117 * Fast - shortcut most common condition
4119 if (atomic_cmpset_int(&pv->pv_hold, PV_HOLD_LOCKED | 2, 1))
4130 * Remove the pmap association from a pv, require that pv_m already be removed,
4131 * then unlock and drop the pv. Any pte operations must have already been
4132 * completed. This call may result in a last-drop which will physically free
4135 * Removing the pmap association entails an additional drop.
4137 * pv must be exclusively locked on call and will be disposed of on return.
4141 _pv_free(pv_entry_t pv, pv_entry_t pvp PMAP_DEBUG_DECL)
4146 pv->pv_func_lastfree = func;
4147 pv->pv_line_lastfree = lineno;
4149 KKASSERT(pv->pv_m == NULL);
4150 KKASSERT((pv->pv_hold & (PV_HOLD_LOCKED|PV_HOLD_MASK)) >=
4151 (PV_HOLD_LOCKED|1));
4152 if ((pmap = pv->pv_pmap) != NULL) {
4153 spin_lock(&pmap->pm_spin);
4154 KKASSERT(pv->pv_pmap == pmap);
4155 if (pmap->pm_pvhint_pt == pv)
4156 pmap->pm_pvhint_pt = NULL;
4157 if (pmap->pm_pvhint_pte == pv)
4158 pmap->pm_pvhint_pte = NULL;
4159 pv_entry_rb_tree_RB_REMOVE(&pmap->pm_pvroot, pv);
4160 atomic_add_long(&pmap->pm_stats.resident_count, -1);
4163 spin_unlock(&pmap->pm_spin);
4166 * Try to shortcut three atomic ops, otherwise fall through
4167 * and do it normally. Drop two refs and the lock all in
4171 vm_page_unwire_quick(pvp->pv_m);
4172 if (atomic_cmpset_int(&pv->pv_hold, PV_HOLD_LOCKED | 2, 0)) {
4174 if (pmap_enter_debug > 0) {
4176 kprintf("pv_free: free pv %p\n", pv);
4182 pv_drop(pv); /* ref for pv_pmap */
4189 * This routine is very drastic, but can save the system
4197 static int warningdone=0;
4199 if (pmap_pagedaemon_waken == 0)
4201 pmap_pagedaemon_waken = 0;
4202 if (warningdone < 5) {
4203 kprintf("pmap_collect: collecting pv entries -- "
4204 "suggest increasing PMAP_SHPGPERPROC\n");
4208 for (i = 0; i < vm_page_array_size; i++) {
4209 m = &vm_page_array[i];
4210 if (m->wire_count || m->hold_count)
4212 if (vm_page_busy_try(m, TRUE) == 0) {
4213 if (m->wire_count == 0 && m->hold_count == 0) {
4222 * Scan the pmap for active page table entries and issue a callback.
4223 * The callback must dispose of pte_pv, whos PTE entry is at *ptep in
4224 * its parent page table.
4226 * pte_pv will be NULL if the page or page table is unmanaged.
4227 * pt_pv will point to the page table page containing the pte for the page.
4229 * NOTE! If we come across an unmanaged page TABLE (verses an unmanaged page),
4230 * we pass a NULL pte_pv and we pass a pt_pv pointing to the passed
4231 * process pmap's PD and page to the callback function. This can be
4232 * confusing because the pt_pv is really a pd_pv, and the target page
4233 * table page is simply aliased by the pmap and not owned by it.
4235 * It is assumed that the start and end are properly rounded to the page size.
4237 * It is assumed that PD pages and above are managed and thus in the RB tree,
4238 * allowing us to use RB_SCAN from the PD pages down for ranged scans.
4240 struct pmap_scan_info {
4244 vm_pindex_t sva_pd_pindex;
4245 vm_pindex_t eva_pd_pindex;
4246 void (*func)(pmap_t, struct pmap_scan_info *,
4247 pv_entry_t, vm_pindex_t *, pv_entry_t,
4249 pt_entry_t *, void *);
4251 pmap_inval_bulk_t bulk_core;
4252 pmap_inval_bulk_t *bulk;
4257 static int pmap_scan_cmp(pv_entry_t pv, void *data);
4258 static int pmap_scan_callback(pv_entry_t pv, void *data);
4261 pmap_scan(struct pmap_scan_info *info, int smp_inval)
4263 struct pmap *pmap = info->pmap;
4264 pv_entry_t pd_pv; /* A page directory PV */
4265 pv_entry_t pt_pv; /* A page table PV */
4266 pv_entry_t pte_pv; /* A page table entry PV */
4267 vm_pindex_t *pte_placemark;
4268 vm_pindex_t *pt_placemark;
4271 struct pv_entry dummy_pv;
4276 if (info->sva == info->eva)
4279 info->bulk = &info->bulk_core;
4280 pmap_inval_bulk_init(&info->bulk_core, pmap);
4286 * Hold the token for stability; if the pmap is empty we have nothing
4290 if (pmap->pm_stats.resident_count == 0) {
4298 * Special handling for scanning one page, which is a very common
4299 * operation (it is?).
4301 * NOTE: Locks must be ordered bottom-up. pte,pt,pd,pdp,pml4
4303 if (info->sva + PAGE_SIZE == info->eva) {
4304 if (info->sva >= VM_MAX_USER_ADDRESS) {
4306 * Kernel mappings do not track wire counts on
4307 * page table pages and only maintain pd_pv and
4308 * pte_pv levels so pmap_scan() works.
4311 pte_pv = pv_get(pmap, pmap_pte_pindex(info->sva),
4313 ptep = vtopte(info->sva);
4316 * User pages which are unmanaged will not have a
4317 * pte_pv. User page table pages which are unmanaged
4318 * (shared from elsewhere) will also not have a pt_pv.
4319 * The func() callback will pass both pte_pv and pt_pv
4320 * as NULL in that case.
4322 * We hold pte_placemark across the operation for
4325 * WARNING! We must hold pt_placemark across the
4326 * *ptep test to prevent misintepreting
4327 * a non-zero *ptep as a shared page
4328 * table page. Hold it across the function
4329 * callback as well for SMP safety.
4331 pte_pv = pv_get(pmap, pmap_pte_pindex(info->sva),
4333 pt_pv = pv_get(pmap, pmap_pt_pindex(info->sva),
4335 if (pt_pv == NULL) {
4336 KKASSERT(pte_pv == NULL);
4337 pd_pv = pv_get(pmap,
4338 pmap_pd_pindex(info->sva),
4341 ptep = pv_pte_lookup(pd_pv,
4342 pmap_pt_index(info->sva));
4344 info->func(pmap, info,
4350 pv_placemarker_wakeup(pmap,
4355 pv_placemarker_wakeup(pmap,
4358 pv_placemarker_wakeup(pmap, pte_placemark);
4361 ptep = pv_pte_lookup(pt_pv, pmap_pte_index(info->sva));
4365 * NOTE: *ptep can't be ripped out from under us if we hold
4366 * pte_pv (or pte_placemark) locked, but bits can
4372 KKASSERT(pte_pv == NULL);
4373 pv_placemarker_wakeup(pmap, pte_placemark);
4374 } else if (pte_pv) {
4375 KASSERT((oldpte & (pmap->pmap_bits[PG_MANAGED_IDX] |
4376 pmap->pmap_bits[PG_V_IDX])) ==
4377 (pmap->pmap_bits[PG_MANAGED_IDX] |
4378 pmap->pmap_bits[PG_V_IDX]),
4379 ("badA *ptep %016lx/%016lx sva %016lx pte_pv %p",
4380 *ptep, oldpte, info->sva, pte_pv));
4381 info->func(pmap, info, pte_pv, NULL, pt_pv, 0,
4382 info->sva, ptep, info->arg);
4384 KASSERT((oldpte & (pmap->pmap_bits[PG_MANAGED_IDX] |
4385 pmap->pmap_bits[PG_V_IDX])) ==
4386 pmap->pmap_bits[PG_V_IDX],
4387 ("badB *ptep %016lx/%016lx sva %016lx pte_pv NULL",
4388 *ptep, oldpte, info->sva));
4389 info->func(pmap, info, NULL, pte_placemark, pt_pv, 0,
4390 info->sva, ptep, info->arg);
4395 pmap_inval_bulk_flush(info->bulk);
4400 * Nominal scan case, RB_SCAN() for PD pages and iterate from
4403 * WARNING! eva can overflow our standard ((N + mask) >> bits)
4404 * bounds, resulting in a pd_pindex of 0. To solve the
4405 * problem we use an inclusive range.
4407 info->sva_pd_pindex = pmap_pd_pindex(info->sva);
4408 info->eva_pd_pindex = pmap_pd_pindex(info->eva - PAGE_SIZE);
4410 if (info->sva >= VM_MAX_USER_ADDRESS) {
4412 * The kernel does not currently maintain any pv_entry's for
4413 * higher-level page tables.
4415 bzero(&dummy_pv, sizeof(dummy_pv));
4416 dummy_pv.pv_pindex = info->sva_pd_pindex;
4417 spin_lock(&pmap->pm_spin);
4418 while (dummy_pv.pv_pindex <= info->eva_pd_pindex) {
4419 pmap_scan_callback(&dummy_pv, info);
4420 ++dummy_pv.pv_pindex;
4421 if (dummy_pv.pv_pindex < info->sva_pd_pindex) /*wrap*/
4424 spin_unlock(&pmap->pm_spin);
4427 * User page tables maintain local PML4, PDP, and PD
4428 * pv_entry's at the very least. PT pv's might be
4429 * unmanaged and thus not exist. PTE pv's might be
4430 * unmanaged and thus not exist.
4432 spin_lock(&pmap->pm_spin);
4433 pv_entry_rb_tree_RB_SCAN(&pmap->pm_pvroot, pmap_scan_cmp,
4434 pmap_scan_callback, info);
4435 spin_unlock(&pmap->pm_spin);
4437 pmap_inval_bulk_flush(info->bulk);
4441 * WARNING! pmap->pm_spin held
4443 * WARNING! eva can overflow our standard ((N + mask) >> bits)
4444 * bounds, resulting in a pd_pindex of 0. To solve the
4445 * problem we use an inclusive range.
4448 pmap_scan_cmp(pv_entry_t pv, void *data)
4450 struct pmap_scan_info *info = data;
4451 if (pv->pv_pindex < info->sva_pd_pindex)
4453 if (pv->pv_pindex > info->eva_pd_pindex)
4459 * pmap_scan() by PDs
4461 * WARNING! pmap->pm_spin held
4464 pmap_scan_callback(pv_entry_t pv, void *data)
4466 struct pmap_scan_info *info = data;
4467 struct pmap *pmap = info->pmap;
4468 pv_entry_t pd_pv; /* A page directory PV */
4469 pv_entry_t pt_pv; /* A page table PV */
4470 vm_pindex_t *pt_placemark;
4475 vm_offset_t va_next;
4476 vm_pindex_t pd_pindex;
4486 * Pull the PD pindex from the pv before releasing the spinlock.
4488 * WARNING: pv is faked for kernel pmap scans.
4490 pd_pindex = pv->pv_pindex;
4491 spin_unlock(&pmap->pm_spin);
4492 pv = NULL; /* invalid after spinlock unlocked */
4495 * Calculate the page range within the PD. SIMPLE pmaps are
4496 * direct-mapped for the entire 2^64 address space. Normal pmaps
4497 * reflect the user and kernel address space which requires
4498 * cannonicalization w/regards to converting pd_pindex's back
4501 sva = (pd_pindex - pmap_pd_pindex(0)) << PDPSHIFT;
4502 if ((pmap->pm_flags & PMAP_FLAG_SIMPLE) == 0 &&
4503 (sva & PML4_SIGNMASK)) {
4504 sva |= PML4_SIGNMASK;
4506 eva = sva + NBPDP; /* can overflow */
4507 if (sva < info->sva)
4509 if (eva < info->sva || eva > info->eva)
4513 * NOTE: kernel mappings do not track page table pages, only
4516 * NOTE: Locks must be ordered bottom-up. pte,pt,pd,pdp,pml4.
4517 * However, for the scan to be efficient we try to
4518 * cache items top-down.
4523 for (; sva < eva; sva = va_next) {
4526 if (sva >= VM_MAX_USER_ADDRESS) {
4535 * PD cache, scan shortcut if it doesn't exist.
4537 if (pd_pv == NULL) {
4538 pd_pv = pv_get(pmap, pmap_pd_pindex(sva), NULL);
4539 } else if (pd_pv->pv_pmap != pmap ||
4540 pd_pv->pv_pindex != pmap_pd_pindex(sva)) {
4542 pd_pv = pv_get(pmap, pmap_pd_pindex(sva), NULL);
4544 if (pd_pv == NULL) {
4545 va_next = (sva + NBPDP) & ~PDPMASK;
4554 * NOTE: The cached pt_pv can be removed from the pmap when
4555 * pmap_dynamic_delete is enabled.
4557 if (pt_pv && (pt_pv->pv_pmap != pmap ||
4558 pt_pv->pv_pindex != pmap_pt_pindex(sva))) {
4562 if (pt_pv == NULL) {
4563 pt_pv = pv_get_try(pmap, pmap_pt_pindex(sva),
4564 &pt_placemark, &error);
4566 pv_put(pd_pv); /* lock order */
4573 pv_placemarker_wait(pmap, pt_placemark);
4578 /* may have to re-check later if pt_pv is NULL here */
4582 * If pt_pv is NULL we either have an shared page table
4583 * page and must issue a callback specific to that case,
4584 * or there is no page table page.
4586 * Either way we can skip the page table page.
4588 * WARNING! pt_pv can also be NULL due to a pv creation
4589 * race where we find it to be NULL and then
4590 * later see a pte_pv. But its possible the pt_pv
4591 * got created inbetween the two operations, so
4594 if (pt_pv == NULL) {
4596 * Possible unmanaged (shared from another pmap)
4599 * WARNING! We must hold pt_placemark across the
4600 * *ptep test to prevent misintepreting
4601 * a non-zero *ptep as a shared page
4602 * table page. Hold it across the function
4603 * callback as well for SMP safety.
4605 ptep = pv_pte_lookup(pd_pv, pmap_pt_index(sva));
4606 if (*ptep & pmap->pmap_bits[PG_V_IDX]) {
4607 info->func(pmap, info, NULL, pt_placemark,
4609 sva, ptep, info->arg);
4611 pv_placemarker_wakeup(pmap, pt_placemark);
4615 * Done, move to next page table page.
4617 va_next = (sva + NBPDR) & ~PDRMASK;
4624 * From this point in the loop testing pt_pv for non-NULL
4625 * means we are in UVM, else if it is NULL we are in KVM.
4627 * Limit our scan to either the end of the va represented
4628 * by the current page table page, or to the end of the
4629 * range being removed.
4632 va_next = (sva + NBPDR) & ~PDRMASK;
4639 * Scan the page table for pages. Some pages may not be
4640 * managed (might not have a pv_entry).
4642 * There is no page table management for kernel pages so
4643 * pt_pv will be NULL in that case, but otherwise pt_pv
4644 * is non-NULL, locked, and referenced.
4648 * At this point a non-NULL pt_pv means a UVA, and a NULL
4649 * pt_pv means a KVA.
4652 ptep = pv_pte_lookup(pt_pv, pmap_pte_index(sva));
4656 while (sva < va_next) {
4658 vm_pindex_t *pte_placemark;
4661 * Yield every 64 pages, stop if requested.
4663 if ((++info->count & 63) == 0)
4669 * We can shortcut our scan if *ptep == 0. This is
4670 * an unlocked check.
4680 * Acquire the related pte_pv, if any. If *ptep == 0
4681 * the related pte_pv should not exist, but if *ptep
4682 * is not zero the pte_pv may or may not exist (e.g.
4683 * will not exist for an unmanaged page).
4685 * However a multitude of races are possible here
4686 * so if we cannot lock definite state we clean out
4687 * our cache and break the inner while() loop to
4688 * force a loop up to the top of the for().
4690 * XXX unlock/relock pd_pv, pt_pv, and re-test their
4691 * validity instead of looping up?
4693 pte_pv = pv_get_try(pmap, pmap_pte_pindex(sva),
4694 &pte_placemark, &error);
4697 pv_put(pd_pv); /* lock order */
4701 pv_put(pt_pv); /* lock order */
4704 if (pte_pv) { /* block */
4709 pv_placemarker_wait(pmap,
4712 va_next = sva; /* retry */
4717 * Reload *ptep after successfully locking the
4718 * pindex. If *ptep == 0 we had better NOT have a
4725 kprintf("Unexpected non-NULL pte_pv "
4727 "*ptep = %016lx/%016lx\n",
4728 pte_pv, pt_pv, *ptep, oldpte);
4729 panic("Unexpected non-NULL pte_pv");
4731 pv_placemarker_wakeup(pmap, pte_placemark);
4739 * We can't hold pd_pv across the callback (because
4740 * we don't pass it to the callback and the callback
4744 vm_page_wire_quick(pd_pv->pv_m);
4749 * Ready for the callback. The locked pte_pv (if any)
4750 * is consumed by the callback. pte_pv will exist if
4751 * the page is managed, and will not exist if it
4754 if (oldpte & pmap->pmap_bits[PG_MANAGED_IDX]) {
4759 (oldpte & pmap->pmap_bits[PG_V_IDX]),
4760 ("badC *ptep %016lx/%016lx sva %016lx "
4762 *ptep, oldpte, sva, pte_pv));
4764 * We must unlock pd_pv across the callback
4765 * to avoid deadlocks on any recursive
4766 * disposal. Re-check that it still exists
4769 * Call target disposes of pte_pv and may
4770 * destroy but will not dispose of pt_pv.
4772 info->func(pmap, info, pte_pv, NULL,
4774 sva, ptep, info->arg);
4779 * We must unlock pd_pv across the callback
4780 * to avoid deadlocks on any recursive
4781 * disposal. Re-check that it still exists
4784 * Call target disposes of pte_pv or
4785 * pte_placemark and may destroy but will
4786 * not dispose of pt_pv.
4788 KASSERT(pte_pv == NULL &&
4789 (oldpte & pmap->pmap_bits[PG_V_IDX]),
4790 ("badD *ptep %016lx/%016lx sva %016lx "
4791 "pte_pv %p pte_pv->pv_m %p ",
4793 pte_pv, (pte_pv ? pte_pv->pv_m : NULL)));
4797 info->func(pmap, info,
4800 sva, ptep, info->arg);
4802 info->func(pmap, info,
4803 NULL, pte_placemark,
4805 sva, ptep, info->arg);
4810 vm_page_unwire_quick(pd_pv->pv_m);
4811 if (pd_pv->pv_pmap == NULL) {
4812 va_next = sva; /* retry */
4818 * NOTE: The cached pt_pv can be removed from the
4819 * pmap when pmap_dynamic_delete is enabled,
4820 * which will cause ptep to become stale.
4822 * This also means that no pages remain under
4823 * the PT, so we can just break out of the inner
4824 * loop and let the outer loop clean everything
4827 if (pt_pv && pt_pv->pv_pmap != pmap)
4842 if ((++info->count & 7) == 0)
4846 * Relock before returning.
4848 spin_lock(&pmap->pm_spin);
4853 pmap_remove(struct pmap *pmap, vm_offset_t sva, vm_offset_t eva)
4855 struct pmap_scan_info info;
4860 info.func = pmap_remove_callback;
4862 pmap_scan(&info, 1);
4865 if (eva - sva < 1024*1024) {
4867 cpu_invlpg((void *)sva);
4875 pmap_remove_noinval(struct pmap *pmap, vm_offset_t sva, vm_offset_t eva)
4877 struct pmap_scan_info info;
4882 info.func = pmap_remove_callback;
4884 pmap_scan(&info, 0);
4888 pmap_remove_callback(pmap_t pmap, struct pmap_scan_info *info,
4889 pv_entry_t pte_pv, vm_pindex_t *pte_placemark,
4890 pv_entry_t pt_pv, int sharept,
4891 vm_offset_t va, pt_entry_t *ptep, void *arg __unused)
4899 * This will also drop pt_pv's wire_count. Note that
4900 * terminal pages are not wired based on mmu presence.
4902 * NOTE: If this is the kernel_pmap, pt_pv can be NULL.
4904 KKASSERT(pte_pv->pv_m != NULL);
4905 pmap_remove_pv_pte(pte_pv, pt_pv, info->bulk, 2);
4906 pte_pv = NULL; /* safety */
4909 * Recursively destroy higher-level page tables.
4911 * This is optional. If we do not, they will still
4912 * be destroyed when the process exits.
4914 * NOTE: Do not destroy pv_entry's with extra hold refs,
4915 * a caller may have unlocked it and intends to
4916 * continue to use it.
4918 if (pmap_dynamic_delete &&
4921 pt_pv->pv_m->wire_count == 1 &&
4922 (pt_pv->pv_hold & PV_HOLD_MASK) == 2 &&
4923 pt_pv->pv_pindex < pmap_pml4_pindex()) {
4924 if (pmap_dynamic_delete == 2)
4925 kprintf("B %jd %08x\n", pt_pv->pv_pindex, pt_pv->pv_hold);
4926 pv_hold(pt_pv); /* extra hold */
4927 pmap_remove_pv_pte(pt_pv, NULL, info->bulk, 1);
4928 pv_lock(pt_pv); /* prior extra hold + relock */
4930 } else if (sharept == 0) {
4932 * Unmanaged pte (pte_placemark is non-NULL)
4934 * pt_pv's wire_count is still bumped by unmanaged pages
4935 * so we must decrement it manually.
4937 * We have to unwire the target page table page.
4939 pte = pmap_inval_bulk(info->bulk, va, ptep, 0);
4940 if (pte & pmap->pmap_bits[PG_W_IDX])
4941 atomic_add_long(&pmap->pm_stats.wired_count, -1);
4942 atomic_add_long(&pmap->pm_stats.resident_count, -1);
4943 if (vm_page_unwire_quick(pt_pv->pv_m))
4944 panic("pmap_remove: insufficient wirecount");
4945 pv_placemarker_wakeup(pmap, pte_placemark);
4948 * Unmanaged page table (pt, pd, or pdp. Not pte) for
4949 * a shared page table.
4951 * pt_pv is actually the pd_pv for our pmap (not the shared
4954 * We have to unwire the target page table page and we
4955 * have to unwire our page directory page.
4957 * It is unclear how we can invalidate a segment so we
4958 * invalidate -1 which invlidates the tlb.
4960 pte = pmap_inval_bulk(info->bulk, (vm_offset_t)-1, ptep, 0);
4961 atomic_add_long(&pmap->pm_stats.resident_count, -1);
4962 KKASSERT((pte & pmap->pmap_bits[PG_DEVICE_IDX]) == 0);
4963 if (vm_page_unwire_quick(PHYS_TO_VM_PAGE(pte & PG_FRAME)))
4964 panic("pmap_remove: shared pgtable1 bad wirecount");
4965 if (vm_page_unwire_quick(pt_pv->pv_m))
4966 panic("pmap_remove: shared pgtable2 bad wirecount");
4967 pv_placemarker_wakeup(pmap, pte_placemark);
4972 * Removes this physical page from all physical maps in which it resides.
4973 * Reflects back modify bits to the pager.
4975 * This routine may not be called from an interrupt.
4979 pmap_remove_all(vm_page_t m)
4982 pmap_inval_bulk_t bulk;
4984 if (!pmap_initialized /* || (m->flags & PG_FICTITIOUS)*/)
4987 vm_page_spin_lock(m);
4988 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
4989 if (pv->pv_m != m) {
4990 kprintf("pmap_remove_all FAILURE\n");
4991 kprintf("pv %p pv->pv_m %p m %p\n", pv, pv->pv_m, m);
4992 kprintf("pvflags %08x\n", pv->pv_flags);
4995 KKASSERT(pv->pv_m == m);
4996 if (pv_hold_try(pv)) {
4997 vm_page_spin_unlock(m);
4999 vm_page_spin_unlock(m);
5002 vm_page_spin_lock(m);
5005 KKASSERT(pv->pv_pmap && pv->pv_m == m);
5008 * Holding no spinlocks, pv is locked. Once we scrap
5009 * pv we can no longer use it as a list iterator (but
5010 * we are doing a TAILQ_FIRST() so we are ok).
5012 pmap_inval_bulk_init(&bulk, pv->pv_pmap);
5013 pmap_remove_pv_pte(pv, NULL, &bulk, 2);
5014 pv = NULL; /* safety */
5015 pmap_inval_bulk_flush(&bulk);
5016 vm_page_spin_lock(m);
5018 KKASSERT((m->flags & (PG_MAPPED|PG_WRITEABLE)) == 0);
5019 vm_page_spin_unlock(m);
5023 * Removes the page from a particular pmap
5026 pmap_remove_specific(pmap_t pmap, vm_page_t m)
5029 pmap_inval_bulk_t bulk;
5031 if (!pmap_initialized)
5035 vm_page_spin_lock(m);
5036 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
5037 if (pv->pv_pmap != pmap)
5039 KKASSERT(pv->pv_m == m);
5040 if (pv_hold_try(pv)) {
5041 vm_page_spin_unlock(m);
5043 vm_page_spin_unlock(m);
5048 KKASSERT(pv->pv_pmap == pmap && pv->pv_m == m);
5051 * Holding no spinlocks, pv is locked. Once gone it can't
5052 * be used as an iterator. In fact, because we couldn't
5053 * necessarily lock it atomically it may have moved within
5054 * the list and ALSO cannot be used as an iterator.
5056 pmap_inval_bulk_init(&bulk, pv->pv_pmap);
5057 pmap_remove_pv_pte(pv, NULL, &bulk, 2);
5058 pv = NULL; /* safety */
5059 pmap_inval_bulk_flush(&bulk);
5062 vm_page_spin_unlock(m);
5066 * Set the physical protection on the specified range of this map
5067 * as requested. This function is typically only used for debug watchpoints
5070 * This function may not be called from an interrupt if the map is
5071 * not the kernel_pmap.
5073 * NOTE! For shared page table pages we just unmap the page.
5076 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
5078 struct pmap_scan_info info;
5079 /* JG review for NX */
5083 if ((prot & (VM_PROT_READ | VM_PROT_EXECUTE)) == VM_PROT_NONE) {
5084 pmap_remove(pmap, sva, eva);
5087 if (prot & VM_PROT_WRITE)
5092 info.func = pmap_protect_callback;
5094 pmap_scan(&info, 1);
5099 pmap_protect_callback(pmap_t pmap, struct pmap_scan_info *info,
5100 pv_entry_t pte_pv, vm_pindex_t *pte_placemark,
5101 pv_entry_t pt_pv, int sharept,
5102 vm_offset_t va, pt_entry_t *ptep, void *arg __unused)
5113 KKASSERT(pte_pv->pv_m != NULL);
5115 if (pbits & pmap->pmap_bits[PG_A_IDX]) {
5116 if ((pbits & pmap->pmap_bits[PG_DEVICE_IDX]) == 0) {
5117 m = PHYS_TO_VM_PAGE(pbits & PG_FRAME);
5118 KKASSERT(m == pte_pv->pv_m);
5119 vm_page_flag_set(m, PG_REFERENCED);
5121 cbits &= ~pmap->pmap_bits[PG_A_IDX];
5123 if (pbits & pmap->pmap_bits[PG_M_IDX]) {
5124 if (pmap_track_modified(pte_pv->pv_pindex)) {
5125 if ((pbits & pmap->pmap_bits[PG_DEVICE_IDX]) == 0) {
5127 m = PHYS_TO_VM_PAGE(pbits &
5132 cbits &= ~pmap->pmap_bits[PG_M_IDX];
5135 } else if (sharept) {
5137 * Unmanaged page table, pt_pv is actually the pd_pv
5138 * for our pmap (not the object's shared pmap).
5140 * When asked to protect something in a shared page table
5141 * page we just unmap the page table page. We have to
5142 * invalidate the tlb in this situation.
5144 * XXX Warning, shared page tables will not be used for
5145 * OBJT_DEVICE or OBJT_MGTDEVICE (PG_FICTITIOUS) mappings
5146 * so PHYS_TO_VM_PAGE() should be safe here.
5148 pte = pmap_inval_smp(pmap, (vm_offset_t)-1, 1, ptep, 0);
5149 if (vm_page_unwire_quick(PHYS_TO_VM_PAGE(pte & PG_FRAME)))
5150 panic("pmap_protect: pgtable1 pg bad wirecount");
5151 if (vm_page_unwire_quick(pt_pv->pv_m))
5152 panic("pmap_protect: pgtable2 pg bad wirecount");
5155 /* else unmanaged page, adjust bits, no wire changes */
5158 cbits &= ~pmap->pmap_bits[PG_RW_IDX];
5160 if (pmap_enter_debug > 0) {
5162 kprintf("pmap_protect va=%lx ptep=%p pte_pv=%p "
5163 "pt_pv=%p cbits=%08lx\n",
5169 if (pbits != cbits) {
5172 xva = (sharept) ? (vm_offset_t)-1 : va;
5173 if (!pmap_inval_smp_cmpset(pmap, xva,
5174 ptep, pbits, cbits)) {
5182 pv_placemarker_wakeup(pmap, pte_placemark);
5186 * Insert the vm_page (m) at the virtual address (va), replacing any prior
5187 * mapping at that address. Set protection and wiring as requested.
5189 * If entry is non-NULL we check to see if the SEG_SIZE optimization is
5190 * possible. If it is we enter the page into the appropriate shared pmap
5191 * hanging off the related VM object instead of the passed pmap, then we
5192 * share the page table page from the VM object's pmap into the current pmap.
5194 * NOTE: This routine MUST insert the page into the pmap now, it cannot
5197 * NOTE: If (m) is PG_UNMANAGED it may also be a temporary fake vm_page_t.
5201 pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
5202 boolean_t wired, vm_map_entry_t entry)
5204 pv_entry_t pt_pv; /* page table */
5205 pv_entry_t pte_pv; /* page table entry */
5206 vm_pindex_t *pte_placemark;
5209 pt_entry_t origpte, newpte;
5214 va = trunc_page(va);
5215 #ifdef PMAP_DIAGNOSTIC
5217 panic("pmap_enter: toobig");
5218 if ((va >= UPT_MIN_ADDRESS) && (va < UPT_MAX_ADDRESS))
5219 panic("pmap_enter: invalid to pmap_enter page table "
5220 "pages (va: 0x%lx)", va);
5222 if (va < UPT_MAX_ADDRESS && pmap == &kernel_pmap) {
5223 kprintf("Warning: pmap_enter called on UVA with "
5226 db_print_backtrace();
5229 if (va >= UPT_MAX_ADDRESS && pmap != &kernel_pmap) {
5230 kprintf("Warning: pmap_enter called on KVA without"
5233 db_print_backtrace();
5238 * Get locked PV entries for our new page table entry (pte_pv or
5239 * pte_placemark) and for its parent page table (pt_pv). We need
5240 * the parent so we can resolve the location of the ptep.
5242 * Only hardware MMU actions can modify the ptep out from
5245 * if (m) is fictitious or unmanaged we do not create a managing
5246 * pte_pv for it. Any pre-existing page's management state must
5247 * match (avoiding code complexity).
5249 * If the pmap is still being initialized we assume existing
5252 * Kernel mapppings do not track page table pages (i.e. pt_pv).
5254 * WARNING! If replacing a managed mapping with an unmanaged mapping
5255 * pte_pv will wind up being non-NULL and must be handled
5258 if (pmap_initialized == FALSE) {
5261 pte_placemark = NULL;
5264 } else if (m->flags & (/*PG_FICTITIOUS |*/ PG_UNMANAGED)) { /* XXX */
5265 pmap_softwait(pmap);
5266 pte_pv = pv_get(pmap, pmap_pte_pindex(va), &pte_placemark);
5267 KKASSERT(pte_pv == NULL);
5268 if (va >= VM_MAX_USER_ADDRESS) {
5272 pt_pv = pmap_allocpte_seg(pmap, pmap_pt_pindex(va),
5274 ptep = pv_pte_lookup(pt_pv, pmap_pte_index(va));
5278 KASSERT(origpte == 0 ||
5279 (origpte & pmap->pmap_bits[PG_MANAGED_IDX]) == 0,
5280 ("Invalid PTE 0x%016jx @ 0x%016jx\n", origpte, va));
5282 pmap_softwait(pmap);
5283 if (va >= VM_MAX_USER_ADDRESS) {
5285 * Kernel map, pv_entry-tracked.
5288 pte_pv = pmap_allocpte(pmap, pmap_pte_pindex(va), NULL);
5294 pte_pv = pmap_allocpte_seg(pmap, pmap_pte_pindex(va),
5296 ptep = pv_pte_lookup(pt_pv, pmap_pte_index(va));
5298 pte_placemark = NULL; /* safety */
5301 KASSERT(origpte == 0 ||
5302 (origpte & pmap->pmap_bits[PG_MANAGED_IDX]),
5303 ("Invalid PTE 0x%016jx @ 0x%016jx\n", origpte, va));
5306 pa = VM_PAGE_TO_PHYS(m);
5307 opa = origpte & PG_FRAME;
5310 * Calculate the new PTE. Note that pte_pv alone does not mean
5311 * the new pte_pv is managed, it could exist because the old pte
5312 * was managed even if the new one is not.
5314 newpte = (pt_entry_t)(pa | pte_prot(pmap, prot) |
5315 pmap->pmap_bits[PG_V_IDX] | pmap->pmap_bits[PG_A_IDX]);
5317 newpte |= pmap->pmap_bits[PG_W_IDX];
5318 if (va < VM_MAX_USER_ADDRESS)
5319 newpte |= pmap->pmap_bits[PG_U_IDX];
5320 if (pte_pv && (m->flags & (/*PG_FICTITIOUS |*/ PG_UNMANAGED)) == 0)
5321 newpte |= pmap->pmap_bits[PG_MANAGED_IDX];
5322 // if (pmap == &kernel_pmap)
5323 // newpte |= pgeflag;
5324 newpte |= pmap->pmap_cache_bits[m->pat_mode];
5325 if (m->flags & PG_FICTITIOUS)
5326 newpte |= pmap->pmap_bits[PG_DEVICE_IDX];
5329 * It is possible for multiple faults to occur in threaded
5330 * environments, the existing pte might be correct.
5332 if (((origpte ^ newpte) &
5333 ~(pt_entry_t)(pmap->pmap_bits[PG_M_IDX] |
5334 pmap->pmap_bits[PG_A_IDX])) == 0) {
5339 * Ok, either the address changed or the protection or wiring
5342 * Clear the current entry, interlocking the removal. For managed
5343 * pte's this will also flush the modified state to the vm_page.
5344 * Atomic ops are mandatory in order to ensure that PG_M events are
5345 * not lost during any transition.
5347 * WARNING: The caller has busied the new page but not the original
5348 * vm_page which we are trying to replace. Because we hold
5349 * the pte_pv lock, but have not busied the page, PG bits
5350 * can be cleared out from under us.
5353 if (origpte & pmap->pmap_bits[PG_MANAGED_IDX]) {
5355 * Old page was managed. Expect pte_pv to exist.
5356 * (it might also exist if the old page was unmanaged).
5358 * NOTE: pt_pv won't exist for a kernel page
5359 * (managed or otherwise).
5361 * NOTE: We may be reusing the pte_pv so we do not
5362 * destroy it in pmap_remove_pv_pte().
5364 KKASSERT(pte_pv && pte_pv->pv_m);
5365 if (prot & VM_PROT_NOSYNC) {
5366 pmap_remove_pv_pte(pte_pv, pt_pv, NULL, 0);
5368 pmap_inval_bulk_t bulk;
5370 pmap_inval_bulk_init(&bulk, pmap);
5371 pmap_remove_pv_pte(pte_pv, pt_pv, &bulk, 0);
5372 pmap_inval_bulk_flush(&bulk);
5374 pmap_remove_pv_page(pte_pv);
5375 /* will either set pte_pv->pv_m or pv_free() later */
5378 * Old page was not managed. If we have a pte_pv
5379 * it better not have a pv_m assigned to it. If the
5380 * new page is managed the pte_pv will be destroyed
5381 * near the end (we need its interlock).
5383 * NOTE: We leave the wire count on the PT page
5384 * intact for the followup enter, but adjust
5385 * the wired-pages count on the pmap.
5387 KKASSERT(pte_pv == NULL);
5388 if (prot & VM_PROT_NOSYNC) {
5390 * NOSYNC (no mmu sync) requested.
5392 (void)pte_load_clear(ptep);
5393 cpu_invlpg((void *)va);
5398 pmap_inval_smp(pmap, va, 1, ptep, 0);
5402 * We must adjust pm_stats manually for unmanaged
5406 atomic_add_long(&pmap->pm_stats.
5407 resident_count, -1);
5409 if (origpte & pmap->pmap_bits[PG_W_IDX]) {
5410 atomic_add_long(&pmap->pm_stats.
5414 KKASSERT(*ptep == 0);
5418 if (pmap_enter_debug > 0) {
5420 kprintf("pmap_enter: va=%lx m=%p origpte=%lx newpte=%lx ptep=%p"
5421 " pte_pv=%p pt_pv=%p opa=%lx prot=%02x\n",
5423 origpte, newpte, ptep,
5424 pte_pv, pt_pv, opa, prot);
5428 if ((newpte & pmap->pmap_bits[PG_MANAGED_IDX]) == 0) {
5430 * Entering an unmanaged page. We must wire the pt_pv unless
5431 * we retained the wiring from an unmanaged page we had
5432 * removed (if we retained it via pte_pv that will go away
5435 if (pt_pv && (opa == 0 ||
5436 (origpte & pmap->pmap_bits[PG_MANAGED_IDX]))) {
5437 vm_page_wire_quick(pt_pv->pv_m);
5440 atomic_add_long(&pmap->pm_stats.wired_count, 1);
5443 * Unmanaged pages need manual resident_count tracking.
5446 atomic_add_long(&pt_pv->pv_pmap->pm_stats.
5449 if (newpte & pmap->pmap_bits[PG_RW_IDX])
5450 vm_page_flag_set(m, PG_WRITEABLE);
5453 * Entering a managed page. Our pte_pv takes care of the
5454 * PT wiring, so if we had removed an unmanaged page before
5457 * We have to take care of the pmap wired count ourselves.
5459 * Enter on the PV list if part of our managed memory.
5462 if (m->object == NULL && pmap_pv_debug > 0) {
5464 kprintf("pte_m %p pv_entry %p NOOBJ\n", m, pte_pv);
5465 print_backtrace(16);
5468 KKASSERT(pte_pv && (pte_pv->pv_m == NULL || pte_pv->pv_m == m));
5469 vm_page_spin_lock(m);
5471 pmap_page_stats_adding(m);
5472 TAILQ_INSERT_TAIL(&m->md.pv_list, pte_pv, pv_list);
5475 * Set vm_page flags. Avoid a cache mastership change if
5476 * the bits are already set.
5478 if ((m->flags & PG_MAPPED) == 0)
5479 vm_page_flag_set(m, PG_MAPPED);
5480 if ((newpte & pmap->pmap_bits[PG_RW_IDX]) &&
5481 (m->flags & PG_WRITEABLE) == 0) {
5482 vm_page_flag_set(m, PG_WRITEABLE);
5484 vm_page_spin_unlock(m);
5487 (origpte & pmap->pmap_bits[PG_MANAGED_IDX]) == 0) {
5488 vm_page_unwire_quick(pt_pv->pv_m);
5492 * Adjust pmap wired pages count for new entry.
5495 atomic_add_long(&pte_pv->pv_pmap->pm_stats.
5501 * Kernel VMAs (pt_pv == NULL) require pmap invalidation interlocks.
5503 * User VMAs do not because those will be zero->non-zero, so no
5504 * stale entries to worry about at this point.
5506 * For KVM there appear to still be issues. Theoretically we
5507 * should be able to scrap the interlocks entirely but we
5510 if ((prot & VM_PROT_NOSYNC) == 0 && pt_pv == NULL) {
5511 pmap_inval_smp(pmap, va, 1, ptep, newpte);
5513 origpte = atomic_swap_long(ptep, newpte);
5514 if (origpte & pmap->pmap_bits[PG_M_IDX]) {
5515 kprintf("pmap [M] race @ %016jx\n", va);
5516 atomic_set_long(ptep, pmap->pmap_bits[PG_M_IDX]);
5519 cpu_invlpg((void *)va);
5526 KKASSERT((newpte & pmap->pmap_bits[PG_MANAGED_IDX]) == 0 ||
5527 (m->flags & PG_MAPPED));
5530 * Cleanup the pv entry, allowing other accessors. If the new page
5531 * is not managed but we have a pte_pv (which was locking our
5532 * operation), we can free it now. pte_pv->pv_m should be NULL.
5534 if (pte_pv && (newpte & pmap->pmap_bits[PG_MANAGED_IDX]) == 0) {
5535 pv_free(pte_pv, pt_pv);
5536 } else if (pte_pv) {
5538 } else if (pte_placemark) {
5539 pv_placemarker_wakeup(pmap, pte_placemark);
5546 * This code works like pmap_enter() but assumes VM_PROT_READ and not-wired.
5547 * This code also assumes that the pmap has no pre-existing entry for this
5550 * This code currently may only be used on user pmaps, not kernel_pmap.
5553 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m)
5555 pmap_enter(pmap, va, m, VM_PROT_READ, FALSE, NULL);
5559 * Make a temporary mapping for a physical address. This is only intended
5560 * to be used for panic dumps.
5562 * The caller is responsible for calling smp_invltlb().
5565 pmap_kenter_temporary(vm_paddr_t pa, long i)
5567 pmap_kenter_quick((vm_offset_t)crashdumpmap + (i * PAGE_SIZE), pa);
5568 return ((void *)crashdumpmap);
5571 #define MAX_INIT_PT (96)
5574 * This routine preloads the ptes for a given object into the specified pmap.
5575 * This eliminates the blast of soft faults on process startup and
5576 * immediately after an mmap.
5578 static int pmap_object_init_pt_callback(vm_page_t p, void *data);
5581 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr, vm_prot_t prot,
5582 vm_object_t object, vm_pindex_t pindex,
5583 vm_size_t size, int limit)
5585 struct rb_vm_page_scan_info info;
5590 * We can't preinit if read access isn't set or there is no pmap
5593 if ((prot & VM_PROT_READ) == 0 || pmap == NULL || object == NULL)
5597 * We can't preinit if the pmap is not the current pmap
5599 lp = curthread->td_lwp;
5600 if (lp == NULL || pmap != vmspace_pmap(lp->lwp_vmspace))
5604 * Misc additional checks
5606 psize = x86_64_btop(size);
5608 if ((object->type != OBJT_VNODE) ||
5609 ((limit & MAP_PREFAULT_PARTIAL) && (psize > MAX_INIT_PT) &&
5610 (object->resident_page_count > MAX_INIT_PT))) {
5614 if (pindex + psize > object->size) {
5615 if (object->size < pindex)
5617 psize = object->size - pindex;
5624 * If everything is segment-aligned do not pre-init here. Instead
5625 * allow the normal vm_fault path to pass a segment hint to
5626 * pmap_enter() which will then use an object-referenced shared
5629 if ((addr & SEG_MASK) == 0 &&
5630 (ctob(psize) & SEG_MASK) == 0 &&
5631 (ctob(pindex) & SEG_MASK) == 0) {
5636 * Use a red-black scan to traverse the requested range and load
5637 * any valid pages found into the pmap.
5639 * We cannot safely scan the object's memq without holding the
5642 info.start_pindex = pindex;
5643 info.end_pindex = pindex + psize - 1;
5648 info.object = object;
5651 * By using the NOLK scan, the callback function must be sure
5652 * to return -1 if the VM page falls out of the object.
5654 vm_object_hold_shared(object);
5655 vm_page_rb_tree_RB_SCAN_NOLK(&object->rb_memq, rb_vm_page_scancmp,
5656 pmap_object_init_pt_callback, &info);
5657 vm_object_drop(object);
5662 pmap_object_init_pt_callback(vm_page_t p, void *data)
5664 struct rb_vm_page_scan_info *info = data;
5665 vm_pindex_t rel_index;
5669 * don't allow an madvise to blow away our really
5670 * free pages allocating pv entries.
5672 if ((info->limit & MAP_PREFAULT_MADVISE) &&
5673 vmstats.v_free_count < vmstats.v_free_reserved) {
5678 * Ignore list markers and ignore pages we cannot instantly
5679 * busy (while holding the object token).
5681 if (p->flags & PG_MARKER)
5686 if (vm_page_busy_try(p, TRUE))
5689 if (vm_page_sbusy_try(p))
5692 if (((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
5693 (p->flags & PG_FICTITIOUS) == 0) {
5694 if ((p->queue - p->pc) == PQ_CACHE) {
5695 if (hard_busy == 0) {
5696 vm_page_sbusy_drop(p);
5700 vm_page_deactivate(p);
5702 rel_index = p->pindex - info->start_pindex;
5703 pmap_enter_quick(info->pmap,
5704 info->addr + x86_64_ptob(rel_index), p);
5709 vm_page_sbusy_drop(p);
5712 * We are using an unlocked scan (that is, the scan expects its
5713 * current element to remain in the tree on return). So we have
5714 * to check here and abort the scan if it isn't.
5716 if (p->object != info->object)
5723 * Return TRUE if the pmap is in shape to trivially pre-fault the specified
5726 * Returns FALSE if it would be non-trivial or if a pte is already loaded
5729 * XXX This is safe only because page table pages are not freed.
5732 pmap_prefault_ok(pmap_t pmap, vm_offset_t addr)
5736 /*spin_lock(&pmap->pm_spin);*/
5737 if ((pte = pmap_pte(pmap, addr)) != NULL) {
5738 if (*pte & pmap->pmap_bits[PG_V_IDX]) {
5739 /*spin_unlock(&pmap->pm_spin);*/
5743 /*spin_unlock(&pmap->pm_spin);*/
5748 * Change the wiring attribute for a pmap/va pair. The mapping must already
5749 * exist in the pmap. The mapping may or may not be managed. The wiring in
5750 * the page is not changed, the page is returned so the caller can adjust
5751 * its wiring (the page is not locked in any way).
5753 * Wiring is not a hardware characteristic so there is no need to invalidate
5754 * TLB. However, in an SMP environment we must use a locked bus cycle to
5755 * update the pte (if we are not using the pmap_inval_*() API that is)...
5756 * it's ok to do this for simple wiring changes.
5759 pmap_unwire(pmap_t pmap, vm_offset_t va)
5770 * Assume elements in the kernel pmap are stable
5772 if (pmap == &kernel_pmap) {
5773 if (pmap_pt(pmap, va) == 0)
5775 ptep = pmap_pte_quick(pmap, va);
5776 if (pmap_pte_v(pmap, ptep)) {
5777 if (pmap_pte_w(pmap, ptep))
5778 atomic_add_long(&pmap->pm_stats.wired_count,-1);
5779 atomic_clear_long(ptep, pmap->pmap_bits[PG_W_IDX]);
5780 pa = *ptep & PG_FRAME;
5781 m = PHYS_TO_VM_PAGE(pa);
5787 * We can only [un]wire pmap-local pages (we cannot wire
5790 pt_pv = pv_get(pmap, pmap_pt_pindex(va), NULL);
5794 ptep = pv_pte_lookup(pt_pv, pmap_pte_index(va));
5795 if ((*ptep & pmap->pmap_bits[PG_V_IDX]) == 0) {
5800 if (pmap_pte_w(pmap, ptep)) {
5801 atomic_add_long(&pt_pv->pv_pmap->pm_stats.wired_count,
5804 /* XXX else return NULL so caller doesn't unwire m ? */
5806 atomic_clear_long(ptep, pmap->pmap_bits[PG_W_IDX]);
5808 pa = *ptep & PG_FRAME;
5809 m = PHYS_TO_VM_PAGE(pa); /* held by wired count */
5816 * Copy the range specified by src_addr/len from the source map to
5817 * the range dst_addr/len in the destination map.
5819 * This routine is only advisory and need not do anything.
5822 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr,
5823 vm_size_t len, vm_offset_t src_addr)
5830 * Zero the specified physical page.
5832 * This function may be called from an interrupt and no locking is
5836 pmap_zero_page(vm_paddr_t phys)
5838 vm_offset_t va = PHYS_TO_DMAP(phys);
5840 pagezero((void *)va);
5846 * Zero part of a physical page by mapping it into memory and clearing
5847 * its contents with bzero.
5849 * off and size may not cover an area beyond a single hardware page.
5852 pmap_zero_page_area(vm_paddr_t phys, int off, int size)
5854 vm_offset_t virt = PHYS_TO_DMAP(phys);
5856 bzero((char *)virt + off, size);
5862 * Copy the physical page from the source PA to the target PA.
5863 * This function may be called from an interrupt. No locking
5867 pmap_copy_page(vm_paddr_t src, vm_paddr_t dst)
5869 vm_offset_t src_virt, dst_virt;
5871 src_virt = PHYS_TO_DMAP(src);
5872 dst_virt = PHYS_TO_DMAP(dst);
5873 bcopy((void *)src_virt, (void *)dst_virt, PAGE_SIZE);
5877 * pmap_copy_page_frag:
5879 * Copy the physical page from the source PA to the target PA.
5880 * This function may be called from an interrupt. No locking
5884 pmap_copy_page_frag(vm_paddr_t src, vm_paddr_t dst, size_t bytes)
5886 vm_offset_t src_virt, dst_virt;
5888 src_virt = PHYS_TO_DMAP(src);
5889 dst_virt = PHYS_TO_DMAP(dst);
5891 bcopy((char *)src_virt + (src & PAGE_MASK),
5892 (char *)dst_virt + (dst & PAGE_MASK),
5897 * Returns true if the pmap's pv is one of the first 16 pvs linked to from
5898 * this page. This count may be changed upwards or downwards in the future;
5899 * it is only necessary that true be returned for a small subset of pmaps
5900 * for proper page aging.
5903 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
5908 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
5911 vm_page_spin_lock(m);
5912 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
5913 if (pv->pv_pmap == pmap) {
5914 vm_page_spin_unlock(m);
5921 vm_page_spin_unlock(m);
5926 * Remove all pages from specified address space this aids process exit
5927 * speeds. Also, this code may be special cased for the current process
5931 pmap_remove_pages(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
5933 pmap_remove_noinval(pmap, sva, eva);
5938 * pmap_testbit tests bits in pte's note that the testbit/clearbit
5939 * routines are inline, and a lot of things compile-time evaluate.
5944 pmap_testbit(vm_page_t m, int bit)
5950 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
5953 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
5955 vm_page_spin_lock(m);
5956 if (TAILQ_FIRST(&m->md.pv_list) == NULL) {
5957 vm_page_spin_unlock(m);
5961 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
5962 #if defined(PMAP_DIAGNOSTIC)
5963 if (pv->pv_pmap == NULL) {
5964 kprintf("Null pmap (tb) at pindex: %"PRIu64"\n",
5972 * If the bit being tested is the modified bit, then
5973 * mark clean_map and ptes as never
5976 * WARNING! Because we do not lock the pv, *pte can be in a
5977 * state of flux. Despite this the value of *pte
5978 * will still be related to the vm_page in some way
5979 * because the pv cannot be destroyed as long as we
5980 * hold the vm_page spin lock.
5982 if (bit == PG_A_IDX || bit == PG_M_IDX) {
5983 //& (pmap->pmap_bits[PG_A_IDX] | pmap->pmap_bits[PG_M_IDX])) {
5984 if (!pmap_track_modified(pv->pv_pindex))
5988 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_pindex << PAGE_SHIFT);
5989 if (*pte & pmap->pmap_bits[bit]) {
5990 vm_page_spin_unlock(m);
5994 vm_page_spin_unlock(m);
5999 * This routine is used to modify bits in ptes. Only one bit should be
6000 * specified. PG_RW requires special handling.
6002 * Caller must NOT hold any spin locks
6006 pmap_clearbit(vm_page_t m, int bit_index)
6013 if (!pmap_initialized || (m->flags & PG_FICTITIOUS)) {
6014 if (bit_index == PG_RW_IDX)
6015 vm_page_flag_clear(m, PG_WRITEABLE);
6022 * Loop over all current mappings setting/clearing as appropos If
6023 * setting RO do we need to clear the VAC?
6025 * NOTE: When clearing PG_M we could also (not implemented) drop
6026 * through to the PG_RW code and clear PG_RW too, forcing
6027 * a fault on write to redetect PG_M for virtual kernels, but
6028 * it isn't necessary since virtual kernels invalidate the
6029 * pte when they clear the VPTE_M bit in their virtual page
6032 * NOTE: Does not re-dirty the page when clearing only PG_M.
6034 * NOTE: Because we do not lock the pv, *pte can be in a state of
6035 * flux. Despite this the value of *pte is still somewhat
6036 * related while we hold the vm_page spin lock.
6038 * *pte can be zero due to this race. Since we are clearing
6039 * bits we basically do no harm when this race occurs.
6041 if (bit_index != PG_RW_IDX) {
6042 vm_page_spin_lock(m);
6043 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
6044 #if defined(PMAP_DIAGNOSTIC)
6045 if (pv->pv_pmap == NULL) {
6046 kprintf("Null pmap (cb) at pindex: %"PRIu64"\n",
6052 pte = pmap_pte_quick(pv->pv_pmap,
6053 pv->pv_pindex << PAGE_SHIFT);
6055 if (pbits & pmap->pmap_bits[bit_index])
6056 atomic_clear_long(pte, pmap->pmap_bits[bit_index]);
6058 vm_page_spin_unlock(m);
6063 * Clear PG_RW. Also clears PG_M and marks the page dirty if PG_M
6067 vm_page_spin_lock(m);
6068 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
6070 * don't write protect pager mappings
6072 if (!pmap_track_modified(pv->pv_pindex))
6075 #if defined(PMAP_DIAGNOSTIC)
6076 if (pv->pv_pmap == NULL) {
6077 kprintf("Null pmap (cb) at pindex: %"PRIu64"\n",
6085 * Skip pages which do not have PG_RW set.
6087 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_pindex << PAGE_SHIFT);
6088 if ((*pte & pmap->pmap_bits[PG_RW_IDX]) == 0)
6092 * We must lock the PV to be able to safely test the pte.
6094 if (pv_hold_try(pv)) {
6095 vm_page_spin_unlock(m);
6097 vm_page_spin_unlock(m);
6098 pv_lock(pv); /* held, now do a blocking lock */
6104 * Reload pte after acquiring pv.
6106 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_pindex << PAGE_SHIFT);
6108 if ((*pte & pmap->pmap_bits[PG_RW_IDX]) == 0) {
6114 KKASSERT(pv->pv_pmap == pmap && pv->pv_m == m);
6120 nbits = pbits & ~(pmap->pmap_bits[PG_RW_IDX] |
6121 pmap->pmap_bits[PG_M_IDX]);
6122 if (pmap_inval_smp_cmpset(pmap,
6123 ((vm_offset_t)pv->pv_pindex << PAGE_SHIFT),
6124 pte, pbits, nbits)) {
6131 * If PG_M was found to be set while we were clearing PG_RW
6132 * we also clear PG_M (done above) and mark the page dirty.
6133 * Callers expect this behavior.
6135 * we lost pv so it cannot be used as an iterator. In fact,
6136 * because we couldn't necessarily lock it atomically it may
6137 * have moved within the list and ALSO cannot be used as an
6140 vm_page_spin_lock(m);
6141 if (pbits & pmap->pmap_bits[PG_M_IDX])
6143 vm_page_spin_unlock(m);
6147 if (bit_index == PG_RW_IDX)
6148 vm_page_flag_clear(m, PG_WRITEABLE);
6149 vm_page_spin_unlock(m);
6153 * Lower the permission for all mappings to a given page.
6155 * Page must be busied by caller. Because page is busied by caller this
6156 * should not be able to race a pmap_enter().
6159 pmap_page_protect(vm_page_t m, vm_prot_t prot)
6161 /* JG NX support? */
6162 if ((prot & VM_PROT_WRITE) == 0) {
6163 if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) {
6165 * NOTE: pmap_clearbit(.. PG_RW) also clears
6166 * the PG_WRITEABLE flag in (m).
6168 pmap_clearbit(m, PG_RW_IDX);
6176 pmap_phys_address(vm_pindex_t ppn)
6178 return (x86_64_ptob(ppn));
6182 * Return a count of reference bits for a page, clearing those bits.
6183 * It is not necessary for every reference bit to be cleared, but it
6184 * is necessary that 0 only be returned when there are truly no
6185 * reference bits set.
6187 * XXX: The exact number of bits to check and clear is a matter that
6188 * should be tested and standardized at some point in the future for
6189 * optimal aging of shared pages.
6191 * This routine may not block.
6194 pmap_ts_referenced(vm_page_t m)
6201 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
6204 vm_page_spin_lock(m);
6205 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
6206 if (!pmap_track_modified(pv->pv_pindex))
6209 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_pindex << PAGE_SHIFT);
6210 if (pte && (*pte & pmap->pmap_bits[PG_A_IDX])) {
6211 atomic_clear_long(pte, pmap->pmap_bits[PG_A_IDX]);
6217 vm_page_spin_unlock(m);
6224 * Return whether or not the specified physical page was modified
6225 * in any physical maps.
6228 pmap_is_modified(vm_page_t m)
6232 res = pmap_testbit(m, PG_M_IDX);
6237 * Clear the modify bits on the specified physical page.
6240 pmap_clear_modify(vm_page_t m)
6242 pmap_clearbit(m, PG_M_IDX);
6246 * pmap_clear_reference:
6248 * Clear the reference bit on the specified physical page.
6251 pmap_clear_reference(vm_page_t m)
6253 pmap_clearbit(m, PG_A_IDX);
6257 * Miscellaneous support routines follow
6262 x86_64_protection_init(void)
6268 * NX supported? (boot time loader.conf override only)
6270 * -1 Automatic (sets mode 1)
6272 * 1 NX implemented, differentiates PROT_READ vs PROT_READ|PROT_EXEC
6273 * 2 NX implemented for all cases
6275 TUNABLE_INT_FETCH("machdep.pmap_nx_enable", &pmap_nx_enable);
6276 if ((amd_feature & AMDID_NX) == 0) {
6277 pmap_bits_default[PG_NX_IDX] = 0;
6279 } else if (pmap_nx_enable < 0) {
6280 pmap_nx_enable = 1; /* default to mode 1 (READ) */
6284 * 0 is basically read-only access, but also set the NX (no-execute)
6285 * bit when VM_PROT_EXECUTE is not specified.
6287 kp = protection_codes;
6288 for (prot = 0; prot < PROTECTION_CODES_SIZE; prot++) {
6290 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_NONE:
6292 * This case handled elsewhere
6296 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_NONE:
6298 * Read-only is 0|NX (pmap_nx_enable mode >= 1)
6300 if (pmap_nx_enable >= 1)
6301 *kp = pmap_bits_default[PG_NX_IDX];
6303 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_EXECUTE:
6304 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_EXECUTE:
6306 * Execute requires read access
6310 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_NONE:
6311 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_NONE:
6313 * Write without execute is RW|NX
6314 * (pmap_nx_enable mode >= 2)
6316 *kp = pmap_bits_default[PG_RW_IDX];
6317 if (pmap_nx_enable >= 2)
6318 *kp |= pmap_bits_default[PG_NX_IDX];
6320 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE:
6321 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_EXECUTE:
6323 * Write with execute is RW
6325 *kp = pmap_bits_default[PG_RW_IDX];
6333 * Map a set of physical memory pages into the kernel virtual
6334 * address space. Return a pointer to where it is mapped. This
6335 * routine is intended to be used for mapping device memory,
6338 * NOTE: We can't use pgeflag unless we invalidate the pages one at
6341 * NOTE: The PAT attributes {WRITE_BACK, WRITE_THROUGH, UNCACHED, UNCACHEABLE}
6342 * work whether the cpu supports PAT or not. The remaining PAT
6343 * attributes {WRITE_PROTECTED, WRITE_COMBINING} only work if the cpu
6347 pmap_mapdev(vm_paddr_t pa, vm_size_t size)
6349 return(pmap_mapdev_attr(pa, size, PAT_WRITE_BACK));
6353 pmap_mapdev_uncacheable(vm_paddr_t pa, vm_size_t size)
6355 return(pmap_mapdev_attr(pa, size, PAT_UNCACHEABLE));
6359 pmap_mapbios(vm_paddr_t pa, vm_size_t size)
6361 return (pmap_mapdev_attr(pa, size, PAT_WRITE_BACK));
6365 * Map a set of physical memory pages into the kernel virtual
6366 * address space. Return a pointer to where it is mapped. This
6367 * routine is intended to be used for mapping device memory,
6371 pmap_mapdev_attr(vm_paddr_t pa, vm_size_t size, int mode)
6373 vm_offset_t va, tmpva, offset;
6377 offset = pa & PAGE_MASK;
6378 size = roundup(offset + size, PAGE_SIZE);
6380 va = kmem_alloc_nofault(&kernel_map, size, VM_SUBSYS_MAPDEV, PAGE_SIZE);
6382 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
6384 pa = pa & ~PAGE_MASK;
6385 for (tmpva = va, tmpsize = size; tmpsize > 0;) {
6386 pte = vtopte(tmpva);
6388 kernel_pmap.pmap_bits[PG_RW_IDX] |
6389 kernel_pmap.pmap_bits[PG_V_IDX] | /* pgeflag | */
6390 kernel_pmap.pmap_cache_bits[mode];
6391 tmpsize -= PAGE_SIZE;
6395 pmap_invalidate_range(&kernel_pmap, va, va + size);
6396 pmap_invalidate_cache_range(va, va + size);
6398 return ((void *)(va + offset));
6402 pmap_unmapdev(vm_offset_t va, vm_size_t size)
6404 vm_offset_t base, offset;
6406 base = va & ~PAGE_MASK;
6407 offset = va & PAGE_MASK;
6408 size = roundup(offset + size, PAGE_SIZE);
6409 pmap_qremove(va, size >> PAGE_SHIFT);
6410 kmem_free(&kernel_map, base, size);
6414 * Sets the memory attribute for the specified page.
6417 pmap_page_set_memattr(vm_page_t m, vm_memattr_t ma)
6423 * If "m" is a normal page, update its direct mapping. This update
6424 * can be relied upon to perform any cache operations that are
6425 * required for data coherence.
6427 if ((m->flags & PG_FICTITIOUS) == 0)
6428 pmap_change_attr(PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m)), 1, m->pat_mode);
6432 * Change the PAT attribute on an existing kernel memory map. Caller
6433 * must ensure that the virtual memory in question is not accessed
6434 * during the adjustment.
6437 pmap_change_attr(vm_offset_t va, vm_size_t count, int mode)
6444 panic("pmap_change_attr: va is NULL");
6445 base = trunc_page(va);
6449 *pte = (*pte & ~(pt_entry_t)(kernel_pmap.pmap_cache_mask)) |
6450 kernel_pmap.pmap_cache_bits[mode];
6455 changed = 1; /* XXX: not optimal */
6458 * Flush CPU caches if required to make sure any data isn't cached that
6459 * shouldn't be, etc.
6462 pmap_invalidate_range(&kernel_pmap, base, va);
6463 pmap_invalidate_cache_range(base, va);
6468 * perform the pmap work for mincore
6471 pmap_mincore(pmap_t pmap, vm_offset_t addr)
6473 pt_entry_t *ptep, pte;
6477 ptep = pmap_pte(pmap, addr);
6479 if (ptep && (pte = *ptep) != 0) {
6482 val = MINCORE_INCORE;
6483 if ((pte & pmap->pmap_bits[PG_MANAGED_IDX]) == 0)
6486 pa = pte & PG_FRAME;
6488 if (pte & pmap->pmap_bits[PG_DEVICE_IDX])
6491 m = PHYS_TO_VM_PAGE(pa);
6496 if (pte & pmap->pmap_bits[PG_M_IDX])
6497 val |= MINCORE_MODIFIED|MINCORE_MODIFIED_OTHER;
6499 * Modified by someone
6501 else if (m && (m->dirty || pmap_is_modified(m)))
6502 val |= MINCORE_MODIFIED_OTHER;
6506 if (pte & pmap->pmap_bits[PG_A_IDX])
6507 val |= MINCORE_REFERENCED|MINCORE_REFERENCED_OTHER;
6510 * Referenced by someone
6512 else if (m && ((m->flags & PG_REFERENCED) ||
6513 pmap_ts_referenced(m))) {
6514 val |= MINCORE_REFERENCED_OTHER;
6515 vm_page_flag_set(m, PG_REFERENCED);
6524 * Replace p->p_vmspace with a new one. If adjrefs is non-zero the new
6525 * vmspace will be ref'd and the old one will be deref'd.
6527 * The vmspace for all lwps associated with the process will be adjusted
6528 * and cr3 will be reloaded if any lwp is the current lwp.
6530 * The process must hold the vmspace->vm_map.token for oldvm and newvm
6533 pmap_replacevm(struct proc *p, struct vmspace *newvm, int adjrefs)
6535 struct vmspace *oldvm;
6538 oldvm = p->p_vmspace;
6539 if (oldvm != newvm) {
6542 p->p_vmspace = newvm;
6543 KKASSERT(p->p_nthreads == 1);
6544 lp = RB_ROOT(&p->p_lwp_tree);
6545 pmap_setlwpvm(lp, newvm);
6552 * Set the vmspace for a LWP. The vmspace is almost universally set the
6553 * same as the process vmspace, but virtual kernels need to swap out contexts
6554 * on a per-lwp basis.
6556 * Caller does not necessarily hold any vmspace tokens. Caller must control
6557 * the lwp (typically be in the context of the lwp). We use a critical
6558 * section to protect against statclock and hardclock (statistics collection).
6561 pmap_setlwpvm(struct lwp *lp, struct vmspace *newvm)
6563 struct vmspace *oldvm;
6567 oldvm = lp->lwp_vmspace;
6569 if (oldvm != newvm) {
6572 KKASSERT((newvm->vm_refcnt & VM_REF_DELETED) == 0);
6573 lp->lwp_vmspace = newvm;
6574 if (td->td_lwp == lp) {
6575 pmap = vmspace_pmap(newvm);
6576 ATOMIC_CPUMASK_ORBIT(pmap->pm_active, mycpu->gd_cpuid);
6577 if (pmap->pm_active_lock & CPULOCK_EXCL)
6578 pmap_interlock_wait(newvm);
6579 #if defined(SWTCH_OPTIM_STATS)
6582 if (pmap->pmap_bits[TYPE_IDX] == REGULAR_PMAP) {
6583 td->td_pcb->pcb_cr3 = vtophys(pmap->pm_pml4);
6584 if (meltdown_mitigation && pmap->pm_pmlpv_iso) {
6585 td->td_pcb->pcb_cr3_iso =
6586 vtophys(pmap->pm_pml4_iso);
6587 td->td_pcb->pcb_flags |= PCB_ISOMMU;
6589 td->td_pcb->pcb_cr3_iso = 0;
6590 td->td_pcb->pcb_flags &= ~PCB_ISOMMU;
6592 } else if (pmap->pmap_bits[TYPE_IDX] == EPT_PMAP) {
6593 td->td_pcb->pcb_cr3 = KPML4phys;
6594 td->td_pcb->pcb_cr3_iso = 0;
6595 td->td_pcb->pcb_flags &= ~PCB_ISOMMU;
6597 panic("pmap_setlwpvm: unknown pmap type\n");
6601 * The MMU separation fields needs to be updated.
6602 * (it can't access the pcb directly from the
6603 * restricted user pmap).
6606 struct trampframe *tramp;
6608 tramp = &pscpu->trampoline;
6609 tramp->tr_pcb_cr3 = td->td_pcb->pcb_cr3;
6610 tramp->tr_pcb_cr3_iso = td->td_pcb->pcb_cr3_iso;
6611 tramp->tr_pcb_flags = td->td_pcb->pcb_flags;
6612 tramp->tr_pcb_rsp = (register_t)td->td_pcb;
6613 /* tr_pcb_rsp doesn't change */
6617 * In kernel-land we always use the normal PML4E
6618 * so the kernel is fully mapped and can also access
6621 load_cr3(td->td_pcb->pcb_cr3);
6622 pmap = vmspace_pmap(oldvm);
6623 ATOMIC_CPUMASK_NANDBIT(pmap->pm_active,
6631 * Called when switching to a locked pmap, used to interlock against pmaps
6632 * undergoing modifications to prevent us from activating the MMU for the
6633 * target pmap until all such modifications have completed. We have to do
6634 * this because the thread making the modifications has already set up its
6635 * SMP synchronization mask.
6637 * This function cannot sleep!
6642 pmap_interlock_wait(struct vmspace *vm)
6644 struct pmap *pmap = &vm->vm_pmap;
6646 if (pmap->pm_active_lock & CPULOCK_EXCL) {
6648 KKASSERT(curthread->td_critcount >= 2);
6649 DEBUG_PUSH_INFO("pmap_interlock_wait");
6650 while (pmap->pm_active_lock & CPULOCK_EXCL) {
6652 lwkt_process_ipiq();
6660 pmap_addr_hint(vm_object_t obj, vm_offset_t addr, vm_size_t size)
6663 if ((obj == NULL) || (size < NBPDR) ||
6664 ((obj->type != OBJT_DEVICE) && (obj->type != OBJT_MGTDEVICE))) {
6668 addr = roundup2(addr, NBPDR);
6673 * Used by kmalloc/kfree, page already exists at va
6676 pmap_kvtom(vm_offset_t va)
6678 pt_entry_t *ptep = vtopte(va);
6680 KKASSERT((*ptep & kernel_pmap.pmap_bits[PG_DEVICE_IDX]) == 0);
6681 return(PHYS_TO_VM_PAGE(*ptep & PG_FRAME));
6685 * Initialize machine-specific shared page directory support. This
6686 * is executed when a VM object is created.
6689 pmap_object_init(vm_object_t object)
6691 object->md.pmap_rw = NULL;
6692 object->md.pmap_ro = NULL;
6696 * Clean up machine-specific shared page directory support. This
6697 * is executed when a VM object is destroyed.
6700 pmap_object_free(vm_object_t object)
6704 if ((pmap = object->md.pmap_rw) != NULL) {
6705 object->md.pmap_rw = NULL;
6706 pmap_remove_noinval(pmap,
6707 VM_MIN_USER_ADDRESS, VM_MAX_USER_ADDRESS);
6708 CPUMASK_ASSZERO(pmap->pm_active);
6711 kfree(pmap, M_OBJPMAP);
6713 if ((pmap = object->md.pmap_ro) != NULL) {
6714 object->md.pmap_ro = NULL;
6715 pmap_remove_noinval(pmap,
6716 VM_MIN_USER_ADDRESS, VM_MAX_USER_ADDRESS);
6717 CPUMASK_ASSZERO(pmap->pm_active);
6720 kfree(pmap, M_OBJPMAP);
6725 * pmap_pgscan_callback - Used by pmap_pgscan to acquire the related
6726 * VM page and issue a pginfo->callback.
6728 * We are expected to dispose of any non-NULL pte_pv.
6732 pmap_pgscan_callback(pmap_t pmap, struct pmap_scan_info *info,
6733 pv_entry_t pte_pv, vm_pindex_t *pte_placemark,
6734 pv_entry_t pt_pv, int sharept,
6735 vm_offset_t va, pt_entry_t *ptep, void *arg)
6737 struct pmap_pgscan_info *pginfo = arg;
6742 * Try to busy the page while we hold the pte_pv locked.
6744 KKASSERT(pte_pv->pv_m);
6745 m = PHYS_TO_VM_PAGE(*ptep & PG_FRAME);
6746 if (vm_page_busy_try(m, TRUE) == 0) {
6747 if (m == PHYS_TO_VM_PAGE(*ptep & PG_FRAME)) {
6749 * The callback is issued with the pte_pv
6750 * unlocked and put away, and the pt_pv
6755 vm_page_wire_quick(pt_pv->pv_m);
6758 if (pginfo->callback(pginfo, va, m) < 0)
6762 vm_page_unwire_quick(pt_pv->pv_m);
6769 ++pginfo->busycount;
6774 * Shared page table or unmanaged page (sharept or !sharept)
6776 pv_placemarker_wakeup(pmap, pte_placemark);
6781 pmap_pgscan(struct pmap_pgscan_info *pginfo)
6783 struct pmap_scan_info info;
6785 pginfo->offset = pginfo->beg_addr;
6786 info.pmap = pginfo->pmap;
6787 info.sva = pginfo->beg_addr;
6788 info.eva = pginfo->end_addr;
6789 info.func = pmap_pgscan_callback;
6791 pmap_scan(&info, 0);
6793 pginfo->offset = pginfo->end_addr;
6797 * Wait for a placemarker that we do not own to clear. The placemarker
6798 * in question is not necessarily set to the pindex we want, we may have
6799 * to wait on the element because we want to reserve it ourselves.
6801 * NOTE: PM_PLACEMARK_WAKEUP sets a bit which is already set in
6802 * PM_NOPLACEMARK, so it does not interfere with placemarks
6803 * which have already been woken up.
6807 pv_placemarker_wait(pmap_t pmap, vm_pindex_t *pmark)
6809 if (*pmark != PM_NOPLACEMARK) {
6810 atomic_set_long(pmark, PM_PLACEMARK_WAKEUP);
6811 tsleep_interlock(pmark, 0);
6812 if (*pmark != PM_NOPLACEMARK)
6813 tsleep(pmark, PINTERLOCKED, "pvplw", 0);
6818 * Wakeup a placemarker that we own. Replace the entry with
6819 * PM_NOPLACEMARK and issue a wakeup() if necessary.
6823 pv_placemarker_wakeup(pmap_t pmap, vm_pindex_t *pmark)
6827 pindex = atomic_swap_long(pmark, PM_NOPLACEMARK);
6828 KKASSERT(pindex != PM_NOPLACEMARK);
6829 if (pindex & PM_PLACEMARK_WAKEUP)