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.
51 #include "opt_msgbuf.h"
53 #include <sys/param.h>
54 #include <sys/kernel.h>
56 #include <sys/msgbuf.h>
57 #include <sys/vmmeter.h>
59 #include <sys/systm.h>
62 #include <vm/vm_param.h>
63 #include <sys/sysctl.h>
65 #include <vm/vm_kern.h>
66 #include <vm/vm_page.h>
67 #include <vm/vm_map.h>
68 #include <vm/vm_object.h>
69 #include <vm/vm_extern.h>
70 #include <vm/vm_pageout.h>
71 #include <vm/vm_pager.h>
72 #include <vm/vm_zone.h>
75 #include <sys/thread2.h>
76 #include <sys/spinlock2.h>
77 #include <vm/vm_page2.h>
79 #include <machine/cputypes.h>
80 #include <machine/md_var.h>
81 #include <machine/specialreg.h>
82 #include <machine/smp.h>
83 #include <machine_base/apic/apicreg.h>
84 #include <machine/globaldata.h>
85 #include <machine/pmap.h>
86 #include <machine/pmap_inval.h>
87 #include <machine/inttypes.h>
91 #define PMAP_KEEP_PDIRS
92 #ifndef PMAP_SHPGPERPROC
93 #define PMAP_SHPGPERPROC 2000
96 #if defined(DIAGNOSTIC)
97 #define PMAP_DIAGNOSTIC
103 * pmap debugging will report who owns a pv lock when blocking.
107 #define PMAP_DEBUG_DECL ,const char *func, int lineno
108 #define PMAP_DEBUG_ARGS , __func__, __LINE__
109 #define PMAP_DEBUG_COPY , func, lineno
111 #define pv_get(pmap, pindex, pmarkp) _pv_get(pmap, pindex, pmarkp \
113 #define pv_lock(pv) _pv_lock(pv \
115 #define pv_hold_try(pv) _pv_hold_try(pv \
117 #define pv_alloc(pmap, pindex, isnewp) _pv_alloc(pmap, pindex, isnewp \
120 #define pv_free(pv, pvp) _pv_free(pv, pvp PMAP_DEBUG_ARGS)
124 #define PMAP_DEBUG_DECL
125 #define PMAP_DEBUG_ARGS
126 #define PMAP_DEBUG_COPY
128 #define pv_get(pmap, pindex, pmarkp) _pv_get(pmap, pindex, pmarkp)
129 #define pv_lock(pv) _pv_lock(pv)
130 #define pv_hold_try(pv) _pv_hold_try(pv)
131 #define pv_alloc(pmap, pindex, isnewp) _pv_alloc(pmap, pindex, isnewp)
132 #define pv_free(pv, pvp) _pv_free(pv, pvp)
137 * Get PDEs and PTEs for user/kernel address space
139 #define pdir_pde(m, v) (m[(vm_offset_t)(v) >> PDRSHIFT])
141 #define pmap_pde_v(pmap, pte) ((*(pd_entry_t *)pte & pmap->pmap_bits[PG_V_IDX]) != 0)
142 #define pmap_pte_w(pmap, pte) ((*(pt_entry_t *)pte & pmap->pmap_bits[PG_W_IDX]) != 0)
143 #define pmap_pte_m(pmap, pte) ((*(pt_entry_t *)pte & pmap->pmap_bits[PG_M_IDX]) != 0)
144 #define pmap_pte_u(pmap, pte) ((*(pt_entry_t *)pte & pmap->pmap_bits[PG_U_IDX]) != 0)
145 #define pmap_pte_v(pmap, pte) ((*(pt_entry_t *)pte & pmap->pmap_bits[PG_V_IDX]) != 0)
148 * Given a map and a machine independent protection code,
149 * convert to a vax protection code.
151 #define pte_prot(m, p) \
152 (m->protection_codes[p & (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE)])
153 static uint64_t protection_codes
[PROTECTION_CODES_SIZE
];
155 struct pmap kernel_pmap
;
157 MALLOC_DEFINE(M_OBJPMAP
, "objpmap", "pmaps associated with VM objects");
159 vm_paddr_t avail_start
; /* PA of first available physical page */
160 vm_paddr_t avail_end
; /* PA of last available physical page */
161 vm_offset_t virtual2_start
; /* cutout free area prior to kernel start */
162 vm_offset_t virtual2_end
;
163 vm_offset_t virtual_start
; /* VA of first avail page (after kernel bss) */
164 vm_offset_t virtual_end
; /* VA of last avail page (end of kernel AS) */
165 vm_offset_t KvaStart
; /* VA start of KVA space */
166 vm_offset_t KvaEnd
; /* VA end of KVA space (non-inclusive) */
167 vm_offset_t KvaSize
; /* max size of kernel virtual address space */
168 static boolean_t pmap_initialized
= FALSE
; /* Has pmap_init completed? */
169 //static int pgeflag; /* PG_G or-in */
173 static vm_paddr_t dmaplimit
;
174 vm_offset_t kernel_vm_end
= VM_MIN_KERNEL_ADDRESS
;
176 static pt_entry_t pat_pte_index
[PAT_INDEX_SIZE
]; /* PAT -> PG_ bits */
177 /*static pt_entry_t pat_pde_index[PAT_INDEX_SIZE];*/ /* PAT -> PG_ bits */
179 static uint64_t KPTbase
;
180 static uint64_t KPTphys
;
181 static uint64_t KPDphys
; /* phys addr of kernel level 2 */
182 static uint64_t KPDbase
; /* phys addr of kernel level 2 @ KERNBASE */
183 uint64_t KPDPphys
; /* phys addr of kernel level 3 */
184 uint64_t KPML4phys
; /* phys addr of kernel level 4 */
186 static uint64_t DMPDphys
; /* phys addr of direct mapped level 2 */
187 static uint64_t DMPDPphys
; /* phys addr of direct mapped level 3 */
190 * Data for the pv entry allocation mechanism
192 static vm_zone_t pvzone
;
193 static struct vm_zone pvzone_store
;
194 static vm_pindex_t pv_entry_max
=0, pv_entry_high_water
=0;
195 static int pmap_pagedaemon_waken
= 0;
196 static struct pv_entry
*pvinit
;
199 * All those kernel PT submaps that BSD is so fond of
201 pt_entry_t
*CMAP1
= NULL
, *ptmmap
;
202 caddr_t CADDR1
= NULL
, ptvmmap
= NULL
;
203 static pt_entry_t
*msgbufmap
;
204 struct msgbuf
*msgbufp
=NULL
;
207 * PMAP default PG_* bits. Needed to be able to add
208 * EPT/NPT pagetable pmap_bits for the VMM module
210 uint64_t pmap_bits_default
[] = {
211 REGULAR_PMAP
, /* TYPE_IDX 0 */
212 X86_PG_V
, /* PG_V_IDX 1 */
213 X86_PG_RW
, /* PG_RW_IDX 2 */
214 X86_PG_U
, /* PG_U_IDX 3 */
215 X86_PG_A
, /* PG_A_IDX 4 */
216 X86_PG_M
, /* PG_M_IDX 5 */
217 X86_PG_PS
, /* PG_PS_IDX3 6 */
218 X86_PG_G
, /* PG_G_IDX 7 */
219 X86_PG_AVAIL1
, /* PG_AVAIL1_IDX 8 */
220 X86_PG_AVAIL2
, /* PG_AVAIL2_IDX 9 */
221 X86_PG_AVAIL3
, /* PG_AVAIL3_IDX 10 */
222 X86_PG_NC_PWT
| X86_PG_NC_PCD
, /* PG_N_IDX 11 */
223 X86_PG_NX
, /* PG_NX_IDX 12 */
228 static pt_entry_t
*pt_crashdumpmap
;
229 static caddr_t crashdumpmap
;
231 static int pmap_debug
= 0;
232 SYSCTL_INT(_machdep
, OID_AUTO
, pmap_debug
, CTLFLAG_RW
,
233 &pmap_debug
, 0, "Debug pmap's");
235 static int pmap_enter_debug
= 0;
236 SYSCTL_INT(_machdep
, OID_AUTO
, pmap_enter_debug
, CTLFLAG_RW
,
237 &pmap_enter_debug
, 0, "Debug pmap_enter's");
239 static int pmap_yield_count
= 64;
240 SYSCTL_INT(_machdep
, OID_AUTO
, pmap_yield_count
, CTLFLAG_RW
,
241 &pmap_yield_count
, 0, "Yield during init_pt/release");
242 static int pmap_mmu_optimize
= 0;
243 SYSCTL_INT(_machdep
, OID_AUTO
, pmap_mmu_optimize
, CTLFLAG_RW
,
244 &pmap_mmu_optimize
, 0, "Share page table pages when possible");
245 int pmap_fast_kernel_cpusync
= 0;
246 SYSCTL_INT(_machdep
, OID_AUTO
, pmap_fast_kernel_cpusync
, CTLFLAG_RW
,
247 &pmap_fast_kernel_cpusync
, 0, "Share page table pages when possible");
248 int pmap_dynamic_delete
= 0;
249 SYSCTL_INT(_machdep
, OID_AUTO
, pmap_dynamic_delete
, CTLFLAG_RW
,
250 &pmap_dynamic_delete
, 0, "Dynamically delete PT/PD/PDPs");
251 int pmap_lock_delay
= 100;
252 SYSCTL_INT(_machdep
, OID_AUTO
, pmap_lock_delay
, CTLFLAG_RW
,
253 &pmap_lock_delay
, 0, "Spin loops");
254 static int vm_isolated_user_pmap
= 0;
255 SYSCTL_INT(_vm
, OID_AUTO
, isolated_user_pmap
, CTLFLAG_RW
,
256 &vm_isolated_user_pmap
, 0, "Userland pmap isolation");
258 static int pmap_nx_enable
= 0;
259 /* needs manual TUNABLE in early probe, see below */
261 /* Standard user access funtions */
262 extern int std_copyinstr (const void *udaddr
, void *kaddr
, size_t len
,
264 extern int std_copyin (const void *udaddr
, void *kaddr
, size_t len
);
265 extern int std_copyout (const void *kaddr
, void *udaddr
, size_t len
);
266 extern int std_fubyte (const uint8_t *base
);
267 extern int std_subyte (uint8_t *base
, uint8_t byte
);
268 extern int32_t std_fuword32 (const uint32_t *base
);
269 extern int64_t std_fuword64 (const uint64_t *base
);
270 extern int std_suword64 (uint64_t *base
, uint64_t word
);
271 extern int std_suword32 (uint32_t *base
, int word
);
272 extern uint32_t std_swapu32 (volatile uint32_t *base
, uint32_t v
);
273 extern uint64_t std_swapu64 (volatile uint64_t *base
, uint64_t v
);
275 static void pv_hold(pv_entry_t pv
);
276 static int _pv_hold_try(pv_entry_t pv
278 static void pv_drop(pv_entry_t pv
);
279 static void _pv_lock(pv_entry_t pv
281 static void pv_unlock(pv_entry_t pv
);
282 static pv_entry_t
_pv_alloc(pmap_t pmap
, vm_pindex_t pindex
, int *isnew
284 static pv_entry_t
_pv_get(pmap_t pmap
, vm_pindex_t pindex
, vm_pindex_t
**pmarkp
286 static void _pv_free(pv_entry_t pv
, pv_entry_t pvp PMAP_DEBUG_DECL
);
287 static pv_entry_t
pv_get_try(pmap_t pmap
, vm_pindex_t pindex
,
288 vm_pindex_t
**pmarkp
, int *errorp
);
289 static void pv_put(pv_entry_t pv
);
290 static void *pv_pte_lookup(pv_entry_t pv
, vm_pindex_t pindex
);
291 static pv_entry_t
pmap_allocpte(pmap_t pmap
, vm_pindex_t ptepindex
,
293 static pv_entry_t
pmap_allocpte_seg(pmap_t pmap
, vm_pindex_t ptepindex
,
294 pv_entry_t
*pvpp
, vm_map_entry_t entry
, vm_offset_t va
);
295 static void pmap_remove_pv_pte(pv_entry_t pv
, pv_entry_t pvp
,
296 pmap_inval_bulk_t
*bulk
, int destroy
);
297 static vm_page_t
pmap_remove_pv_page(pv_entry_t pv
);
298 static int pmap_release_pv(pv_entry_t pv
, pv_entry_t pvp
,
299 pmap_inval_bulk_t
*bulk
);
301 struct pmap_scan_info
;
302 static void pmap_remove_callback(pmap_t pmap
, struct pmap_scan_info
*info
,
303 pv_entry_t pte_pv
, vm_pindex_t
*pte_placemark
,
304 pv_entry_t pt_pv
, int sharept
,
305 vm_offset_t va
, pt_entry_t
*ptep
, void *arg __unused
);
306 static void pmap_protect_callback(pmap_t pmap
, struct pmap_scan_info
*info
,
307 pv_entry_t pte_pv
, vm_pindex_t
*pte_placemark
,
308 pv_entry_t pt_pv
, int sharept
,
309 vm_offset_t va
, pt_entry_t
*ptep
, void *arg __unused
);
311 static void x86_64_protection_init (void);
312 static void create_pagetables(vm_paddr_t
*firstaddr
);
313 static void pmap_remove_all (vm_page_t m
);
314 static boolean_t
pmap_testbit (vm_page_t m
, int bit
);
316 static pt_entry_t
* pmap_pte_quick (pmap_t pmap
, vm_offset_t va
);
317 static vm_offset_t
pmap_kmem_choose(vm_offset_t addr
);
319 static void pmap_pinit_defaults(struct pmap
*pmap
);
320 static void pv_placemarker_wait(pmap_t pmap
, vm_pindex_t
*pmark
);
321 static void pv_placemarker_wakeup(pmap_t pmap
, vm_pindex_t
*pmark
);
324 pv_entry_compare(pv_entry_t pv1
, pv_entry_t pv2
)
326 if (pv1
->pv_pindex
< pv2
->pv_pindex
)
328 if (pv1
->pv_pindex
> pv2
->pv_pindex
)
333 RB_GENERATE2(pv_entry_rb_tree
, pv_entry
, pv_entry
,
334 pv_entry_compare
, vm_pindex_t
, pv_pindex
);
338 pmap_page_stats_adding(vm_page_t m
)
340 globaldata_t gd
= mycpu
;
342 if (TAILQ_EMPTY(&m
->md
.pv_list
)) {
343 ++gd
->gd_vmtotal
.t_arm
;
344 } else if (TAILQ_FIRST(&m
->md
.pv_list
) ==
345 TAILQ_LAST(&m
->md
.pv_list
, md_page_pv_list
)) {
346 ++gd
->gd_vmtotal
.t_armshr
;
347 ++gd
->gd_vmtotal
.t_avmshr
;
349 ++gd
->gd_vmtotal
.t_avmshr
;
355 pmap_page_stats_deleting(vm_page_t m
)
357 globaldata_t gd
= mycpu
;
359 if (TAILQ_EMPTY(&m
->md
.pv_list
)) {
360 --gd
->gd_vmtotal
.t_arm
;
361 } else if (TAILQ_FIRST(&m
->md
.pv_list
) ==
362 TAILQ_LAST(&m
->md
.pv_list
, md_page_pv_list
)) {
363 --gd
->gd_vmtotal
.t_armshr
;
364 --gd
->gd_vmtotal
.t_avmshr
;
366 --gd
->gd_vmtotal
.t_avmshr
;
371 * This is an ineligent crowbar to prevent heavily threaded programs
372 * from creating long live-locks in the pmap code when pmap_mmu_optimize
373 * is enabled. Without it a pmap-local page table page can wind up being
374 * constantly created and destroyed (without injury, but also without
375 * progress) as the optimization tries to switch to the object's shared page
379 pmap_softwait(pmap_t pmap
)
381 while (pmap
->pm_softhold
) {
382 tsleep_interlock(&pmap
->pm_softhold
, 0);
383 if (pmap
->pm_softhold
)
384 tsleep(&pmap
->pm_softhold
, PINTERLOCKED
, "mmopt", 0);
389 pmap_softhold(pmap_t pmap
)
391 while (atomic_swap_int(&pmap
->pm_softhold
, 1) == 1) {
392 tsleep_interlock(&pmap
->pm_softhold
, 0);
393 if (atomic_swap_int(&pmap
->pm_softhold
, 1) == 1)
394 tsleep(&pmap
->pm_softhold
, PINTERLOCKED
, "mmopt", 0);
399 pmap_softdone(pmap_t pmap
)
401 atomic_swap_int(&pmap
->pm_softhold
, 0);
402 wakeup(&pmap
->pm_softhold
);
406 * Move the kernel virtual free pointer to the next
407 * 2MB. This is used to help improve performance
408 * by using a large (2MB) page for much of the kernel
409 * (.text, .data, .bss)
413 pmap_kmem_choose(vm_offset_t addr
)
415 vm_offset_t newaddr
= addr
;
417 newaddr
= roundup2(addr
, NBPDR
);
422 * Returns the pindex of a page table entry (representing a terminal page).
423 * There are NUPTE_TOTAL page table entries possible (a huge number)
425 * x86-64 has a 48-bit address space, where bit 47 is sign-extended out.
426 * We want to properly translate negative KVAs.
430 pmap_pte_pindex(vm_offset_t va
)
432 return ((va
>> PAGE_SHIFT
) & (NUPTE_TOTAL
- 1));
436 * Returns the pindex of a page table.
440 pmap_pt_pindex(vm_offset_t va
)
442 return (NUPTE_TOTAL
+ ((va
>> PDRSHIFT
) & (NUPT_TOTAL
- 1)));
446 * Returns the pindex of a page directory.
450 pmap_pd_pindex(vm_offset_t va
)
452 return (NUPTE_TOTAL
+ NUPT_TOTAL
+
453 ((va
>> PDPSHIFT
) & (NUPD_TOTAL
- 1)));
458 pmap_pdp_pindex(vm_offset_t va
)
460 return (NUPTE_TOTAL
+ NUPT_TOTAL
+ NUPD_TOTAL
+
461 ((va
>> PML4SHIFT
) & (NUPDP_TOTAL
- 1)));
466 pmap_pml4_pindex(void)
468 return (NUPTE_TOTAL
+ NUPT_TOTAL
+ NUPD_TOTAL
+ NUPDP_TOTAL
);
472 * Return various clipped indexes for a given VA
474 * Returns the index of a pt in a page directory, representing a page
479 pmap_pt_index(vm_offset_t va
)
481 return ((va
>> PDRSHIFT
) & ((1ul << NPDEPGSHIFT
) - 1));
485 * Returns the index of a pd in a page directory page, representing a page
490 pmap_pd_index(vm_offset_t va
)
492 return ((va
>> PDPSHIFT
) & ((1ul << NPDPEPGSHIFT
) - 1));
496 * Returns the index of a pdp in the pml4 table, representing a page
501 pmap_pdp_index(vm_offset_t va
)
503 return ((va
>> PML4SHIFT
) & ((1ul << NPML4EPGSHIFT
) - 1));
507 * Locate the requested pt_entry
511 pv_entry_lookup(pmap_t pmap
, vm_pindex_t pindex
)
515 if (pindex
< pmap_pt_pindex(0))
516 pv
= pmap
->pm_pvhint_pte
;
517 else if (pindex
< pmap_pd_pindex(0))
518 pv
= pmap
->pm_pvhint_pt
;
522 if (pv
== NULL
|| pv
->pv_pmap
!= pmap
) {
523 pv
= pv_entry_rb_tree_RB_LOOKUP(&pmap
->pm_pvroot
,
525 } else if (pv
->pv_pindex
!= pindex
) {
526 pv
= pv_entry_rb_tree_RB_LOOKUP_REL(&pmap
->pm_pvroot
,
535 * Super fast pmap_pte routine best used when scanning the pv lists.
536 * This eliminates many course-grained invltlb calls. Note that many of
537 * the pv list scans are across different pmaps and it is very wasteful
538 * to do an entire invltlb when checking a single mapping.
540 static __inline pt_entry_t
*pmap_pte(pmap_t pmap
, vm_offset_t va
);
544 pmap_pte_quick(pmap_t pmap
, vm_offset_t va
)
546 return pmap_pte(pmap
, va
);
550 * The placemarker hash must be broken up into four zones so lock
551 * ordering semantics continue to work (e.g. pte, pt, pd, then pdp).
553 * Placemarkers are used to 'lock' page table indices that do not have
554 * a pv_entry. This allows the pmap to support managed and unmanaged
555 * pages and shared page tables.
557 #define PM_PLACE_BASE (PM_PLACEMARKS >> 2)
561 pmap_placemarker_hash(pmap_t pmap
, vm_pindex_t pindex
)
565 if (pindex
< pmap_pt_pindex(0)) /* zone 0 - PTE */
567 else if (pindex
< pmap_pd_pindex(0)) /* zone 1 - PT */
569 else if (pindex
< pmap_pdp_pindex(0)) /* zone 2 - PD */
570 hi
= PM_PLACE_BASE
<< 1;
571 else /* zone 3 - PDP (and PML4E) */
572 hi
= PM_PLACE_BASE
| (PM_PLACE_BASE
<< 1);
573 hi
+= pindex
& (PM_PLACE_BASE
- 1);
575 return (&pmap
->pm_placemarks
[hi
]);
580 * Generic procedure to index a pte from a pt, pd, or pdp.
582 * NOTE: Normally passed pindex as pmap_xx_index(). pmap_xx_pindex() is NOT
583 * a page table page index but is instead of PV lookup index.
587 pv_pte_lookup(pv_entry_t pv
, vm_pindex_t pindex
)
591 pte
= (pt_entry_t
*)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(pv
->pv_m
));
592 return(&pte
[pindex
]);
596 * Return pointer to PDP slot in the PML4
600 pmap_pdp(pmap_t pmap
, vm_offset_t va
)
602 return (&pmap
->pm_pml4
[pmap_pdp_index(va
)]);
606 * Return pointer to PD slot in the PDP given a pointer to the PDP
610 pmap_pdp_to_pd(pml4_entry_t pdp_pte
, vm_offset_t va
)
614 pd
= (pdp_entry_t
*)PHYS_TO_DMAP(pdp_pte
& PG_FRAME
);
615 return (&pd
[pmap_pd_index(va
)]);
619 * Return pointer to PD slot in the PDP.
623 pmap_pd(pmap_t pmap
, vm_offset_t va
)
627 pdp
= pmap_pdp(pmap
, va
);
628 if ((*pdp
& pmap
->pmap_bits
[PG_V_IDX
]) == 0)
630 return (pmap_pdp_to_pd(*pdp
, va
));
634 * Return pointer to PT slot in the PD given a pointer to the PD
638 pmap_pd_to_pt(pdp_entry_t pd_pte
, vm_offset_t va
)
642 pt
= (pd_entry_t
*)PHYS_TO_DMAP(pd_pte
& PG_FRAME
);
643 return (&pt
[pmap_pt_index(va
)]);
647 * Return pointer to PT slot in the PD
649 * SIMPLE PMAP NOTE: Simple pmaps (embedded in objects) do not have PDPs,
650 * so we cannot lookup the PD via the PDP. Instead we
651 * must look it up via the pmap.
655 pmap_pt(pmap_t pmap
, vm_offset_t va
)
659 vm_pindex_t pd_pindex
;
662 if (pmap
->pm_flags
& PMAP_FLAG_SIMPLE
) {
663 pd_pindex
= pmap_pd_pindex(va
);
664 spin_lock_shared(&pmap
->pm_spin
);
665 pv
= pv_entry_rb_tree_RB_LOOKUP(&pmap
->pm_pvroot
, pd_pindex
);
666 if (pv
== NULL
|| pv
->pv_m
== NULL
) {
667 spin_unlock_shared(&pmap
->pm_spin
);
670 phys
= VM_PAGE_TO_PHYS(pv
->pv_m
);
671 spin_unlock_shared(&pmap
->pm_spin
);
672 return (pmap_pd_to_pt(phys
, va
));
674 pd
= pmap_pd(pmap
, va
);
675 if (pd
== NULL
|| (*pd
& pmap
->pmap_bits
[PG_V_IDX
]) == 0)
677 return (pmap_pd_to_pt(*pd
, va
));
682 * Return pointer to PTE slot in the PT given a pointer to the PT
686 pmap_pt_to_pte(pd_entry_t pt_pte
, vm_offset_t va
)
690 pte
= (pt_entry_t
*)PHYS_TO_DMAP(pt_pte
& PG_FRAME
);
691 return (&pte
[pmap_pte_index(va
)]);
695 * Return pointer to PTE slot in the PT
699 pmap_pte(pmap_t pmap
, vm_offset_t va
)
703 pt
= pmap_pt(pmap
, va
);
704 if (pt
== NULL
|| (*pt
& pmap
->pmap_bits
[PG_V_IDX
]) == 0)
706 if ((*pt
& pmap
->pmap_bits
[PG_PS_IDX
]) != 0)
707 return ((pt_entry_t
*)pt
);
708 return (pmap_pt_to_pte(*pt
, va
));
712 * Of all the layers (PTE, PT, PD, PDP, PML4) the best one to cache is
713 * the PT layer. This will speed up core pmap operations considerably.
715 * NOTE: The pmap spinlock does not need to be held but the passed-in pv
716 * must be in a known associated state (typically by being locked when
717 * the pmap spinlock isn't held). We allow the race for that case.
719 * NOTE: pm_pvhint* is only accessed (read) with the spin-lock held, using
720 * cpu_ccfence() to prevent compiler optimizations from reloading the
725 pv_cache(pv_entry_t pv
, vm_pindex_t pindex
)
727 if (pindex
< pmap_pt_pindex(0)) {
729 pv
->pv_pmap
->pm_pvhint_pte
= pv
;
730 } else if (pindex
< pmap_pd_pindex(0)) {
732 pv
->pv_pmap
->pm_pvhint_pt
= pv
;
738 * Return address of PT slot in PD (KVM only)
740 * Cannot be used for user page tables because it might interfere with
741 * the shared page-table-page optimization (pmap_mmu_optimize).
745 vtopt(vm_offset_t va
)
747 uint64_t mask
= ((1ul << (NPDEPGSHIFT
+ NPDPEPGSHIFT
+
748 NPML4EPGSHIFT
)) - 1);
750 return (PDmap
+ ((va
>> PDRSHIFT
) & mask
));
754 * KVM - return address of PTE slot in PT
758 vtopte(vm_offset_t va
)
760 uint64_t mask
= ((1ul << (NPTEPGSHIFT
+ NPDEPGSHIFT
+
761 NPDPEPGSHIFT
+ NPML4EPGSHIFT
)) - 1);
763 return (PTmap
+ ((va
>> PAGE_SHIFT
) & mask
));
767 * Returns the physical address translation from va for a user address.
768 * (vm_paddr_t)-1 is returned on failure.
771 uservtophys(vm_offset_t va
)
773 uint64_t mask
= ((1ul << (NPTEPGSHIFT
+ NPDEPGSHIFT
+
774 NPDPEPGSHIFT
+ NPML4EPGSHIFT
)) - 1);
779 pmap
= vmspace_pmap(mycpu
->gd_curthread
->td_lwp
->lwp_vmspace
);
781 if (va
< VM_MAX_USER_ADDRESS
) {
782 pte
= kreadmem64(PTmap
+ ((va
>> PAGE_SHIFT
) & mask
));
783 if (pte
& pmap
->pmap_bits
[PG_V_IDX
])
784 pa
= (pte
& PG_FRAME
) | (va
& PAGE_MASK
);
790 allocpages(vm_paddr_t
*firstaddr
, long n
)
795 bzero((void *)ret
, n
* PAGE_SIZE
);
796 *firstaddr
+= n
* PAGE_SIZE
;
802 create_pagetables(vm_paddr_t
*firstaddr
)
804 long i
; /* must be 64 bits */
811 * We are running (mostly) V=P at this point
813 * Calculate how many 1GB PD entries in our PDP pages are needed
814 * for the DMAP. This is only allocated if the system does not
815 * support 1GB pages. Otherwise ndmpdp is simply a count of
816 * the number of 1G terminal entries in our PDP pages are needed.
818 * NOTE: Maxmem is in pages
820 ndmpdp
= (ptoa(Maxmem
) + NBPDP
- 1) >> PDPSHIFT
;
821 if (ndmpdp
< 4) /* Minimum 4GB of dirmap */
823 KKASSERT(ndmpdp
<= NDMPML4E
* NPML4EPG
);
826 * Starting at KERNBASE - map all 2G worth of page table pages.
827 * KERNBASE is offset -2G from the end of kvm. This will accomodate
828 * all KVM allocations above KERNBASE, including the SYSMAPs below.
830 * We do this by allocating 2*512 PT pages. Each PT page can map
831 * 2MB, for 2GB total.
833 nkpt_base
= (NPDPEPG
- KPDPI
) * NPTEPG
; /* typically 2 x 512 */
836 * Starting at the beginning of kvm (VM_MIN_KERNEL_ADDRESS),
837 * Calculate how many page table pages we need to preallocate
838 * for early vm_map allocations.
840 * A few extra won't hurt, they will get used up in the running
846 nkpt_phys
= (Maxmem
* sizeof(struct vm_page
) + NBPDR
- 1) / NBPDR
;
847 nkpt_phys
+= (Maxmem
* sizeof(struct pv_entry
) + NBPDR
- 1) / NBPDR
;
848 nkpt_phys
+= 128; /* a few extra */
851 * The highest value nkpd_phys can be set to is
852 * NKPDPE - (NPDPEPG - KPDPI) (i.e. NKPDPE - 2).
854 * Doing so would cause all PD pages to be pre-populated for
855 * a maximal KVM space (approximately 16*512 pages, or 32MB.
856 * We can save memory by not doing this.
858 nkpd_phys
= (nkpt_phys
+ NPDPEPG
- 1) / NPDPEPG
;
863 * Normally NKPML4E=1-16 (1-16 kernel PDP page)
864 * Normally NKPDPE= NKPML4E*512-1 (511 min kernel PD pages)
866 * Only allocate enough PD pages
867 * NOTE: We allocate all kernel PD pages up-front, typically
868 * ~511G of KVM, requiring 511 PD pages.
870 KPTbase
= allocpages(firstaddr
, nkpt_base
); /* KERNBASE to end */
871 KPTphys
= allocpages(firstaddr
, nkpt_phys
); /* KVA start */
872 KPML4phys
= allocpages(firstaddr
, 1); /* recursive PML4 map */
873 KPDPphys
= allocpages(firstaddr
, NKPML4E
); /* kernel PDP pages */
874 KPDphys
= allocpages(firstaddr
, nkpd_phys
); /* kernel PD pages */
877 * Alloc PD pages for the area starting at KERNBASE.
879 KPDbase
= allocpages(firstaddr
, NPDPEPG
- KPDPI
);
884 DMPDPphys
= allocpages(firstaddr
, NDMPML4E
);
885 if ((amd_feature
& AMDID_PAGE1GB
) == 0)
886 DMPDphys
= allocpages(firstaddr
, ndmpdp
);
887 dmaplimit
= (vm_paddr_t
)ndmpdp
<< PDPSHIFT
;
890 * Fill in the underlying page table pages for the area around
891 * KERNBASE. This remaps low physical memory to KERNBASE.
893 * Read-only from zero to physfree
894 * XXX not fully used, underneath 2M pages
896 for (i
= 0; (i
<< PAGE_SHIFT
) < *firstaddr
; i
++) {
897 ((pt_entry_t
*)KPTbase
)[i
] = i
<< PAGE_SHIFT
;
898 ((pt_entry_t
*)KPTbase
)[i
] |=
899 pmap_bits_default
[PG_RW_IDX
] |
900 pmap_bits_default
[PG_V_IDX
] |
901 pmap_bits_default
[PG_G_IDX
];
905 * Now map the initial kernel page tables. One block of page
906 * tables is placed at the beginning of kernel virtual memory,
907 * and another block is placed at KERNBASE to map the kernel binary,
908 * data, bss, and initial pre-allocations.
910 for (i
= 0; i
< nkpt_base
; i
++) {
911 ((pd_entry_t
*)KPDbase
)[i
] = KPTbase
+ (i
<< PAGE_SHIFT
);
912 ((pd_entry_t
*)KPDbase
)[i
] |=
913 pmap_bits_default
[PG_RW_IDX
] |
914 pmap_bits_default
[PG_V_IDX
];
916 for (i
= 0; i
< nkpt_phys
; i
++) {
917 ((pd_entry_t
*)KPDphys
)[i
] = KPTphys
+ (i
<< PAGE_SHIFT
);
918 ((pd_entry_t
*)KPDphys
)[i
] |=
919 pmap_bits_default
[PG_RW_IDX
] |
920 pmap_bits_default
[PG_V_IDX
];
924 * Map from zero to end of allocations using 2M pages as an
925 * optimization. This will bypass some of the KPTBase pages
926 * above in the KERNBASE area.
928 for (i
= 0; (i
<< PDRSHIFT
) < *firstaddr
; i
++) {
929 ((pd_entry_t
*)KPDbase
)[i
] = i
<< PDRSHIFT
;
930 ((pd_entry_t
*)KPDbase
)[i
] |=
931 pmap_bits_default
[PG_RW_IDX
] |
932 pmap_bits_default
[PG_V_IDX
] |
933 pmap_bits_default
[PG_PS_IDX
] |
934 pmap_bits_default
[PG_G_IDX
];
938 * Load PD addresses into the PDP pages for primary KVA space to
939 * cover existing page tables. PD's for KERNBASE are handled in
942 * expected to pre-populate all of its PDs. See NKPDPE in vmparam.h.
944 for (i
= 0; i
< nkpd_phys
; i
++) {
945 ((pdp_entry_t
*)KPDPphys
)[NKPML4E
* NPDPEPG
- NKPDPE
+ i
] =
946 KPDphys
+ (i
<< PAGE_SHIFT
);
947 ((pdp_entry_t
*)KPDPphys
)[NKPML4E
* NPDPEPG
- NKPDPE
+ i
] |=
948 pmap_bits_default
[PG_RW_IDX
] |
949 pmap_bits_default
[PG_V_IDX
] |
950 pmap_bits_default
[PG_U_IDX
];
954 * Load PDs for KERNBASE to the end
956 i
= (NKPML4E
- 1) * NPDPEPG
+ KPDPI
;
957 for (j
= 0; j
< NPDPEPG
- KPDPI
; ++j
) {
958 ((pdp_entry_t
*)KPDPphys
)[i
+ j
] =
959 KPDbase
+ (j
<< PAGE_SHIFT
);
960 ((pdp_entry_t
*)KPDPphys
)[i
+ j
] |=
961 pmap_bits_default
[PG_RW_IDX
] |
962 pmap_bits_default
[PG_V_IDX
] |
963 pmap_bits_default
[PG_U_IDX
];
967 * Now set up the direct map space using either 2MB or 1GB pages
968 * Preset PG_M and PG_A because demotion expects it.
970 * When filling in entries in the PD pages make sure any excess
971 * entries are set to zero as we allocated enough PD pages
973 if ((amd_feature
& AMDID_PAGE1GB
) == 0) {
974 for (i
= 0; i
< NPDEPG
* ndmpdp
; i
++) {
975 ((pd_entry_t
*)DMPDphys
)[i
] = i
<< PDRSHIFT
;
976 ((pd_entry_t
*)DMPDphys
)[i
] |=
977 pmap_bits_default
[PG_RW_IDX
] |
978 pmap_bits_default
[PG_V_IDX
] |
979 pmap_bits_default
[PG_PS_IDX
] |
980 pmap_bits_default
[PG_G_IDX
] |
981 pmap_bits_default
[PG_M_IDX
] |
982 pmap_bits_default
[PG_A_IDX
];
986 * And the direct map space's PDP
988 for (i
= 0; i
< ndmpdp
; i
++) {
989 ((pdp_entry_t
*)DMPDPphys
)[i
] = DMPDphys
+
991 ((pdp_entry_t
*)DMPDPphys
)[i
] |=
992 pmap_bits_default
[PG_RW_IDX
] |
993 pmap_bits_default
[PG_V_IDX
] |
994 pmap_bits_default
[PG_U_IDX
];
997 for (i
= 0; i
< ndmpdp
; i
++) {
998 ((pdp_entry_t
*)DMPDPphys
)[i
] =
999 (vm_paddr_t
)i
<< PDPSHIFT
;
1000 ((pdp_entry_t
*)DMPDPphys
)[i
] |=
1001 pmap_bits_default
[PG_RW_IDX
] |
1002 pmap_bits_default
[PG_V_IDX
] |
1003 pmap_bits_default
[PG_PS_IDX
] |
1004 pmap_bits_default
[PG_G_IDX
] |
1005 pmap_bits_default
[PG_M_IDX
] |
1006 pmap_bits_default
[PG_A_IDX
];
1010 /* And recursively map PML4 to itself in order to get PTmap */
1011 ((pdp_entry_t
*)KPML4phys
)[PML4PML4I
] = KPML4phys
;
1012 ((pdp_entry_t
*)KPML4phys
)[PML4PML4I
] |=
1013 pmap_bits_default
[PG_RW_IDX
] |
1014 pmap_bits_default
[PG_V_IDX
] |
1015 pmap_bits_default
[PG_U_IDX
];
1018 * Connect the Direct Map slots up to the PML4
1020 for (j
= 0; j
< NDMPML4E
; ++j
) {
1021 ((pdp_entry_t
*)KPML4phys
)[DMPML4I
+ j
] =
1022 (DMPDPphys
+ ((vm_paddr_t
)j
<< PAGE_SHIFT
)) |
1023 pmap_bits_default
[PG_RW_IDX
] |
1024 pmap_bits_default
[PG_V_IDX
] |
1025 pmap_bits_default
[PG_U_IDX
];
1029 * Connect the KVA slot up to the PML4
1031 for (j
= 0; j
< NKPML4E
; ++j
) {
1032 ((pdp_entry_t
*)KPML4phys
)[KPML4I
+ j
] =
1033 KPDPphys
+ ((vm_paddr_t
)j
<< PAGE_SHIFT
);
1034 ((pdp_entry_t
*)KPML4phys
)[KPML4I
+ j
] |=
1035 pmap_bits_default
[PG_RW_IDX
] |
1036 pmap_bits_default
[PG_V_IDX
] |
1037 pmap_bits_default
[PG_U_IDX
];
1044 * Bootstrap the system enough to run with virtual memory.
1046 * On x86_64 this is called after mapping has already been enabled
1047 * and just syncs the pmap module with what has already been done.
1048 * [We can't call it easily with mapping off since the kernel is not
1049 * mapped with PA == VA, hence we would have to relocate every address
1050 * from the linked base (virtual) address "KERNBASE" to the actual
1051 * (physical) address starting relative to 0]
1054 pmap_bootstrap(vm_paddr_t
*firstaddr
)
1060 KvaStart
= VM_MIN_KERNEL_ADDRESS
;
1061 KvaEnd
= VM_MAX_KERNEL_ADDRESS
;
1062 KvaSize
= KvaEnd
- KvaStart
;
1064 avail_start
= *firstaddr
;
1067 * Create an initial set of page tables to run the kernel in.
1069 create_pagetables(firstaddr
);
1071 virtual2_start
= KvaStart
;
1072 virtual2_end
= PTOV_OFFSET
;
1074 virtual_start
= (vm_offset_t
) PTOV_OFFSET
+ *firstaddr
;
1075 virtual_start
= pmap_kmem_choose(virtual_start
);
1077 virtual_end
= VM_MAX_KERNEL_ADDRESS
;
1079 /* XXX do %cr0 as well */
1080 load_cr4(rcr4() | CR4_PGE
| CR4_PSE
);
1081 load_cr3(KPML4phys
);
1084 * Initialize protection array.
1086 x86_64_protection_init();
1089 * The kernel's pmap is statically allocated so we don't have to use
1090 * pmap_create, which is unlikely to work correctly at this part of
1091 * the boot sequence (XXX and which no longer exists).
1093 kernel_pmap
.pm_pml4
= (pdp_entry_t
*) (PTOV_OFFSET
+ KPML4phys
);
1094 kernel_pmap
.pm_count
= 1;
1095 CPUMASK_ASSALLONES(kernel_pmap
.pm_active
);
1096 RB_INIT(&kernel_pmap
.pm_pvroot
);
1097 spin_init(&kernel_pmap
.pm_spin
, "pmapbootstrap");
1098 for (i
= 0; i
< PM_PLACEMARKS
; ++i
)
1099 kernel_pmap
.pm_placemarks
[i
] = PM_NOPLACEMARK
;
1102 * Reserve some special page table entries/VA space for temporary
1105 #define SYSMAP(c, p, v, n) \
1106 v = (c)va; va += ((n)*PAGE_SIZE); p = pte; pte += (n);
1112 * CMAP1/CMAP2 are used for zeroing and copying pages.
1114 SYSMAP(caddr_t
, CMAP1
, CADDR1
, 1)
1119 SYSMAP(caddr_t
, pt_crashdumpmap
, crashdumpmap
, MAXDUMPPGS
);
1122 * ptvmmap is used for reading arbitrary physical pages via
1125 SYSMAP(caddr_t
, ptmmap
, ptvmmap
, 1)
1128 * msgbufp is used to map the system message buffer.
1129 * XXX msgbufmap is not used.
1131 SYSMAP(struct msgbuf
*, msgbufmap
, msgbufp
,
1132 atop(round_page(MSGBUF_SIZE
)))
1135 virtual_start
= pmap_kmem_choose(virtual_start
);
1140 * PG_G is terribly broken on SMP because we IPI invltlb's in some
1141 * cases rather then invl1pg. Actually, I don't even know why it
1142 * works under UP because self-referential page table mappings
1148 /* Initialize the PAT MSR */
1150 pmap_pinit_defaults(&kernel_pmap
);
1152 TUNABLE_INT_FETCH("machdep.pmap_fast_kernel_cpusync",
1153 &pmap_fast_kernel_cpusync
);
1158 * Setup the PAT MSR.
1167 * Default values mapping PATi,PCD,PWT bits at system reset.
1168 * The default values effectively ignore the PATi bit by
1169 * repeating the encodings for 0-3 in 4-7, and map the PCD
1170 * and PWT bit combinations to the expected PAT types.
1172 pat_msr
= PAT_VALUE(0, PAT_WRITE_BACK
) | /* 000 */
1173 PAT_VALUE(1, PAT_WRITE_THROUGH
) | /* 001 */
1174 PAT_VALUE(2, PAT_UNCACHED
) | /* 010 */
1175 PAT_VALUE(3, PAT_UNCACHEABLE
) | /* 011 */
1176 PAT_VALUE(4, PAT_WRITE_BACK
) | /* 100 */
1177 PAT_VALUE(5, PAT_WRITE_THROUGH
) | /* 101 */
1178 PAT_VALUE(6, PAT_UNCACHED
) | /* 110 */
1179 PAT_VALUE(7, PAT_UNCACHEABLE
); /* 111 */
1180 pat_pte_index
[PAT_WRITE_BACK
] = 0;
1181 pat_pte_index
[PAT_WRITE_THROUGH
]= 0 | X86_PG_NC_PWT
;
1182 pat_pte_index
[PAT_UNCACHED
] = X86_PG_NC_PCD
;
1183 pat_pte_index
[PAT_UNCACHEABLE
] = X86_PG_NC_PCD
| X86_PG_NC_PWT
;
1184 pat_pte_index
[PAT_WRITE_PROTECTED
] = pat_pte_index
[PAT_UNCACHEABLE
];
1185 pat_pte_index
[PAT_WRITE_COMBINING
] = pat_pte_index
[PAT_UNCACHEABLE
];
1187 if (cpu_feature
& CPUID_PAT
) {
1189 * If we support the PAT then set-up entries for
1190 * WRITE_PROTECTED and WRITE_COMBINING using bit patterns
1193 pat_msr
= (pat_msr
& ~PAT_MASK(5)) |
1194 PAT_VALUE(5, PAT_WRITE_PROTECTED
);
1195 pat_msr
= (pat_msr
& ~PAT_MASK(6)) |
1196 PAT_VALUE(6, PAT_WRITE_COMBINING
);
1197 pat_pte_index
[PAT_WRITE_PROTECTED
] = X86_PG_PTE_PAT
| X86_PG_NC_PWT
;
1198 pat_pte_index
[PAT_WRITE_COMBINING
] = X86_PG_PTE_PAT
| X86_PG_NC_PCD
;
1201 * Then enable the PAT
1206 load_cr4(cr4
& ~CR4_PGE
);
1208 /* Disable caches (CD = 1, NW = 0). */
1210 load_cr0((cr0
& ~CR0_NW
) | CR0_CD
);
1212 /* Flushes caches and TLBs. */
1216 /* Update PAT and index table. */
1217 wrmsr(MSR_PAT
, pat_msr
);
1219 /* Flush caches and TLBs again. */
1223 /* Restore caches and PGE. */
1231 * Set 4mb pdir for mp startup
1236 if (cpu_feature
& CPUID_PSE
) {
1237 load_cr4(rcr4() | CR4_PSE
);
1238 if (mycpu
->gd_cpuid
== 0) /* only on BSP */
1244 * Initialize the pmap module.
1245 * Called by vm_init, to initialize any structures that the pmap
1246 * system needs to map virtual memory.
1247 * pmap_init has been enhanced to support in a fairly consistant
1248 * way, discontiguous physical memory.
1253 vm_pindex_t initial_pvs
;
1257 * Allocate memory for random pmap data structures. Includes the
1261 for (i
= 0; i
< vm_page_array_size
; i
++) {
1264 m
= &vm_page_array
[i
];
1265 TAILQ_INIT(&m
->md
.pv_list
);
1269 * init the pv free list
1271 initial_pvs
= vm_page_array_size
;
1272 if (initial_pvs
< MINPV
)
1273 initial_pvs
= MINPV
;
1274 pvzone
= &pvzone_store
;
1275 pvinit
= (void *)kmem_alloc(&kernel_map
,
1276 initial_pvs
* sizeof (struct pv_entry
),
1278 zbootinit(pvzone
, "PV ENTRY", sizeof (struct pv_entry
),
1279 pvinit
, initial_pvs
);
1282 * Now it is safe to enable pv_table recording.
1284 pmap_initialized
= TRUE
;
1288 * Initialize the address space (zone) for the pv_entries. Set a
1289 * high water mark so that the system can recover from excessive
1290 * numbers of pv entries.
1295 vm_pindex_t shpgperproc
= PMAP_SHPGPERPROC
;
1296 vm_pindex_t entry_max
;
1298 TUNABLE_LONG_FETCH("vm.pmap.shpgperproc", &shpgperproc
);
1299 pv_entry_max
= shpgperproc
* maxproc
+ vm_page_array_size
;
1300 TUNABLE_LONG_FETCH("vm.pmap.pv_entries", &pv_entry_max
);
1301 pv_entry_high_water
= 9 * (pv_entry_max
/ 10);
1304 * Subtract out pages already installed in the zone (hack)
1306 entry_max
= pv_entry_max
- vm_page_array_size
;
1310 zinitna(pvzone
, NULL
, 0, entry_max
, ZONE_INTERRUPT
);
1313 * Enable dynamic deletion of empty higher-level page table pages
1314 * by default only if system memory is < 8GB (use 7GB for slop).
1315 * This can save a little memory, but imposes significant
1316 * performance overhead for things like bulk builds, and for programs
1317 * which do a lot of memory mapping and memory unmapping.
1319 if (pmap_dynamic_delete
< 0) {
1320 if (vmstats
.v_page_count
< 7LL * 1024 * 1024 * 1024 / PAGE_SIZE
)
1321 pmap_dynamic_delete
= 1;
1323 pmap_dynamic_delete
= 0;
1328 * Typically used to initialize a fictitious page by vm/device_pager.c
1331 pmap_page_init(struct vm_page
*m
)
1334 TAILQ_INIT(&m
->md
.pv_list
);
1337 /***************************************************
1338 * Low level helper routines.....
1339 ***************************************************/
1342 * this routine defines the region(s) of memory that should
1343 * not be tested for the modified bit.
1347 pmap_track_modified(vm_pindex_t pindex
)
1349 vm_offset_t va
= (vm_offset_t
)pindex
<< PAGE_SHIFT
;
1350 if ((va
< clean_sva
) || (va
>= clean_eva
))
1357 * Extract the physical page address associated with the map/VA pair.
1358 * The page must be wired for this to work reliably.
1361 pmap_extract(pmap_t pmap
, vm_offset_t va
, void **handlep
)
1368 if (va
>= VM_MAX_USER_ADDRESS
) {
1370 * Kernel page directories might be direct-mapped and
1371 * there is typically no PV tracking of pte's
1375 pt
= pmap_pt(pmap
, va
);
1376 if (pt
&& (*pt
& pmap
->pmap_bits
[PG_V_IDX
])) {
1377 if (*pt
& pmap
->pmap_bits
[PG_PS_IDX
]) {
1378 rtval
= *pt
& PG_PS_FRAME
;
1379 rtval
|= va
& PDRMASK
;
1381 ptep
= pmap_pt_to_pte(*pt
, va
);
1382 if (*pt
& pmap
->pmap_bits
[PG_V_IDX
]) {
1383 rtval
= *ptep
& PG_FRAME
;
1384 rtval
|= va
& PAGE_MASK
;
1392 * User pages currently do not direct-map the page directory
1393 * and some pages might not used managed PVs. But all PT's
1396 pt_pv
= pv_get(pmap
, pmap_pt_pindex(va
), NULL
);
1398 ptep
= pv_pte_lookup(pt_pv
, pmap_pte_index(va
));
1399 if (*ptep
& pmap
->pmap_bits
[PG_V_IDX
]) {
1400 rtval
= *ptep
& PG_FRAME
;
1401 rtval
|= va
& PAGE_MASK
;
1404 *handlep
= pt_pv
; /* locked until done */
1407 } else if (handlep
) {
1415 pmap_extract_done(void *handle
)
1418 pv_put((pv_entry_t
)handle
);
1422 * Similar to extract but checks protections, SMP-friendly short-cut for
1423 * vm_fault_page[_quick](). Can return NULL to cause the caller to
1424 * fall-through to the real fault code. Does not work with HVM page
1427 * if busyp is NULL the returned page, if not NULL, is held (and not busied).
1429 * If busyp is not NULL and this function sets *busyp non-zero, the returned
1430 * page is busied (and not held).
1432 * If busyp is not NULL and this function sets *busyp to zero, the returned
1433 * page is held (and not busied).
1435 * If VM_PROT_WRITE is set in prot, and the pte is already writable, the
1436 * returned page will be dirtied. If the pte is not already writable NULL
1437 * is returned. In otherwords, if the bit is set and a vm_page_t is returned,
1438 * any COW will already have happened and that page can be written by the
1441 * WARNING! THE RETURNED PAGE IS ONLY HELD AND NOT SUITABLE FOR READING
1445 pmap_fault_page_quick(pmap_t pmap
, vm_offset_t va
, vm_prot_t prot
, int *busyp
)
1448 va
< VM_MAX_USER_ADDRESS
&&
1449 (pmap
->pm_flags
& PMAP_HVM
) == 0) {
1457 req
= pmap
->pmap_bits
[PG_V_IDX
] |
1458 pmap
->pmap_bits
[PG_U_IDX
];
1459 if (prot
& VM_PROT_WRITE
)
1460 req
|= pmap
->pmap_bits
[PG_RW_IDX
];
1462 pt_pv
= pv_get(pmap
, pmap_pt_pindex(va
), NULL
);
1465 ptep
= pv_pte_lookup(pt_pv
, pmap_pte_index(va
));
1466 if ((*ptep
& req
) != req
) {
1470 pte_pv
= pv_get_try(pmap
, pmap_pte_pindex(va
), NULL
, &error
);
1471 if (pte_pv
&& error
== 0) {
1473 if (prot
& VM_PROT_WRITE
) {
1474 /* interlocked by presence of pv_entry */
1478 if (prot
& VM_PROT_WRITE
) {
1479 if (vm_page_busy_try(m
, TRUE
))
1490 } else if (pte_pv
) {
1494 /* error, since we didn't request a placemarker */
1505 * Extract the physical page address associated kernel virtual address.
1508 pmap_kextract(vm_offset_t va
)
1510 pd_entry_t pt
; /* pt entry in pd */
1513 if (va
>= DMAP_MIN_ADDRESS
&& va
< DMAP_MAX_ADDRESS
) {
1514 pa
= DMAP_TO_PHYS(va
);
1517 if (pt
& kernel_pmap
.pmap_bits
[PG_PS_IDX
]) {
1518 pa
= (pt
& PG_PS_FRAME
) | (va
& PDRMASK
);
1521 * Beware of a concurrent promotion that changes the
1522 * PDE at this point! For example, vtopte() must not
1523 * be used to access the PTE because it would use the
1524 * new PDE. It is, however, safe to use the old PDE
1525 * because the page table page is preserved by the
1528 pa
= *pmap_pt_to_pte(pt
, va
);
1529 pa
= (pa
& PG_FRAME
) | (va
& PAGE_MASK
);
1535 /***************************************************
1536 * Low level mapping routines.....
1537 ***************************************************/
1540 * Routine: pmap_kenter
1542 * Add a wired page to the KVA
1543 * NOTE! note that in order for the mapping to take effect -- you
1544 * should do an invltlb after doing the pmap_kenter().
1547 pmap_kenter(vm_offset_t va
, vm_paddr_t pa
)
1553 kernel_pmap
.pmap_bits
[PG_RW_IDX
] |
1554 kernel_pmap
.pmap_bits
[PG_V_IDX
];
1558 pmap_inval_smp(&kernel_pmap
, va
, 1, ptep
, npte
);
1562 pmap_inval_smp(&kernel_pmap
, va
, ptep
, npte
);
1569 * Similar to pmap_kenter(), except we only invalidate the mapping on the
1570 * current CPU. Returns 0 if the previous pte was 0, 1 if it wasn't
1571 * (caller can conditionalize calling smp_invltlb()).
1574 pmap_kenter_quick(vm_offset_t va
, vm_paddr_t pa
)
1580 npte
= pa
| kernel_pmap
.pmap_bits
[PG_RW_IDX
] |
1581 kernel_pmap
.pmap_bits
[PG_V_IDX
];
1590 atomic_swap_long(ptep
, npte
);
1591 cpu_invlpg((void *)va
);
1597 * Enter addresses into the kernel pmap but don't bother
1598 * doing any tlb invalidations. Caller will do a rollup
1599 * invalidation via pmap_rollup_inval().
1602 pmap_kenter_noinval(vm_offset_t va
, vm_paddr_t pa
)
1609 kernel_pmap
.pmap_bits
[PG_RW_IDX
] |
1610 kernel_pmap
.pmap_bits
[PG_V_IDX
];
1619 atomic_swap_long(ptep
, npte
);
1620 cpu_invlpg((void *)va
);
1626 * remove a page from the kernel pagetables
1629 pmap_kremove(vm_offset_t va
)
1634 pmap_inval_smp(&kernel_pmap
, va
, 1, ptep
, 0);
1638 pmap_kremove_quick(vm_offset_t va
)
1643 (void)pte_load_clear(ptep
);
1644 cpu_invlpg((void *)va
);
1648 * Remove addresses from the kernel pmap but don't bother
1649 * doing any tlb invalidations. Caller will do a rollup
1650 * invalidation via pmap_rollup_inval().
1653 pmap_kremove_noinval(vm_offset_t va
)
1658 (void)pte_load_clear(ptep
);
1662 * XXX these need to be recoded. They are not used in any critical path.
1665 pmap_kmodify_rw(vm_offset_t va
)
1667 atomic_set_long(vtopte(va
), kernel_pmap
.pmap_bits
[PG_RW_IDX
]);
1668 cpu_invlpg((void *)va
);
1673 pmap_kmodify_nc(vm_offset_t va)
1675 atomic_set_long(vtopte(va), PG_N);
1676 cpu_invlpg((void *)va);
1681 * Used to map a range of physical addresses into kernel virtual
1682 * address space during the low level boot, typically to map the
1683 * dump bitmap, message buffer, and vm_page_array.
1685 * These mappings are typically made at some pointer after the end of the
1688 * We could return PHYS_TO_DMAP(start) here and not allocate any
1689 * via (*virtp), but then kmem from userland and kernel dumps won't
1690 * have access to the related pointers.
1693 pmap_map(vm_offset_t
*virtp
, vm_paddr_t start
, vm_paddr_t end
, int prot
)
1696 vm_offset_t va_start
;
1698 /*return PHYS_TO_DMAP(start);*/
1703 while (start
< end
) {
1704 pmap_kenter_quick(va
, start
);
1712 #define PMAP_CLFLUSH_THRESHOLD (2 * 1024 * 1024)
1715 * Remove the specified set of pages from the data and instruction caches.
1717 * In contrast to pmap_invalidate_cache_range(), this function does not
1718 * rely on the CPU's self-snoop feature, because it is intended for use
1719 * when moving pages into a different cache domain.
1722 pmap_invalidate_cache_pages(vm_page_t
*pages
, int count
)
1724 vm_offset_t daddr
, eva
;
1727 if (count
>= PMAP_CLFLUSH_THRESHOLD
/ PAGE_SIZE
||
1728 (cpu_feature
& CPUID_CLFSH
) == 0)
1732 for (i
= 0; i
< count
; i
++) {
1733 daddr
= PHYS_TO_DMAP(VM_PAGE_TO_PHYS(pages
[i
]));
1734 eva
= daddr
+ PAGE_SIZE
;
1735 for (; daddr
< eva
; daddr
+= cpu_clflush_line_size
)
1743 pmap_invalidate_cache_range(vm_offset_t sva
, vm_offset_t eva
)
1745 KASSERT((sva
& PAGE_MASK
) == 0,
1746 ("pmap_invalidate_cache_range: sva not page-aligned"));
1747 KASSERT((eva
& PAGE_MASK
) == 0,
1748 ("pmap_invalidate_cache_range: eva not page-aligned"));
1750 if (cpu_feature
& CPUID_SS
) {
1751 ; /* If "Self Snoop" is supported, do nothing. */
1753 /* Globally invalidate caches */
1754 cpu_wbinvd_on_all_cpus();
1759 * Invalidate the specified range of virtual memory on all cpus associated
1763 pmap_invalidate_range(pmap_t pmap
, vm_offset_t sva
, vm_offset_t eva
)
1765 pmap_inval_smp(pmap
, sva
, (eva
- sva
) >> PAGE_SHIFT
, NULL
, 0);
1769 * Add a list of wired pages to the kva. This routine is used for temporary
1770 * kernel mappings such as those found in buffer cache buffer. Page
1771 * modifications and accesses are not tracked or recorded.
1773 * NOTE! Old mappings are simply overwritten, and we cannot assume relaxed
1774 * semantics as previous mappings may have been zerod without any
1777 * The page *must* be wired.
1779 static __inline
void
1780 _pmap_qenter(vm_offset_t beg_va
, vm_page_t
*m
, int count
, int doinval
)
1785 end_va
= beg_va
+ count
* PAGE_SIZE
;
1787 for (va
= beg_va
; va
< end_va
; va
+= PAGE_SIZE
) {
1792 pte
= VM_PAGE_TO_PHYS(*m
) |
1793 kernel_pmap
.pmap_bits
[PG_RW_IDX
] |
1794 kernel_pmap
.pmap_bits
[PG_V_IDX
] |
1795 kernel_pmap
.pmap_cache_bits
[(*m
)->pat_mode
];
1797 atomic_swap_long(ptep
, pte
);
1801 pmap_invalidate_range(&kernel_pmap
, beg_va
, end_va
);
1805 pmap_qenter(vm_offset_t beg_va
, vm_page_t
*m
, int count
)
1807 _pmap_qenter(beg_va
, m
, count
, 1);
1811 pmap_qenter_noinval(vm_offset_t beg_va
, vm_page_t
*m
, int count
)
1813 _pmap_qenter(beg_va
, m
, count
, 0);
1817 * This routine jerks page mappings from the kernel -- it is meant only
1818 * for temporary mappings such as those found in buffer cache buffers.
1819 * No recording modified or access status occurs.
1821 * MPSAFE, INTERRUPT SAFE (cluster callback)
1824 pmap_qremove(vm_offset_t beg_va
, int count
)
1829 end_va
= beg_va
+ count
* PAGE_SIZE
;
1831 for (va
= beg_va
; va
< end_va
; va
+= PAGE_SIZE
) {
1835 (void)pte_load_clear(pte
);
1836 cpu_invlpg((void *)va
);
1838 pmap_invalidate_range(&kernel_pmap
, beg_va
, end_va
);
1842 * This routine removes temporary kernel mappings, only invalidating them
1843 * on the current cpu. It should only be used under carefully controlled
1847 pmap_qremove_quick(vm_offset_t beg_va
, int count
)
1852 end_va
= beg_va
+ count
* PAGE_SIZE
;
1854 for (va
= beg_va
; va
< end_va
; va
+= PAGE_SIZE
) {
1858 (void)pte_load_clear(pte
);
1859 cpu_invlpg((void *)va
);
1864 * This routine removes temporary kernel mappings *without* invalidating
1865 * the TLB. It can only be used on permanent kva reservations such as those
1866 * found in buffer cache buffers, under carefully controlled circumstances.
1868 * NOTE: Repopulating these KVAs requires unconditional invalidation.
1869 * (pmap_qenter() does unconditional invalidation).
1872 pmap_qremove_noinval(vm_offset_t beg_va
, int count
)
1877 end_va
= beg_va
+ count
* PAGE_SIZE
;
1879 for (va
= beg_va
; va
< end_va
; va
+= PAGE_SIZE
) {
1883 (void)pte_load_clear(pte
);
1888 * Create a new thread and optionally associate it with a (new) process.
1889 * NOTE! the new thread's cpu may not equal the current cpu.
1892 pmap_init_thread(thread_t td
)
1894 /* enforce pcb placement & alignment */
1895 td
->td_pcb
= (struct pcb
*)(td
->td_kstack
+ td
->td_kstack_size
) - 1;
1896 td
->td_pcb
= (struct pcb
*)((intptr_t)td
->td_pcb
& ~(intptr_t)0xF);
1897 td
->td_savefpu
= &td
->td_pcb
->pcb_save
;
1898 td
->td_sp
= (char *)td
->td_pcb
; /* no -16 */
1902 * This routine directly affects the fork perf for a process.
1905 pmap_init_proc(struct proc
*p
)
1910 pmap_pinit_defaults(struct pmap
*pmap
)
1912 bcopy(pmap_bits_default
, pmap
->pmap_bits
,
1913 sizeof(pmap_bits_default
));
1914 bcopy(protection_codes
, pmap
->protection_codes
,
1915 sizeof(protection_codes
));
1916 bcopy(pat_pte_index
, pmap
->pmap_cache_bits
,
1917 sizeof(pat_pte_index
));
1918 pmap
->pmap_cache_mask
= X86_PG_NC_PWT
| X86_PG_NC_PCD
| X86_PG_PTE_PAT
;
1919 pmap
->copyinstr
= std_copyinstr
;
1920 pmap
->copyin
= std_copyin
;
1921 pmap
->copyout
= std_copyout
;
1922 pmap
->fubyte
= std_fubyte
;
1923 pmap
->subyte
= std_subyte
;
1924 pmap
->fuword32
= std_fuword32
;
1925 pmap
->fuword64
= std_fuword64
;
1926 pmap
->suword32
= std_suword32
;
1927 pmap
->suword64
= std_suword64
;
1928 pmap
->swapu32
= std_swapu32
;
1929 pmap
->swapu64
= std_swapu64
;
1932 * Initialize pmap0/vmspace0.
1934 * On architectures where the kernel pmap is not integrated into the user
1935 * process pmap, this pmap represents the process pmap, not the kernel pmap.
1936 * kernel_pmap should be used to directly access the kernel_pmap.
1939 pmap_pinit0(struct pmap
*pmap
)
1943 pmap
->pm_pml4
= (pml4_entry_t
*)(PTOV_OFFSET
+ KPML4phys
);
1945 CPUMASK_ASSZERO(pmap
->pm_active
);
1946 pmap
->pm_pvhint_pt
= NULL
;
1947 pmap
->pm_pvhint_pte
= NULL
;
1948 RB_INIT(&pmap
->pm_pvroot
);
1949 spin_init(&pmap
->pm_spin
, "pmapinit0");
1950 for (i
= 0; i
< PM_PLACEMARKS
; ++i
)
1951 pmap
->pm_placemarks
[i
] = PM_NOPLACEMARK
;
1952 bzero(&pmap
->pm_stats
, sizeof pmap
->pm_stats
);
1953 pmap_pinit_defaults(pmap
);
1957 * Initialize a preallocated and zeroed pmap structure,
1958 * such as one in a vmspace structure.
1961 pmap_pinit_simple(struct pmap
*pmap
)
1966 * Misc initialization
1969 CPUMASK_ASSZERO(pmap
->pm_active
);
1970 pmap
->pm_pvhint_pt
= NULL
;
1971 pmap
->pm_pvhint_pte
= NULL
;
1972 pmap
->pm_flags
= PMAP_FLAG_SIMPLE
;
1974 pmap_pinit_defaults(pmap
);
1977 * Don't blow up locks/tokens on re-use (XXX fix/use drop code
1980 if (pmap
->pm_pmlpv
== NULL
) {
1981 RB_INIT(&pmap
->pm_pvroot
);
1982 bzero(&pmap
->pm_stats
, sizeof pmap
->pm_stats
);
1983 spin_init(&pmap
->pm_spin
, "pmapinitsimple");
1984 for (i
= 0; i
< PM_PLACEMARKS
; ++i
)
1985 pmap
->pm_placemarks
[i
] = PM_NOPLACEMARK
;
1990 pmap_pinit(struct pmap
*pmap
)
1995 if (pmap
->pm_pmlpv
) {
1996 if (pmap
->pmap_bits
[TYPE_IDX
] != REGULAR_PMAP
) {
2001 pmap_pinit_simple(pmap
);
2002 pmap
->pm_flags
&= ~PMAP_FLAG_SIMPLE
;
2005 * No need to allocate page table space yet but we do need a valid
2006 * page directory table.
2008 if (pmap
->pm_pml4
== NULL
) {
2010 (pml4_entry_t
*)kmem_alloc_pageable(&kernel_map
,
2013 pmap
->pm_pml4_iso
= (void *)((char *)pmap
->pm_pml4
+ PAGE_SIZE
);
2017 * Allocate the PML4e table, which wires it even though it isn't
2018 * being entered into some higher level page table (it being the
2019 * highest level). If one is already cached we don't have to do
2022 if ((pv
= pmap
->pm_pmlpv
) == NULL
) {
2023 pv
= pmap_allocpte(pmap
, pmap_pml4_pindex(), NULL
);
2024 pmap
->pm_pmlpv
= pv
;
2025 pmap_kenter((vm_offset_t
)pmap
->pm_pml4
,
2026 VM_PAGE_TO_PHYS(pv
->pv_m
));
2030 * Install DMAP and KMAP.
2032 for (j
= 0; j
< NDMPML4E
; ++j
) {
2033 pmap
->pm_pml4
[DMPML4I
+ j
] =
2034 (DMPDPphys
+ ((vm_paddr_t
)j
<< PAGE_SHIFT
)) |
2035 pmap
->pmap_bits
[PG_RW_IDX
] |
2036 pmap
->pmap_bits
[PG_V_IDX
] |
2037 pmap
->pmap_bits
[PG_U_IDX
];
2039 for (j
= 0; j
< NKPML4E
; ++j
) {
2040 pmap
->pm_pml4
[KPML4I
+ j
] =
2041 (KPDPphys
+ ((vm_paddr_t
)j
<< PAGE_SHIFT
)) |
2042 pmap
->pmap_bits
[PG_RW_IDX
] |
2043 pmap
->pmap_bits
[PG_V_IDX
] |
2044 pmap
->pmap_bits
[PG_U_IDX
];
2048 * install self-referential address mapping entry
2050 pmap
->pm_pml4
[PML4PML4I
] = VM_PAGE_TO_PHYS(pv
->pv_m
) |
2051 pmap
->pmap_bits
[PG_V_IDX
] |
2052 pmap
->pmap_bits
[PG_RW_IDX
] |
2053 pmap
->pmap_bits
[PG_A_IDX
] |
2054 pmap
->pmap_bits
[PG_M_IDX
];
2056 KKASSERT(pv
->pv_m
->flags
& PG_MAPPED
);
2057 KKASSERT(pv
->pv_m
->flags
& PG_WRITEABLE
);
2059 KKASSERT(pmap
->pm_pml4
[255] == 0);
2062 * When implementing an isolated userland pmap, a second PML4e table
2063 * is needed. We use pmap_pml4_pindex() + 1 for convenience, but
2064 * note that we do not operate on this table using our API functions
2065 * so handling of the + 1 case is mostly just to prevent implosions.
2067 if ((pv
= pmap
->pm_pmlpv_iso
) == NULL
&& vm_isolated_user_pmap
) {
2068 pv
= pmap_allocpte(pmap
, pmap_pml4_pindex() + 1, NULL
);
2069 pmap
->pm_pmlpv_iso
= pv
;
2070 pmap_kenter((vm_offset_t
)pmap
->pm_pml4_iso
,
2071 VM_PAGE_TO_PHYS(pv
->pv_m
));
2075 * Install just enough KMAP for our trampoline. DMAP not
2076 * needed at all. XXX
2078 for (j
= 0; j
< NKPML4E
; ++j
) {
2079 pmap
->pm_pml4_iso
[KPML4I
+ j
] =
2080 (KPDPphys
+ ((vm_paddr_t
)j
<< PAGE_SHIFT
)) |
2081 pmap
->pmap_bits
[PG_RW_IDX
] |
2082 pmap
->pmap_bits
[PG_V_IDX
] |
2083 pmap
->pmap_bits
[PG_U_IDX
];
2085 KKASSERT(pmap
->pm_pml4_iso
[255] == 0);
2087 KKASSERT(pv
->pv_m
->flags
& PG_MAPPED
);
2088 KKASSERT(pv
->pv_m
->flags
& PG_WRITEABLE
);
2093 * Clean up a pmap structure so it can be physically freed. This routine
2094 * is called by the vmspace dtor function. A great deal of pmap data is
2095 * left passively mapped to improve vmspace management so we have a bit
2096 * of cleanup work to do here.
2099 pmap_puninit(pmap_t pmap
)
2104 KKASSERT(CPUMASK_TESTZERO(pmap
->pm_active
));
2105 if ((pv
= pmap
->pm_pmlpv
) != NULL
) {
2106 if (pv_hold_try(pv
) == 0)
2108 KKASSERT(pv
== pmap
->pm_pmlpv
);
2109 p
= pmap_remove_pv_page(pv
);
2111 pv
= NULL
; /* safety */
2112 pmap_kremove((vm_offset_t
)pmap
->pm_pml4
);
2113 vm_page_busy_wait(p
, FALSE
, "pgpun");
2114 KKASSERT(p
->flags
& (PG_FICTITIOUS
|PG_UNMANAGED
));
2115 vm_page_unwire(p
, 0);
2116 vm_page_flag_clear(p
, PG_MAPPED
| PG_WRITEABLE
);
2118 pmap
->pm_pmlpv
= NULL
;
2120 if ((pv
= pmap
->pm_pmlpv_iso
) != NULL
) {
2121 if (pv_hold_try(pv
) == 0)
2123 KKASSERT(pv
== pmap
->pm_pmlpv_iso
);
2124 p
= pmap_remove_pv_page(pv
);
2126 pv
= NULL
; /* safety */
2127 pmap_kremove((vm_offset_t
)pmap
->pm_pml4_iso
);
2128 vm_page_busy_wait(p
, FALSE
, "pgpun");
2129 KKASSERT(p
->flags
& (PG_FICTITIOUS
|PG_UNMANAGED
));
2130 vm_page_unwire(p
, 0);
2131 vm_page_flag_clear(p
, PG_MAPPED
| PG_WRITEABLE
);
2133 pmap
->pm_pmlpv_iso
= NULL
;
2135 if (pmap
->pm_pml4
) {
2136 KKASSERT(pmap
->pm_pml4
!= (void *)(PTOV_OFFSET
+ KPML4phys
));
2137 kmem_free(&kernel_map
,
2138 (vm_offset_t
)pmap
->pm_pml4
, PAGE_SIZE
* 2);
2139 pmap
->pm_pml4
= NULL
;
2140 pmap
->pm_pml4_iso
= NULL
;
2142 KKASSERT(pmap
->pm_stats
.resident_count
== 0);
2143 KKASSERT(pmap
->pm_stats
.wired_count
== 0);
2147 * This function is now unused (used to add the pmap to the pmap_list)
2150 pmap_pinit2(struct pmap
*pmap
)
2155 * This routine is called when various levels in the page table need to
2156 * be populated. This routine cannot fail.
2158 * This function returns two locked pv_entry's, one representing the
2159 * requested pv and one representing the requested pv's parent pv. If
2160 * an intermediate page table does not exist it will be created, mapped,
2161 * wired, and the parent page table will be given an additional hold
2162 * count representing the presence of the child pv_entry.
2166 pmap_allocpte(pmap_t pmap
, vm_pindex_t ptepindex
, pv_entry_t
*pvpp
)
2169 pt_entry_t
*ptep_iso
;
2173 vm_pindex_t pt_pindex
;
2179 * If the pv already exists and we aren't being asked for the
2180 * parent page table page we can just return it. A locked+held pv
2181 * is returned. The pv will also have a second hold related to the
2182 * pmap association that we don't have to worry about.
2185 pv
= pv_alloc(pmap
, ptepindex
, &isnew
);
2186 if (isnew
== 0 && pvpp
== NULL
)
2190 * Special case terminal PVs. These are not page table pages so
2191 * no vm_page is allocated (the caller supplied the vm_page). If
2192 * pvpp is non-NULL we are being asked to also removed the pt_pv
2195 * Note that pt_pv's are only returned for user VAs. We assert that
2196 * a pt_pv is not being requested for kernel VAs. The kernel
2197 * pre-wires all higher-level page tables so don't overload managed
2198 * higher-level page tables on top of it!
2200 if (ptepindex
< pmap_pt_pindex(0)) {
2201 if (ptepindex
>= NUPTE_USER
) {
2202 /* kernel manages this manually for KVM */
2203 KKASSERT(pvpp
== NULL
);
2205 KKASSERT(pvpp
!= NULL
);
2206 pt_pindex
= NUPTE_TOTAL
+ (ptepindex
>> NPTEPGSHIFT
);
2207 pvp
= pmap_allocpte(pmap
, pt_pindex
, NULL
);
2209 vm_page_wire_quick(pvp
->pv_m
);
2216 * The kernel never uses managed PT/PD/PDP pages.
2218 KKASSERT(pmap
!= &kernel_pmap
);
2221 * Non-terminal PVs allocate a VM page to represent the page table,
2222 * so we have to resolve pvp and calculate ptepindex for the pvp
2223 * and then for the page table entry index in the pvp for
2226 if (ptepindex
< pmap_pd_pindex(0)) {
2228 * pv is PT, pvp is PD
2230 ptepindex
= (ptepindex
- pmap_pt_pindex(0)) >> NPDEPGSHIFT
;
2231 ptepindex
+= NUPTE_TOTAL
+ NUPT_TOTAL
;
2232 pvp
= pmap_allocpte(pmap
, ptepindex
, NULL
);
2237 ptepindex
= pv
->pv_pindex
- pmap_pt_pindex(0);
2238 ptepindex
&= ((1ul << NPDEPGSHIFT
) - 1);
2240 } else if (ptepindex
< pmap_pdp_pindex(0)) {
2242 * pv is PD, pvp is PDP
2244 * SIMPLE PMAP NOTE: Simple pmaps do not allocate above
2247 ptepindex
= (ptepindex
- pmap_pd_pindex(0)) >> NPDPEPGSHIFT
;
2248 ptepindex
+= NUPTE_TOTAL
+ NUPT_TOTAL
+ NUPD_TOTAL
;
2250 if (pmap
->pm_flags
& PMAP_FLAG_SIMPLE
) {
2251 KKASSERT(pvpp
== NULL
);
2254 pvp
= pmap_allocpte(pmap
, ptepindex
, NULL
);
2260 ptepindex
= pv
->pv_pindex
- pmap_pd_pindex(0);
2261 ptepindex
&= ((1ul << NPDPEPGSHIFT
) - 1);
2262 } else if (ptepindex
< pmap_pml4_pindex()) {
2264 * pv is PDP, pvp is the root pml4 table
2266 pvp
= pmap_allocpte(pmap
, pmap_pml4_pindex(), NULL
);
2271 ptepindex
= pv
->pv_pindex
- pmap_pdp_pindex(0);
2272 ptepindex
&= ((1ul << NPML4EPGSHIFT
) - 1);
2275 * pv represents the top-level PML4, there is no parent.
2284 * (isnew) is TRUE, pv is not terminal.
2286 * (1) Add a wire count to the parent page table (pvp).
2287 * (2) Allocate a VM page for the page table.
2288 * (3) Enter the VM page into the parent page table.
2290 * page table pages are marked PG_WRITEABLE and PG_MAPPED.
2293 vm_page_wire_quick(pvp
->pv_m
);
2296 m
= vm_page_alloc(NULL
, pv
->pv_pindex
,
2297 VM_ALLOC_NORMAL
| VM_ALLOC_SYSTEM
|
2298 VM_ALLOC_INTERRUPT
);
2303 vm_page_wire(m
); /* wire for mapping in parent */
2304 vm_page_unmanage(m
); /* m must be spinunlocked */
2305 pmap_zero_page(VM_PAGE_TO_PHYS(m
));
2306 m
->valid
= VM_PAGE_BITS_ALL
;
2308 vm_page_spin_lock(m
);
2309 pmap_page_stats_adding(m
);
2310 TAILQ_INSERT_TAIL(&m
->md
.pv_list
, pv
, pv_list
);
2312 vm_page_flag_set(m
, PG_MAPPED
| PG_WRITEABLE
);
2313 vm_page_spin_unlock(m
);
2316 * (isnew) is TRUE, pv is not terminal.
2318 * Wire the page into pvp. Bump the resident_count for the pmap.
2319 * There is no pvp for the top level, address the pm_pml4[] array
2322 * If the caller wants the parent we return it, otherwise
2323 * we just put it away.
2325 * No interlock is needed for pte 0 -> non-zero.
2327 * In the situation where *ptep is valid we might have an unmanaged
2328 * page table page shared from another page table which we need to
2329 * unshare before installing our private page table page.
2332 v
= VM_PAGE_TO_PHYS(m
) |
2333 (pmap
->pmap_bits
[PG_U_IDX
] |
2334 pmap
->pmap_bits
[PG_RW_IDX
] |
2335 pmap
->pmap_bits
[PG_V_IDX
] |
2336 pmap
->pmap_bits
[PG_A_IDX
] |
2337 pmap
->pmap_bits
[PG_M_IDX
]);
2338 ptep
= pv_pte_lookup(pvp
, ptepindex
);
2339 if (pvp
== pmap
->pm_pmlpv
&& pmap
->pm_pmlpv_iso
)
2340 ptep_iso
= pv_pte_lookup(pmap
->pm_pmlpv_iso
, ptepindex
);
2343 if (*ptep
& pmap
->pmap_bits
[PG_V_IDX
]) {
2347 panic("pmap_allocpte: unexpected pte %p/%d",
2348 pvp
, (int)ptepindex
);
2350 pte
= pmap_inval_smp(pmap
, (vm_offset_t
)-1, 1,
2353 pmap_inval_smp(pmap
, (vm_offset_t
)-1, 1,
2356 if (vm_page_unwire_quick(
2357 PHYS_TO_VM_PAGE(pte
& PG_FRAME
))) {
2358 panic("pmap_allocpte: shared pgtable "
2359 "pg bad wirecount");
2364 pte
= atomic_swap_long(ptep
, v
);
2366 atomic_swap_long(ptep_iso
, v
);
2368 kprintf("install pgtbl mixup 0x%016jx "
2369 "old/new 0x%016jx/0x%016jx\n",
2370 (intmax_t)ptepindex
, pte
, v
);
2377 * (isnew) may be TRUE or FALSE, pv may or may not be terminal.
2381 KKASSERT(pvp
->pv_m
!= NULL
);
2382 ptep
= pv_pte_lookup(pvp
, ptepindex
);
2383 v
= VM_PAGE_TO_PHYS(pv
->pv_m
) |
2384 (pmap
->pmap_bits
[PG_U_IDX
] |
2385 pmap
->pmap_bits
[PG_RW_IDX
] |
2386 pmap
->pmap_bits
[PG_V_IDX
] |
2387 pmap
->pmap_bits
[PG_A_IDX
] |
2388 pmap
->pmap_bits
[PG_M_IDX
]);
2390 kprintf("mismatched upper level pt %016jx/%016jx\n",
2402 * This version of pmap_allocpte() checks for possible segment optimizations
2403 * that would allow page-table sharing. It can be called for terminal
2404 * page or page table page ptepindex's.
2406 * The function is called with page table page ptepindex's for fictitious
2407 * and unmanaged terminal pages. That is, we don't want to allocate a
2408 * terminal pv, we just want the pt_pv. pvpp is usually passed as NULL
2411 * This function can return a pv and *pvpp associated with the passed in pmap
2412 * OR a pv and *pvpp associated with the shared pmap. In the latter case
2413 * an unmanaged page table page will be entered into the pass in pmap.
2417 pmap_allocpte_seg(pmap_t pmap
, vm_pindex_t ptepindex
, pv_entry_t
*pvpp
,
2418 vm_map_entry_t entry
, vm_offset_t va
)
2423 vm_pindex_t
*pt_placemark
;
2425 pv_entry_t pte_pv
; /* in original or shared pmap */
2426 pv_entry_t pt_pv
; /* in original or shared pmap */
2427 pv_entry_t proc_pd_pv
; /* in original pmap */
2428 pv_entry_t proc_pt_pv
; /* in original pmap */
2429 pv_entry_t xpv
; /* PT in shared pmap */
2430 pd_entry_t
*pt
; /* PT entry in PD of original pmap */
2431 pd_entry_t opte
; /* contents of *pt */
2432 pd_entry_t npte
; /* contents of *pt */
2437 * Basic tests, require a non-NULL vm_map_entry, require proper
2438 * alignment and type for the vm_map_entry, require that the
2439 * underlying object already be allocated.
2441 * We allow almost any type of object to use this optimization.
2442 * The object itself does NOT have to be sized to a multiple of the
2443 * segment size, but the memory mapping does.
2445 * XXX don't handle devices currently, because VM_PAGE_TO_PHYS()
2446 * won't work as expected.
2448 if (entry
== NULL
||
2449 pmap_mmu_optimize
== 0 || /* not enabled */
2450 (pmap
->pm_flags
& PMAP_HVM
) || /* special pmap */
2451 ptepindex
>= pmap_pd_pindex(0) || /* not terminal or pt */
2452 entry
->inheritance
!= VM_INHERIT_SHARE
|| /* not shared */
2453 entry
->maptype
!= VM_MAPTYPE_NORMAL
|| /* weird map type */
2454 entry
->object
.vm_object
== NULL
|| /* needs VM object */
2455 entry
->object
.vm_object
->type
== OBJT_DEVICE
|| /* ick */
2456 entry
->object
.vm_object
->type
== OBJT_MGTDEVICE
|| /* ick */
2457 (entry
->offset
& SEG_MASK
) || /* must be aligned */
2458 (entry
->start
& SEG_MASK
)) {
2459 return(pmap_allocpte(pmap
, ptepindex
, pvpp
));
2463 * Make sure the full segment can be represented.
2465 b
= va
& ~(vm_offset_t
)SEG_MASK
;
2466 if (b
< entry
->start
|| b
+ SEG_SIZE
> entry
->end
)
2467 return(pmap_allocpte(pmap
, ptepindex
, pvpp
));
2470 * If the full segment can be represented dive the VM object's
2471 * shared pmap, allocating as required.
2473 object
= entry
->object
.vm_object
;
2475 if (entry
->protection
& VM_PROT_WRITE
)
2476 obpmapp
= &object
->md
.pmap_rw
;
2478 obpmapp
= &object
->md
.pmap_ro
;
2481 if (pmap_enter_debug
> 0) {
2483 kprintf("pmap_allocpte_seg: va=%jx prot %08x o=%p "
2485 va
, entry
->protection
, object
,
2487 kprintf("pmap_allocpte_seg: entry %p %jx-%jx\n",
2488 entry
, entry
->start
, entry
->end
);
2493 * We allocate what appears to be a normal pmap but because portions
2494 * of this pmap are shared with other unrelated pmaps we have to
2495 * set pm_active to point to all cpus.
2497 * XXX Currently using pmap_spin to interlock the update, can't use
2498 * vm_object_hold/drop because the token might already be held
2499 * shared OR exclusive and we don't know.
2501 while ((obpmap
= *obpmapp
) == NULL
) {
2502 obpmap
= kmalloc(sizeof(*obpmap
), M_OBJPMAP
, M_WAITOK
|M_ZERO
);
2503 pmap_pinit_simple(obpmap
);
2504 pmap_pinit2(obpmap
);
2505 spin_lock(&pmap_spin
);
2506 if (*obpmapp
!= NULL
) {
2510 spin_unlock(&pmap_spin
);
2511 pmap_release(obpmap
);
2512 pmap_puninit(obpmap
);
2513 kfree(obpmap
, M_OBJPMAP
);
2514 obpmap
= *obpmapp
; /* safety */
2516 obpmap
->pm_active
= smp_active_mask
;
2517 obpmap
->pm_flags
|= PMAP_SEGSHARED
;
2519 spin_unlock(&pmap_spin
);
2524 * Layering is: PTE, PT, PD, PDP, PML4. We have to return the
2525 * pte/pt using the shared pmap from the object but also adjust
2526 * the process pmap's page table page as a side effect.
2530 * Resolve the terminal PTE and PT in the shared pmap. This is what
2531 * we will return. This is true if ptepindex represents a terminal
2532 * page, otherwise pte_pv is actually the PT and pt_pv is actually
2536 pte_pv
= pmap_allocpte(obpmap
, ptepindex
, &pt_pv
);
2539 if (ptepindex
>= pmap_pt_pindex(0))
2545 * Resolve the PD in the process pmap so we can properly share the
2546 * page table page. Lock order is bottom-up (leaf first)!
2548 * NOTE: proc_pt_pv can be NULL.
2550 proc_pt_pv
= pv_get(pmap
, pmap_pt_pindex(b
), &pt_placemark
);
2551 proc_pd_pv
= pmap_allocpte(pmap
, pmap_pd_pindex(b
), NULL
);
2553 if (pmap_enter_debug
> 0) {
2555 kprintf("proc_pt_pv %p (wc %d) pd_pv %p va=%jx\n",
2557 (proc_pt_pv
? proc_pt_pv
->pv_m
->wire_count
: -1),
2564 * xpv is the page table page pv from the shared object
2565 * (for convenience), from above.
2567 * Calculate the pte value for the PT to load into the process PD.
2568 * If we have to change it we must properly dispose of the previous
2571 pt
= pv_pte_lookup(proc_pd_pv
, pmap_pt_index(b
));
2572 npte
= VM_PAGE_TO_PHYS(xpv
->pv_m
) |
2573 (pmap
->pmap_bits
[PG_U_IDX
] |
2574 pmap
->pmap_bits
[PG_RW_IDX
] |
2575 pmap
->pmap_bits
[PG_V_IDX
] |
2576 pmap
->pmap_bits
[PG_A_IDX
] |
2577 pmap
->pmap_bits
[PG_M_IDX
]);
2580 * Dispose of previous page table page if it was local to the
2581 * process pmap. If the old pt is not empty we cannot dispose of it
2582 * until we clean it out. This case should not arise very often so
2583 * it is not optimized.
2585 * Leave pt_pv and pte_pv (in our object pmap) locked and intact
2589 pmap_inval_bulk_t bulk
;
2591 if (proc_pt_pv
->pv_m
->wire_count
!= 1) {
2593 * The page table has a bunch of stuff in it
2594 * which we have to scrap.
2596 if (softhold
== 0) {
2598 pmap_softhold(pmap
);
2603 va
& ~(vm_offset_t
)SEG_MASK
,
2604 (va
+ SEG_SIZE
) & ~(vm_offset_t
)SEG_MASK
);
2607 * The page table is empty and can be destroyed.
2608 * However, doing so leaves the pt slot unlocked,
2609 * so we have to loop-up to handle any races until
2610 * we get a NULL proc_pt_pv and a proper pt_placemark.
2612 pmap_inval_bulk_init(&bulk
, proc_pt_pv
->pv_pmap
);
2613 pmap_release_pv(proc_pt_pv
, proc_pd_pv
, &bulk
);
2614 pmap_inval_bulk_flush(&bulk
);
2621 * Handle remaining cases. We are holding pt_placemark to lock
2622 * the page table page in the primary pmap while we manipulate
2626 atomic_swap_long(pt
, npte
);
2627 vm_page_wire_quick(xpv
->pv_m
); /* shared pt -> proc */
2628 vm_page_wire_quick(proc_pd_pv
->pv_m
); /* proc pd for sh pt */
2629 atomic_add_long(&pmap
->pm_stats
.resident_count
, 1);
2630 } else if (*pt
!= npte
) {
2631 opte
= pmap_inval_smp(pmap
, (vm_offset_t
)-1, 1, pt
, npte
);
2634 opte
= pte_load_clear(pt
);
2635 KKASSERT(opte
&& opte
!= npte
);
2639 vm_page_wire_quick(xpv
->pv_m
); /* shared pt -> proc */
2642 * Clean up opte, bump the wire_count for the process
2643 * PD page representing the new entry if it was
2646 * If the entry was not previously empty and we have
2647 * a PT in the proc pmap then opte must match that
2648 * pt. The proc pt must be retired (this is done
2649 * later on in this procedure).
2651 * NOTE: replacing valid pte, wire_count on proc_pd_pv
2654 KKASSERT(opte
& pmap
->pmap_bits
[PG_V_IDX
]);
2655 m
= PHYS_TO_VM_PAGE(opte
& PG_FRAME
);
2656 if (vm_page_unwire_quick(m
)) {
2657 panic("pmap_allocpte_seg: "
2658 "bad wire count %p",
2664 pmap_softdone(pmap
);
2667 * Remove our earmark on the page table page.
2669 pv_placemarker_wakeup(pmap
, pt_placemark
);
2672 * The existing process page table was replaced and must be destroyed
2685 * Release any resources held by the given physical map.
2687 * Called when a pmap initialized by pmap_pinit is being released. Should
2688 * only be called if the map contains no valid mappings.
2690 struct pmap_release_info
{
2696 static int pmap_release_callback(pv_entry_t pv
, void *data
);
2699 pmap_release(struct pmap
*pmap
)
2701 struct pmap_release_info info
;
2703 KASSERT(CPUMASK_TESTZERO(pmap
->pm_active
),
2704 ("pmap still active! %016jx",
2705 (uintmax_t)CPUMASK_LOWMASK(pmap
->pm_active
)));
2708 * There is no longer a pmap_list, if there were we would remove the
2709 * pmap from it here.
2713 * Pull pv's off the RB tree in order from low to high and release
2721 spin_lock(&pmap
->pm_spin
);
2722 RB_SCAN(pv_entry_rb_tree
, &pmap
->pm_pvroot
, NULL
,
2723 pmap_release_callback
, &info
);
2724 spin_unlock(&pmap
->pm_spin
);
2728 } while (info
.retry
);
2732 * One resident page (the pml4 page) should remain. Two if
2733 * the pmap has implemented an isolated userland PML4E table.
2734 * No wired pages should remain.
2736 int expected_res
= 0;
2738 if ((pmap
->pm_flags
& PMAP_FLAG_SIMPLE
) == 0)
2740 if (pmap
->pm_pmlpv_iso
)
2744 if (pmap
->pm_stats
.resident_count
!= expected_res
||
2745 pmap
->pm_stats
.wired_count
!= 0) {
2746 kprintf("fatal pmap problem - pmap %p flags %08x "
2747 "rescnt=%jd wirecnt=%jd\n",
2750 pmap
->pm_stats
.resident_count
,
2751 pmap
->pm_stats
.wired_count
);
2752 tsleep(pmap
, 0, "DEAD", 0);
2755 KKASSERT(pmap
->pm_stats
.resident_count
== expected_res
);
2756 KKASSERT(pmap
->pm_stats
.wired_count
== 0);
2761 * Called from low to high. We must cache the proper parent pv so we
2762 * can adjust its wired count.
2765 pmap_release_callback(pv_entry_t pv
, void *data
)
2767 struct pmap_release_info
*info
= data
;
2768 pmap_t pmap
= info
->pmap
;
2773 * Acquire a held and locked pv, check for release race
2775 pindex
= pv
->pv_pindex
;
2776 if (info
->pvp
== pv
) {
2777 spin_unlock(&pmap
->pm_spin
);
2779 } else if (pv_hold_try(pv
)) {
2780 spin_unlock(&pmap
->pm_spin
);
2782 spin_unlock(&pmap
->pm_spin
);
2786 spin_lock(&pmap
->pm_spin
);
2790 KKASSERT(pv
->pv_pmap
== pmap
&& pindex
== pv
->pv_pindex
);
2792 if (pv
->pv_pindex
< pmap_pt_pindex(0)) {
2794 * I am PTE, parent is PT
2796 pindex
= pv
->pv_pindex
>> NPTEPGSHIFT
;
2797 pindex
+= NUPTE_TOTAL
;
2798 } else if (pv
->pv_pindex
< pmap_pd_pindex(0)) {
2800 * I am PT, parent is PD
2802 pindex
= (pv
->pv_pindex
- NUPTE_TOTAL
) >> NPDEPGSHIFT
;
2803 pindex
+= NUPTE_TOTAL
+ NUPT_TOTAL
;
2804 } else if (pv
->pv_pindex
< pmap_pdp_pindex(0)) {
2806 * I am PD, parent is PDP
2808 pindex
= (pv
->pv_pindex
- NUPTE_TOTAL
- NUPT_TOTAL
) >>
2810 pindex
+= NUPTE_TOTAL
+ NUPT_TOTAL
+ NUPD_TOTAL
;
2811 } else if (pv
->pv_pindex
< pmap_pml4_pindex()) {
2813 * I am PDP, parent is PML4. We always calculate the
2814 * normal PML4 here, not the isolated PML4.
2816 pindex
= pmap_pml4_pindex();
2828 if (info
->pvp
&& info
->pvp
->pv_pindex
!= pindex
) {
2832 if (info
->pvp
== NULL
)
2833 info
->pvp
= pv_get(pmap
, pindex
, NULL
);
2840 r
= pmap_release_pv(pv
, info
->pvp
, NULL
);
2841 spin_lock(&pmap
->pm_spin
);
2847 * Called with held (i.e. also locked) pv. This function will dispose of
2848 * the lock along with the pv.
2850 * If the caller already holds the locked parent page table for pv it
2851 * must pass it as pvp, allowing us to avoid a deadlock, else it can
2852 * pass NULL for pvp.
2855 pmap_release_pv(pv_entry_t pv
, pv_entry_t pvp
, pmap_inval_bulk_t
*bulk
)
2860 * The pmap is currently not spinlocked, pv is held+locked.
2861 * Remove the pv's page from its parent's page table. The
2862 * parent's page table page's wire_count will be decremented.
2864 * This will clean out the pte at any level of the page table.
2865 * If smp != 0 all cpus are affected.
2867 * Do not tear-down recursively, its faster to just let the
2868 * release run its course.
2870 pmap_remove_pv_pte(pv
, pvp
, bulk
, 0);
2873 * Terminal pvs are unhooked from their vm_pages. Because
2874 * terminal pages aren't page table pages they aren't wired
2875 * by us, so we have to be sure not to unwire them either.
2877 if (pv
->pv_pindex
< pmap_pt_pindex(0)) {
2878 pmap_remove_pv_page(pv
);
2883 * We leave the top-level page table page cached, wired, and
2884 * mapped in the pmap until the dtor function (pmap_puninit())
2887 * Since we are leaving the top-level pv intact we need
2888 * to break out of what would otherwise be an infinite loop.
2890 * This covers both the normal and the isolated PML4 page.
2892 if (pv
->pv_pindex
>= pmap_pml4_pindex()) {
2898 * For page table pages (other than the top-level page),
2899 * remove and free the vm_page. The representitive mapping
2900 * removed above by pmap_remove_pv_pte() did not undo the
2901 * last wire_count so we have to do that as well.
2903 p
= pmap_remove_pv_page(pv
);
2904 vm_page_busy_wait(p
, FALSE
, "pmaprl");
2905 if (p
->wire_count
!= 1) {
2906 kprintf("p->wire_count was %016lx %d\n",
2907 pv
->pv_pindex
, p
->wire_count
);
2909 KKASSERT(p
->wire_count
== 1);
2910 KKASSERT(p
->flags
& PG_UNMANAGED
);
2912 vm_page_unwire(p
, 0);
2913 KKASSERT(p
->wire_count
== 0);
2923 * This function will remove the pte associated with a pv from its parent.
2924 * Terminal pv's are supported. All cpus specified by (bulk) are properly
2927 * The wire count will be dropped on the parent page table. The wire
2928 * count on the page being removed (pv->pv_m) from the parent page table
2929 * is NOT touched. Note that terminal pages will not have any additional
2930 * wire counts while page table pages will have at least one representing
2931 * the mapping, plus others representing sub-mappings.
2933 * NOTE: Cannot be called on kernel page table pages, only KVM terminal
2934 * pages and user page table and terminal pages.
2936 * NOTE: The pte being removed might be unmanaged, and the pv supplied might
2937 * be freshly allocated and not imply that the pte is managed. In this
2938 * case pv->pv_m should be NULL.
2940 * The pv must be locked. The pvp, if supplied, must be locked. All
2941 * supplied pv's will remain locked on return.
2943 * XXX must lock parent pv's if they exist to remove pte XXX
2947 pmap_remove_pv_pte(pv_entry_t pv
, pv_entry_t pvp
, pmap_inval_bulk_t
*bulk
,
2950 vm_pindex_t ptepindex
= pv
->pv_pindex
;
2951 pmap_t pmap
= pv
->pv_pmap
;
2957 if (ptepindex
>= pmap_pml4_pindex()) {
2959 * We are the top level PML4E table, there is no parent.
2961 * This is either the normal or isolated PML4E table.
2962 * Only the normal is used in regular operation, the isolated
2963 * is only passed in when breaking down the whole pmap.
2965 p
= pmap
->pm_pmlpv
->pv_m
;
2966 KKASSERT(pv
->pv_m
== p
); /* debugging */
2967 } else if (ptepindex
>= pmap_pdp_pindex(0)) {
2969 * Remove a PDP page from the PML4E. This can only occur
2970 * with user page tables. We do not have to lock the
2971 * pml4 PV so just ignore pvp.
2973 vm_pindex_t pml4_pindex
;
2974 vm_pindex_t pdp_index
;
2976 pml4_entry_t
*pdp_iso
;
2978 pdp_index
= ptepindex
- pmap_pdp_pindex(0);
2980 pml4_pindex
= pmap_pml4_pindex();
2981 pvp
= pv_get(pv
->pv_pmap
, pml4_pindex
, NULL
);
2986 pdp
= &pmap
->pm_pml4
[pdp_index
& ((1ul << NPML4EPGSHIFT
) - 1)];
2987 KKASSERT((*pdp
& pmap
->pmap_bits
[PG_V_IDX
]) != 0);
2988 p
= PHYS_TO_VM_PAGE(*pdp
& PG_FRAME
);
2989 pmap_inval_bulk(bulk
, (vm_offset_t
)-1, pdp
, 0);
2992 * Also remove the PDP from the isolated PML4E if the
2995 if (pvp
== pmap
->pm_pmlpv
&& pmap
->pm_pmlpv_iso
) {
2996 pdp_iso
= &pmap
->pm_pml4_iso
[pdp_index
&
2997 ((1ul << NPML4EPGSHIFT
) - 1)];
2998 pmap_inval_bulk(bulk
, (vm_offset_t
)-1, pdp_iso
, 0);
3000 KKASSERT(pv
->pv_m
== p
); /* debugging */
3001 } else if (ptepindex
>= pmap_pd_pindex(0)) {
3003 * Remove a PD page from the PDP
3005 * SIMPLE PMAP NOTE: Non-existant pvp's are ok in the case
3006 * of a simple pmap because it stops at
3009 vm_pindex_t pdp_pindex
;
3010 vm_pindex_t pd_index
;
3013 pd_index
= ptepindex
- pmap_pd_pindex(0);
3016 pdp_pindex
= NUPTE_TOTAL
+ NUPT_TOTAL
+ NUPD_TOTAL
+
3017 (pd_index
>> NPML4EPGSHIFT
);
3018 pvp
= pv_get(pv
->pv_pmap
, pdp_pindex
, NULL
);
3023 pd
= pv_pte_lookup(pvp
, pd_index
&
3024 ((1ul << NPDPEPGSHIFT
) - 1));
3025 KKASSERT((*pd
& pmap
->pmap_bits
[PG_V_IDX
]) != 0);
3026 p
= PHYS_TO_VM_PAGE(*pd
& PG_FRAME
);
3027 pmap_inval_bulk(bulk
, (vm_offset_t
)-1, pd
, 0);
3029 KKASSERT(pmap
->pm_flags
& PMAP_FLAG_SIMPLE
);
3030 p
= pv
->pv_m
; /* degenerate test later */
3032 KKASSERT(pv
->pv_m
== p
); /* debugging */
3033 } else if (ptepindex
>= pmap_pt_pindex(0)) {
3035 * Remove a PT page from the PD
3037 vm_pindex_t pd_pindex
;
3038 vm_pindex_t pt_index
;
3041 pt_index
= ptepindex
- pmap_pt_pindex(0);
3044 pd_pindex
= NUPTE_TOTAL
+ NUPT_TOTAL
+
3045 (pt_index
>> NPDPEPGSHIFT
);
3046 pvp
= pv_get(pv
->pv_pmap
, pd_pindex
, NULL
);
3051 pt
= pv_pte_lookup(pvp
, pt_index
& ((1ul << NPDPEPGSHIFT
) - 1));
3053 KASSERT((*pt
& pmap
->pmap_bits
[PG_V_IDX
]) != 0,
3054 ("*pt unexpectedly invalid %016jx "
3055 "gotpvp=%d ptepindex=%ld ptindex=%ld pv=%p pvp=%p",
3056 *pt
, gotpvp
, ptepindex
, pt_index
, pv
, pvp
));
3057 p
= PHYS_TO_VM_PAGE(*pt
& PG_FRAME
);
3059 if ((*pt
& pmap
->pmap_bits
[PG_V_IDX
]) == 0) {
3060 kprintf("*pt unexpectedly invalid %016jx "
3061 "gotpvp=%d ptepindex=%ld ptindex=%ld "
3063 *pt
, gotpvp
, ptepindex
, pt_index
, pv
, pvp
);
3064 tsleep(pt
, 0, "DEAD", 0);
3067 p
= PHYS_TO_VM_PAGE(*pt
& PG_FRAME
);
3070 pmap_inval_bulk(bulk
, (vm_offset_t
)-1, pt
, 0);
3071 KKASSERT(pv
->pv_m
== p
); /* debugging */
3074 * Remove a PTE from the PT page. The PV might exist even if
3075 * the PTE is not managed, in whichcase pv->pv_m should be
3078 * NOTE: Userland pmaps manage the parent PT/PD/PDP page
3079 * table pages but the kernel_pmap does not.
3081 * NOTE: pv's must be locked bottom-up to avoid deadlocking.
3082 * pv is a pte_pv so we can safely lock pt_pv.
3084 * NOTE: FICTITIOUS pages may have multiple physical mappings
3085 * so PHYS_TO_VM_PAGE() will not necessarily work for
3088 vm_pindex_t pt_pindex
;
3093 pt_pindex
= ptepindex
>> NPTEPGSHIFT
;
3094 va
= (vm_offset_t
)ptepindex
<< PAGE_SHIFT
;
3096 if (ptepindex
>= NUPTE_USER
) {
3097 ptep
= vtopte(ptepindex
<< PAGE_SHIFT
);
3098 KKASSERT(pvp
== NULL
);
3099 /* pvp remains NULL */
3102 pt_pindex
= NUPTE_TOTAL
+
3103 (ptepindex
>> NPDPEPGSHIFT
);
3104 pvp
= pv_get(pv
->pv_pmap
, pt_pindex
, NULL
);
3108 ptep
= pv_pte_lookup(pvp
, ptepindex
&
3109 ((1ul << NPDPEPGSHIFT
) - 1));
3111 pte
= pmap_inval_bulk(bulk
, va
, ptep
, 0);
3112 if (bulk
== NULL
) /* XXX */
3113 cpu_invlpg((void *)va
); /* XXX */
3116 * Now update the vm_page_t
3118 if ((pte
& pmap
->pmap_bits
[PG_MANAGED_IDX
]) &&
3119 (pte
& pmap
->pmap_bits
[PG_V_IDX
])) {
3121 * Valid managed page, adjust (p).
3123 if (pte
& pmap
->pmap_bits
[PG_DEVICE_IDX
]) {
3126 p
= PHYS_TO_VM_PAGE(pte
& PG_FRAME
);
3127 KKASSERT(pv
->pv_m
== p
);
3129 if (pte
& pmap
->pmap_bits
[PG_M_IDX
]) {
3130 if (pmap_track_modified(ptepindex
))
3133 if (pte
& pmap
->pmap_bits
[PG_A_IDX
]) {
3134 vm_page_flag_set(p
, PG_REFERENCED
);
3138 * Unmanaged page, do not try to adjust the vm_page_t.
3139 * pv could be freshly allocated for a pmap_enter(),
3140 * replacing an unmanaged page with a managed one.
3142 * pv->pv_m might reflect the new page and not the
3145 * We could extract p from the physical address and
3146 * adjust it but we explicitly do not for unmanaged
3151 if (pte
& pmap
->pmap_bits
[PG_W_IDX
])
3152 atomic_add_long(&pmap
->pm_stats
.wired_count
, -1);
3153 if (pte
& pmap
->pmap_bits
[PG_G_IDX
])
3154 cpu_invlpg((void *)va
);
3158 * If requested, scrap the underlying pv->pv_m and the underlying
3159 * pv. If this is a page-table-page we must also free the page.
3161 * pvp must be returned locked.
3165 * page table page (PT, PD, PDP, PML4), caller was responsible
3166 * for testing wired_count.
3168 KKASSERT(pv
->pv_m
->wire_count
== 1);
3169 p
= pmap_remove_pv_page(pv
);
3173 vm_page_busy_wait(p
, FALSE
, "pgpun");
3174 vm_page_unwire(p
, 0);
3175 vm_page_flag_clear(p
, PG_MAPPED
| PG_WRITEABLE
);
3177 } else if (destroy
== 2) {
3179 * Normal page, remove from pmap and leave the underlying
3182 pmap_remove_pv_page(pv
);
3184 pv
= NULL
; /* safety */
3188 * If we acquired pvp ourselves then we are responsible for
3189 * recursively deleting it.
3191 if (pvp
&& gotpvp
) {
3193 * Recursively destroy higher-level page tables.
3195 * This is optional. If we do not, they will still
3196 * be destroyed when the process exits.
3198 * NOTE: Do not destroy pv_entry's with extra hold refs,
3199 * a caller may have unlocked it and intends to
3200 * continue to use it.
3202 if (pmap_dynamic_delete
&&
3204 pvp
->pv_m
->wire_count
== 1 &&
3205 (pvp
->pv_hold
& PV_HOLD_MASK
) == 2 &&
3206 pvp
->pv_pindex
< pmap_pml4_pindex()) {
3207 if (pmap_dynamic_delete
== 2)
3208 kprintf("A %jd %08x\n", pvp
->pv_pindex
, pvp
->pv_hold
);
3209 if (pmap
!= &kernel_pmap
) {
3210 pmap_remove_pv_pte(pvp
, NULL
, bulk
, 1);
3211 pvp
= NULL
; /* safety */
3213 kprintf("Attempt to remove kernel_pmap pindex "
3214 "%jd\n", pvp
->pv_pindex
);
3224 * Remove the vm_page association to a pv. The pv must be locked.
3228 pmap_remove_pv_page(pv_entry_t pv
)
3233 vm_page_spin_lock(m
);
3234 KKASSERT(m
&& m
== pv
->pv_m
);
3236 TAILQ_REMOVE(&m
->md
.pv_list
, pv
, pv_list
);
3237 pmap_page_stats_deleting(m
);
3238 if (TAILQ_EMPTY(&m
->md
.pv_list
))
3239 vm_page_flag_clear(m
, PG_MAPPED
| PG_WRITEABLE
);
3240 vm_page_spin_unlock(m
);
3246 * Grow the number of kernel page table entries, if needed.
3248 * This routine is always called to validate any address space
3249 * beyond KERNBASE (for kldloads). kernel_vm_end only governs the address
3250 * space below KERNBASE.
3252 * kernel_map must be locked exclusively by the caller.
3255 pmap_growkernel(vm_offset_t kstart
, vm_offset_t kend
)
3258 vm_offset_t ptppaddr
;
3260 pd_entry_t
*pt
, newpt
;
3261 pdp_entry_t
*pd
, newpd
;
3262 int update_kernel_vm_end
;
3265 * bootstrap kernel_vm_end on first real VM use
3267 if (kernel_vm_end
== 0) {
3268 kernel_vm_end
= VM_MIN_KERNEL_ADDRESS
;
3271 pt
= pmap_pt(&kernel_pmap
, kernel_vm_end
);
3274 if ((*pt
& kernel_pmap
.pmap_bits
[PG_V_IDX
]) == 0)
3276 kernel_vm_end
= (kernel_vm_end
+ PAGE_SIZE
* NPTEPG
) &
3277 ~(vm_offset_t
)(PAGE_SIZE
* NPTEPG
- 1);
3278 if (kernel_vm_end
- 1 >= kernel_map
.max_offset
) {
3279 kernel_vm_end
= kernel_map
.max_offset
;
3286 * Fill in the gaps. kernel_vm_end is only adjusted for ranges
3287 * below KERNBASE. Ranges above KERNBASE are kldloaded and we
3288 * do not want to force-fill 128G worth of page tables.
3290 if (kstart
< KERNBASE
) {
3291 if (kstart
> kernel_vm_end
)
3292 kstart
= kernel_vm_end
;
3293 KKASSERT(kend
<= KERNBASE
);
3294 update_kernel_vm_end
= 1;
3296 update_kernel_vm_end
= 0;
3299 kstart
= rounddown2(kstart
, (vm_offset_t
)(PAGE_SIZE
* NPTEPG
));
3300 kend
= roundup2(kend
, (vm_offset_t
)(PAGE_SIZE
* NPTEPG
));
3302 if (kend
- 1 >= kernel_map
.max_offset
)
3303 kend
= kernel_map
.max_offset
;
3305 while (kstart
< kend
) {
3306 pt
= pmap_pt(&kernel_pmap
, kstart
);
3309 * We need a new PD entry
3311 nkpg
= vm_page_alloc(NULL
, mycpu
->gd_rand_incr
++,
3314 VM_ALLOC_INTERRUPT
);
3316 panic("pmap_growkernel: no memory to grow "
3319 paddr
= VM_PAGE_TO_PHYS(nkpg
);
3320 pmap_zero_page(paddr
);
3321 pd
= pmap_pd(&kernel_pmap
, kstart
);
3323 newpd
= (pdp_entry_t
)
3325 kernel_pmap
.pmap_bits
[PG_V_IDX
] |
3326 kernel_pmap
.pmap_bits
[PG_RW_IDX
] |
3327 kernel_pmap
.pmap_bits
[PG_A_IDX
] |
3328 kernel_pmap
.pmap_bits
[PG_M_IDX
]);
3329 atomic_swap_long(pd
, newpd
);
3332 kprintf("NEWPD pd=%p pde=%016jx phys=%016jx\n",
3336 continue; /* try again */
3339 if ((*pt
& kernel_pmap
.pmap_bits
[PG_V_IDX
]) != 0) {
3340 kstart
= (kstart
+ PAGE_SIZE
* NPTEPG
) &
3341 ~(vm_offset_t
)(PAGE_SIZE
* NPTEPG
- 1);
3342 if (kstart
- 1 >= kernel_map
.max_offset
) {
3343 kstart
= kernel_map
.max_offset
;
3352 * This index is bogus, but out of the way
3354 nkpg
= vm_page_alloc(NULL
, mycpu
->gd_rand_incr
++,
3357 VM_ALLOC_INTERRUPT
);
3359 panic("pmap_growkernel: no memory to grow kernel");
3362 ptppaddr
= VM_PAGE_TO_PHYS(nkpg
);
3363 pmap_zero_page(ptppaddr
);
3364 newpt
= (pd_entry_t
)(ptppaddr
|
3365 kernel_pmap
.pmap_bits
[PG_V_IDX
] |
3366 kernel_pmap
.pmap_bits
[PG_RW_IDX
] |
3367 kernel_pmap
.pmap_bits
[PG_A_IDX
] |
3368 kernel_pmap
.pmap_bits
[PG_M_IDX
]);
3369 atomic_swap_long(pt
, newpt
);
3371 kstart
= (kstart
+ PAGE_SIZE
* NPTEPG
) &
3372 ~(vm_offset_t
)(PAGE_SIZE
* NPTEPG
- 1);
3374 if (kstart
- 1 >= kernel_map
.max_offset
) {
3375 kstart
= kernel_map
.max_offset
;
3381 * Only update kernel_vm_end for areas below KERNBASE.
3383 if (update_kernel_vm_end
&& kernel_vm_end
< kstart
)
3384 kernel_vm_end
= kstart
;
3388 * Add a reference to the specified pmap.
3391 pmap_reference(pmap_t pmap
)
3394 atomic_add_int(&pmap
->pm_count
, 1);
3397 /***************************************************
3398 * page management routines.
3399 ***************************************************/
3402 * Hold a pv without locking it
3405 pv_hold(pv_entry_t pv
)
3407 atomic_add_int(&pv
->pv_hold
, 1);
3411 * Hold a pv_entry, preventing its destruction. TRUE is returned if the pv
3412 * was successfully locked, FALSE if it wasn't. The caller must dispose of
3415 * Either the pmap->pm_spin or the related vm_page_spin (if traversing a
3416 * pv list via its page) must be held by the caller in order to stabilize
3420 _pv_hold_try(pv_entry_t pv PMAP_DEBUG_DECL
)
3425 * Critical path shortcut expects pv to already have one ref
3426 * (for the pv->pv_pmap).
3428 if (atomic_cmpset_int(&pv
->pv_hold
, 1, PV_HOLD_LOCKED
| 2)) {
3431 pv
->pv_line
= lineno
;
3437 count
= pv
->pv_hold
;
3439 if ((count
& PV_HOLD_LOCKED
) == 0) {
3440 if (atomic_cmpset_int(&pv
->pv_hold
, count
,
3441 (count
+ 1) | PV_HOLD_LOCKED
)) {
3444 pv
->pv_line
= lineno
;
3449 if (atomic_cmpset_int(&pv
->pv_hold
, count
, count
+ 1))
3457 * Drop a previously held pv_entry which could not be locked, allowing its
3460 * Must not be called with a spinlock held as we might zfree() the pv if it
3461 * is no longer associated with a pmap and this was the last hold count.
3464 pv_drop(pv_entry_t pv
)
3469 count
= pv
->pv_hold
;
3471 KKASSERT((count
& PV_HOLD_MASK
) > 0);
3472 KKASSERT((count
& (PV_HOLD_LOCKED
| PV_HOLD_MASK
)) !=
3473 (PV_HOLD_LOCKED
| 1));
3474 if (atomic_cmpset_int(&pv
->pv_hold
, count
, count
- 1)) {
3475 if ((count
& PV_HOLD_MASK
) == 1) {
3477 if (pmap_enter_debug
> 0) {
3479 kprintf("pv_drop: free pv %p\n", pv
);
3482 KKASSERT(count
== 1);
3483 KKASSERT(pv
->pv_pmap
== NULL
);
3493 * Find or allocate the requested PV entry, returning a locked, held pv.
3495 * If (*isnew) is non-zero, the returned pv will have two hold counts, one
3496 * for the caller and one representing the pmap and vm_page association.
3498 * If (*isnew) is zero, the returned pv will have only one hold count.
3500 * Since both associations can only be adjusted while the pv is locked,
3501 * together they represent just one additional hold.
3505 _pv_alloc(pmap_t pmap
, vm_pindex_t pindex
, int *isnew PMAP_DEBUG_DECL
)
3507 struct mdglobaldata
*md
= mdcpu
;
3515 pnew
= atomic_swap_ptr((void *)&md
->gd_newpv
, NULL
);
3518 pnew
= md
->gd_newpv
; /* might race NULL */
3519 md
->gd_newpv
= NULL
;
3524 pnew
= zalloc(pvzone
);
3526 spin_lock_shared(&pmap
->pm_spin
);
3531 pv
= pv_entry_lookup(pmap
, pindex
);
3536 * Requires exclusive pmap spinlock
3538 if (pmap_excl
== 0) {
3540 if (!spin_lock_upgrade_try(&pmap
->pm_spin
)) {
3541 spin_unlock_shared(&pmap
->pm_spin
);
3542 spin_lock(&pmap
->pm_spin
);
3548 * We need to block if someone is holding our
3549 * placemarker. As long as we determine the
3550 * placemarker has not been aquired we do not
3551 * need to get it as acquision also requires
3552 * the pmap spin lock.
3554 * However, we can race the wakeup.
3556 pmark
= pmap_placemarker_hash(pmap
, pindex
);
3558 if (((*pmark
^ pindex
) & ~PM_PLACEMARK_WAKEUP
) == 0) {
3559 atomic_set_long(pmark
, PM_PLACEMARK_WAKEUP
);
3560 tsleep_interlock(pmark
, 0);
3561 if (((*pmark
^ pindex
) &
3562 ~PM_PLACEMARK_WAKEUP
) == 0) {
3563 spin_unlock(&pmap
->pm_spin
);
3564 tsleep(pmark
, PINTERLOCKED
, "pvplc", 0);
3565 spin_lock(&pmap
->pm_spin
);
3571 * Setup the new entry
3573 pnew
->pv_pmap
= pmap
;
3574 pnew
->pv_pindex
= pindex
;
3575 pnew
->pv_hold
= PV_HOLD_LOCKED
| 2;
3577 pnew
->pv_func
= func
;
3578 pnew
->pv_line
= lineno
;
3579 if (pnew
->pv_line_lastfree
> 0) {
3580 pnew
->pv_line_lastfree
=
3581 -pnew
->pv_line_lastfree
;
3584 pv
= pv_entry_rb_tree_RB_INSERT(&pmap
->pm_pvroot
, pnew
);
3585 atomic_add_long(&pmap
->pm_stats
.resident_count
, 1);
3586 spin_unlock(&pmap
->pm_spin
);
3589 KKASSERT(pv
== NULL
);
3594 * We already have an entry, cleanup the staged pnew if
3595 * we can get the lock, otherwise block and retry.
3597 if (__predict_true(_pv_hold_try(pv PMAP_DEBUG_COPY
))) {
3599 spin_unlock(&pmap
->pm_spin
);
3601 spin_unlock_shared(&pmap
->pm_spin
);
3603 pnew
= atomic_swap_ptr((void *)&md
->gd_newpv
, pnew
);
3605 zfree(pvzone
, pnew
);
3608 if (md
->gd_newpv
== NULL
)
3609 md
->gd_newpv
= pnew
;
3611 zfree(pvzone
, pnew
);
3614 KKASSERT(pv
->pv_pmap
== pmap
&&
3615 pv
->pv_pindex
== pindex
);
3620 spin_unlock(&pmap
->pm_spin
);
3621 _pv_lock(pv PMAP_DEBUG_COPY
);
3623 spin_lock(&pmap
->pm_spin
);
3625 spin_unlock_shared(&pmap
->pm_spin
);
3626 _pv_lock(pv PMAP_DEBUG_COPY
);
3628 spin_lock_shared(&pmap
->pm_spin
);
3635 * Find the requested PV entry, returning a locked+held pv or NULL
3639 _pv_get(pmap_t pmap
, vm_pindex_t pindex
, vm_pindex_t
**pmarkp PMAP_DEBUG_DECL
)
3644 spin_lock_shared(&pmap
->pm_spin
);
3649 pv
= pv_entry_lookup(pmap
, pindex
);
3652 * Block if there is ANY placemarker. If we are to
3653 * return it, we must also aquire the spot, so we
3654 * have to block even if the placemarker is held on
3655 * a different address.
3657 * OPTIMIZATION: If pmarkp is passed as NULL the
3658 * caller is just probing (or looking for a real
3659 * pv_entry), and in this case we only need to check
3660 * to see if the placemarker matches pindex.
3665 * Requires exclusive pmap spinlock
3667 if (pmap_excl
== 0) {
3669 if (!spin_lock_upgrade_try(&pmap
->pm_spin
)) {
3670 spin_unlock_shared(&pmap
->pm_spin
);
3671 spin_lock(&pmap
->pm_spin
);
3676 pmark
= pmap_placemarker_hash(pmap
, pindex
);
3678 if ((pmarkp
&& *pmark
!= PM_NOPLACEMARK
) ||
3679 ((*pmark
^ pindex
) & ~PM_PLACEMARK_WAKEUP
) == 0) {
3680 atomic_set_long(pmark
, PM_PLACEMARK_WAKEUP
);
3681 tsleep_interlock(pmark
, 0);
3682 if ((pmarkp
&& *pmark
!= PM_NOPLACEMARK
) ||
3683 ((*pmark
^ pindex
) &
3684 ~PM_PLACEMARK_WAKEUP
) == 0) {
3685 spin_unlock(&pmap
->pm_spin
);
3686 tsleep(pmark
, PINTERLOCKED
, "pvpld", 0);
3687 spin_lock(&pmap
->pm_spin
);
3692 if (atomic_swap_long(pmark
, pindex
) !=
3694 panic("_pv_get: pmark race");
3698 spin_unlock(&pmap
->pm_spin
);
3701 if (_pv_hold_try(pv PMAP_DEBUG_COPY
)) {
3702 pv_cache(pv
, pindex
);
3704 spin_unlock(&pmap
->pm_spin
);
3706 spin_unlock_shared(&pmap
->pm_spin
);
3707 KKASSERT(pv
->pv_pmap
== pmap
&&
3708 pv
->pv_pindex
== pindex
);
3712 spin_unlock(&pmap
->pm_spin
);
3713 _pv_lock(pv PMAP_DEBUG_COPY
);
3715 spin_lock(&pmap
->pm_spin
);
3717 spin_unlock_shared(&pmap
->pm_spin
);
3718 _pv_lock(pv PMAP_DEBUG_COPY
);
3720 spin_lock_shared(&pmap
->pm_spin
);
3726 * Lookup, hold, and attempt to lock (pmap,pindex).
3728 * If the entry does not exist NULL is returned and *errorp is set to 0
3730 * If the entry exists and could be successfully locked it is returned and
3731 * errorp is set to 0.
3733 * If the entry exists but could NOT be successfully locked it is returned
3734 * held and *errorp is set to 1.
3736 * If the entry is placemarked by someone else NULL is returned and *errorp
3741 pv_get_try(pmap_t pmap
, vm_pindex_t pindex
, vm_pindex_t
**pmarkp
, int *errorp
)
3745 spin_lock_shared(&pmap
->pm_spin
);
3747 pv
= pv_entry_lookup(pmap
, pindex
);
3751 pmark
= pmap_placemarker_hash(pmap
, pindex
);
3753 if (((*pmark
^ pindex
) & ~PM_PLACEMARK_WAKEUP
) == 0) {
3755 } else if (pmarkp
&&
3756 atomic_cmpset_long(pmark
, PM_NOPLACEMARK
, pindex
)) {
3760 * Can't set a placemark with a NULL pmarkp, or if
3761 * pmarkp is non-NULL but we failed to set our
3768 spin_unlock_shared(&pmap
->pm_spin
);
3774 * XXX This has problems if the lock is shared, why?
3776 if (pv_hold_try(pv
)) {
3777 pv_cache(pv
, pindex
); /* overwrite ok (shared lock) */
3778 spin_unlock_shared(&pmap
->pm_spin
);
3780 KKASSERT(pv
->pv_pmap
== pmap
&& pv
->pv_pindex
== pindex
);
3781 return(pv
); /* lock succeeded */
3783 spin_unlock_shared(&pmap
->pm_spin
);
3786 return (pv
); /* lock failed */
3790 * Lock a held pv, keeping the hold count
3794 _pv_lock(pv_entry_t pv PMAP_DEBUG_DECL
)
3799 count
= pv
->pv_hold
;
3801 if ((count
& PV_HOLD_LOCKED
) == 0) {
3802 if (atomic_cmpset_int(&pv
->pv_hold
, count
,
3803 count
| PV_HOLD_LOCKED
)) {
3806 pv
->pv_line
= lineno
;
3812 tsleep_interlock(pv
, 0);
3813 if (atomic_cmpset_int(&pv
->pv_hold
, count
,
3814 count
| PV_HOLD_WAITING
)) {
3816 if (pmap_enter_debug
> 0) {
3818 kprintf("pv waiting on %s:%d\n",
3819 pv
->pv_func
, pv
->pv_line
);
3822 tsleep(pv
, PINTERLOCKED
, "pvwait", hz
);
3829 * Unlock a held and locked pv, keeping the hold count.
3833 pv_unlock(pv_entry_t pv
)
3838 count
= pv
->pv_hold
;
3840 KKASSERT((count
& (PV_HOLD_LOCKED
| PV_HOLD_MASK
)) >=
3841 (PV_HOLD_LOCKED
| 1));
3842 if (atomic_cmpset_int(&pv
->pv_hold
, count
,
3844 ~(PV_HOLD_LOCKED
| PV_HOLD_WAITING
))) {
3845 if (count
& PV_HOLD_WAITING
)
3853 * Unlock and drop a pv. If the pv is no longer associated with a pmap
3854 * and the hold count drops to zero we will free it.
3856 * Caller should not hold any spin locks. We are protected from hold races
3857 * by virtue of holds only occuring only with a pmap_spin or vm_page_spin
3858 * lock held. A pv cannot be located otherwise.
3862 pv_put(pv_entry_t pv
)
3865 if (pmap_enter_debug
> 0) {
3867 kprintf("pv_put pv=%p hold=%08x\n", pv
, pv
->pv_hold
);
3872 * Normal put-aways must have a pv_m associated with the pv,
3873 * but allow the case where the pv has been destructed due
3874 * to pmap_dynamic_delete.
3876 KKASSERT(pv
->pv_pmap
== NULL
|| pv
->pv_m
!= NULL
);
3879 * Fast - shortcut most common condition
3881 if (atomic_cmpset_int(&pv
->pv_hold
, PV_HOLD_LOCKED
| 2, 1))
3892 * Remove the pmap association from a pv, require that pv_m already be removed,
3893 * then unlock and drop the pv. Any pte operations must have already been
3894 * completed. This call may result in a last-drop which will physically free
3897 * Removing the pmap association entails an additional drop.
3899 * pv must be exclusively locked on call and will be disposed of on return.
3903 _pv_free(pv_entry_t pv
, pv_entry_t pvp PMAP_DEBUG_DECL
)
3908 pv
->pv_func_lastfree
= func
;
3909 pv
->pv_line_lastfree
= lineno
;
3911 KKASSERT(pv
->pv_m
== NULL
);
3912 KKASSERT((pv
->pv_hold
& (PV_HOLD_LOCKED
|PV_HOLD_MASK
)) >=
3913 (PV_HOLD_LOCKED
|1));
3914 if ((pmap
= pv
->pv_pmap
) != NULL
) {
3915 spin_lock(&pmap
->pm_spin
);
3916 KKASSERT(pv
->pv_pmap
== pmap
);
3917 if (pmap
->pm_pvhint_pt
== pv
)
3918 pmap
->pm_pvhint_pt
= NULL
;
3919 if (pmap
->pm_pvhint_pte
== pv
)
3920 pmap
->pm_pvhint_pte
= NULL
;
3921 pv_entry_rb_tree_RB_REMOVE(&pmap
->pm_pvroot
, pv
);
3922 atomic_add_long(&pmap
->pm_stats
.resident_count
, -1);
3925 spin_unlock(&pmap
->pm_spin
);
3928 * Try to shortcut three atomic ops, otherwise fall through
3929 * and do it normally. Drop two refs and the lock all in
3933 vm_page_unwire_quick(pvp
->pv_m
);
3934 if (atomic_cmpset_int(&pv
->pv_hold
, PV_HOLD_LOCKED
| 2, 0)) {
3936 if (pmap_enter_debug
> 0) {
3938 kprintf("pv_free: free pv %p\n", pv
);
3944 pv_drop(pv
); /* ref for pv_pmap */
3951 * This routine is very drastic, but can save the system
3959 static int warningdone
=0;
3961 if (pmap_pagedaemon_waken
== 0)
3963 pmap_pagedaemon_waken
= 0;
3964 if (warningdone
< 5) {
3965 kprintf("pmap_collect: collecting pv entries -- "
3966 "suggest increasing PMAP_SHPGPERPROC\n");
3970 for (i
= 0; i
< vm_page_array_size
; i
++) {
3971 m
= &vm_page_array
[i
];
3972 if (m
->wire_count
|| m
->hold_count
)
3974 if (vm_page_busy_try(m
, TRUE
) == 0) {
3975 if (m
->wire_count
== 0 && m
->hold_count
== 0) {
3984 * Scan the pmap for active page table entries and issue a callback.
3985 * The callback must dispose of pte_pv, whos PTE entry is at *ptep in
3986 * its parent page table.
3988 * pte_pv will be NULL if the page or page table is unmanaged.
3989 * pt_pv will point to the page table page containing the pte for the page.
3991 * NOTE! If we come across an unmanaged page TABLE (verses an unmanaged page),
3992 * we pass a NULL pte_pv and we pass a pt_pv pointing to the passed
3993 * process pmap's PD and page to the callback function. This can be
3994 * confusing because the pt_pv is really a pd_pv, and the target page
3995 * table page is simply aliased by the pmap and not owned by it.
3997 * It is assumed that the start and end are properly rounded to the page size.
3999 * It is assumed that PD pages and above are managed and thus in the RB tree,
4000 * allowing us to use RB_SCAN from the PD pages down for ranged scans.
4002 struct pmap_scan_info
{
4006 vm_pindex_t sva_pd_pindex
;
4007 vm_pindex_t eva_pd_pindex
;
4008 void (*func
)(pmap_t
, struct pmap_scan_info
*,
4009 pv_entry_t
, vm_pindex_t
*, pv_entry_t
,
4011 pt_entry_t
*, void *);
4013 pmap_inval_bulk_t bulk_core
;
4014 pmap_inval_bulk_t
*bulk
;
4019 static int pmap_scan_cmp(pv_entry_t pv
, void *data
);
4020 static int pmap_scan_callback(pv_entry_t pv
, void *data
);
4023 pmap_scan(struct pmap_scan_info
*info
, int smp_inval
)
4025 struct pmap
*pmap
= info
->pmap
;
4026 pv_entry_t pd_pv
; /* A page directory PV */
4027 pv_entry_t pt_pv
; /* A page table PV */
4028 pv_entry_t pte_pv
; /* A page table entry PV */
4029 vm_pindex_t
*pte_placemark
;
4030 vm_pindex_t
*pt_placemark
;
4033 struct pv_entry dummy_pv
;
4038 if (info
->sva
== info
->eva
)
4041 info
->bulk
= &info
->bulk_core
;
4042 pmap_inval_bulk_init(&info
->bulk_core
, pmap
);
4048 * Hold the token for stability; if the pmap is empty we have nothing
4052 if (pmap
->pm_stats
.resident_count
== 0) {
4060 * Special handling for scanning one page, which is a very common
4061 * operation (it is?).
4063 * NOTE: Locks must be ordered bottom-up. pte,pt,pd,pdp,pml4
4065 if (info
->sva
+ PAGE_SIZE
== info
->eva
) {
4066 if (info
->sva
>= VM_MAX_USER_ADDRESS
) {
4068 * Kernel mappings do not track wire counts on
4069 * page table pages and only maintain pd_pv and
4070 * pte_pv levels so pmap_scan() works.
4073 pte_pv
= pv_get(pmap
, pmap_pte_pindex(info
->sva
),
4075 ptep
= vtopte(info
->sva
);
4078 * User pages which are unmanaged will not have a
4079 * pte_pv. User page table pages which are unmanaged
4080 * (shared from elsewhere) will also not have a pt_pv.
4081 * The func() callback will pass both pte_pv and pt_pv
4082 * as NULL in that case.
4084 * We hold pte_placemark across the operation for
4087 * WARNING! We must hold pt_placemark across the
4088 * *ptep test to prevent misintepreting
4089 * a non-zero *ptep as a shared page
4090 * table page. Hold it across the function
4091 * callback as well for SMP safety.
4093 pte_pv
= pv_get(pmap
, pmap_pte_pindex(info
->sva
),
4095 pt_pv
= pv_get(pmap
, pmap_pt_pindex(info
->sva
),
4097 if (pt_pv
== NULL
) {
4098 KKASSERT(pte_pv
== NULL
);
4099 pd_pv
= pv_get(pmap
,
4100 pmap_pd_pindex(info
->sva
),
4103 ptep
= pv_pte_lookup(pd_pv
,
4104 pmap_pt_index(info
->sva
));
4106 info
->func(pmap
, info
,
4112 pv_placemarker_wakeup(pmap
,
4117 pv_placemarker_wakeup(pmap
,
4120 pv_placemarker_wakeup(pmap
, pte_placemark
);
4123 ptep
= pv_pte_lookup(pt_pv
, pmap_pte_index(info
->sva
));
4127 * NOTE: *ptep can't be ripped out from under us if we hold
4128 * pte_pv (or pte_placemark) locked, but bits can
4134 KKASSERT(pte_pv
== NULL
);
4135 pv_placemarker_wakeup(pmap
, pte_placemark
);
4136 } else if (pte_pv
) {
4137 KASSERT((oldpte
& (pmap
->pmap_bits
[PG_MANAGED_IDX
] |
4138 pmap
->pmap_bits
[PG_V_IDX
])) ==
4139 (pmap
->pmap_bits
[PG_MANAGED_IDX
] |
4140 pmap
->pmap_bits
[PG_V_IDX
]),
4141 ("badA *ptep %016lx/%016lx sva %016lx pte_pv %p",
4142 *ptep
, oldpte
, info
->sva
, pte_pv
));
4143 info
->func(pmap
, info
, pte_pv
, NULL
, pt_pv
, 0,
4144 info
->sva
, ptep
, info
->arg
);
4146 KASSERT((oldpte
& (pmap
->pmap_bits
[PG_MANAGED_IDX
] |
4147 pmap
->pmap_bits
[PG_V_IDX
])) ==
4148 pmap
->pmap_bits
[PG_V_IDX
],
4149 ("badB *ptep %016lx/%016lx sva %016lx pte_pv NULL",
4150 *ptep
, oldpte
, info
->sva
));
4151 info
->func(pmap
, info
, NULL
, pte_placemark
, pt_pv
, 0,
4152 info
->sva
, ptep
, info
->arg
);
4157 pmap_inval_bulk_flush(info
->bulk
);
4162 * Nominal scan case, RB_SCAN() for PD pages and iterate from
4165 * WARNING! eva can overflow our standard ((N + mask) >> bits)
4166 * bounds, resulting in a pd_pindex of 0. To solve the
4167 * problem we use an inclusive range.
4169 info
->sva_pd_pindex
= pmap_pd_pindex(info
->sva
);
4170 info
->eva_pd_pindex
= pmap_pd_pindex(info
->eva
- PAGE_SIZE
);
4172 if (info
->sva
>= VM_MAX_USER_ADDRESS
) {
4174 * The kernel does not currently maintain any pv_entry's for
4175 * higher-level page tables.
4177 bzero(&dummy_pv
, sizeof(dummy_pv
));
4178 dummy_pv
.pv_pindex
= info
->sva_pd_pindex
;
4179 spin_lock(&pmap
->pm_spin
);
4180 while (dummy_pv
.pv_pindex
<= info
->eva_pd_pindex
) {
4181 pmap_scan_callback(&dummy_pv
, info
);
4182 ++dummy_pv
.pv_pindex
;
4183 if (dummy_pv
.pv_pindex
< info
->sva_pd_pindex
) /*wrap*/
4186 spin_unlock(&pmap
->pm_spin
);
4189 * User page tables maintain local PML4, PDP, and PD
4190 * pv_entry's at the very least. PT pv's might be
4191 * unmanaged and thus not exist. PTE pv's might be
4192 * unmanaged and thus not exist.
4194 spin_lock(&pmap
->pm_spin
);
4195 pv_entry_rb_tree_RB_SCAN(&pmap
->pm_pvroot
, pmap_scan_cmp
,
4196 pmap_scan_callback
, info
);
4197 spin_unlock(&pmap
->pm_spin
);
4199 pmap_inval_bulk_flush(info
->bulk
);
4203 * WARNING! pmap->pm_spin held
4205 * WARNING! eva can overflow our standard ((N + mask) >> bits)
4206 * bounds, resulting in a pd_pindex of 0. To solve the
4207 * problem we use an inclusive range.
4210 pmap_scan_cmp(pv_entry_t pv
, void *data
)
4212 struct pmap_scan_info
*info
= data
;
4213 if (pv
->pv_pindex
< info
->sva_pd_pindex
)
4215 if (pv
->pv_pindex
> info
->eva_pd_pindex
)
4221 * pmap_scan() by PDs
4223 * WARNING! pmap->pm_spin held
4226 pmap_scan_callback(pv_entry_t pv
, void *data
)
4228 struct pmap_scan_info
*info
= data
;
4229 struct pmap
*pmap
= info
->pmap
;
4230 pv_entry_t pd_pv
; /* A page directory PV */
4231 pv_entry_t pt_pv
; /* A page table PV */
4232 vm_pindex_t
*pt_placemark
;
4237 vm_offset_t va_next
;
4238 vm_pindex_t pd_pindex
;
4248 * Pull the PD pindex from the pv before releasing the spinlock.
4250 * WARNING: pv is faked for kernel pmap scans.
4252 pd_pindex
= pv
->pv_pindex
;
4253 spin_unlock(&pmap
->pm_spin
);
4254 pv
= NULL
; /* invalid after spinlock unlocked */
4257 * Calculate the page range within the PD. SIMPLE pmaps are
4258 * direct-mapped for the entire 2^64 address space. Normal pmaps
4259 * reflect the user and kernel address space which requires
4260 * cannonicalization w/regards to converting pd_pindex's back
4263 sva
= (pd_pindex
- pmap_pd_pindex(0)) << PDPSHIFT
;
4264 if ((pmap
->pm_flags
& PMAP_FLAG_SIMPLE
) == 0 &&
4265 (sva
& PML4_SIGNMASK
)) {
4266 sva
|= PML4_SIGNMASK
;
4268 eva
= sva
+ NBPDP
; /* can overflow */
4269 if (sva
< info
->sva
)
4271 if (eva
< info
->sva
|| eva
> info
->eva
)
4275 * NOTE: kernel mappings do not track page table pages, only
4278 * NOTE: Locks must be ordered bottom-up. pte,pt,pd,pdp,pml4.
4279 * However, for the scan to be efficient we try to
4280 * cache items top-down.
4285 for (; sva
< eva
; sva
= va_next
) {
4288 if (sva
>= VM_MAX_USER_ADDRESS
) {
4297 * PD cache, scan shortcut if it doesn't exist.
4299 if (pd_pv
== NULL
) {
4300 pd_pv
= pv_get(pmap
, pmap_pd_pindex(sva
), NULL
);
4301 } else if (pd_pv
->pv_pmap
!= pmap
||
4302 pd_pv
->pv_pindex
!= pmap_pd_pindex(sva
)) {
4304 pd_pv
= pv_get(pmap
, pmap_pd_pindex(sva
), NULL
);
4306 if (pd_pv
== NULL
) {
4307 va_next
= (sva
+ NBPDP
) & ~PDPMASK
;
4316 * NOTE: The cached pt_pv can be removed from the pmap when
4317 * pmap_dynamic_delete is enabled.
4319 if (pt_pv
&& (pt_pv
->pv_pmap
!= pmap
||
4320 pt_pv
->pv_pindex
!= pmap_pt_pindex(sva
))) {
4324 if (pt_pv
== NULL
) {
4325 pt_pv
= pv_get_try(pmap
, pmap_pt_pindex(sva
),
4326 &pt_placemark
, &error
);
4328 pv_put(pd_pv
); /* lock order */
4335 pv_placemarker_wait(pmap
, pt_placemark
);
4340 /* may have to re-check later if pt_pv is NULL here */
4344 * If pt_pv is NULL we either have an shared page table
4345 * page and must issue a callback specific to that case,
4346 * or there is no page table page.
4348 * Either way we can skip the page table page.
4350 * WARNING! pt_pv can also be NULL due to a pv creation
4351 * race where we find it to be NULL and then
4352 * later see a pte_pv. But its possible the pt_pv
4353 * got created inbetween the two operations, so
4356 if (pt_pv
== NULL
) {
4358 * Possible unmanaged (shared from another pmap)
4361 * WARNING! We must hold pt_placemark across the
4362 * *ptep test to prevent misintepreting
4363 * a non-zero *ptep as a shared page
4364 * table page. Hold it across the function
4365 * callback as well for SMP safety.
4367 ptep
= pv_pte_lookup(pd_pv
, pmap_pt_index(sva
));
4368 if (*ptep
& pmap
->pmap_bits
[PG_V_IDX
]) {
4369 info
->func(pmap
, info
, NULL
, pt_placemark
,
4371 sva
, ptep
, info
->arg
);
4373 pv_placemarker_wakeup(pmap
, pt_placemark
);
4377 * Done, move to next page table page.
4379 va_next
= (sva
+ NBPDR
) & ~PDRMASK
;
4386 * From this point in the loop testing pt_pv for non-NULL
4387 * means we are in UVM, else if it is NULL we are in KVM.
4389 * Limit our scan to either the end of the va represented
4390 * by the current page table page, or to the end of the
4391 * range being removed.
4394 va_next
= (sva
+ NBPDR
) & ~PDRMASK
;
4401 * Scan the page table for pages. Some pages may not be
4402 * managed (might not have a pv_entry).
4404 * There is no page table management for kernel pages so
4405 * pt_pv will be NULL in that case, but otherwise pt_pv
4406 * is non-NULL, locked, and referenced.
4410 * At this point a non-NULL pt_pv means a UVA, and a NULL
4411 * pt_pv means a KVA.
4414 ptep
= pv_pte_lookup(pt_pv
, pmap_pte_index(sva
));
4418 while (sva
< va_next
) {
4420 vm_pindex_t
*pte_placemark
;
4423 * Yield every 64 pages, stop if requested.
4425 if ((++info
->count
& 63) == 0)
4431 * We can shortcut our scan if *ptep == 0. This is
4432 * an unlocked check.
4442 * Acquire the related pte_pv, if any. If *ptep == 0
4443 * the related pte_pv should not exist, but if *ptep
4444 * is not zero the pte_pv may or may not exist (e.g.
4445 * will not exist for an unmanaged page).
4447 * However a multitude of races are possible here
4448 * so if we cannot lock definite state we clean out
4449 * our cache and break the inner while() loop to
4450 * force a loop up to the top of the for().
4452 * XXX unlock/relock pd_pv, pt_pv, and re-test their
4453 * validity instead of looping up?
4455 pte_pv
= pv_get_try(pmap
, pmap_pte_pindex(sva
),
4456 &pte_placemark
, &error
);
4458 pv_put(pd_pv
); /* lock order */
4461 pv_put(pt_pv
); /* lock order */
4464 if (pte_pv
) { /* block */
4469 pv_placemarker_wait(pmap
,
4472 va_next
= sva
; /* retry */
4477 * Reload *ptep after successfully locking the
4478 * pindex. If *ptep == 0 we had better NOT have a
4485 kprintf("Unexpected non-NULL pte_pv "
4487 "*ptep = %016lx/%016lx\n",
4488 pte_pv
, pt_pv
, *ptep
, oldpte
);
4489 panic("Unexpected non-NULL pte_pv");
4491 pv_placemarker_wakeup(pmap
, pte_placemark
);
4499 * We can't hold pd_pv across the callback (because
4500 * we don't pass it to the callback and the callback
4504 vm_page_wire_quick(pd_pv
->pv_m
);
4509 * Ready for the callback. The locked pte_pv (if any)
4510 * is consumed by the callback. pte_pv will exist if
4511 * the page is managed, and will not exist if it
4514 if (oldpte
& pmap
->pmap_bits
[PG_MANAGED_IDX
]) {
4519 (oldpte
& pmap
->pmap_bits
[PG_V_IDX
]),
4520 ("badC *ptep %016lx/%016lx sva %016lx "
4522 *ptep
, oldpte
, sva
, pte_pv
));
4524 * We must unlock pd_pv across the callback
4525 * to avoid deadlocks on any recursive
4526 * disposal. Re-check that it still exists
4529 * Call target disposes of pte_pv and may
4530 * destroy but will not dispose of pt_pv.
4532 info
->func(pmap
, info
, pte_pv
, NULL
,
4534 sva
, ptep
, info
->arg
);
4539 * We must unlock pd_pv across the callback
4540 * to avoid deadlocks on any recursive
4541 * disposal. Re-check that it still exists
4544 * Call target disposes of pte_pv or
4545 * pte_placemark and may destroy but will
4546 * not dispose of pt_pv.
4548 KASSERT(pte_pv
== NULL
&&
4549 (oldpte
& pmap
->pmap_bits
[PG_V_IDX
]),
4550 ("badD *ptep %016lx/%016lx sva %016lx "
4551 "pte_pv %p pte_pv->pv_m %p ",
4553 pte_pv
, (pte_pv
? pte_pv
->pv_m
: NULL
)));
4557 info
->func(pmap
, info
,
4560 sva
, ptep
, info
->arg
);
4562 info
->func(pmap
, info
,
4563 NULL
, pte_placemark
,
4565 sva
, ptep
, info
->arg
);
4570 vm_page_unwire_quick(pd_pv
->pv_m
);
4571 if (pd_pv
->pv_pmap
== NULL
) {
4572 va_next
= sva
; /* retry */
4578 * NOTE: The cached pt_pv can be removed from the
4579 * pmap when pmap_dynamic_delete is enabled,
4580 * which will cause ptep to become stale.
4582 * This also means that no pages remain under
4583 * the PT, so we can just break out of the inner
4584 * loop and let the outer loop clean everything
4587 if (pt_pv
&& pt_pv
->pv_pmap
!= pmap
)
4602 if ((++info
->count
& 7) == 0)
4606 * Relock before returning.
4608 spin_lock(&pmap
->pm_spin
);
4613 pmap_remove(struct pmap
*pmap
, vm_offset_t sva
, vm_offset_t eva
)
4615 struct pmap_scan_info info
;
4620 info
.func
= pmap_remove_callback
;
4622 pmap_scan(&info
, 1);
4625 if (eva
- sva
< 1024*1024) {
4627 cpu_invlpg((void *)sva
);
4635 pmap_remove_noinval(struct pmap
*pmap
, vm_offset_t sva
, vm_offset_t eva
)
4637 struct pmap_scan_info info
;
4642 info
.func
= pmap_remove_callback
;
4644 pmap_scan(&info
, 0);
4648 pmap_remove_callback(pmap_t pmap
, struct pmap_scan_info
*info
,
4649 pv_entry_t pte_pv
, vm_pindex_t
*pte_placemark
,
4650 pv_entry_t pt_pv
, int sharept
,
4651 vm_offset_t va
, pt_entry_t
*ptep
, void *arg __unused
)
4659 * This will also drop pt_pv's wire_count. Note that
4660 * terminal pages are not wired based on mmu presence.
4662 * NOTE: If this is the kernel_pmap, pt_pv can be NULL.
4664 KKASSERT(pte_pv
->pv_m
!= NULL
);
4665 pmap_remove_pv_pte(pte_pv
, pt_pv
, info
->bulk
, 2);
4666 pte_pv
= NULL
; /* safety */
4669 * Recursively destroy higher-level page tables.
4671 * This is optional. If we do not, they will still
4672 * be destroyed when the process exits.
4674 * NOTE: Do not destroy pv_entry's with extra hold refs,
4675 * a caller may have unlocked it and intends to
4676 * continue to use it.
4678 if (pmap_dynamic_delete
&&
4681 pt_pv
->pv_m
->wire_count
== 1 &&
4682 (pt_pv
->pv_hold
& PV_HOLD_MASK
) == 2 &&
4683 pt_pv
->pv_pindex
< pmap_pml4_pindex()) {
4684 if (pmap_dynamic_delete
== 2)
4685 kprintf("B %jd %08x\n", pt_pv
->pv_pindex
, pt_pv
->pv_hold
);
4686 pv_hold(pt_pv
); /* extra hold */
4687 pmap_remove_pv_pte(pt_pv
, NULL
, info
->bulk
, 1);
4688 pv_lock(pt_pv
); /* prior extra hold + relock */
4690 } else if (sharept
== 0) {
4692 * Unmanaged pte (pte_placemark is non-NULL)
4694 * pt_pv's wire_count is still bumped by unmanaged pages
4695 * so we must decrement it manually.
4697 * We have to unwire the target page table page.
4699 pte
= pmap_inval_bulk(info
->bulk
, va
, ptep
, 0);
4700 if (pte
& pmap
->pmap_bits
[PG_W_IDX
])
4701 atomic_add_long(&pmap
->pm_stats
.wired_count
, -1);
4702 atomic_add_long(&pmap
->pm_stats
.resident_count
, -1);
4703 if (vm_page_unwire_quick(pt_pv
->pv_m
))
4704 panic("pmap_remove: insufficient wirecount");
4705 pv_placemarker_wakeup(pmap
, pte_placemark
);
4708 * Unmanaged page table (pt, pd, or pdp. Not pte) for
4709 * a shared page table.
4711 * pt_pv is actually the pd_pv for our pmap (not the shared
4714 * We have to unwire the target page table page and we
4715 * have to unwire our page directory page.
4717 * It is unclear how we can invalidate a segment so we
4718 * invalidate -1 which invlidates the tlb.
4720 pte
= pmap_inval_bulk(info
->bulk
, (vm_offset_t
)-1, ptep
, 0);
4721 atomic_add_long(&pmap
->pm_stats
.resident_count
, -1);
4722 KKASSERT((pte
& pmap
->pmap_bits
[PG_DEVICE_IDX
]) == 0);
4723 if (vm_page_unwire_quick(PHYS_TO_VM_PAGE(pte
& PG_FRAME
)))
4724 panic("pmap_remove: shared pgtable1 bad wirecount");
4725 if (vm_page_unwire_quick(pt_pv
->pv_m
))
4726 panic("pmap_remove: shared pgtable2 bad wirecount");
4727 pv_placemarker_wakeup(pmap
, pte_placemark
);
4732 * Removes this physical page from all physical maps in which it resides.
4733 * Reflects back modify bits to the pager.
4735 * This routine may not be called from an interrupt.
4739 pmap_remove_all(vm_page_t m
)
4742 pmap_inval_bulk_t bulk
;
4744 if (!pmap_initialized
/* || (m->flags & PG_FICTITIOUS)*/)
4747 vm_page_spin_lock(m
);
4748 while ((pv
= TAILQ_FIRST(&m
->md
.pv_list
)) != NULL
) {
4749 KKASSERT(pv
->pv_m
== m
);
4750 if (pv_hold_try(pv
)) {
4751 vm_page_spin_unlock(m
);
4753 vm_page_spin_unlock(m
);
4756 vm_page_spin_lock(m
);
4759 KKASSERT(pv
->pv_pmap
&& pv
->pv_m
== m
);
4762 * Holding no spinlocks, pv is locked. Once we scrap
4763 * pv we can no longer use it as a list iterator (but
4764 * we are doing a TAILQ_FIRST() so we are ok).
4766 pmap_inval_bulk_init(&bulk
, pv
->pv_pmap
);
4767 pmap_remove_pv_pte(pv
, NULL
, &bulk
, 2);
4768 pv
= NULL
; /* safety */
4769 pmap_inval_bulk_flush(&bulk
);
4770 vm_page_spin_lock(m
);
4772 KKASSERT((m
->flags
& (PG_MAPPED
|PG_WRITEABLE
)) == 0);
4773 vm_page_spin_unlock(m
);
4777 * Removes the page from a particular pmap
4780 pmap_remove_specific(pmap_t pmap
, vm_page_t m
)
4783 pmap_inval_bulk_t bulk
;
4785 if (!pmap_initialized
)
4789 vm_page_spin_lock(m
);
4790 TAILQ_FOREACH(pv
, &m
->md
.pv_list
, pv_list
) {
4791 if (pv
->pv_pmap
!= pmap
)
4793 KKASSERT(pv
->pv_m
== m
);
4794 if (pv_hold_try(pv
)) {
4795 vm_page_spin_unlock(m
);
4797 vm_page_spin_unlock(m
);
4802 KKASSERT(pv
->pv_pmap
== pmap
&& pv
->pv_m
== m
);
4805 * Holding no spinlocks, pv is locked. Once gone it can't
4806 * be used as an iterator. In fact, because we couldn't
4807 * necessarily lock it atomically it may have moved within
4808 * the list and ALSO cannot be used as an iterator.
4810 pmap_inval_bulk_init(&bulk
, pv
->pv_pmap
);
4811 pmap_remove_pv_pte(pv
, NULL
, &bulk
, 2);
4812 pv
= NULL
; /* safety */
4813 pmap_inval_bulk_flush(&bulk
);
4816 vm_page_spin_unlock(m
);
4820 * Set the physical protection on the specified range of this map
4821 * as requested. This function is typically only used for debug watchpoints
4824 * This function may not be called from an interrupt if the map is
4825 * not the kernel_pmap.
4827 * NOTE! For shared page table pages we just unmap the page.
4830 pmap_protect(pmap_t pmap
, vm_offset_t sva
, vm_offset_t eva
, vm_prot_t prot
)
4832 struct pmap_scan_info info
;
4833 /* JG review for NX */
4837 if ((prot
& (VM_PROT_READ
| VM_PROT_EXECUTE
)) == VM_PROT_NONE
) {
4838 pmap_remove(pmap
, sva
, eva
);
4841 if (prot
& VM_PROT_WRITE
)
4846 info
.func
= pmap_protect_callback
;
4848 pmap_scan(&info
, 1);
4853 pmap_protect_callback(pmap_t pmap
, struct pmap_scan_info
*info
,
4854 pv_entry_t pte_pv
, vm_pindex_t
*pte_placemark
,
4855 pv_entry_t pt_pv
, int sharept
,
4856 vm_offset_t va
, pt_entry_t
*ptep
, void *arg __unused
)
4867 KKASSERT(pte_pv
->pv_m
!= NULL
);
4869 if (pbits
& pmap
->pmap_bits
[PG_A_IDX
]) {
4870 if ((pbits
& pmap
->pmap_bits
[PG_DEVICE_IDX
]) == 0) {
4871 m
= PHYS_TO_VM_PAGE(pbits
& PG_FRAME
);
4872 KKASSERT(m
== pte_pv
->pv_m
);
4873 vm_page_flag_set(m
, PG_REFERENCED
);
4875 cbits
&= ~pmap
->pmap_bits
[PG_A_IDX
];
4877 if (pbits
& pmap
->pmap_bits
[PG_M_IDX
]) {
4878 if (pmap_track_modified(pte_pv
->pv_pindex
)) {
4879 if ((pbits
& pmap
->pmap_bits
[PG_DEVICE_IDX
]) == 0) {
4881 m
= PHYS_TO_VM_PAGE(pbits
&
4886 cbits
&= ~pmap
->pmap_bits
[PG_M_IDX
];
4889 } else if (sharept
) {
4891 * Unmanaged page table, pt_pv is actually the pd_pv
4892 * for our pmap (not the object's shared pmap).
4894 * When asked to protect something in a shared page table
4895 * page we just unmap the page table page. We have to
4896 * invalidate the tlb in this situation.
4898 * XXX Warning, shared page tables will not be used for
4899 * OBJT_DEVICE or OBJT_MGTDEVICE (PG_FICTITIOUS) mappings
4900 * so PHYS_TO_VM_PAGE() should be safe here.
4902 pte
= pmap_inval_smp(pmap
, (vm_offset_t
)-1, 1, ptep
, 0);
4903 if (vm_page_unwire_quick(PHYS_TO_VM_PAGE(pte
& PG_FRAME
)))
4904 panic("pmap_protect: pgtable1 pg bad wirecount");
4905 if (vm_page_unwire_quick(pt_pv
->pv_m
))
4906 panic("pmap_protect: pgtable2 pg bad wirecount");
4909 /* else unmanaged page, adjust bits, no wire changes */
4912 cbits
&= ~pmap
->pmap_bits
[PG_RW_IDX
];
4914 if (pmap_enter_debug
> 0) {
4916 kprintf("pmap_protect va=%lx ptep=%p pte_pv=%p "
4917 "pt_pv=%p cbits=%08lx\n",
4923 if (pbits
!= cbits
) {
4926 xva
= (sharept
) ? (vm_offset_t
)-1 : va
;
4927 if (!pmap_inval_smp_cmpset(pmap
, xva
,
4928 ptep
, pbits
, cbits
)) {
4936 pv_placemarker_wakeup(pmap
, pte_placemark
);
4940 * Insert the vm_page (m) at the virtual address (va), replacing any prior
4941 * mapping at that address. Set protection and wiring as requested.
4943 * If entry is non-NULL we check to see if the SEG_SIZE optimization is
4944 * possible. If it is we enter the page into the appropriate shared pmap
4945 * hanging off the related VM object instead of the passed pmap, then we
4946 * share the page table page from the VM object's pmap into the current pmap.
4948 * NOTE: This routine MUST insert the page into the pmap now, it cannot
4951 * NOTE: If (m) is PG_UNMANAGED it may also be a temporary fake vm_page_t.
4955 pmap_enter(pmap_t pmap
, vm_offset_t va
, vm_page_t m
, vm_prot_t prot
,
4956 boolean_t wired
, vm_map_entry_t entry
)
4958 pv_entry_t pt_pv
; /* page table */
4959 pv_entry_t pte_pv
; /* page table entry */
4960 vm_pindex_t
*pte_placemark
;
4963 pt_entry_t origpte
, newpte
;
4968 va
= trunc_page(va
);
4969 #ifdef PMAP_DIAGNOSTIC
4971 panic("pmap_enter: toobig");
4972 if ((va
>= UPT_MIN_ADDRESS
) && (va
< UPT_MAX_ADDRESS
))
4973 panic("pmap_enter: invalid to pmap_enter page table "
4974 "pages (va: 0x%lx)", va
);
4976 if (va
< UPT_MAX_ADDRESS
&& pmap
== &kernel_pmap
) {
4977 kprintf("Warning: pmap_enter called on UVA with "
4980 db_print_backtrace();
4983 if (va
>= UPT_MAX_ADDRESS
&& pmap
!= &kernel_pmap
) {
4984 kprintf("Warning: pmap_enter called on KVA without"
4987 db_print_backtrace();
4992 * Get locked PV entries for our new page table entry (pte_pv or
4993 * pte_placemark) and for its parent page table (pt_pv). We need
4994 * the parent so we can resolve the location of the ptep.
4996 * Only hardware MMU actions can modify the ptep out from
4999 * if (m) is fictitious or unmanaged we do not create a managing
5000 * pte_pv for it. Any pre-existing page's management state must
5001 * match (avoiding code complexity).
5003 * If the pmap is still being initialized we assume existing
5006 * Kernel mapppings do not track page table pages (i.e. pt_pv).
5008 * WARNING! If replacing a managed mapping with an unmanaged mapping
5009 * pte_pv will wind up being non-NULL and must be handled
5012 if (pmap_initialized
== FALSE
) {
5015 pte_placemark
= NULL
;
5018 } else if (m
->flags
& (/*PG_FICTITIOUS |*/ PG_UNMANAGED
)) { /* XXX */
5019 pmap_softwait(pmap
);
5020 pte_pv
= pv_get(pmap
, pmap_pte_pindex(va
), &pte_placemark
);
5021 KKASSERT(pte_pv
== NULL
);
5022 if (va
>= VM_MAX_USER_ADDRESS
) {
5026 pt_pv
= pmap_allocpte_seg(pmap
, pmap_pt_pindex(va
),
5028 ptep
= pv_pte_lookup(pt_pv
, pmap_pte_index(va
));
5032 KASSERT(origpte
== 0 ||
5033 (origpte
& pmap
->pmap_bits
[PG_MANAGED_IDX
]) == 0,
5034 ("Invalid PTE 0x%016jx @ 0x%016jx\n", origpte
, va
));
5036 pmap_softwait(pmap
);
5037 if (va
>= VM_MAX_USER_ADDRESS
) {
5039 * Kernel map, pv_entry-tracked.
5042 pte_pv
= pmap_allocpte(pmap
, pmap_pte_pindex(va
), NULL
);
5048 pte_pv
= pmap_allocpte_seg(pmap
, pmap_pte_pindex(va
),
5050 ptep
= pv_pte_lookup(pt_pv
, pmap_pte_index(va
));
5052 pte_placemark
= NULL
; /* safety */
5055 KASSERT(origpte
== 0 ||
5056 (origpte
& pmap
->pmap_bits
[PG_MANAGED_IDX
]),
5057 ("Invalid PTE 0x%016jx @ 0x%016jx\n", origpte
, va
));
5060 pa
= VM_PAGE_TO_PHYS(m
);
5061 opa
= origpte
& PG_FRAME
;
5064 * Calculate the new PTE. Note that pte_pv alone does not mean
5065 * the new pte_pv is managed, it could exist because the old pte
5066 * was managed even if the new one is not.
5068 newpte
= (pt_entry_t
)(pa
| pte_prot(pmap
, prot
) |
5069 pmap
->pmap_bits
[PG_V_IDX
] | pmap
->pmap_bits
[PG_A_IDX
]);
5071 newpte
|= pmap
->pmap_bits
[PG_W_IDX
];
5072 if (va
< VM_MAX_USER_ADDRESS
)
5073 newpte
|= pmap
->pmap_bits
[PG_U_IDX
];
5074 if (pte_pv
&& (m
->flags
& (/*PG_FICTITIOUS |*/ PG_UNMANAGED
)) == 0)
5075 newpte
|= pmap
->pmap_bits
[PG_MANAGED_IDX
];
5076 // if (pmap == &kernel_pmap)
5077 // newpte |= pgeflag;
5078 newpte
|= pmap
->pmap_cache_bits
[m
->pat_mode
];
5079 if (m
->flags
& PG_FICTITIOUS
)
5080 newpte
|= pmap
->pmap_bits
[PG_DEVICE_IDX
];
5083 * It is possible for multiple faults to occur in threaded
5084 * environments, the existing pte might be correct.
5086 if (((origpte
^ newpte
) &
5087 ~(pt_entry_t
)(pmap
->pmap_bits
[PG_M_IDX
] |
5088 pmap
->pmap_bits
[PG_A_IDX
])) == 0) {
5093 * Ok, either the address changed or the protection or wiring
5096 * Clear the current entry, interlocking the removal. For managed
5097 * pte's this will also flush the modified state to the vm_page.
5098 * Atomic ops are mandatory in order to ensure that PG_M events are
5099 * not lost during any transition.
5101 * WARNING: The caller has busied the new page but not the original
5102 * vm_page which we are trying to replace. Because we hold
5103 * the pte_pv lock, but have not busied the page, PG bits
5104 * can be cleared out from under us.
5107 if (origpte
& pmap
->pmap_bits
[PG_MANAGED_IDX
]) {
5109 * Old page was managed. Expect pte_pv to exist.
5110 * (it might also exist if the old page was unmanaged).
5112 * NOTE: pt_pv won't exist for a kernel page
5113 * (managed or otherwise).
5115 * NOTE: We may be reusing the pte_pv so we do not
5116 * destroy it in pmap_remove_pv_pte().
5118 KKASSERT(pte_pv
&& pte_pv
->pv_m
);
5119 if (prot
& VM_PROT_NOSYNC
) {
5120 pmap_remove_pv_pte(pte_pv
, pt_pv
, NULL
, 0);
5122 pmap_inval_bulk_t bulk
;
5124 pmap_inval_bulk_init(&bulk
, pmap
);
5125 pmap_remove_pv_pte(pte_pv
, pt_pv
, &bulk
, 0);
5126 pmap_inval_bulk_flush(&bulk
);
5128 pmap_remove_pv_page(pte_pv
);
5129 /* will either set pte_pv->pv_m or pv_free() later */
5132 * Old page was not managed. If we have a pte_pv
5133 * it better not have a pv_m assigned to it. If the
5134 * new page is managed the pte_pv will be destroyed
5135 * near the end (we need its interlock).
5137 * NOTE: We leave the wire count on the PT page
5138 * intact for the followup enter, but adjust
5139 * the wired-pages count on the pmap.
5141 KKASSERT(pte_pv
== NULL
);
5142 if (prot
& VM_PROT_NOSYNC
) {
5144 * NOSYNC (no mmu sync) requested.
5146 (void)pte_load_clear(ptep
);
5147 cpu_invlpg((void *)va
);
5152 pmap_inval_smp(pmap
, va
, 1, ptep
, 0);
5156 * We must adjust pm_stats manually for unmanaged
5160 atomic_add_long(&pmap
->pm_stats
.
5161 resident_count
, -1);
5163 if (origpte
& pmap
->pmap_bits
[PG_W_IDX
]) {
5164 atomic_add_long(&pmap
->pm_stats
.
5168 KKASSERT(*ptep
== 0);
5172 if (pmap_enter_debug
> 0) {
5174 kprintf("pmap_enter: va=%lx m=%p origpte=%lx newpte=%lx ptep=%p"
5175 " pte_pv=%p pt_pv=%p opa=%lx prot=%02x\n",
5177 origpte
, newpte
, ptep
,
5178 pte_pv
, pt_pv
, opa
, prot
);
5182 if ((newpte
& pmap
->pmap_bits
[PG_MANAGED_IDX
]) == 0) {
5184 * Entering an unmanaged page. We must wire the pt_pv unless
5185 * we retained the wiring from an unmanaged page we had
5186 * removed (if we retained it via pte_pv that will go away
5189 if (pt_pv
&& (opa
== 0 ||
5190 (origpte
& pmap
->pmap_bits
[PG_MANAGED_IDX
]))) {
5191 vm_page_wire_quick(pt_pv
->pv_m
);
5194 atomic_add_long(&pmap
->pm_stats
.wired_count
, 1);
5197 * Unmanaged pages need manual resident_count tracking.
5200 atomic_add_long(&pt_pv
->pv_pmap
->pm_stats
.
5203 if (newpte
& pmap
->pmap_bits
[PG_RW_IDX
])
5204 vm_page_flag_set(m
, PG_WRITEABLE
);
5207 * Entering a managed page. Our pte_pv takes care of the
5208 * PT wiring, so if we had removed an unmanaged page before
5211 * We have to take care of the pmap wired count ourselves.
5213 * Enter on the PV list if part of our managed memory.
5215 KKASSERT(pte_pv
&& (pte_pv
->pv_m
== NULL
|| pte_pv
->pv_m
== m
));
5216 vm_page_spin_lock(m
);
5218 pmap_page_stats_adding(m
);
5219 TAILQ_INSERT_TAIL(&m
->md
.pv_list
, pte_pv
, pv_list
);
5220 vm_page_flag_set(m
, PG_MAPPED
);
5221 if (newpte
& pmap
->pmap_bits
[PG_RW_IDX
])
5222 vm_page_flag_set(m
, PG_WRITEABLE
);
5223 vm_page_spin_unlock(m
);
5226 (origpte
& pmap
->pmap_bits
[PG_MANAGED_IDX
]) == 0) {
5227 vm_page_unwire_quick(pt_pv
->pv_m
);
5231 * Adjust pmap wired pages count for new entry.
5234 atomic_add_long(&pte_pv
->pv_pmap
->pm_stats
.
5240 * Kernel VMAs (pt_pv == NULL) require pmap invalidation interlocks.
5242 * User VMAs do not because those will be zero->non-zero, so no
5243 * stale entries to worry about at this point.
5245 * For KVM there appear to still be issues. Theoretically we
5246 * should be able to scrap the interlocks entirely but we
5249 if ((prot
& VM_PROT_NOSYNC
) == 0 && pt_pv
== NULL
) {
5250 pmap_inval_smp(pmap
, va
, 1, ptep
, newpte
);
5252 origpte
= atomic_swap_long(ptep
, newpte
);
5253 if (origpte
& pmap
->pmap_bits
[PG_M_IDX
]) {
5254 kprintf("pmap [M] race @ %016jx\n", va
);
5255 atomic_set_long(ptep
, pmap
->pmap_bits
[PG_M_IDX
]);
5258 cpu_invlpg((void *)va
);
5265 KKASSERT((newpte
& pmap
->pmap_bits
[PG_MANAGED_IDX
]) == 0 ||
5266 (m
->flags
& PG_MAPPED
));
5269 * Cleanup the pv entry, allowing other accessors. If the new page
5270 * is not managed but we have a pte_pv (which was locking our
5271 * operation), we can free it now. pte_pv->pv_m should be NULL.
5273 if (pte_pv
&& (newpte
& pmap
->pmap_bits
[PG_MANAGED_IDX
]) == 0) {
5274 pv_free(pte_pv
, pt_pv
);
5275 } else if (pte_pv
) {
5277 } else if (pte_placemark
) {
5278 pv_placemarker_wakeup(pmap
, pte_placemark
);
5285 * This code works like pmap_enter() but assumes VM_PROT_READ and not-wired.
5286 * This code also assumes that the pmap has no pre-existing entry for this
5289 * This code currently may only be used on user pmaps, not kernel_pmap.
5292 pmap_enter_quick(pmap_t pmap
, vm_offset_t va
, vm_page_t m
)
5294 pmap_enter(pmap
, va
, m
, VM_PROT_READ
, FALSE
, NULL
);
5298 * Make a temporary mapping for a physical address. This is only intended
5299 * to be used for panic dumps.
5301 * The caller is responsible for calling smp_invltlb().
5304 pmap_kenter_temporary(vm_paddr_t pa
, long i
)
5306 pmap_kenter_quick((vm_offset_t
)crashdumpmap
+ (i
* PAGE_SIZE
), pa
);
5307 return ((void *)crashdumpmap
);
5310 #define MAX_INIT_PT (96)
5313 * This routine preloads the ptes for a given object into the specified pmap.
5314 * This eliminates the blast of soft faults on process startup and
5315 * immediately after an mmap.
5317 static int pmap_object_init_pt_callback(vm_page_t p
, void *data
);
5320 pmap_object_init_pt(pmap_t pmap
, vm_offset_t addr
, vm_prot_t prot
,
5321 vm_object_t object
, vm_pindex_t pindex
,
5322 vm_size_t size
, int limit
)
5324 struct rb_vm_page_scan_info info
;
5329 * We can't preinit if read access isn't set or there is no pmap
5332 if ((prot
& VM_PROT_READ
) == 0 || pmap
== NULL
|| object
== NULL
)
5336 * We can't preinit if the pmap is not the current pmap
5338 lp
= curthread
->td_lwp
;
5339 if (lp
== NULL
|| pmap
!= vmspace_pmap(lp
->lwp_vmspace
))
5343 * Misc additional checks
5345 psize
= x86_64_btop(size
);
5347 if ((object
->type
!= OBJT_VNODE
) ||
5348 ((limit
& MAP_PREFAULT_PARTIAL
) && (psize
> MAX_INIT_PT
) &&
5349 (object
->resident_page_count
> MAX_INIT_PT
))) {
5353 if (pindex
+ psize
> object
->size
) {
5354 if (object
->size
< pindex
)
5356 psize
= object
->size
- pindex
;
5363 * If everything is segment-aligned do not pre-init here. Instead
5364 * allow the normal vm_fault path to pass a segment hint to
5365 * pmap_enter() which will then use an object-referenced shared
5368 if ((addr
& SEG_MASK
) == 0 &&
5369 (ctob(psize
) & SEG_MASK
) == 0 &&
5370 (ctob(pindex
) & SEG_MASK
) == 0) {
5375 * Use a red-black scan to traverse the requested range and load
5376 * any valid pages found into the pmap.
5378 * We cannot safely scan the object's memq without holding the
5381 info
.start_pindex
= pindex
;
5382 info
.end_pindex
= pindex
+ psize
- 1;
5387 info
.object
= object
;
5390 * By using the NOLK scan, the callback function must be sure
5391 * to return -1 if the VM page falls out of the object.
5393 vm_object_hold_shared(object
);
5394 vm_page_rb_tree_RB_SCAN_NOLK(&object
->rb_memq
, rb_vm_page_scancmp
,
5395 pmap_object_init_pt_callback
, &info
);
5396 vm_object_drop(object
);
5401 pmap_object_init_pt_callback(vm_page_t p
, void *data
)
5403 struct rb_vm_page_scan_info
*info
= data
;
5404 vm_pindex_t rel_index
;
5408 * don't allow an madvise to blow away our really
5409 * free pages allocating pv entries.
5411 if ((info
->limit
& MAP_PREFAULT_MADVISE
) &&
5412 vmstats
.v_free_count
< vmstats
.v_free_reserved
) {
5417 * Ignore list markers and ignore pages we cannot instantly
5418 * busy (while holding the object token).
5420 if (p
->flags
& PG_MARKER
)
5425 if (vm_page_busy_try(p
, TRUE
))
5428 if (vm_page_sbusy_try(p
))
5431 if (((p
->valid
& VM_PAGE_BITS_ALL
) == VM_PAGE_BITS_ALL
) &&
5432 (p
->flags
& PG_FICTITIOUS
) == 0) {
5433 if ((p
->queue
- p
->pc
) == PQ_CACHE
) {
5434 if (hard_busy
== 0) {
5435 vm_page_sbusy_drop(p
);
5439 vm_page_deactivate(p
);
5441 rel_index
= p
->pindex
- info
->start_pindex
;
5442 pmap_enter_quick(info
->pmap
,
5443 info
->addr
+ x86_64_ptob(rel_index
), p
);
5448 vm_page_sbusy_drop(p
);
5451 * We are using an unlocked scan (that is, the scan expects its
5452 * current element to remain in the tree on return). So we have
5453 * to check here and abort the scan if it isn't.
5455 if (p
->object
!= info
->object
)
5462 * Return TRUE if the pmap is in shape to trivially pre-fault the specified
5465 * Returns FALSE if it would be non-trivial or if a pte is already loaded
5468 * XXX This is safe only because page table pages are not freed.
5471 pmap_prefault_ok(pmap_t pmap
, vm_offset_t addr
)
5475 /*spin_lock(&pmap->pm_spin);*/
5476 if ((pte
= pmap_pte(pmap
, addr
)) != NULL
) {
5477 if (*pte
& pmap
->pmap_bits
[PG_V_IDX
]) {
5478 /*spin_unlock(&pmap->pm_spin);*/
5482 /*spin_unlock(&pmap->pm_spin);*/
5487 * Change the wiring attribute for a pmap/va pair. The mapping must already
5488 * exist in the pmap. The mapping may or may not be managed. The wiring in
5489 * the page is not changed, the page is returned so the caller can adjust
5490 * its wiring (the page is not locked in any way).
5492 * Wiring is not a hardware characteristic so there is no need to invalidate
5493 * TLB. However, in an SMP environment we must use a locked bus cycle to
5494 * update the pte (if we are not using the pmap_inval_*() API that is)...
5495 * it's ok to do this for simple wiring changes.
5498 pmap_unwire(pmap_t pmap
, vm_offset_t va
)
5509 * Assume elements in the kernel pmap are stable
5511 if (pmap
== &kernel_pmap
) {
5512 if (pmap_pt(pmap
, va
) == 0)
5514 ptep
= pmap_pte_quick(pmap
, va
);
5515 if (pmap_pte_v(pmap
, ptep
)) {
5516 if (pmap_pte_w(pmap
, ptep
))
5517 atomic_add_long(&pmap
->pm_stats
.wired_count
,-1);
5518 atomic_clear_long(ptep
, pmap
->pmap_bits
[PG_W_IDX
]);
5519 pa
= *ptep
& PG_FRAME
;
5520 m
= PHYS_TO_VM_PAGE(pa
);
5526 * We can only [un]wire pmap-local pages (we cannot wire
5529 pt_pv
= pv_get(pmap
, pmap_pt_pindex(va
), NULL
);
5533 ptep
= pv_pte_lookup(pt_pv
, pmap_pte_index(va
));
5534 if ((*ptep
& pmap
->pmap_bits
[PG_V_IDX
]) == 0) {
5539 if (pmap_pte_w(pmap
, ptep
)) {
5540 atomic_add_long(&pt_pv
->pv_pmap
->pm_stats
.wired_count
,
5543 /* XXX else return NULL so caller doesn't unwire m ? */
5545 atomic_clear_long(ptep
, pmap
->pmap_bits
[PG_W_IDX
]);
5547 pa
= *ptep
& PG_FRAME
;
5548 m
= PHYS_TO_VM_PAGE(pa
); /* held by wired count */
5555 * Copy the range specified by src_addr/len from the source map to
5556 * the range dst_addr/len in the destination map.
5558 * This routine is only advisory and need not do anything.
5561 pmap_copy(pmap_t dst_pmap
, pmap_t src_pmap
, vm_offset_t dst_addr
,
5562 vm_size_t len
, vm_offset_t src_addr
)
5569 * Zero the specified physical page.
5571 * This function may be called from an interrupt and no locking is
5575 pmap_zero_page(vm_paddr_t phys
)
5577 vm_offset_t va
= PHYS_TO_DMAP(phys
);
5579 pagezero((void *)va
);
5585 * Zero part of a physical page by mapping it into memory and clearing
5586 * its contents with bzero.
5588 * off and size may not cover an area beyond a single hardware page.
5591 pmap_zero_page_area(vm_paddr_t phys
, int off
, int size
)
5593 vm_offset_t virt
= PHYS_TO_DMAP(phys
);
5595 bzero((char *)virt
+ off
, size
);
5601 * Copy the physical page from the source PA to the target PA.
5602 * This function may be called from an interrupt. No locking
5606 pmap_copy_page(vm_paddr_t src
, vm_paddr_t dst
)
5608 vm_offset_t src_virt
, dst_virt
;
5610 src_virt
= PHYS_TO_DMAP(src
);
5611 dst_virt
= PHYS_TO_DMAP(dst
);
5612 bcopy((void *)src_virt
, (void *)dst_virt
, PAGE_SIZE
);
5616 * pmap_copy_page_frag:
5618 * Copy the physical page from the source PA to the target PA.
5619 * This function may be called from an interrupt. No locking
5623 pmap_copy_page_frag(vm_paddr_t src
, vm_paddr_t dst
, size_t bytes
)
5625 vm_offset_t src_virt
, dst_virt
;
5627 src_virt
= PHYS_TO_DMAP(src
);
5628 dst_virt
= PHYS_TO_DMAP(dst
);
5630 bcopy((char *)src_virt
+ (src
& PAGE_MASK
),
5631 (char *)dst_virt
+ (dst
& PAGE_MASK
),
5636 * Returns true if the pmap's pv is one of the first 16 pvs linked to from
5637 * this page. This count may be changed upwards or downwards in the future;
5638 * it is only necessary that true be returned for a small subset of pmaps
5639 * for proper page aging.
5642 pmap_page_exists_quick(pmap_t pmap
, vm_page_t m
)
5647 if (!pmap_initialized
|| (m
->flags
& PG_FICTITIOUS
))
5650 vm_page_spin_lock(m
);
5651 TAILQ_FOREACH(pv
, &m
->md
.pv_list
, pv_list
) {
5652 if (pv
->pv_pmap
== pmap
) {
5653 vm_page_spin_unlock(m
);
5660 vm_page_spin_unlock(m
);
5665 * Remove all pages from specified address space this aids process exit
5666 * speeds. Also, this code may be special cased for the current process
5670 pmap_remove_pages(pmap_t pmap
, vm_offset_t sva
, vm_offset_t eva
)
5672 pmap_remove_noinval(pmap
, sva
, eva
);
5677 * pmap_testbit tests bits in pte's note that the testbit/clearbit
5678 * routines are inline, and a lot of things compile-time evaluate.
5683 pmap_testbit(vm_page_t m
, int bit
)
5689 if (!pmap_initialized
|| (m
->flags
& PG_FICTITIOUS
))
5692 if (TAILQ_FIRST(&m
->md
.pv_list
) == NULL
)
5694 vm_page_spin_lock(m
);
5695 if (TAILQ_FIRST(&m
->md
.pv_list
) == NULL
) {
5696 vm_page_spin_unlock(m
);
5700 TAILQ_FOREACH(pv
, &m
->md
.pv_list
, pv_list
) {
5701 #if defined(PMAP_DIAGNOSTIC)
5702 if (pv
->pv_pmap
== NULL
) {
5703 kprintf("Null pmap (tb) at pindex: %"PRIu64
"\n",
5711 * If the bit being tested is the modified bit, then
5712 * mark clean_map and ptes as never
5715 * WARNING! Because we do not lock the pv, *pte can be in a
5716 * state of flux. Despite this the value of *pte
5717 * will still be related to the vm_page in some way
5718 * because the pv cannot be destroyed as long as we
5719 * hold the vm_page spin lock.
5721 if (bit
== PG_A_IDX
|| bit
== PG_M_IDX
) {
5722 //& (pmap->pmap_bits[PG_A_IDX] | pmap->pmap_bits[PG_M_IDX])) {
5723 if (!pmap_track_modified(pv
->pv_pindex
))
5727 pte
= pmap_pte_quick(pv
->pv_pmap
, pv
->pv_pindex
<< PAGE_SHIFT
);
5728 if (*pte
& pmap
->pmap_bits
[bit
]) {
5729 vm_page_spin_unlock(m
);
5733 vm_page_spin_unlock(m
);
5738 * This routine is used to modify bits in ptes. Only one bit should be
5739 * specified. PG_RW requires special handling.
5741 * Caller must NOT hold any spin locks
5745 pmap_clearbit(vm_page_t m
, int bit_index
)
5752 if (!pmap_initialized
|| (m
->flags
& PG_FICTITIOUS
)) {
5753 if (bit_index
== PG_RW_IDX
)
5754 vm_page_flag_clear(m
, PG_WRITEABLE
);
5761 * Loop over all current mappings setting/clearing as appropos If
5762 * setting RO do we need to clear the VAC?
5764 * NOTE: When clearing PG_M we could also (not implemented) drop
5765 * through to the PG_RW code and clear PG_RW too, forcing
5766 * a fault on write to redetect PG_M for virtual kernels, but
5767 * it isn't necessary since virtual kernels invalidate the
5768 * pte when they clear the VPTE_M bit in their virtual page
5771 * NOTE: Does not re-dirty the page when clearing only PG_M.
5773 * NOTE: Because we do not lock the pv, *pte can be in a state of
5774 * flux. Despite this the value of *pte is still somewhat
5775 * related while we hold the vm_page spin lock.
5777 * *pte can be zero due to this race. Since we are clearing
5778 * bits we basically do no harm when this race occurs.
5780 if (bit_index
!= PG_RW_IDX
) {
5781 vm_page_spin_lock(m
);
5782 TAILQ_FOREACH(pv
, &m
->md
.pv_list
, pv_list
) {
5783 #if defined(PMAP_DIAGNOSTIC)
5784 if (pv
->pv_pmap
== NULL
) {
5785 kprintf("Null pmap (cb) at pindex: %"PRIu64
"\n",
5791 pte
= pmap_pte_quick(pv
->pv_pmap
,
5792 pv
->pv_pindex
<< PAGE_SHIFT
);
5794 if (pbits
& pmap
->pmap_bits
[bit_index
])
5795 atomic_clear_long(pte
, pmap
->pmap_bits
[bit_index
]);
5797 vm_page_spin_unlock(m
);
5802 * Clear PG_RW. Also clears PG_M and marks the page dirty if PG_M
5806 vm_page_spin_lock(m
);
5807 TAILQ_FOREACH(pv
, &m
->md
.pv_list
, pv_list
) {
5809 * don't write protect pager mappings
5811 if (!pmap_track_modified(pv
->pv_pindex
))
5814 #if defined(PMAP_DIAGNOSTIC)
5815 if (pv
->pv_pmap
== NULL
) {
5816 kprintf("Null pmap (cb) at pindex: %"PRIu64
"\n",
5824 * Skip pages which do not have PG_RW set.
5826 pte
= pmap_pte_quick(pv
->pv_pmap
, pv
->pv_pindex
<< PAGE_SHIFT
);
5827 if ((*pte
& pmap
->pmap_bits
[PG_RW_IDX
]) == 0)
5831 * We must lock the PV to be able to safely test the pte.
5833 if (pv_hold_try(pv
)) {
5834 vm_page_spin_unlock(m
);
5836 vm_page_spin_unlock(m
);
5837 pv_lock(pv
); /* held, now do a blocking lock */
5843 * Reload pte after acquiring pv.
5845 pte
= pmap_pte_quick(pv
->pv_pmap
, pv
->pv_pindex
<< PAGE_SHIFT
);
5847 if ((*pte
& pmap
->pmap_bits
[PG_RW_IDX
]) == 0) {
5853 KKASSERT(pv
->pv_pmap
== pmap
&& pv
->pv_m
== m
);
5859 nbits
= pbits
& ~(pmap
->pmap_bits
[PG_RW_IDX
] |
5860 pmap
->pmap_bits
[PG_M_IDX
]);
5861 if (pmap_inval_smp_cmpset(pmap
,
5862 ((vm_offset_t
)pv
->pv_pindex
<< PAGE_SHIFT
),
5863 pte
, pbits
, nbits
)) {
5870 * If PG_M was found to be set while we were clearing PG_RW
5871 * we also clear PG_M (done above) and mark the page dirty.
5872 * Callers expect this behavior.
5874 * we lost pv so it cannot be used as an iterator. In fact,
5875 * because we couldn't necessarily lock it atomically it may
5876 * have moved within the list and ALSO cannot be used as an
5879 vm_page_spin_lock(m
);
5880 if (pbits
& pmap
->pmap_bits
[PG_M_IDX
])
5882 vm_page_spin_unlock(m
);
5886 if (bit_index
== PG_RW_IDX
)
5887 vm_page_flag_clear(m
, PG_WRITEABLE
);
5888 vm_page_spin_unlock(m
);
5892 * Lower the permission for all mappings to a given page.
5894 * Page must be busied by caller. Because page is busied by caller this
5895 * should not be able to race a pmap_enter().
5898 pmap_page_protect(vm_page_t m
, vm_prot_t prot
)
5900 /* JG NX support? */
5901 if ((prot
& VM_PROT_WRITE
) == 0) {
5902 if (prot
& (VM_PROT_READ
| VM_PROT_EXECUTE
)) {
5904 * NOTE: pmap_clearbit(.. PG_RW) also clears
5905 * the PG_WRITEABLE flag in (m).
5907 pmap_clearbit(m
, PG_RW_IDX
);
5915 pmap_phys_address(vm_pindex_t ppn
)
5917 return (x86_64_ptob(ppn
));
5921 * Return a count of reference bits for a page, clearing those bits.
5922 * It is not necessary for every reference bit to be cleared, but it
5923 * is necessary that 0 only be returned when there are truly no
5924 * reference bits set.
5926 * XXX: The exact number of bits to check and clear is a matter that
5927 * should be tested and standardized at some point in the future for
5928 * optimal aging of shared pages.
5930 * This routine may not block.
5933 pmap_ts_referenced(vm_page_t m
)
5940 if (!pmap_initialized
|| (m
->flags
& PG_FICTITIOUS
))
5943 vm_page_spin_lock(m
);
5944 TAILQ_FOREACH(pv
, &m
->md
.pv_list
, pv_list
) {
5945 if (!pmap_track_modified(pv
->pv_pindex
))
5948 pte
= pmap_pte_quick(pv
->pv_pmap
, pv
->pv_pindex
<< PAGE_SHIFT
);
5949 if (pte
&& (*pte
& pmap
->pmap_bits
[PG_A_IDX
])) {
5950 atomic_clear_long(pte
, pmap
->pmap_bits
[PG_A_IDX
]);
5956 vm_page_spin_unlock(m
);
5963 * Return whether or not the specified physical page was modified
5964 * in any physical maps.
5967 pmap_is_modified(vm_page_t m
)
5971 res
= pmap_testbit(m
, PG_M_IDX
);
5976 * Clear the modify bits on the specified physical page.
5979 pmap_clear_modify(vm_page_t m
)
5981 pmap_clearbit(m
, PG_M_IDX
);
5985 * pmap_clear_reference:
5987 * Clear the reference bit on the specified physical page.
5990 pmap_clear_reference(vm_page_t m
)
5992 pmap_clearbit(m
, PG_A_IDX
);
5996 * Miscellaneous support routines follow
6001 x86_64_protection_init(void)
6007 * NX supported? (boot time loader.conf override only)
6009 TUNABLE_INT_FETCH("machdep.pmap_nx_enable", &pmap_nx_enable
);
6010 if (pmap_nx_enable
== 0 || (amd_feature
& AMDID_NX
) == 0)
6011 pmap_bits_default
[PG_NX_IDX
] = 0;
6014 * 0 is basically read-only access, but also set the NX (no-execute)
6015 * bit when VM_PROT_EXECUTE is not specified.
6017 kp
= protection_codes
;
6018 for (prot
= 0; prot
< PROTECTION_CODES_SIZE
; prot
++) {
6020 case VM_PROT_NONE
| VM_PROT_NONE
| VM_PROT_NONE
:
6022 * This case handled elsewhere
6026 case VM_PROT_READ
| VM_PROT_NONE
| VM_PROT_NONE
:
6030 *kp
++ = pmap_bits_default
[PG_NX_IDX
];
6032 case VM_PROT_READ
| VM_PROT_NONE
| VM_PROT_EXECUTE
:
6033 case VM_PROT_NONE
| VM_PROT_NONE
| VM_PROT_EXECUTE
:
6035 * Execute requires read access
6039 case VM_PROT_NONE
| VM_PROT_WRITE
| VM_PROT_NONE
:
6040 case VM_PROT_READ
| VM_PROT_WRITE
| VM_PROT_NONE
:
6042 * Write without execute is RW|NX
6044 *kp
++ = pmap_bits_default
[PG_RW_IDX
] |
6045 pmap_bits_default
[PG_NX_IDX
];
6047 case VM_PROT_READ
| VM_PROT_WRITE
| VM_PROT_EXECUTE
:
6048 case VM_PROT_NONE
| VM_PROT_WRITE
| VM_PROT_EXECUTE
:
6050 * Write with execute is RW
6052 *kp
++ = pmap_bits_default
[PG_RW_IDX
];
6059 * Map a set of physical memory pages into the kernel virtual
6060 * address space. Return a pointer to where it is mapped. This
6061 * routine is intended to be used for mapping device memory,
6064 * NOTE: We can't use pgeflag unless we invalidate the pages one at
6067 * NOTE: The PAT attributes {WRITE_BACK, WRITE_THROUGH, UNCACHED, UNCACHEABLE}
6068 * work whether the cpu supports PAT or not. The remaining PAT
6069 * attributes {WRITE_PROTECTED, WRITE_COMBINING} only work if the cpu
6073 pmap_mapdev(vm_paddr_t pa
, vm_size_t size
)
6075 return(pmap_mapdev_attr(pa
, size
, PAT_WRITE_BACK
));
6079 pmap_mapdev_uncacheable(vm_paddr_t pa
, vm_size_t size
)
6081 return(pmap_mapdev_attr(pa
, size
, PAT_UNCACHEABLE
));
6085 pmap_mapbios(vm_paddr_t pa
, vm_size_t size
)
6087 return (pmap_mapdev_attr(pa
, size
, PAT_WRITE_BACK
));
6091 * Map a set of physical memory pages into the kernel virtual
6092 * address space. Return a pointer to where it is mapped. This
6093 * routine is intended to be used for mapping device memory,
6097 pmap_mapdev_attr(vm_paddr_t pa
, vm_size_t size
, int mode
)
6099 vm_offset_t va
, tmpva
, offset
;
6103 offset
= pa
& PAGE_MASK
;
6104 size
= roundup(offset
+ size
, PAGE_SIZE
);
6106 va
= kmem_alloc_nofault(&kernel_map
, size
, VM_SUBSYS_MAPDEV
, PAGE_SIZE
);
6108 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
6110 pa
= pa
& ~PAGE_MASK
;
6111 for (tmpva
= va
, tmpsize
= size
; tmpsize
> 0;) {
6112 pte
= vtopte(tmpva
);
6114 kernel_pmap
.pmap_bits
[PG_RW_IDX
] |
6115 kernel_pmap
.pmap_bits
[PG_V_IDX
] | /* pgeflag | */
6116 kernel_pmap
.pmap_cache_bits
[mode
];
6117 tmpsize
-= PAGE_SIZE
;
6121 pmap_invalidate_range(&kernel_pmap
, va
, va
+ size
);
6122 pmap_invalidate_cache_range(va
, va
+ size
);
6124 return ((void *)(va
+ offset
));
6128 pmap_unmapdev(vm_offset_t va
, vm_size_t size
)
6130 vm_offset_t base
, offset
;
6132 base
= va
& ~PAGE_MASK
;
6133 offset
= va
& PAGE_MASK
;
6134 size
= roundup(offset
+ size
, PAGE_SIZE
);
6135 pmap_qremove(va
, size
>> PAGE_SHIFT
);
6136 kmem_free(&kernel_map
, base
, size
);
6140 * Sets the memory attribute for the specified page.
6143 pmap_page_set_memattr(vm_page_t m
, vm_memattr_t ma
)
6149 * If "m" is a normal page, update its direct mapping. This update
6150 * can be relied upon to perform any cache operations that are
6151 * required for data coherence.
6153 if ((m
->flags
& PG_FICTITIOUS
) == 0)
6154 pmap_change_attr(PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m
)), 1, m
->pat_mode
);
6158 * Change the PAT attribute on an existing kernel memory map. Caller
6159 * must ensure that the virtual memory in question is not accessed
6160 * during the adjustment.
6163 pmap_change_attr(vm_offset_t va
, vm_size_t count
, int mode
)
6170 panic("pmap_change_attr: va is NULL");
6171 base
= trunc_page(va
);
6175 *pte
= (*pte
& ~(pt_entry_t
)(kernel_pmap
.pmap_cache_mask
)) |
6176 kernel_pmap
.pmap_cache_bits
[mode
];
6181 changed
= 1; /* XXX: not optimal */
6184 * Flush CPU caches if required to make sure any data isn't cached that
6185 * shouldn't be, etc.
6188 pmap_invalidate_range(&kernel_pmap
, base
, va
);
6189 pmap_invalidate_cache_range(base
, va
);
6194 * perform the pmap work for mincore
6197 pmap_mincore(pmap_t pmap
, vm_offset_t addr
)
6199 pt_entry_t
*ptep
, pte
;
6203 ptep
= pmap_pte(pmap
, addr
);
6205 if (ptep
&& (pte
= *ptep
) != 0) {
6208 val
= MINCORE_INCORE
;
6209 if ((pte
& pmap
->pmap_bits
[PG_MANAGED_IDX
]) == 0)
6212 pa
= pte
& PG_FRAME
;
6214 if (pte
& pmap
->pmap_bits
[PG_DEVICE_IDX
])
6217 m
= PHYS_TO_VM_PAGE(pa
);
6222 if (pte
& pmap
->pmap_bits
[PG_M_IDX
])
6223 val
|= MINCORE_MODIFIED
|MINCORE_MODIFIED_OTHER
;
6225 * Modified by someone
6227 else if (m
&& (m
->dirty
|| pmap_is_modified(m
)))
6228 val
|= MINCORE_MODIFIED_OTHER
;
6232 if (pte
& pmap
->pmap_bits
[PG_A_IDX
])
6233 val
|= MINCORE_REFERENCED
|MINCORE_REFERENCED_OTHER
;
6236 * Referenced by someone
6238 else if (m
&& ((m
->flags
& PG_REFERENCED
) ||
6239 pmap_ts_referenced(m
))) {
6240 val
|= MINCORE_REFERENCED_OTHER
;
6241 vm_page_flag_set(m
, PG_REFERENCED
);
6250 * Replace p->p_vmspace with a new one. If adjrefs is non-zero the new
6251 * vmspace will be ref'd and the old one will be deref'd.
6253 * The vmspace for all lwps associated with the process will be adjusted
6254 * and cr3 will be reloaded if any lwp is the current lwp.
6256 * The process must hold the vmspace->vm_map.token for oldvm and newvm
6259 pmap_replacevm(struct proc
*p
, struct vmspace
*newvm
, int adjrefs
)
6261 struct vmspace
*oldvm
;
6264 oldvm
= p
->p_vmspace
;
6265 if (oldvm
!= newvm
) {
6268 p
->p_vmspace
= newvm
;
6269 KKASSERT(p
->p_nthreads
== 1);
6270 lp
= RB_ROOT(&p
->p_lwp_tree
);
6271 pmap_setlwpvm(lp
, newvm
);
6278 * Set the vmspace for a LWP. The vmspace is almost universally set the
6279 * same as the process vmspace, but virtual kernels need to swap out contexts
6280 * on a per-lwp basis.
6282 * Caller does not necessarily hold any vmspace tokens. Caller must control
6283 * the lwp (typically be in the context of the lwp). We use a critical
6284 * section to protect against statclock and hardclock (statistics collection).
6287 pmap_setlwpvm(struct lwp
*lp
, struct vmspace
*newvm
)
6289 struct vmspace
*oldvm
;
6293 oldvm
= lp
->lwp_vmspace
;
6295 if (oldvm
!= newvm
) {
6298 KKASSERT((newvm
->vm_refcnt
& VM_REF_DELETED
) == 0);
6299 lp
->lwp_vmspace
= newvm
;
6300 if (td
->td_lwp
== lp
) {
6301 pmap
= vmspace_pmap(newvm
);
6302 ATOMIC_CPUMASK_ORBIT(pmap
->pm_active
, mycpu
->gd_cpuid
);
6303 if (pmap
->pm_active_lock
& CPULOCK_EXCL
)
6304 pmap_interlock_wait(newvm
);
6305 #if defined(SWTCH_OPTIM_STATS)
6308 if (pmap
->pmap_bits
[TYPE_IDX
] == REGULAR_PMAP
) {
6309 td
->td_pcb
->pcb_cr3
= vtophys(pmap
->pm_pml4
);
6310 if (vm_isolated_user_pmap
&&
6311 pmap
->pm_pmlpv_iso
) {
6312 td
->td_pcb
->pcb_cr3_iso
=
6313 vtophys(pmap
->pm_pml4_iso
);
6314 td
->td_pcb
->pcb_flags
|= PCB_ISOMMU
;
6316 td
->td_pcb
->pcb_cr3_iso
= 0;
6317 td
->td_pcb
->pcb_flags
&= ~PCB_ISOMMU
;
6319 } else if (pmap
->pmap_bits
[TYPE_IDX
] == EPT_PMAP
) {
6320 td
->td_pcb
->pcb_cr3
= KPML4phys
;
6321 td
->td_pcb
->pcb_cr3_iso
= 0;
6322 td
->td_pcb
->pcb_flags
&= ~PCB_ISOMMU
;
6324 panic("pmap_setlwpvm: unknown pmap type\n");
6328 * The MMU separation fields needs to be updated.
6329 * (it can't access the pcb directly from the
6330 * restricted user pmap).
6332 if (td
== curthread
) {
6333 struct trampframe
*tramp
;
6335 tramp
= &pscpu
->trampoline
;
6336 tramp
->tr_pcb_cr3
= td
->td_pcb
->pcb_cr3
;
6337 tramp
->tr_pcb_cr3_iso
= td
->td_pcb
->pcb_cr3_iso
;
6338 tramp
->tr_pcb_flags
= td
->td_pcb
->pcb_flags
;
6339 /* tr_pcb_rsp doesn't change */
6343 * In kernel-land we always use the normal PML4E
6344 * so the kernel is fully mapped and can also access
6347 load_cr3(td
->td_pcb
->pcb_cr3
);
6348 pmap
= vmspace_pmap(oldvm
);
6349 ATOMIC_CPUMASK_NANDBIT(pmap
->pm_active
,
6357 * Called when switching to a locked pmap, used to interlock against pmaps
6358 * undergoing modifications to prevent us from activating the MMU for the
6359 * target pmap until all such modifications have completed. We have to do
6360 * this because the thread making the modifications has already set up its
6361 * SMP synchronization mask.
6363 * This function cannot sleep!
6368 pmap_interlock_wait(struct vmspace
*vm
)
6370 struct pmap
*pmap
= &vm
->vm_pmap
;
6372 if (pmap
->pm_active_lock
& CPULOCK_EXCL
) {
6374 KKASSERT(curthread
->td_critcount
>= 2);
6375 DEBUG_PUSH_INFO("pmap_interlock_wait");
6376 while (pmap
->pm_active_lock
& CPULOCK_EXCL
) {
6378 lwkt_process_ipiq();
6386 pmap_addr_hint(vm_object_t obj
, vm_offset_t addr
, vm_size_t size
)
6389 if ((obj
== NULL
) || (size
< NBPDR
) ||
6390 ((obj
->type
!= OBJT_DEVICE
) && (obj
->type
!= OBJT_MGTDEVICE
))) {
6394 addr
= roundup2(addr
, NBPDR
);
6399 * Used by kmalloc/kfree, page already exists at va
6402 pmap_kvtom(vm_offset_t va
)
6404 pt_entry_t
*ptep
= vtopte(va
);
6406 KKASSERT((*ptep
& kernel_pmap
.pmap_bits
[PG_DEVICE_IDX
]) == 0);
6407 return(PHYS_TO_VM_PAGE(*ptep
& PG_FRAME
));
6411 * Initialize machine-specific shared page directory support. This
6412 * is executed when a VM object is created.
6415 pmap_object_init(vm_object_t object
)
6417 object
->md
.pmap_rw
= NULL
;
6418 object
->md
.pmap_ro
= NULL
;
6422 * Clean up machine-specific shared page directory support. This
6423 * is executed when a VM object is destroyed.
6426 pmap_object_free(vm_object_t object
)
6430 if ((pmap
= object
->md
.pmap_rw
) != NULL
) {
6431 object
->md
.pmap_rw
= NULL
;
6432 pmap_remove_noinval(pmap
,
6433 VM_MIN_USER_ADDRESS
, VM_MAX_USER_ADDRESS
);
6434 CPUMASK_ASSZERO(pmap
->pm_active
);
6437 kfree(pmap
, M_OBJPMAP
);
6439 if ((pmap
= object
->md
.pmap_ro
) != NULL
) {
6440 object
->md
.pmap_ro
= NULL
;
6441 pmap_remove_noinval(pmap
,
6442 VM_MIN_USER_ADDRESS
, VM_MAX_USER_ADDRESS
);
6443 CPUMASK_ASSZERO(pmap
->pm_active
);
6446 kfree(pmap
, M_OBJPMAP
);
6451 * pmap_pgscan_callback - Used by pmap_pgscan to acquire the related
6452 * VM page and issue a pginfo->callback.
6454 * We are expected to dispose of any non-NULL pte_pv.
6458 pmap_pgscan_callback(pmap_t pmap
, struct pmap_scan_info
*info
,
6459 pv_entry_t pte_pv
, vm_pindex_t
*pte_placemark
,
6460 pv_entry_t pt_pv
, int sharept
,
6461 vm_offset_t va
, pt_entry_t
*ptep
, void *arg
)
6463 struct pmap_pgscan_info
*pginfo
= arg
;
6468 * Try to busy the page while we hold the pte_pv locked.
6470 KKASSERT(pte_pv
->pv_m
);
6471 m
= PHYS_TO_VM_PAGE(*ptep
& PG_FRAME
);
6472 if (vm_page_busy_try(m
, TRUE
) == 0) {
6473 if (m
== PHYS_TO_VM_PAGE(*ptep
& PG_FRAME
)) {
6475 * The callback is issued with the pte_pv
6476 * unlocked and put away, and the pt_pv
6481 vm_page_wire_quick(pt_pv
->pv_m
);
6484 if (pginfo
->callback(pginfo
, va
, m
) < 0)
6488 vm_page_unwire_quick(pt_pv
->pv_m
);
6495 ++pginfo
->busycount
;
6500 * Shared page table or unmanaged page (sharept or !sharept)
6502 pv_placemarker_wakeup(pmap
, pte_placemark
);
6507 pmap_pgscan(struct pmap_pgscan_info
*pginfo
)
6509 struct pmap_scan_info info
;
6511 pginfo
->offset
= pginfo
->beg_addr
;
6512 info
.pmap
= pginfo
->pmap
;
6513 info
.sva
= pginfo
->beg_addr
;
6514 info
.eva
= pginfo
->end_addr
;
6515 info
.func
= pmap_pgscan_callback
;
6517 pmap_scan(&info
, 0);
6519 pginfo
->offset
= pginfo
->end_addr
;
6523 * Wait for a placemarker that we do not own to clear. The placemarker
6524 * in question is not necessarily set to the pindex we want, we may have
6525 * to wait on the element because we want to reserve it ourselves.
6527 * NOTE: PM_PLACEMARK_WAKEUP sets a bit which is already set in
6528 * PM_NOPLACEMARK, so it does not interfere with placemarks
6529 * which have already been woken up.
6533 pv_placemarker_wait(pmap_t pmap
, vm_pindex_t
*pmark
)
6535 if (*pmark
!= PM_NOPLACEMARK
) {
6536 atomic_set_long(pmark
, PM_PLACEMARK_WAKEUP
);
6537 tsleep_interlock(pmark
, 0);
6538 if (*pmark
!= PM_NOPLACEMARK
)
6539 tsleep(pmark
, PINTERLOCKED
, "pvplw", 0);
6544 * Wakeup a placemarker that we own. Replace the entry with
6545 * PM_NOPLACEMARK and issue a wakeup() if necessary.
6549 pv_placemarker_wakeup(pmap_t pmap
, vm_pindex_t
*pmark
)
6553 pindex
= atomic_swap_long(pmark
, PM_NOPLACEMARK
);
6554 KKASSERT(pindex
!= PM_NOPLACEMARK
);
6555 if (pindex
& PM_PLACEMARK_WAKEUP
)