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-2012 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.
49 #include "opt_disable_pse.h"
52 #include "opt_msgbuf.h"
54 #include <sys/param.h>
55 #include <sys/kernel.h>
57 #include <sys/msgbuf.h>
58 #include <sys/vmmeter.h>
60 #include <sys/systm.h>
63 #include <vm/vm_param.h>
64 #include <sys/sysctl.h>
66 #include <vm/vm_kern.h>
67 #include <vm/vm_page.h>
68 #include <vm/vm_map.h>
69 #include <vm/vm_object.h>
70 #include <vm/vm_extern.h>
71 #include <vm/vm_pageout.h>
72 #include <vm/vm_pager.h>
73 #include <vm/vm_zone.h>
76 #include <sys/thread2.h>
77 #include <sys/sysref2.h>
78 #include <sys/spinlock2.h>
79 #include <vm/vm_page2.h>
81 #include <machine/cputypes.h>
82 #include <machine/md_var.h>
83 #include <machine/specialreg.h>
84 #include <machine/smp.h>
85 #include <machine_base/apic/apicreg.h>
86 #include <machine/globaldata.h>
87 #include <machine/pmap.h>
88 #include <machine/pmap_inval.h>
89 #include <machine/inttypes.h>
93 #define PMAP_KEEP_PDIRS
94 #ifndef PMAP_SHPGPERPROC
95 #define PMAP_SHPGPERPROC 2000
98 #if defined(DIAGNOSTIC)
99 #define PMAP_DIAGNOSTIC
105 * pmap debugging will report who owns a pv lock when blocking.
109 #define PMAP_DEBUG_DECL ,const char *func, int lineno
110 #define PMAP_DEBUG_ARGS , __func__, __LINE__
111 #define PMAP_DEBUG_COPY , func, lineno
113 #define pv_get(pmap, pindex) _pv_get(pmap, pindex \
115 #define pv_lock(pv) _pv_lock(pv \
117 #define pv_hold_try(pv) _pv_hold_try(pv \
119 #define pv_alloc(pmap, pindex, isnewp) _pv_alloc(pmap, pindex, isnewp \
124 #define PMAP_DEBUG_DECL
125 #define PMAP_DEBUG_ARGS
126 #define PMAP_DEBUG_COPY
128 #define pv_get(pmap, pindex) _pv_get(pmap, pindex)
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)
136 * Get PDEs and PTEs for user/kernel address space
138 #define pdir_pde(m, v) (m[(vm_offset_t)(v) >> PDRSHIFT])
140 #define pmap_pde_v(pmap, pte) ((*(pd_entry_t *)pte & pmap->pmap_bits[PG_V_IDX]) != 0)
141 #define pmap_pte_w(pmap, pte) ((*(pt_entry_t *)pte & pmap->pmap_bits[PG_W_IDX]) != 0)
142 #define pmap_pte_m(pmap, pte) ((*(pt_entry_t *)pte & pmap->pmap_bits[PG_M_IDX]) != 0)
143 #define pmap_pte_u(pmap, pte) ((*(pt_entry_t *)pte & pmap->pmap_bits[PG_U_IDX]) != 0)
144 #define pmap_pte_v(pmap, pte) ((*(pt_entry_t *)pte & pmap->pmap_bits[PG_V_IDX]) != 0)
147 * Given a map and a machine independent protection code,
148 * convert to a vax protection code.
150 #define pte_prot(m, p) \
151 (m->protection_codes[p & (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE)])
152 static int protection_codes
[PROTECTION_CODES_SIZE
];
154 struct pmap kernel_pmap
;
155 static TAILQ_HEAD(,pmap
) pmap_list
= TAILQ_HEAD_INITIALIZER(pmap_list
);
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 */
170 //static int pseflag; /* PG_PS or-in */
174 static vm_paddr_t dmaplimit
;
176 vm_offset_t kernel_vm_end
= VM_MIN_KERNEL_ADDRESS
;
178 static pt_entry_t pat_pte_index
[PAT_INDEX_SIZE
]; /* PAT -> PG_ bits */
179 /*static pt_entry_t pat_pde_index[PAT_INDEX_SIZE];*/ /* PAT -> PG_ bits */
181 static uint64_t KPTbase
;
182 static uint64_t KPTphys
;
183 static uint64_t KPDphys
; /* phys addr of kernel level 2 */
184 static uint64_t KPDbase
; /* phys addr of kernel level 2 @ KERNBASE */
185 uint64_t KPDPphys
; /* phys addr of kernel level 3 */
186 uint64_t KPML4phys
; /* phys addr of kernel level 4 */
188 static uint64_t DMPDphys
; /* phys addr of direct mapped level 2 */
189 static uint64_t DMPDPphys
; /* phys addr of direct mapped level 3 */
192 * Data for the pv entry allocation mechanism
194 static vm_zone_t pvzone
;
195 static struct vm_zone pvzone_store
;
196 static struct vm_object pvzone_obj
;
197 static int pv_entry_max
=0, pv_entry_high_water
=0;
198 static int pmap_pagedaemon_waken
= 0;
199 static struct pv_entry
*pvinit
;
202 * All those kernel PT submaps that BSD is so fond of
204 pt_entry_t
*CMAP1
= NULL
, *ptmmap
;
205 caddr_t CADDR1
= NULL
, ptvmmap
= NULL
;
206 static pt_entry_t
*msgbufmap
;
207 struct msgbuf
*msgbufp
=NULL
;
210 * PMAP default PG_* bits. Needed to be able to add
211 * EPT/NPT pagetable pmap_bits for the VMM module
213 uint64_t pmap_bits_default
[] = {
214 REGULAR_PMAP
, /* TYPE_IDX 0 */
215 X86_PG_V
, /* PG_V_IDX 1 */
216 X86_PG_RW
, /* PG_RW_IDX 2 */
217 X86_PG_U
, /* PG_U_IDX 3 */
218 X86_PG_A
, /* PG_A_IDX 4 */
219 X86_PG_M
, /* PG_M_IDX 5 */
220 X86_PG_PS
, /* PG_PS_IDX3 6 */
221 X86_PG_G
, /* PG_G_IDX 7 */
222 X86_PG_AVAIL1
, /* PG_AVAIL1_IDX 8 */
223 X86_PG_AVAIL2
, /* PG_AVAIL2_IDX 9 */
224 X86_PG_AVAIL3
, /* PG_AVAIL3_IDX 10 */
225 X86_PG_NC_PWT
| X86_PG_NC_PCD
, /* PG_N_IDX 11 */
230 static pt_entry_t
*pt_crashdumpmap
;
231 static caddr_t crashdumpmap
;
234 static int pmap_enter_debug
= 0;
235 SYSCTL_INT(_machdep
, OID_AUTO
, pmap_enter_debug
, CTLFLAG_RW
,
236 &pmap_enter_debug
, 0, "Debug pmap_enter's");
238 static int pmap_yield_count
= 64;
239 SYSCTL_INT(_machdep
, OID_AUTO
, pmap_yield_count
, CTLFLAG_RW
,
240 &pmap_yield_count
, 0, "Yield during init_pt/release");
241 static int pmap_mmu_optimize
= 0;
242 SYSCTL_INT(_machdep
, OID_AUTO
, pmap_mmu_optimize
, CTLFLAG_RW
,
243 &pmap_mmu_optimize
, 0, "Share page table pages when possible");
244 int pmap_fast_kernel_cpusync
= 0;
245 SYSCTL_INT(_machdep
, OID_AUTO
, pmap_fast_kernel_cpusync
, CTLFLAG_RW
,
246 &pmap_fast_kernel_cpusync
, 0, "Share page table pages when possible");
250 /* Standard user access funtions */
251 extern int std_copyinstr (const void *udaddr
, void *kaddr
, size_t len
,
253 extern int std_copyin (const void *udaddr
, void *kaddr
, size_t len
);
254 extern int std_copyout (const void *kaddr
, void *udaddr
, size_t len
);
255 extern int std_fubyte (const void *base
);
256 extern int std_subyte (void *base
, int byte
);
257 extern long std_fuword (const void *base
);
258 extern int std_suword (void *base
, long word
);
259 extern int std_suword32 (void *base
, int word
);
261 static void pv_hold(pv_entry_t pv
);
262 static int _pv_hold_try(pv_entry_t pv
264 static void pv_drop(pv_entry_t pv
);
265 static void _pv_lock(pv_entry_t pv
267 static void pv_unlock(pv_entry_t pv
);
268 static pv_entry_t
_pv_alloc(pmap_t pmap
, vm_pindex_t pindex
, int *isnew
270 static pv_entry_t
_pv_get(pmap_t pmap
, vm_pindex_t pindex
272 static pv_entry_t
pv_get_try(pmap_t pmap
, vm_pindex_t pindex
, int *errorp
);
273 static pv_entry_t
pv_find(pmap_t pmap
, vm_pindex_t pindex
);
274 static void pv_put(pv_entry_t pv
);
275 static void pv_free(pv_entry_t pv
);
276 static void *pv_pte_lookup(pv_entry_t pv
, vm_pindex_t pindex
);
277 static pv_entry_t
pmap_allocpte(pmap_t pmap
, vm_pindex_t ptepindex
,
279 static pv_entry_t
pmap_allocpte_seg(pmap_t pmap
, vm_pindex_t ptepindex
,
280 pv_entry_t
*pvpp
, vm_map_entry_t entry
, vm_offset_t va
);
281 static void pmap_remove_pv_pte(pv_entry_t pv
, pv_entry_t pvp
,
282 pmap_inval_bulk_t
*bulk
);
283 static vm_page_t
pmap_remove_pv_page(pv_entry_t pv
);
284 static int pmap_release_pv(pv_entry_t pv
, pv_entry_t pvp
,
285 pmap_inval_bulk_t
*bulk
);
287 struct pmap_scan_info
;
288 static void pmap_remove_callback(pmap_t pmap
, struct pmap_scan_info
*info
,
289 pv_entry_t pte_pv
, pv_entry_t pt_pv
, int sharept
,
290 vm_offset_t va
, pt_entry_t
*ptep
, void *arg __unused
);
291 static void pmap_protect_callback(pmap_t pmap
, struct pmap_scan_info
*info
,
292 pv_entry_t pte_pv
, pv_entry_t pt_pv
, int sharept
,
293 vm_offset_t va
, pt_entry_t
*ptep
, void *arg __unused
);
295 static void i386_protection_init (void);
296 static void create_pagetables(vm_paddr_t
*firstaddr
);
297 static void pmap_remove_all (vm_page_t m
);
298 static boolean_t
pmap_testbit (vm_page_t m
, int bit
);
300 static pt_entry_t
* pmap_pte_quick (pmap_t pmap
, vm_offset_t va
);
301 static vm_offset_t
pmap_kmem_choose(vm_offset_t addr
);
303 static void pmap_pinit_defaults(struct pmap
*pmap
);
305 static unsigned pdir4mb
;
308 pv_entry_compare(pv_entry_t pv1
, pv_entry_t pv2
)
310 if (pv1
->pv_pindex
< pv2
->pv_pindex
)
312 if (pv1
->pv_pindex
> pv2
->pv_pindex
)
317 RB_GENERATE2(pv_entry_rb_tree
, pv_entry
, pv_entry
,
318 pv_entry_compare
, vm_pindex_t
, pv_pindex
);
322 pmap_page_stats_adding(vm_page_t m
)
324 globaldata_t gd
= mycpu
;
326 if (TAILQ_EMPTY(&m
->md
.pv_list
)) {
327 ++gd
->gd_vmtotal
.t_arm
;
328 } else if (TAILQ_FIRST(&m
->md
.pv_list
) ==
329 TAILQ_LAST(&m
->md
.pv_list
, md_page_pv_list
)) {
330 ++gd
->gd_vmtotal
.t_armshr
;
331 ++gd
->gd_vmtotal
.t_avmshr
;
333 ++gd
->gd_vmtotal
.t_avmshr
;
339 pmap_page_stats_deleting(vm_page_t m
)
341 globaldata_t gd
= mycpu
;
343 if (TAILQ_EMPTY(&m
->md
.pv_list
)) {
344 --gd
->gd_vmtotal
.t_arm
;
345 } else if (TAILQ_FIRST(&m
->md
.pv_list
) ==
346 TAILQ_LAST(&m
->md
.pv_list
, md_page_pv_list
)) {
347 --gd
->gd_vmtotal
.t_armshr
;
348 --gd
->gd_vmtotal
.t_avmshr
;
350 --gd
->gd_vmtotal
.t_avmshr
;
355 * Move the kernel virtual free pointer to the next
356 * 2MB. This is used to help improve performance
357 * by using a large (2MB) page for much of the kernel
358 * (.text, .data, .bss)
362 pmap_kmem_choose(vm_offset_t addr
)
364 vm_offset_t newaddr
= addr
;
366 newaddr
= roundup2(addr
, NBPDR
);
373 * Super fast pmap_pte routine best used when scanning the pv lists.
374 * This eliminates many course-grained invltlb calls. Note that many of
375 * the pv list scans are across different pmaps and it is very wasteful
376 * to do an entire invltlb when checking a single mapping.
378 static __inline pt_entry_t
*pmap_pte(pmap_t pmap
, vm_offset_t va
);
382 pmap_pte_quick(pmap_t pmap
, vm_offset_t va
)
384 return pmap_pte(pmap
, va
);
388 * Returns the pindex of a page table entry (representing a terminal page).
389 * There are NUPTE_TOTAL page table entries possible (a huge number)
391 * x86-64 has a 48-bit address space, where bit 47 is sign-extended out.
392 * We want to properly translate negative KVAs.
396 pmap_pte_pindex(vm_offset_t va
)
398 return ((va
>> PAGE_SHIFT
) & (NUPTE_TOTAL
- 1));
402 * Returns the pindex of a page table.
406 pmap_pt_pindex(vm_offset_t va
)
408 return (NUPTE_TOTAL
+ ((va
>> PDRSHIFT
) & (NUPT_TOTAL
- 1)));
412 * Returns the pindex of a page directory.
416 pmap_pd_pindex(vm_offset_t va
)
418 return (NUPTE_TOTAL
+ NUPT_TOTAL
+
419 ((va
>> PDPSHIFT
) & (NUPD_TOTAL
- 1)));
424 pmap_pdp_pindex(vm_offset_t va
)
426 return (NUPTE_TOTAL
+ NUPT_TOTAL
+ NUPD_TOTAL
+
427 ((va
>> PML4SHIFT
) & (NUPDP_TOTAL
- 1)));
432 pmap_pml4_pindex(void)
434 return (NUPTE_TOTAL
+ NUPT_TOTAL
+ NUPD_TOTAL
+ NUPDP_TOTAL
);
438 * Return various clipped indexes for a given VA
440 * Returns the index of a pt in a page directory, representing a page
445 pmap_pt_index(vm_offset_t va
)
447 return ((va
>> PDRSHIFT
) & ((1ul << NPDEPGSHIFT
) - 1));
451 * Returns the index of a pd in a page directory page, representing a page
456 pmap_pd_index(vm_offset_t va
)
458 return ((va
>> PDPSHIFT
) & ((1ul << NPDPEPGSHIFT
) - 1));
462 * Returns the index of a pdp in the pml4 table, representing a page
467 pmap_pdp_index(vm_offset_t va
)
469 return ((va
>> PML4SHIFT
) & ((1ul << NPML4EPGSHIFT
) - 1));
473 * Generic procedure to index a pte from a pt, pd, or pdp.
475 * NOTE: Normally passed pindex as pmap_xx_index(). pmap_xx_pindex() is NOT
476 * a page table page index but is instead of PV lookup index.
480 pv_pte_lookup(pv_entry_t pv
, vm_pindex_t pindex
)
484 pte
= (pt_entry_t
*)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(pv
->pv_m
));
485 return(&pte
[pindex
]);
489 * Return pointer to PDP slot in the PML4
493 pmap_pdp(pmap_t pmap
, vm_offset_t va
)
495 return (&pmap
->pm_pml4
[pmap_pdp_index(va
)]);
499 * Return pointer to PD slot in the PDP given a pointer to the PDP
503 pmap_pdp_to_pd(pml4_entry_t pdp_pte
, vm_offset_t va
)
507 pd
= (pdp_entry_t
*)PHYS_TO_DMAP(pdp_pte
& PG_FRAME
);
508 return (&pd
[pmap_pd_index(va
)]);
512 * Return pointer to PD slot in the PDP.
516 pmap_pd(pmap_t pmap
, vm_offset_t va
)
520 pdp
= pmap_pdp(pmap
, va
);
521 if ((*pdp
& pmap
->pmap_bits
[PG_V_IDX
]) == 0)
523 return (pmap_pdp_to_pd(*pdp
, va
));
527 * Return pointer to PT slot in the PD given a pointer to the PD
531 pmap_pd_to_pt(pdp_entry_t pd_pte
, vm_offset_t va
)
535 pt
= (pd_entry_t
*)PHYS_TO_DMAP(pd_pte
& PG_FRAME
);
536 return (&pt
[pmap_pt_index(va
)]);
540 * Return pointer to PT slot in the PD
542 * SIMPLE PMAP NOTE: Simple pmaps (embedded in objects) do not have PDPs,
543 * so we cannot lookup the PD via the PDP. Instead we
544 * must look it up via the pmap.
548 pmap_pt(pmap_t pmap
, vm_offset_t va
)
552 vm_pindex_t pd_pindex
;
554 if (pmap
->pm_flags
& PMAP_FLAG_SIMPLE
) {
555 pd_pindex
= pmap_pd_pindex(va
);
556 spin_lock(&pmap
->pm_spin
);
557 pv
= pv_entry_rb_tree_RB_LOOKUP(&pmap
->pm_pvroot
, pd_pindex
);
558 spin_unlock(&pmap
->pm_spin
);
559 if (pv
== NULL
|| pv
->pv_m
== NULL
)
561 return (pmap_pd_to_pt(VM_PAGE_TO_PHYS(pv
->pv_m
), va
));
563 pd
= pmap_pd(pmap
, va
);
564 if (pd
== NULL
|| (*pd
& pmap
->pmap_bits
[PG_V_IDX
]) == 0)
566 return (pmap_pd_to_pt(*pd
, va
));
571 * Return pointer to PTE slot in the PT given a pointer to the PT
575 pmap_pt_to_pte(pd_entry_t pt_pte
, vm_offset_t va
)
579 pte
= (pt_entry_t
*)PHYS_TO_DMAP(pt_pte
& PG_FRAME
);
580 return (&pte
[pmap_pte_index(va
)]);
584 * Return pointer to PTE slot in the PT
588 pmap_pte(pmap_t pmap
, vm_offset_t va
)
592 pt
= pmap_pt(pmap
, va
);
593 if (pt
== NULL
|| (*pt
& pmap
->pmap_bits
[PG_V_IDX
]) == 0)
595 if ((*pt
& pmap
->pmap_bits
[PG_PS_IDX
]) != 0)
596 return ((pt_entry_t
*)pt
);
597 return (pmap_pt_to_pte(*pt
, va
));
601 * Of all the layers (PTE, PT, PD, PDP, PML4) the best one to cache is
602 * the PT layer. This will speed up core pmap operations considerably.
604 * NOTE: The pmap spinlock does not need to be held but the passed-in pv
605 * must be in a known associated state (typically by being locked when
606 * the pmap spinlock isn't held). We allow the race for that case.
610 pv_cache(pv_entry_t pv
, vm_pindex_t pindex
)
612 if (pindex
>= pmap_pt_pindex(0) && pindex
<= pmap_pd_pindex(0))
613 pv
->pv_pmap
->pm_pvhint
= pv
;
618 * Return address of PT slot in PD (KVM only)
620 * Cannot be used for user page tables because it might interfere with
621 * the shared page-table-page optimization (pmap_mmu_optimize).
625 vtopt(vm_offset_t va
)
627 uint64_t mask
= ((1ul << (NPDEPGSHIFT
+ NPDPEPGSHIFT
+
628 NPML4EPGSHIFT
)) - 1);
630 return (PDmap
+ ((va
>> PDRSHIFT
) & mask
));
634 * KVM - return address of PTE slot in PT
638 vtopte(vm_offset_t va
)
640 uint64_t mask
= ((1ul << (NPTEPGSHIFT
+ NPDEPGSHIFT
+
641 NPDPEPGSHIFT
+ NPML4EPGSHIFT
)) - 1);
643 return (PTmap
+ ((va
>> PAGE_SHIFT
) & mask
));
647 allocpages(vm_paddr_t
*firstaddr
, long n
)
652 bzero((void *)ret
, n
* PAGE_SIZE
);
653 *firstaddr
+= n
* PAGE_SIZE
;
659 create_pagetables(vm_paddr_t
*firstaddr
)
661 long i
; /* must be 64 bits */
667 * We are running (mostly) V=P at this point
669 * Calculate NKPT - number of kernel page tables. We have to
670 * accomodoate prealloction of the vm_page_array, dump bitmap,
671 * MSGBUF_SIZE, and other stuff. Be generous.
673 * Maxmem is in pages.
675 * ndmpdp is the number of 1GB pages we wish to map.
677 ndmpdp
= (ptoa(Maxmem
) + NBPDP
- 1) >> PDPSHIFT
;
678 if (ndmpdp
< 4) /* Minimum 4GB of dirmap */
680 KKASSERT(ndmpdp
<= NKPDPE
* NPDEPG
);
683 * Starting at the beginning of kvm (not KERNBASE).
685 nkpt_phys
= (Maxmem
* sizeof(struct vm_page
) + NBPDR
- 1) / NBPDR
;
686 nkpt_phys
+= (Maxmem
* sizeof(struct pv_entry
) + NBPDR
- 1) / NBPDR
;
687 nkpt_phys
+= ((nkpt
+ nkpt
+ 1 + NKPML4E
+ NKPDPE
+ NDMPML4E
+
688 ndmpdp
) + 511) / 512;
692 * Starting at KERNBASE - map 2G worth of page table pages.
693 * KERNBASE is offset -2G from the end of kvm.
695 nkpt_base
= (NPDPEPG
- KPDPI
) * NPTEPG
; /* typically 2 x 512 */
700 KPTbase
= allocpages(firstaddr
, nkpt_base
);
701 KPTphys
= allocpages(firstaddr
, nkpt_phys
);
702 KPML4phys
= allocpages(firstaddr
, 1);
703 KPDPphys
= allocpages(firstaddr
, NKPML4E
);
704 KPDphys
= allocpages(firstaddr
, NKPDPE
);
707 * Calculate the page directory base for KERNBASE,
708 * that is where we start populating the page table pages.
709 * Basically this is the end - 2.
711 KPDbase
= KPDphys
+ ((NKPDPE
- (NPDPEPG
- KPDPI
)) << PAGE_SHIFT
);
713 DMPDPphys
= allocpages(firstaddr
, NDMPML4E
);
714 if ((amd_feature
& AMDID_PAGE1GB
) == 0)
715 DMPDphys
= allocpages(firstaddr
, ndmpdp
);
716 dmaplimit
= (vm_paddr_t
)ndmpdp
<< PDPSHIFT
;
719 * Fill in the underlying page table pages for the area around
720 * KERNBASE. This remaps low physical memory to KERNBASE.
722 * Read-only from zero to physfree
723 * XXX not fully used, underneath 2M pages
725 for (i
= 0; (i
<< PAGE_SHIFT
) < *firstaddr
; i
++) {
726 ((pt_entry_t
*)KPTbase
)[i
] = i
<< PAGE_SHIFT
;
727 ((pt_entry_t
*)KPTbase
)[i
] |=
728 pmap_bits_default
[PG_RW_IDX
] |
729 pmap_bits_default
[PG_V_IDX
] |
730 pmap_bits_default
[PG_G_IDX
];
734 * Now map the initial kernel page tables. One block of page
735 * tables is placed at the beginning of kernel virtual memory,
736 * and another block is placed at KERNBASE to map the kernel binary,
737 * data, bss, and initial pre-allocations.
739 for (i
= 0; i
< nkpt_base
; i
++) {
740 ((pd_entry_t
*)KPDbase
)[i
] = KPTbase
+ (i
<< PAGE_SHIFT
);
741 ((pd_entry_t
*)KPDbase
)[i
] |=
742 pmap_bits_default
[PG_RW_IDX
] |
743 pmap_bits_default
[PG_V_IDX
];
745 for (i
= 0; i
< nkpt_phys
; i
++) {
746 ((pd_entry_t
*)KPDphys
)[i
] = KPTphys
+ (i
<< PAGE_SHIFT
);
747 ((pd_entry_t
*)KPDphys
)[i
] |=
748 pmap_bits_default
[PG_RW_IDX
] |
749 pmap_bits_default
[PG_V_IDX
];
753 * Map from zero to end of allocations using 2M pages as an
754 * optimization. This will bypass some of the KPTBase pages
755 * above in the KERNBASE area.
757 for (i
= 0; (i
<< PDRSHIFT
) < *firstaddr
; i
++) {
758 ((pd_entry_t
*)KPDbase
)[i
] = i
<< PDRSHIFT
;
759 ((pd_entry_t
*)KPDbase
)[i
] |=
760 pmap_bits_default
[PG_RW_IDX
] |
761 pmap_bits_default
[PG_V_IDX
] |
762 pmap_bits_default
[PG_PS_IDX
] |
763 pmap_bits_default
[PG_G_IDX
];
767 * And connect up the PD to the PDP. The kernel pmap is expected
768 * to pre-populate all of its PDs. See NKPDPE in vmparam.h.
770 for (i
= 0; i
< NKPDPE
; i
++) {
771 ((pdp_entry_t
*)KPDPphys
)[NPDPEPG
- NKPDPE
+ i
] =
772 KPDphys
+ (i
<< PAGE_SHIFT
);
773 ((pdp_entry_t
*)KPDPphys
)[NPDPEPG
- NKPDPE
+ i
] |=
774 pmap_bits_default
[PG_RW_IDX
] |
775 pmap_bits_default
[PG_V_IDX
] |
776 pmap_bits_default
[PG_U_IDX
];
780 * Now set up the direct map space using either 2MB or 1GB pages
781 * Preset PG_M and PG_A because demotion expects it.
783 * When filling in entries in the PD pages make sure any excess
784 * entries are set to zero as we allocated enough PD pages
786 if ((amd_feature
& AMDID_PAGE1GB
) == 0) {
787 for (i
= 0; i
< NPDEPG
* ndmpdp
; i
++) {
788 ((pd_entry_t
*)DMPDphys
)[i
] = i
<< PDRSHIFT
;
789 ((pd_entry_t
*)DMPDphys
)[i
] |=
790 pmap_bits_default
[PG_RW_IDX
] |
791 pmap_bits_default
[PG_V_IDX
] |
792 pmap_bits_default
[PG_PS_IDX
] |
793 pmap_bits_default
[PG_G_IDX
] |
794 pmap_bits_default
[PG_M_IDX
] |
795 pmap_bits_default
[PG_A_IDX
];
799 * And the direct map space's PDP
801 for (i
= 0; i
< ndmpdp
; i
++) {
802 ((pdp_entry_t
*)DMPDPphys
)[i
] = DMPDphys
+
804 ((pdp_entry_t
*)DMPDPphys
)[i
] |=
805 pmap_bits_default
[PG_RW_IDX
] |
806 pmap_bits_default
[PG_V_IDX
] |
807 pmap_bits_default
[PG_U_IDX
];
810 for (i
= 0; i
< ndmpdp
; i
++) {
811 ((pdp_entry_t
*)DMPDPphys
)[i
] =
812 (vm_paddr_t
)i
<< PDPSHIFT
;
813 ((pdp_entry_t
*)DMPDPphys
)[i
] |=
814 pmap_bits_default
[PG_RW_IDX
] |
815 pmap_bits_default
[PG_V_IDX
] |
816 pmap_bits_default
[PG_PS_IDX
] |
817 pmap_bits_default
[PG_G_IDX
] |
818 pmap_bits_default
[PG_M_IDX
] |
819 pmap_bits_default
[PG_A_IDX
];
823 /* And recursively map PML4 to itself in order to get PTmap */
824 ((pdp_entry_t
*)KPML4phys
)[PML4PML4I
] = KPML4phys
;
825 ((pdp_entry_t
*)KPML4phys
)[PML4PML4I
] |=
826 pmap_bits_default
[PG_RW_IDX
] |
827 pmap_bits_default
[PG_V_IDX
] |
828 pmap_bits_default
[PG_U_IDX
];
831 * Connect the Direct Map slots up to the PML4
833 for (j
= 0; j
< NDMPML4E
; ++j
) {
834 ((pdp_entry_t
*)KPML4phys
)[DMPML4I
+ j
] =
835 (DMPDPphys
+ ((vm_paddr_t
)j
<< PML4SHIFT
)) |
836 pmap_bits_default
[PG_RW_IDX
] |
837 pmap_bits_default
[PG_V_IDX
] |
838 pmap_bits_default
[PG_U_IDX
];
842 * Connect the KVA slot up to the PML4
844 ((pdp_entry_t
*)KPML4phys
)[KPML4I
] = KPDPphys
;
845 ((pdp_entry_t
*)KPML4phys
)[KPML4I
] |=
846 pmap_bits_default
[PG_RW_IDX
] |
847 pmap_bits_default
[PG_V_IDX
] |
848 pmap_bits_default
[PG_U_IDX
];
852 * Bootstrap the system enough to run with virtual memory.
854 * On the i386 this is called after mapping has already been enabled
855 * and just syncs the pmap module with what has already been done.
856 * [We can't call it easily with mapping off since the kernel is not
857 * mapped with PA == VA, hence we would have to relocate every address
858 * from the linked base (virtual) address "KERNBASE" to the actual
859 * (physical) address starting relative to 0]
862 pmap_bootstrap(vm_paddr_t
*firstaddr
)
867 KvaStart
= VM_MIN_KERNEL_ADDRESS
;
868 KvaEnd
= VM_MAX_KERNEL_ADDRESS
;
869 KvaSize
= KvaEnd
- KvaStart
;
871 avail_start
= *firstaddr
;
874 * Create an initial set of page tables to run the kernel in.
876 create_pagetables(firstaddr
);
878 virtual2_start
= KvaStart
;
879 virtual2_end
= PTOV_OFFSET
;
881 virtual_start
= (vm_offset_t
) PTOV_OFFSET
+ *firstaddr
;
882 virtual_start
= pmap_kmem_choose(virtual_start
);
884 virtual_end
= VM_MAX_KERNEL_ADDRESS
;
886 /* XXX do %cr0 as well */
887 load_cr4(rcr4() | CR4_PGE
| CR4_PSE
);
891 * Initialize protection array.
893 i386_protection_init();
896 * The kernel's pmap is statically allocated so we don't have to use
897 * pmap_create, which is unlikely to work correctly at this part of
898 * the boot sequence (XXX and which no longer exists).
900 kernel_pmap
.pm_pml4
= (pdp_entry_t
*) (PTOV_OFFSET
+ KPML4phys
);
901 kernel_pmap
.pm_count
= 1;
902 CPUMASK_ASSALLONES(kernel_pmap
.pm_active
);
903 RB_INIT(&kernel_pmap
.pm_pvroot
);
904 spin_init(&kernel_pmap
.pm_spin
, "pmapbootstrap");
905 lwkt_token_init(&kernel_pmap
.pm_token
, "kpmap_tok");
908 * Reserve some special page table entries/VA space for temporary
911 #define SYSMAP(c, p, v, n) \
912 v = (c)va; va += ((n)*PAGE_SIZE); p = pte; pte += (n);
918 * CMAP1/CMAP2 are used for zeroing and copying pages.
920 SYSMAP(caddr_t
, CMAP1
, CADDR1
, 1)
925 SYSMAP(caddr_t
, pt_crashdumpmap
, crashdumpmap
, MAXDUMPPGS
);
928 * ptvmmap is used for reading arbitrary physical pages via
931 SYSMAP(caddr_t
, ptmmap
, ptvmmap
, 1)
934 * msgbufp is used to map the system message buffer.
935 * XXX msgbufmap is not used.
937 SYSMAP(struct msgbuf
*, msgbufmap
, msgbufp
,
938 atop(round_page(MSGBUF_SIZE
)))
941 virtual_start
= pmap_kmem_choose(virtual_start
);
946 * PG_G is terribly broken on SMP because we IPI invltlb's in some
947 * cases rather then invl1pg. Actually, I don't even know why it
948 * works under UP because self-referential page table mappings
953 * Initialize the 4MB page size flag
957 * The 4MB page version of the initial
958 * kernel page mapping.
962 #if !defined(DISABLE_PSE)
963 if (cpu_feature
& CPUID_PSE
) {
966 * Note that we have enabled PSE mode
968 // pseflag = kernel_pmap.pmap_bits[PG_PS_IDX];
969 ptditmp
= *(PTmap
+ x86_64_btop(KERNBASE
));
970 ptditmp
&= ~(NBPDR
- 1);
971 ptditmp
|= pmap_bits_default
[PG_V_IDX
] |
972 pmap_bits_default
[PG_RW_IDX
] |
973 pmap_bits_default
[PG_PS_IDX
] |
974 pmap_bits_default
[PG_U_IDX
];
981 /* Initialize the PAT MSR */
983 pmap_pinit_defaults(&kernel_pmap
);
985 TUNABLE_INT_FETCH("machdep.pmap_fast_kernel_cpusync",
986 &pmap_fast_kernel_cpusync
);
1000 * Default values mapping PATi,PCD,PWT bits at system reset.
1001 * The default values effectively ignore the PATi bit by
1002 * repeating the encodings for 0-3 in 4-7, and map the PCD
1003 * and PWT bit combinations to the expected PAT types.
1005 pat_msr
= PAT_VALUE(0, PAT_WRITE_BACK
) | /* 000 */
1006 PAT_VALUE(1, PAT_WRITE_THROUGH
) | /* 001 */
1007 PAT_VALUE(2, PAT_UNCACHED
) | /* 010 */
1008 PAT_VALUE(3, PAT_UNCACHEABLE
) | /* 011 */
1009 PAT_VALUE(4, PAT_WRITE_BACK
) | /* 100 */
1010 PAT_VALUE(5, PAT_WRITE_THROUGH
) | /* 101 */
1011 PAT_VALUE(6, PAT_UNCACHED
) | /* 110 */
1012 PAT_VALUE(7, PAT_UNCACHEABLE
); /* 111 */
1013 pat_pte_index
[PAT_WRITE_BACK
] = 0;
1014 pat_pte_index
[PAT_WRITE_THROUGH
]= 0 | X86_PG_NC_PWT
;
1015 pat_pte_index
[PAT_UNCACHED
] = X86_PG_NC_PCD
;
1016 pat_pte_index
[PAT_UNCACHEABLE
] = X86_PG_NC_PCD
| X86_PG_NC_PWT
;
1017 pat_pte_index
[PAT_WRITE_PROTECTED
] = pat_pte_index
[PAT_UNCACHEABLE
];
1018 pat_pte_index
[PAT_WRITE_COMBINING
] = pat_pte_index
[PAT_UNCACHEABLE
];
1020 if (cpu_feature
& CPUID_PAT
) {
1022 * If we support the PAT then set-up entries for
1023 * WRITE_PROTECTED and WRITE_COMBINING using bit patterns
1026 pat_msr
= (pat_msr
& ~PAT_MASK(4)) |
1027 PAT_VALUE(4, PAT_WRITE_PROTECTED
);
1028 pat_msr
= (pat_msr
& ~PAT_MASK(5)) |
1029 PAT_VALUE(5, PAT_WRITE_COMBINING
);
1030 pat_pte_index
[PAT_WRITE_PROTECTED
] = X86_PG_PTE_PAT
| 0;
1031 pat_pte_index
[PAT_WRITE_COMBINING
] = X86_PG_PTE_PAT
| X86_PG_NC_PWT
;
1034 * Then enable the PAT
1039 load_cr4(cr4
& ~CR4_PGE
);
1041 /* Disable caches (CD = 1, NW = 0). */
1043 load_cr0((cr0
& ~CR0_NW
) | CR0_CD
);
1045 /* Flushes caches and TLBs. */
1049 /* Update PAT and index table. */
1050 wrmsr(MSR_PAT
, pat_msr
);
1052 /* Flush caches and TLBs again. */
1056 /* Restore caches and PGE. */
1064 * Set 4mb pdir for mp startup
1069 if (cpu_feature
& CPUID_PSE
) {
1070 load_cr4(rcr4() | CR4_PSE
);
1071 if (pdir4mb
&& mycpu
->gd_cpuid
== 0) { /* only on BSP */
1078 * Initialize the pmap module.
1079 * Called by vm_init, to initialize any structures that the pmap
1080 * system needs to map virtual memory.
1081 * pmap_init has been enhanced to support in a fairly consistant
1082 * way, discontiguous physical memory.
1091 * Allocate memory for random pmap data structures. Includes the
1095 for (i
= 0; i
< vm_page_array_size
; i
++) {
1098 m
= &vm_page_array
[i
];
1099 TAILQ_INIT(&m
->md
.pv_list
);
1103 * init the pv free list
1105 initial_pvs
= vm_page_array_size
;
1106 if (initial_pvs
< MINPV
)
1107 initial_pvs
= MINPV
;
1108 pvzone
= &pvzone_store
;
1109 pvinit
= (void *)kmem_alloc(&kernel_map
,
1110 initial_pvs
* sizeof (struct pv_entry
),
1112 zbootinit(pvzone
, "PV ENTRY", sizeof (struct pv_entry
),
1113 pvinit
, initial_pvs
);
1116 * Now it is safe to enable pv_table recording.
1118 pmap_initialized
= TRUE
;
1122 * Initialize the address space (zone) for the pv_entries. Set a
1123 * high water mark so that the system can recover from excessive
1124 * numbers of pv entries.
1129 int shpgperproc
= PMAP_SHPGPERPROC
;
1132 TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc
);
1133 pv_entry_max
= shpgperproc
* maxproc
+ vm_page_array_size
;
1134 TUNABLE_INT_FETCH("vm.pmap.pv_entries", &pv_entry_max
);
1135 pv_entry_high_water
= 9 * (pv_entry_max
/ 10);
1138 * Subtract out pages already installed in the zone (hack)
1140 entry_max
= pv_entry_max
- vm_page_array_size
;
1144 zinitna(pvzone
, &pvzone_obj
, NULL
, 0, entry_max
, ZONE_INTERRUPT
);
1148 * Typically used to initialize a fictitious page by vm/device_pager.c
1151 pmap_page_init(struct vm_page
*m
)
1154 TAILQ_INIT(&m
->md
.pv_list
);
1157 /***************************************************
1158 * Low level helper routines.....
1159 ***************************************************/
1162 * this routine defines the region(s) of memory that should
1163 * not be tested for the modified bit.
1167 pmap_track_modified(vm_pindex_t pindex
)
1169 vm_offset_t va
= (vm_offset_t
)pindex
<< PAGE_SHIFT
;
1170 if ((va
< clean_sva
) || (va
>= clean_eva
))
1177 * Extract the physical page address associated with the map/VA pair.
1178 * The page must be wired for this to work reliably.
1180 * XXX for the moment we're using pv_find() instead of pv_get(), as
1181 * callers might be expecting non-blocking operation.
1184 pmap_extract(pmap_t pmap
, vm_offset_t va
)
1191 if (va
>= VM_MAX_USER_ADDRESS
) {
1193 * Kernel page directories might be direct-mapped and
1194 * there is typically no PV tracking of pte's
1198 pt
= pmap_pt(pmap
, va
);
1199 if (pt
&& (*pt
& pmap
->pmap_bits
[PG_V_IDX
])) {
1200 if (*pt
& pmap
->pmap_bits
[PG_PS_IDX
]) {
1201 rtval
= *pt
& PG_PS_FRAME
;
1202 rtval
|= va
& PDRMASK
;
1204 ptep
= pmap_pt_to_pte(*pt
, va
);
1205 if (*pt
& pmap
->pmap_bits
[PG_V_IDX
]) {
1206 rtval
= *ptep
& PG_FRAME
;
1207 rtval
|= va
& PAGE_MASK
;
1213 * User pages currently do not direct-map the page directory
1214 * and some pages might not used managed PVs. But all PT's
1217 pt_pv
= pv_find(pmap
, pmap_pt_pindex(va
));
1219 ptep
= pv_pte_lookup(pt_pv
, pmap_pte_index(va
));
1220 if (*ptep
& pmap
->pmap_bits
[PG_V_IDX
]) {
1221 rtval
= *ptep
& PG_FRAME
;
1222 rtval
|= va
& PAGE_MASK
;
1231 * Similar to extract but checks protections, SMP-friendly short-cut for
1232 * vm_fault_page[_quick](). Can return NULL to cause the caller to
1233 * fall-through to the real fault code.
1235 * The returned page, if not NULL, is held (and not busied).
1238 pmap_fault_page_quick(pmap_t pmap
, vm_offset_t va
, vm_prot_t prot
)
1240 if (pmap
&& va
< VM_MAX_USER_ADDRESS
) {
1248 req
= pmap
->pmap_bits
[PG_V_IDX
] |
1249 pmap
->pmap_bits
[PG_U_IDX
];
1250 if (prot
& VM_PROT_WRITE
)
1251 req
|= pmap
->pmap_bits
[PG_RW_IDX
];
1253 pt_pv
= pv_find(pmap
, pmap_pt_pindex(va
));
1256 ptep
= pv_pte_lookup(pt_pv
, pmap_pte_index(va
));
1257 if ((*ptep
& req
) != req
) {
1261 pte_pv
= pv_get_try(pmap
, pmap_pte_pindex(va
), &error
);
1262 if (pte_pv
&& error
== 0) {
1265 if (prot
& VM_PROT_WRITE
)
1268 } else if (pte_pv
) {
1282 * Extract the physical page address associated kernel virtual address.
1285 pmap_kextract(vm_offset_t va
)
1287 pd_entry_t pt
; /* pt entry in pd */
1290 if (va
>= DMAP_MIN_ADDRESS
&& va
< DMAP_MAX_ADDRESS
) {
1291 pa
= DMAP_TO_PHYS(va
);
1294 if (pt
& kernel_pmap
.pmap_bits
[PG_PS_IDX
]) {
1295 pa
= (pt
& PG_PS_FRAME
) | (va
& PDRMASK
);
1298 * Beware of a concurrent promotion that changes the
1299 * PDE at this point! For example, vtopte() must not
1300 * be used to access the PTE because it would use the
1301 * new PDE. It is, however, safe to use the old PDE
1302 * because the page table page is preserved by the
1305 pa
= *pmap_pt_to_pte(pt
, va
);
1306 pa
= (pa
& PG_FRAME
) | (va
& PAGE_MASK
);
1312 /***************************************************
1313 * Low level mapping routines.....
1314 ***************************************************/
1317 * Routine: pmap_kenter
1319 * Add a wired page to the KVA
1320 * NOTE! note that in order for the mapping to take effect -- you
1321 * should do an invltlb after doing the pmap_kenter().
1324 pmap_kenter(vm_offset_t va
, vm_paddr_t pa
)
1330 kernel_pmap
.pmap_bits
[PG_RW_IDX
] |
1331 kernel_pmap
.pmap_bits
[PG_V_IDX
];
1335 pmap_inval_smp(&kernel_pmap
, va
, 1, ptep
, npte
);
1339 pmap_inval_smp(&kernel_pmap
, va
, ptep
, npte
);
1346 * Similar to pmap_kenter(), except we only invalidate the mapping on the
1347 * current CPU. Returns 0 if the previous pte was 0, 1 if it wasn't
1348 * (caller can conditionalize calling smp_invltlb()).
1351 pmap_kenter_quick(vm_offset_t va
, vm_paddr_t pa
)
1358 kernel_pmap
.pmap_bits
[PG_RW_IDX
] |
1359 kernel_pmap
.pmap_bits
[PG_V_IDX
];
1369 cpu_invlpg((void *)va
);
1375 * Enter addresses into the kernel pmap but don't bother
1376 * doing any tlb invalidations. Caller will do a rollup
1377 * invalidation via pmap_rollup_inval().
1380 pmap_kenter_noinval(vm_offset_t va
, vm_paddr_t pa
)
1387 kernel_pmap
.pmap_bits
[PG_RW_IDX
] |
1388 kernel_pmap
.pmap_bits
[PG_V_IDX
];
1398 cpu_invlpg((void *)va
);
1404 * remove a page from the kernel pagetables
1407 pmap_kremove(vm_offset_t va
)
1412 pmap_inval_smp(&kernel_pmap
, va
, 1, ptep
, 0);
1416 pmap_kremove_quick(vm_offset_t va
)
1421 (void)pte_load_clear(ptep
);
1422 cpu_invlpg((void *)va
);
1426 * Remove addresses from the kernel pmap but don't bother
1427 * doing any tlb invalidations. Caller will do a rollup
1428 * invalidation via pmap_rollup_inval().
1431 pmap_kremove_noinval(vm_offset_t va
)
1436 (void)pte_load_clear(ptep
);
1440 * XXX these need to be recoded. They are not used in any critical path.
1443 pmap_kmodify_rw(vm_offset_t va
)
1445 atomic_set_long(vtopte(va
), kernel_pmap
.pmap_bits
[PG_RW_IDX
]);
1446 cpu_invlpg((void *)va
);
1451 pmap_kmodify_nc(vm_offset_t va)
1453 atomic_set_long(vtopte(va), PG_N);
1454 cpu_invlpg((void *)va);
1459 * Used to map a range of physical addresses into kernel virtual
1460 * address space during the low level boot, typically to map the
1461 * dump bitmap, message buffer, and vm_page_array.
1463 * These mappings are typically made at some pointer after the end of the
1466 * We could return PHYS_TO_DMAP(start) here and not allocate any
1467 * via (*virtp), but then kmem from userland and kernel dumps won't
1468 * have access to the related pointers.
1471 pmap_map(vm_offset_t
*virtp
, vm_paddr_t start
, vm_paddr_t end
, int prot
)
1474 vm_offset_t va_start
;
1476 /*return PHYS_TO_DMAP(start);*/
1481 while (start
< end
) {
1482 pmap_kenter_quick(va
, start
);
1490 #define PMAP_CLFLUSH_THRESHOLD (2 * 1024 * 1024)
1493 * Remove the specified set of pages from the data and instruction caches.
1495 * In contrast to pmap_invalidate_cache_range(), this function does not
1496 * rely on the CPU's self-snoop feature, because it is intended for use
1497 * when moving pages into a different cache domain.
1500 pmap_invalidate_cache_pages(vm_page_t
*pages
, int count
)
1502 vm_offset_t daddr
, eva
;
1505 if (count
>= PMAP_CLFLUSH_THRESHOLD
/ PAGE_SIZE
||
1506 (cpu_feature
& CPUID_CLFSH
) == 0)
1510 for (i
= 0; i
< count
; i
++) {
1511 daddr
= PHYS_TO_DMAP(VM_PAGE_TO_PHYS(pages
[i
]));
1512 eva
= daddr
+ PAGE_SIZE
;
1513 for (; daddr
< eva
; daddr
+= cpu_clflush_line_size
)
1521 pmap_invalidate_cache_range(vm_offset_t sva
, vm_offset_t eva
)
1523 KASSERT((sva
& PAGE_MASK
) == 0,
1524 ("pmap_invalidate_cache_range: sva not page-aligned"));
1525 KASSERT((eva
& PAGE_MASK
) == 0,
1526 ("pmap_invalidate_cache_range: eva not page-aligned"));
1528 if (cpu_feature
& CPUID_SS
) {
1529 ; /* If "Self Snoop" is supported, do nothing. */
1531 /* Globally invalidate caches */
1532 cpu_wbinvd_on_all_cpus();
1537 * Invalidate the specified range of virtual memory on all cpus associated
1541 pmap_invalidate_range(pmap_t pmap
, vm_offset_t sva
, vm_offset_t eva
)
1543 pmap_inval_smp(pmap
, sva
, (eva
- sva
) >> PAGE_SHIFT
, NULL
, 0);
1547 * Add a list of wired pages to the kva. This routine is used for temporary
1548 * kernel mappings such as those found in buffer cache buffer. Page
1549 * modifications and accesses are not tracked or recorded.
1551 * NOTE! Old mappings are simply overwritten, and we cannot assume relaxed
1552 * semantics as previous mappings may have been zerod without any
1555 * The page *must* be wired.
1558 pmap_qenter(vm_offset_t beg_va
, vm_page_t
*m
, int count
)
1563 end_va
= beg_va
+ count
* PAGE_SIZE
;
1565 for (va
= beg_va
; va
< end_va
; va
+= PAGE_SIZE
) {
1569 *pte
= VM_PAGE_TO_PHYS(*m
) |
1570 kernel_pmap
.pmap_bits
[PG_RW_IDX
] |
1571 kernel_pmap
.pmap_bits
[PG_V_IDX
] |
1572 kernel_pmap
.pmap_cache_bits
[(*m
)->pat_mode
];
1576 pmap_invalidate_range(&kernel_pmap
, beg_va
, end_va
);
1580 * This routine jerks page mappings from the kernel -- it is meant only
1581 * for temporary mappings such as those found in buffer cache buffers.
1582 * No recording modified or access status occurs.
1584 * MPSAFE, INTERRUPT SAFE (cluster callback)
1587 pmap_qremove(vm_offset_t beg_va
, int count
)
1592 end_va
= beg_va
+ count
* PAGE_SIZE
;
1594 for (va
= beg_va
; va
< end_va
; va
+= PAGE_SIZE
) {
1598 (void)pte_load_clear(pte
);
1599 cpu_invlpg((void *)va
);
1601 pmap_invalidate_range(&kernel_pmap
, beg_va
, end_va
);
1605 * This routine removes temporary kernel mappings, only invalidating them
1606 * on the current cpu. It should only be used under carefully controlled
1610 pmap_qremove_quick(vm_offset_t beg_va
, int count
)
1615 end_va
= beg_va
+ count
* PAGE_SIZE
;
1617 for (va
= beg_va
; va
< end_va
; va
+= PAGE_SIZE
) {
1621 (void)pte_load_clear(pte
);
1622 cpu_invlpg((void *)va
);
1627 * This routine removes temporary kernel mappings *without* invalidating
1628 * the TLB. It can only be used on permanent kva reservations such as those
1629 * found in buffer cache buffers, under carefully controlled circumstances.
1631 * NOTE: Repopulating these KVAs requires unconditional invalidation.
1632 * (pmap_qenter() does unconditional invalidation).
1635 pmap_qremove_noinval(vm_offset_t beg_va
, int count
)
1640 end_va
= beg_va
+ count
* PAGE_SIZE
;
1642 for (va
= beg_va
; va
< end_va
; va
+= PAGE_SIZE
) {
1646 (void)pte_load_clear(pte
);
1651 * Create a new thread and optionally associate it with a (new) process.
1652 * NOTE! the new thread's cpu may not equal the current cpu.
1655 pmap_init_thread(thread_t td
)
1657 /* enforce pcb placement & alignment */
1658 td
->td_pcb
= (struct pcb
*)(td
->td_kstack
+ td
->td_kstack_size
) - 1;
1659 td
->td_pcb
= (struct pcb
*)((intptr_t)td
->td_pcb
& ~(intptr_t)0xF);
1660 td
->td_savefpu
= &td
->td_pcb
->pcb_save
;
1661 td
->td_sp
= (char *)td
->td_pcb
; /* no -16 */
1665 * This routine directly affects the fork perf for a process.
1668 pmap_init_proc(struct proc
*p
)
1673 pmap_pinit_defaults(struct pmap
*pmap
)
1675 bcopy(pmap_bits_default
, pmap
->pmap_bits
,
1676 sizeof(pmap_bits_default
));
1677 bcopy(protection_codes
, pmap
->protection_codes
,
1678 sizeof(protection_codes
));
1679 bcopy(pat_pte_index
, pmap
->pmap_cache_bits
,
1680 sizeof(pat_pte_index
));
1681 pmap
->pmap_cache_mask
= X86_PG_NC_PWT
| X86_PG_NC_PCD
| X86_PG_PTE_PAT
;
1682 pmap
->copyinstr
= std_copyinstr
;
1683 pmap
->copyin
= std_copyin
;
1684 pmap
->copyout
= std_copyout
;
1685 pmap
->fubyte
= std_fubyte
;
1686 pmap
->subyte
= std_subyte
;
1687 pmap
->fuword
= std_fuword
;
1688 pmap
->suword
= std_suword
;
1689 pmap
->suword32
= std_suword32
;
1692 * Initialize pmap0/vmspace0. This pmap is not added to pmap_list because
1693 * it, and IdlePTD, represents the template used to update all other pmaps.
1695 * On architectures where the kernel pmap is not integrated into the user
1696 * process pmap, this pmap represents the process pmap, not the kernel pmap.
1697 * kernel_pmap should be used to directly access the kernel_pmap.
1700 pmap_pinit0(struct pmap
*pmap
)
1702 pmap
->pm_pml4
= (pml4_entry_t
*)(PTOV_OFFSET
+ KPML4phys
);
1704 CPUMASK_ASSZERO(pmap
->pm_active
);
1705 pmap
->pm_pvhint
= NULL
;
1706 RB_INIT(&pmap
->pm_pvroot
);
1707 spin_init(&pmap
->pm_spin
, "pmapinit0");
1708 lwkt_token_init(&pmap
->pm_token
, "pmap_tok");
1709 bzero(&pmap
->pm_stats
, sizeof pmap
->pm_stats
);
1710 pmap_pinit_defaults(pmap
);
1714 * Initialize a preallocated and zeroed pmap structure,
1715 * such as one in a vmspace structure.
1718 pmap_pinit_simple(struct pmap
*pmap
)
1721 * Misc initialization
1724 CPUMASK_ASSZERO(pmap
->pm_active
);
1725 pmap
->pm_pvhint
= NULL
;
1726 pmap
->pm_flags
= PMAP_FLAG_SIMPLE
;
1728 pmap_pinit_defaults(pmap
);
1731 * Don't blow up locks/tokens on re-use (XXX fix/use drop code
1734 if (pmap
->pm_pmlpv
== NULL
) {
1735 RB_INIT(&pmap
->pm_pvroot
);
1736 bzero(&pmap
->pm_stats
, sizeof pmap
->pm_stats
);
1737 spin_init(&pmap
->pm_spin
, "pmapinitsimple");
1738 lwkt_token_init(&pmap
->pm_token
, "pmap_tok");
1743 pmap_pinit(struct pmap
*pmap
)
1748 if (pmap
->pm_pmlpv
) {
1749 if (pmap
->pmap_bits
[TYPE_IDX
] != REGULAR_PMAP
) {
1754 pmap_pinit_simple(pmap
);
1755 pmap
->pm_flags
&= ~PMAP_FLAG_SIMPLE
;
1758 * No need to allocate page table space yet but we do need a valid
1759 * page directory table.
1761 if (pmap
->pm_pml4
== NULL
) {
1763 (pml4_entry_t
*)kmem_alloc_pageable(&kernel_map
,
1769 * Allocate the page directory page, which wires it even though
1770 * it isn't being entered into some higher level page table (it
1771 * being the highest level). If one is already cached we don't
1772 * have to do anything.
1774 if ((pv
= pmap
->pm_pmlpv
) == NULL
) {
1775 pv
= pmap_allocpte(pmap
, pmap_pml4_pindex(), NULL
);
1776 pmap
->pm_pmlpv
= pv
;
1777 pmap_kenter((vm_offset_t
)pmap
->pm_pml4
,
1778 VM_PAGE_TO_PHYS(pv
->pv_m
));
1782 * Install DMAP and KMAP.
1784 for (j
= 0; j
< NDMPML4E
; ++j
) {
1785 pmap
->pm_pml4
[DMPML4I
+ j
] =
1786 (DMPDPphys
+ ((vm_paddr_t
)j
<< PML4SHIFT
)) |
1787 pmap
->pmap_bits
[PG_RW_IDX
] |
1788 pmap
->pmap_bits
[PG_V_IDX
] |
1789 pmap
->pmap_bits
[PG_U_IDX
];
1791 pmap
->pm_pml4
[KPML4I
] = KPDPphys
|
1792 pmap
->pmap_bits
[PG_RW_IDX
] |
1793 pmap
->pmap_bits
[PG_V_IDX
] |
1794 pmap
->pmap_bits
[PG_U_IDX
];
1797 * install self-referential address mapping entry
1799 pmap
->pm_pml4
[PML4PML4I
] = VM_PAGE_TO_PHYS(pv
->pv_m
) |
1800 pmap
->pmap_bits
[PG_V_IDX
] |
1801 pmap
->pmap_bits
[PG_RW_IDX
] |
1802 pmap
->pmap_bits
[PG_A_IDX
] |
1803 pmap
->pmap_bits
[PG_M_IDX
];
1805 KKASSERT(pv
->pv_m
->flags
& PG_MAPPED
);
1806 KKASSERT(pv
->pv_m
->flags
& PG_WRITEABLE
);
1808 KKASSERT(pmap
->pm_pml4
[255] == 0);
1809 KKASSERT(RB_ROOT(&pmap
->pm_pvroot
) == pv
);
1810 KKASSERT(pv
->pv_entry
.rbe_left
== NULL
);
1811 KKASSERT(pv
->pv_entry
.rbe_right
== NULL
);
1815 * Clean up a pmap structure so it can be physically freed. This routine
1816 * is called by the vmspace dtor function. A great deal of pmap data is
1817 * left passively mapped to improve vmspace management so we have a bit
1818 * of cleanup work to do here.
1821 pmap_puninit(pmap_t pmap
)
1826 KKASSERT(CPUMASK_TESTZERO(pmap
->pm_active
));
1827 if ((pv
= pmap
->pm_pmlpv
) != NULL
) {
1828 if (pv_hold_try(pv
) == 0)
1830 KKASSERT(pv
== pmap
->pm_pmlpv
);
1831 p
= pmap_remove_pv_page(pv
);
1833 pmap_kremove((vm_offset_t
)pmap
->pm_pml4
);
1834 vm_page_busy_wait(p
, FALSE
, "pgpun");
1835 KKASSERT(p
->flags
& (PG_FICTITIOUS
|PG_UNMANAGED
));
1836 vm_page_unwire(p
, 0);
1837 vm_page_flag_clear(p
, PG_MAPPED
| PG_WRITEABLE
);
1840 * XXX eventually clean out PML4 static entries and
1841 * use vm_page_free_zero()
1844 pmap
->pm_pmlpv
= NULL
;
1846 if (pmap
->pm_pml4
) {
1847 KKASSERT(pmap
->pm_pml4
!= (void *)(PTOV_OFFSET
+ KPML4phys
));
1848 kmem_free(&kernel_map
, (vm_offset_t
)pmap
->pm_pml4
, PAGE_SIZE
);
1849 pmap
->pm_pml4
= NULL
;
1851 KKASSERT(pmap
->pm_stats
.resident_count
== 0);
1852 KKASSERT(pmap
->pm_stats
.wired_count
== 0);
1856 * Wire in kernel global address entries. To avoid a race condition
1857 * between pmap initialization and pmap_growkernel, this procedure
1858 * adds the pmap to the master list (which growkernel scans to update),
1859 * then copies the template.
1862 pmap_pinit2(struct pmap
*pmap
)
1864 spin_lock(&pmap_spin
);
1865 TAILQ_INSERT_TAIL(&pmap_list
, pmap
, pm_pmnode
);
1866 spin_unlock(&pmap_spin
);
1870 * This routine is called when various levels in the page table need to
1871 * be populated. This routine cannot fail.
1873 * This function returns two locked pv_entry's, one representing the
1874 * requested pv and one representing the requested pv's parent pv. If
1875 * the pv did not previously exist it will be mapped into its parent
1876 * and wired, otherwise no additional wire count will be added.
1880 pmap_allocpte(pmap_t pmap
, vm_pindex_t ptepindex
, pv_entry_t
*pvpp
)
1885 vm_pindex_t pt_pindex
;
1891 * If the pv already exists and we aren't being asked for the
1892 * parent page table page we can just return it. A locked+held pv
1893 * is returned. The pv will also have a second hold related to the
1894 * pmap association that we don't have to worry about.
1897 pv
= pv_alloc(pmap
, ptepindex
, &isnew
);
1898 if (isnew
== 0 && pvpp
== NULL
)
1902 * Special case terminal PVs. These are not page table pages so
1903 * no vm_page is allocated (the caller supplied the vm_page). If
1904 * pvpp is non-NULL we are being asked to also removed the pt_pv
1907 * Note that pt_pv's are only returned for user VAs. We assert that
1908 * a pt_pv is not being requested for kernel VAs.
1910 if (ptepindex
< pmap_pt_pindex(0)) {
1911 if (ptepindex
>= NUPTE_USER
)
1912 KKASSERT(pvpp
== NULL
);
1914 KKASSERT(pvpp
!= NULL
);
1916 pt_pindex
= NUPTE_TOTAL
+ (ptepindex
>> NPTEPGSHIFT
);
1917 pvp
= pmap_allocpte(pmap
, pt_pindex
, NULL
);
1919 vm_page_wire_quick(pvp
->pv_m
);
1928 * Non-terminal PVs allocate a VM page to represent the page table,
1929 * so we have to resolve pvp and calculate ptepindex for the pvp
1930 * and then for the page table entry index in the pvp for
1933 if (ptepindex
< pmap_pd_pindex(0)) {
1935 * pv is PT, pvp is PD
1937 ptepindex
= (ptepindex
- pmap_pt_pindex(0)) >> NPDEPGSHIFT
;
1938 ptepindex
+= NUPTE_TOTAL
+ NUPT_TOTAL
;
1939 pvp
= pmap_allocpte(pmap
, ptepindex
, NULL
);
1946 ptepindex
= pv
->pv_pindex
- pmap_pt_pindex(0);
1947 ptepindex
&= ((1ul << NPDEPGSHIFT
) - 1);
1949 } else if (ptepindex
< pmap_pdp_pindex(0)) {
1951 * pv is PD, pvp is PDP
1953 * SIMPLE PMAP NOTE: Simple pmaps do not allocate above
1956 ptepindex
= (ptepindex
- pmap_pd_pindex(0)) >> NPDPEPGSHIFT
;
1957 ptepindex
+= NUPTE_TOTAL
+ NUPT_TOTAL
+ NUPD_TOTAL
;
1959 if (pmap
->pm_flags
& PMAP_FLAG_SIMPLE
) {
1960 KKASSERT(pvpp
== NULL
);
1963 pvp
= pmap_allocpte(pmap
, ptepindex
, NULL
);
1971 ptepindex
= pv
->pv_pindex
- pmap_pd_pindex(0);
1972 ptepindex
&= ((1ul << NPDPEPGSHIFT
) - 1);
1973 } else if (ptepindex
< pmap_pml4_pindex()) {
1975 * pv is PDP, pvp is the root pml4 table
1977 pvp
= pmap_allocpte(pmap
, pmap_pml4_pindex(), NULL
);
1984 ptepindex
= pv
->pv_pindex
- pmap_pdp_pindex(0);
1985 ptepindex
&= ((1ul << NPML4EPGSHIFT
) - 1);
1988 * pv represents the top-level PML4, there is no parent.
1996 * This code is only reached if isnew is TRUE and this is not a
1997 * terminal PV. We need to allocate a vm_page for the page table
1998 * at this level and enter it into the parent page table.
2000 * page table pages are marked PG_WRITEABLE and PG_MAPPED.
2003 m
= vm_page_alloc(NULL
, pv
->pv_pindex
,
2004 VM_ALLOC_NORMAL
| VM_ALLOC_SYSTEM
|
2005 VM_ALLOC_INTERRUPT
);
2010 vm_page_spin_lock(m
);
2011 pmap_page_stats_adding(m
);
2012 TAILQ_INSERT_TAIL(&m
->md
.pv_list
, pv
, pv_list
);
2014 vm_page_flag_set(m
, PG_MAPPED
| PG_WRITEABLE
);
2015 vm_page_spin_unlock(m
);
2016 vm_page_unmanage(m
); /* m must be spinunlocked */
2018 pmap_zero_page(VM_PAGE_TO_PHYS(m
));
2019 m
->valid
= VM_PAGE_BITS_ALL
;
2020 vm_page_wire(m
); /* wire for mapping in parent */
2023 * Wire the page into pvp, bump the wire-count for pvp's page table
2024 * page. Bump the resident_count for the pmap. There is no pvp
2025 * for the top level, address the pm_pml4[] array directly.
2027 * If the caller wants the parent we return it, otherwise
2028 * we just put it away.
2030 * No interlock is needed for pte 0 -> non-zero.
2032 * In the situation where *ptep is valid we might have an unmanaged
2033 * page table page shared from another page table which we need to
2034 * unshare before installing our private page table page.
2037 ptep
= pv_pte_lookup(pvp
, ptepindex
);
2038 if (*ptep
& pmap
->pmap_bits
[PG_V_IDX
]) {
2042 panic("pmap_allocpte: unexpected pte %p/%d",
2043 pvp
, (int)ptepindex
);
2045 pte
= pmap_inval_smp(pmap
, (vm_offset_t
)-1, 1, ptep
, 0);
2046 if (vm_page_unwire_quick(
2047 PHYS_TO_VM_PAGE(pte
& PG_FRAME
))) {
2048 panic("pmap_allocpte: shared pgtable "
2049 "pg bad wirecount");
2051 atomic_add_long(&pmap
->pm_stats
.resident_count
, -1);
2053 vm_page_wire_quick(pvp
->pv_m
);
2055 *ptep
= VM_PAGE_TO_PHYS(m
) |
2056 (pmap
->pmap_bits
[PG_U_IDX
] |
2057 pmap
->pmap_bits
[PG_RW_IDX
] |
2058 pmap
->pmap_bits
[PG_V_IDX
] |
2059 pmap
->pmap_bits
[PG_A_IDX
] |
2060 pmap
->pmap_bits
[PG_M_IDX
]);
2072 * This version of pmap_allocpte() checks for possible segment optimizations
2073 * that would allow page-table sharing. It can be called for terminal
2074 * page or page table page ptepindex's.
2076 * The function is called with page table page ptepindex's for fictitious
2077 * and unmanaged terminal pages. That is, we don't want to allocate a
2078 * terminal pv, we just want the pt_pv. pvpp is usually passed as NULL
2081 * This function can return a pv and *pvpp associated with the passed in pmap
2082 * OR a pv and *pvpp associated with the shared pmap. In the latter case
2083 * an unmanaged page table page will be entered into the pass in pmap.
2087 pmap_allocpte_seg(pmap_t pmap
, vm_pindex_t ptepindex
, pv_entry_t
*pvpp
,
2088 vm_map_entry_t entry
, vm_offset_t va
)
2094 pv_entry_t pte_pv
; /* in original or shared pmap */
2095 pv_entry_t pt_pv
; /* in original or shared pmap */
2096 pv_entry_t proc_pd_pv
; /* in original pmap */
2097 pv_entry_t proc_pt_pv
; /* in original pmap */
2098 pv_entry_t xpv
; /* PT in shared pmap */
2099 pd_entry_t
*pt
; /* PT entry in PD of original pmap */
2100 pd_entry_t opte
; /* contents of *pt */
2101 pd_entry_t npte
; /* contents of *pt */
2106 * Basic tests, require a non-NULL vm_map_entry, require proper
2107 * alignment and type for the vm_map_entry, require that the
2108 * underlying object already be allocated.
2110 * We allow almost any type of object to use this optimization.
2111 * The object itself does NOT have to be sized to a multiple of the
2112 * segment size, but the memory mapping does.
2114 * XXX don't handle devices currently, because VM_PAGE_TO_PHYS()
2115 * won't work as expected.
2117 if (entry
== NULL
||
2118 pmap_mmu_optimize
== 0 || /* not enabled */
2119 (pmap
->pm_flags
& PMAP_HVM
) || /* special pmap */
2120 ptepindex
>= pmap_pd_pindex(0) || /* not terminal or pt */
2121 entry
->inheritance
!= VM_INHERIT_SHARE
|| /* not shared */
2122 entry
->maptype
!= VM_MAPTYPE_NORMAL
|| /* weird map type */
2123 entry
->object
.vm_object
== NULL
|| /* needs VM object */
2124 entry
->object
.vm_object
->type
== OBJT_DEVICE
|| /* ick */
2125 entry
->object
.vm_object
->type
== OBJT_MGTDEVICE
|| /* ick */
2126 (entry
->offset
& SEG_MASK
) || /* must be aligned */
2127 (entry
->start
& SEG_MASK
)) {
2128 return(pmap_allocpte(pmap
, ptepindex
, pvpp
));
2132 * Make sure the full segment can be represented.
2134 b
= va
& ~(vm_offset_t
)SEG_MASK
;
2135 if (b
< entry
->start
|| b
+ SEG_SIZE
> entry
->end
)
2136 return(pmap_allocpte(pmap
, ptepindex
, pvpp
));
2139 * If the full segment can be represented dive the VM object's
2140 * shared pmap, allocating as required.
2142 object
= entry
->object
.vm_object
;
2144 if (entry
->protection
& VM_PROT_WRITE
)
2145 obpmapp
= &object
->md
.pmap_rw
;
2147 obpmapp
= &object
->md
.pmap_ro
;
2150 if (pmap_enter_debug
> 0) {
2152 kprintf("pmap_allocpte_seg: va=%jx prot %08x o=%p "
2154 va
, entry
->protection
, object
,
2156 kprintf("pmap_allocpte_seg: entry %p %jx-%jx\n",
2157 entry
, entry
->start
, entry
->end
);
2162 * We allocate what appears to be a normal pmap but because portions
2163 * of this pmap are shared with other unrelated pmaps we have to
2164 * set pm_active to point to all cpus.
2166 * XXX Currently using pmap_spin to interlock the update, can't use
2167 * vm_object_hold/drop because the token might already be held
2168 * shared OR exclusive and we don't know.
2170 while ((obpmap
= *obpmapp
) == NULL
) {
2171 obpmap
= kmalloc(sizeof(*obpmap
), M_OBJPMAP
, M_WAITOK
|M_ZERO
);
2172 pmap_pinit_simple(obpmap
);
2173 pmap_pinit2(obpmap
);
2174 spin_lock(&pmap_spin
);
2175 if (*obpmapp
!= NULL
) {
2179 spin_unlock(&pmap_spin
);
2180 pmap_release(obpmap
);
2181 pmap_puninit(obpmap
);
2182 kfree(obpmap
, M_OBJPMAP
);
2183 obpmap
= *obpmapp
; /* safety */
2185 obpmap
->pm_active
= smp_active_mask
;
2186 obpmap
->pm_flags
|= PMAP_SEGSHARED
;
2188 spin_unlock(&pmap_spin
);
2193 * Layering is: PTE, PT, PD, PDP, PML4. We have to return the
2194 * pte/pt using the shared pmap from the object but also adjust
2195 * the process pmap's page table page as a side effect.
2199 * Resolve the terminal PTE and PT in the shared pmap. This is what
2200 * we will return. This is true if ptepindex represents a terminal
2201 * page, otherwise pte_pv is actually the PT and pt_pv is actually
2205 pte_pv
= pmap_allocpte(obpmap
, ptepindex
, &pt_pv
);
2206 if (ptepindex
>= pmap_pt_pindex(0))
2212 * Resolve the PD in the process pmap so we can properly share the
2213 * page table page. Lock order is bottom-up (leaf first)!
2215 * NOTE: proc_pt_pv can be NULL.
2217 proc_pt_pv
= pv_get(pmap
, pmap_pt_pindex(b
));
2218 proc_pd_pv
= pmap_allocpte(pmap
, pmap_pd_pindex(b
), NULL
);
2220 if (pmap_enter_debug
> 0) {
2222 kprintf("proc_pt_pv %p (wc %d) pd_pv %p va=%jx\n",
2224 (proc_pt_pv
? proc_pt_pv
->pv_m
->wire_count
: -1),
2231 * xpv is the page table page pv from the shared object
2232 * (for convenience), from above.
2234 * Calculate the pte value for the PT to load into the process PD.
2235 * If we have to change it we must properly dispose of the previous
2238 pt
= pv_pte_lookup(proc_pd_pv
, pmap_pt_index(b
));
2239 npte
= VM_PAGE_TO_PHYS(xpv
->pv_m
) |
2240 (pmap
->pmap_bits
[PG_U_IDX
] |
2241 pmap
->pmap_bits
[PG_RW_IDX
] |
2242 pmap
->pmap_bits
[PG_V_IDX
] |
2243 pmap
->pmap_bits
[PG_A_IDX
] |
2244 pmap
->pmap_bits
[PG_M_IDX
]);
2247 * Dispose of previous page table page if it was local to the
2248 * process pmap. If the old pt is not empty we cannot dispose of it
2249 * until we clean it out. This case should not arise very often so
2250 * it is not optimized.
2253 pmap_inval_bulk_t bulk
;
2255 if (proc_pt_pv
->pv_m
->wire_count
!= 1) {
2261 va
& ~(vm_offset_t
)SEG_MASK
,
2262 (va
+ SEG_SIZE
) & ~(vm_offset_t
)SEG_MASK
);
2267 * The release call will indirectly clean out *pt
2269 pmap_inval_bulk_init(&bulk
, proc_pt_pv
->pv_pmap
);
2270 pmap_release_pv(proc_pt_pv
, proc_pd_pv
, &bulk
);
2271 pmap_inval_bulk_flush(&bulk
);
2274 pt
= pv_pte_lookup(proc_pd_pv
, pmap_pt_index(b
));
2278 * Handle remaining cases.
2282 vm_page_wire_quick(xpv
->pv_m
);
2283 vm_page_wire_quick(proc_pd_pv
->pv_m
);
2284 atomic_add_long(&pmap
->pm_stats
.resident_count
, 1);
2285 } else if (*pt
!= npte
) {
2286 opte
= pmap_inval_smp(pmap
, (vm_offset_t
)-1, 1, pt
, npte
);
2289 opte
= pte_load_clear(pt
);
2290 KKASSERT(opte
&& opte
!= npte
);
2294 vm_page_wire_quick(xpv
->pv_m
); /* pgtable pg that is npte */
2297 * Clean up opte, bump the wire_count for the process
2298 * PD page representing the new entry if it was
2301 * If the entry was not previously empty and we have
2302 * a PT in the proc pmap then opte must match that
2303 * pt. The proc pt must be retired (this is done
2304 * later on in this procedure).
2306 * NOTE: replacing valid pte, wire_count on proc_pd_pv
2309 KKASSERT(opte
& pmap
->pmap_bits
[PG_V_IDX
]);
2310 m
= PHYS_TO_VM_PAGE(opte
& PG_FRAME
);
2311 if (vm_page_unwire_quick(m
)) {
2312 panic("pmap_allocpte_seg: "
2313 "bad wire count %p",
2319 * The existing process page table was replaced and must be destroyed
2333 * Release any resources held by the given physical map.
2335 * Called when a pmap initialized by pmap_pinit is being released. Should
2336 * only be called if the map contains no valid mappings.
2338 * Caller must hold pmap->pm_token
2340 struct pmap_release_info
{
2345 static int pmap_release_callback(pv_entry_t pv
, void *data
);
2348 pmap_release(struct pmap
*pmap
)
2350 struct pmap_release_info info
;
2352 KASSERT(CPUMASK_TESTZERO(pmap
->pm_active
),
2353 ("pmap still active! %016jx",
2354 (uintmax_t)CPUMASK_LOWMASK(pmap
->pm_active
)));
2356 spin_lock(&pmap_spin
);
2357 TAILQ_REMOVE(&pmap_list
, pmap
, pm_pmnode
);
2358 spin_unlock(&pmap_spin
);
2361 * Pull pv's off the RB tree in order from low to high and release
2367 spin_lock(&pmap
->pm_spin
);
2368 RB_SCAN(pv_entry_rb_tree
, &pmap
->pm_pvroot
, NULL
,
2369 pmap_release_callback
, &info
);
2370 spin_unlock(&pmap
->pm_spin
);
2371 } while (info
.retry
);
2375 * One resident page (the pml4 page) should remain.
2376 * No wired pages should remain.
2378 KKASSERT(pmap
->pm_stats
.resident_count
==
2379 ((pmap
->pm_flags
& PMAP_FLAG_SIMPLE
) ? 0 : 1));
2381 KKASSERT(pmap
->pm_stats
.wired_count
== 0);
2385 pmap_release_callback(pv_entry_t pv
, void *data
)
2387 struct pmap_release_info
*info
= data
;
2388 pmap_t pmap
= info
->pmap
;
2391 if (pv_hold_try(pv
)) {
2392 spin_unlock(&pmap
->pm_spin
);
2394 spin_unlock(&pmap
->pm_spin
);
2397 if (pv
->pv_pmap
!= pmap
) {
2399 spin_lock(&pmap
->pm_spin
);
2403 r
= pmap_release_pv(pv
, NULL
, NULL
);
2404 spin_lock(&pmap
->pm_spin
);
2409 * Called with held (i.e. also locked) pv. This function will dispose of
2410 * the lock along with the pv.
2412 * If the caller already holds the locked parent page table for pv it
2413 * must pass it as pvp, allowing us to avoid a deadlock, else it can
2414 * pass NULL for pvp.
2417 pmap_release_pv(pv_entry_t pv
, pv_entry_t pvp
, pmap_inval_bulk_t
*bulk
)
2422 * The pmap is currently not spinlocked, pv is held+locked.
2423 * Remove the pv's page from its parent's page table. The
2424 * parent's page table page's wire_count will be decremented.
2426 * This will clean out the pte at any level of the page table.
2427 * If smp != 0 all cpus are affected.
2429 pmap_remove_pv_pte(pv
, pvp
, bulk
);
2432 * Terminal pvs are unhooked from their vm_pages. Because
2433 * terminal pages aren't page table pages they aren't wired
2434 * by us, so we have to be sure not to unwire them either.
2436 if (pv
->pv_pindex
< pmap_pt_pindex(0)) {
2437 pmap_remove_pv_page(pv
);
2442 * We leave the top-level page table page cached, wired, and
2443 * mapped in the pmap until the dtor function (pmap_puninit())
2446 * Since we are leaving the top-level pv intact we need
2447 * to break out of what would otherwise be an infinite loop.
2449 if (pv
->pv_pindex
== pmap_pml4_pindex()) {
2455 * For page table pages (other than the top-level page),
2456 * remove and free the vm_page. The representitive mapping
2457 * removed above by pmap_remove_pv_pte() did not undo the
2458 * last wire_count so we have to do that as well.
2460 p
= pmap_remove_pv_page(pv
);
2461 vm_page_busy_wait(p
, FALSE
, "pmaprl");
2462 if (p
->wire_count
!= 1) {
2463 kprintf("p->wire_count was %016lx %d\n",
2464 pv
->pv_pindex
, p
->wire_count
);
2466 KKASSERT(p
->wire_count
== 1);
2467 KKASSERT(p
->flags
& PG_UNMANAGED
);
2469 vm_page_unwire(p
, 0);
2470 KKASSERT(p
->wire_count
== 0);
2479 * This function will remove the pte associated with a pv from its parent.
2480 * Terminal pv's are supported. All cpus are affected if smp != 0.
2482 * The wire count will be dropped on the parent page table. The wire
2483 * count on the page being removed (pv->pv_m) from the parent page table
2484 * is NOT touched. Note that terminal pages will not have any additional
2485 * wire counts while page table pages will have at least one representing
2486 * the mapping, plus others representing sub-mappings.
2488 * NOTE: Cannot be called on kernel page table pages, only KVM terminal
2489 * pages and user page table and terminal pages.
2491 * The pv must be locked.
2493 * XXX must lock parent pv's if they exist to remove pte XXX
2497 pmap_remove_pv_pte(pv_entry_t pv
, pv_entry_t pvp
, pmap_inval_bulk_t
*bulk
)
2499 vm_pindex_t ptepindex
= pv
->pv_pindex
;
2500 pmap_t pmap
= pv
->pv_pmap
;
2506 if (ptepindex
== pmap_pml4_pindex()) {
2508 * We are the top level pml4 table, there is no parent.
2510 p
= pmap
->pm_pmlpv
->pv_m
;
2511 } else if (ptepindex
>= pmap_pdp_pindex(0)) {
2513 * Remove a PDP page from the pml4e. This can only occur
2514 * with user page tables. We do not have to lock the
2515 * pml4 PV so just ignore pvp.
2517 vm_pindex_t pml4_pindex
;
2518 vm_pindex_t pdp_index
;
2521 pdp_index
= ptepindex
- pmap_pdp_pindex(0);
2523 pml4_pindex
= pmap_pml4_pindex();
2524 pvp
= pv_get(pv
->pv_pmap
, pml4_pindex
);
2528 pdp
= &pmap
->pm_pml4
[pdp_index
& ((1ul << NPML4EPGSHIFT
) - 1)];
2529 KKASSERT((*pdp
& pmap
->pmap_bits
[PG_V_IDX
]) != 0);
2530 p
= PHYS_TO_VM_PAGE(*pdp
& PG_FRAME
);
2531 pmap_inval_bulk(bulk
, (vm_offset_t
)-1, pdp
, 0);
2532 } else if (ptepindex
>= pmap_pd_pindex(0)) {
2534 * Remove a PD page from the pdp
2536 * SIMPLE PMAP NOTE: Non-existant pvp's are ok in the case
2537 * of a simple pmap because it stops at
2540 vm_pindex_t pdp_pindex
;
2541 vm_pindex_t pd_index
;
2544 pd_index
= ptepindex
- pmap_pd_pindex(0);
2547 pdp_pindex
= NUPTE_TOTAL
+ NUPT_TOTAL
+ NUPD_TOTAL
+
2548 (pd_index
>> NPML4EPGSHIFT
);
2549 pvp
= pv_get(pv
->pv_pmap
, pdp_pindex
);
2554 pd
= pv_pte_lookup(pvp
, pd_index
&
2555 ((1ul << NPDPEPGSHIFT
) - 1));
2556 KKASSERT((*pd
& pmap
->pmap_bits
[PG_V_IDX
]) != 0);
2557 p
= PHYS_TO_VM_PAGE(*pd
& PG_FRAME
);
2558 pmap_inval_bulk(bulk
, (vm_offset_t
)-1, pd
, 0);
2560 KKASSERT(pmap
->pm_flags
& PMAP_FLAG_SIMPLE
);
2561 p
= pv
->pv_m
; /* degenerate test later */
2563 } else if (ptepindex
>= pmap_pt_pindex(0)) {
2565 * Remove a PT page from the pd
2567 vm_pindex_t pd_pindex
;
2568 vm_pindex_t pt_index
;
2571 pt_index
= ptepindex
- pmap_pt_pindex(0);
2574 pd_pindex
= NUPTE_TOTAL
+ NUPT_TOTAL
+
2575 (pt_index
>> NPDPEPGSHIFT
);
2576 pvp
= pv_get(pv
->pv_pmap
, pd_pindex
);
2580 pt
= pv_pte_lookup(pvp
, pt_index
& ((1ul << NPDPEPGSHIFT
) - 1));
2581 KKASSERT((*pt
& pmap
->pmap_bits
[PG_V_IDX
]) != 0);
2582 p
= PHYS_TO_VM_PAGE(*pt
& PG_FRAME
);
2583 pmap_inval_bulk(bulk
, (vm_offset_t
)-1, pt
, 0);
2586 * Remove a PTE from the PT page
2588 * NOTE: pv's must be locked bottom-up to avoid deadlocking.
2589 * pv is a pte_pv so we can safely lock pt_pv.
2591 * NOTE: FICTITIOUS pages may have multiple physical mappings
2592 * so PHYS_TO_VM_PAGE() will not necessarily work for
2595 vm_pindex_t pt_pindex
;
2600 pt_pindex
= ptepindex
>> NPTEPGSHIFT
;
2601 va
= (vm_offset_t
)ptepindex
<< PAGE_SHIFT
;
2603 if (ptepindex
>= NUPTE_USER
) {
2604 ptep
= vtopte(ptepindex
<< PAGE_SHIFT
);
2605 KKASSERT(pvp
== NULL
);
2608 pt_pindex
= NUPTE_TOTAL
+
2609 (ptepindex
>> NPDPEPGSHIFT
);
2610 pvp
= pv_get(pv
->pv_pmap
, pt_pindex
);
2614 ptep
= pv_pte_lookup(pvp
, ptepindex
&
2615 ((1ul << NPDPEPGSHIFT
) - 1));
2617 pte
= pmap_inval_bulk(bulk
, va
, ptep
, 0);
2618 if (bulk
== NULL
) /* XXX */
2619 cpu_invlpg((void *)va
); /* XXX */
2622 * Now update the vm_page_t
2624 if ((pte
& (pmap
->pmap_bits
[PG_MANAGED_IDX
] | pmap
->pmap_bits
[PG_V_IDX
])) !=
2625 (pmap
->pmap_bits
[PG_MANAGED_IDX
]|pmap
->pmap_bits
[PG_V_IDX
])) {
2626 kprintf("remove_pte badpte %016lx %016lx %d\n",
2628 pv
->pv_pindex
< pmap_pt_pindex(0));
2630 /* PHYS_TO_VM_PAGE() will not work for FICTITIOUS pages */
2631 /*KKASSERT((pte & (PG_MANAGED|PG_V)) == (PG_MANAGED|PG_V));*/
2632 if (pte
& pmap
->pmap_bits
[PG_DEVICE_IDX
])
2635 p
= PHYS_TO_VM_PAGE(pte
& PG_FRAME
);
2638 if (pte
& pmap
->pmap_bits
[PG_M_IDX
]) {
2639 if (pmap_track_modified(ptepindex
))
2642 if (pte
& pmap
->pmap_bits
[PG_A_IDX
]) {
2643 vm_page_flag_set(p
, PG_REFERENCED
);
2645 if (pte
& pmap
->pmap_bits
[PG_W_IDX
])
2646 atomic_add_long(&pmap
->pm_stats
.wired_count
, -1);
2647 if (pte
& pmap
->pmap_bits
[PG_G_IDX
])
2648 cpu_invlpg((void *)va
);
2652 * Unwire the parent page table page. The wire_count cannot go below
2653 * 1 here because the parent page table page is itself still mapped.
2655 * XXX remove the assertions later.
2657 KKASSERT(pv
->pv_m
== p
);
2658 if (pvp
&& vm_page_unwire_quick(pvp
->pv_m
))
2659 panic("pmap_remove_pv_pte: Insufficient wire_count");
2666 * Remove the vm_page association to a pv. The pv must be locked.
2670 pmap_remove_pv_page(pv_entry_t pv
)
2676 vm_page_spin_lock(m
);
2678 TAILQ_REMOVE(&m
->md
.pv_list
, pv
, pv_list
);
2679 pmap_page_stats_deleting(m
);
2682 atomic_add_int(&m->object->agg_pv_list_count, -1);
2684 if (TAILQ_EMPTY(&m
->md
.pv_list
))
2685 vm_page_flag_clear(m
, PG_MAPPED
| PG_WRITEABLE
);
2686 vm_page_spin_unlock(m
);
2691 * Grow the number of kernel page table entries, if needed.
2693 * This routine is always called to validate any address space
2694 * beyond KERNBASE (for kldloads). kernel_vm_end only governs the address
2695 * space below KERNBASE.
2698 pmap_growkernel(vm_offset_t kstart
, vm_offset_t kend
)
2701 vm_offset_t ptppaddr
;
2703 pd_entry_t
*pt
, newpt
;
2705 int update_kernel_vm_end
;
2708 * bootstrap kernel_vm_end on first real VM use
2710 if (kernel_vm_end
== 0) {
2711 kernel_vm_end
= VM_MIN_KERNEL_ADDRESS
;
2713 while ((*pmap_pt(&kernel_pmap
, kernel_vm_end
) & kernel_pmap
.pmap_bits
[PG_V_IDX
]) != 0) {
2714 kernel_vm_end
= (kernel_vm_end
+ PAGE_SIZE
* NPTEPG
) &
2715 ~(PAGE_SIZE
* NPTEPG
- 1);
2717 if (kernel_vm_end
- 1 >= kernel_map
.max_offset
) {
2718 kernel_vm_end
= kernel_map
.max_offset
;
2725 * Fill in the gaps. kernel_vm_end is only adjusted for ranges
2726 * below KERNBASE. Ranges above KERNBASE are kldloaded and we
2727 * do not want to force-fill 128G worth of page tables.
2729 if (kstart
< KERNBASE
) {
2730 if (kstart
> kernel_vm_end
)
2731 kstart
= kernel_vm_end
;
2732 KKASSERT(kend
<= KERNBASE
);
2733 update_kernel_vm_end
= 1;
2735 update_kernel_vm_end
= 0;
2738 kstart
= rounddown2(kstart
, PAGE_SIZE
* NPTEPG
);
2739 kend
= roundup2(kend
, PAGE_SIZE
* NPTEPG
);
2741 if (kend
- 1 >= kernel_map
.max_offset
)
2742 kend
= kernel_map
.max_offset
;
2744 while (kstart
< kend
) {
2745 pt
= pmap_pt(&kernel_pmap
, kstart
);
2747 /* We need a new PDP entry */
2748 nkpg
= vm_page_alloc(NULL
, nkpt
,
2751 VM_ALLOC_INTERRUPT
);
2753 panic("pmap_growkernel: no memory to grow "
2756 paddr
= VM_PAGE_TO_PHYS(nkpg
);
2757 pmap_zero_page(paddr
);
2758 newpd
= (pdp_entry_t
)
2760 kernel_pmap
.pmap_bits
[PG_V_IDX
] |
2761 kernel_pmap
.pmap_bits
[PG_RW_IDX
] |
2762 kernel_pmap
.pmap_bits
[PG_A_IDX
] |
2763 kernel_pmap
.pmap_bits
[PG_M_IDX
]);
2764 *pmap_pd(&kernel_pmap
, kstart
) = newpd
;
2766 continue; /* try again */
2768 if ((*pt
& kernel_pmap
.pmap_bits
[PG_V_IDX
]) != 0) {
2769 kstart
= (kstart
+ PAGE_SIZE
* NPTEPG
) &
2770 ~(PAGE_SIZE
* NPTEPG
- 1);
2771 if (kstart
- 1 >= kernel_map
.max_offset
) {
2772 kstart
= kernel_map
.max_offset
;
2779 * This index is bogus, but out of the way
2781 nkpg
= vm_page_alloc(NULL
, nkpt
,
2784 VM_ALLOC_INTERRUPT
);
2786 panic("pmap_growkernel: no memory to grow kernel");
2789 ptppaddr
= VM_PAGE_TO_PHYS(nkpg
);
2790 pmap_zero_page(ptppaddr
);
2791 newpt
= (pd_entry_t
) (ptppaddr
|
2792 kernel_pmap
.pmap_bits
[PG_V_IDX
] |
2793 kernel_pmap
.pmap_bits
[PG_RW_IDX
] |
2794 kernel_pmap
.pmap_bits
[PG_A_IDX
] |
2795 kernel_pmap
.pmap_bits
[PG_M_IDX
]);
2796 *pmap_pt(&kernel_pmap
, kstart
) = newpt
;
2799 kstart
= (kstart
+ PAGE_SIZE
* NPTEPG
) &
2800 ~(PAGE_SIZE
* NPTEPG
- 1);
2802 if (kstart
- 1 >= kernel_map
.max_offset
) {
2803 kstart
= kernel_map
.max_offset
;
2809 * Only update kernel_vm_end for areas below KERNBASE.
2811 if (update_kernel_vm_end
&& kernel_vm_end
< kstart
)
2812 kernel_vm_end
= kstart
;
2816 * Add a reference to the specified pmap.
2819 pmap_reference(pmap_t pmap
)
2822 lwkt_gettoken(&pmap
->pm_token
);
2824 lwkt_reltoken(&pmap
->pm_token
);
2828 /***************************************************
2829 * page management routines.
2830 ***************************************************/
2833 * Hold a pv without locking it
2836 pv_hold(pv_entry_t pv
)
2838 atomic_add_int(&pv
->pv_hold
, 1);
2842 * Hold a pv_entry, preventing its destruction. TRUE is returned if the pv
2843 * was successfully locked, FALSE if it wasn't. The caller must dispose of
2846 * Either the pmap->pm_spin or the related vm_page_spin (if traversing a
2847 * pv list via its page) must be held by the caller.
2850 _pv_hold_try(pv_entry_t pv PMAP_DEBUG_DECL
)
2855 * Critical path shortcut expects pv to already have one ref
2856 * (for the pv->pv_pmap).
2858 if (atomic_cmpset_int(&pv
->pv_hold
, 1, PV_HOLD_LOCKED
| 2)) {
2861 pv
->pv_line
= lineno
;
2867 count
= pv
->pv_hold
;
2869 if ((count
& PV_HOLD_LOCKED
) == 0) {
2870 if (atomic_cmpset_int(&pv
->pv_hold
, count
,
2871 (count
+ 1) | PV_HOLD_LOCKED
)) {
2874 pv
->pv_line
= lineno
;
2879 if (atomic_cmpset_int(&pv
->pv_hold
, count
, count
+ 1))
2887 * Drop a previously held pv_entry which could not be locked, allowing its
2890 * Must not be called with a spinlock held as we might zfree() the pv if it
2891 * is no longer associated with a pmap and this was the last hold count.
2894 pv_drop(pv_entry_t pv
)
2899 count
= pv
->pv_hold
;
2901 KKASSERT((count
& PV_HOLD_MASK
) > 0);
2902 KKASSERT((count
& (PV_HOLD_LOCKED
| PV_HOLD_MASK
)) !=
2903 (PV_HOLD_LOCKED
| 1));
2904 if (atomic_cmpset_int(&pv
->pv_hold
, count
, count
- 1)) {
2905 if ((count
& PV_HOLD_MASK
) == 1) {
2907 if (pmap_enter_debug
> 0) {
2909 kprintf("pv_drop: free pv %p\n", pv
);
2912 KKASSERT(count
== 1);
2913 KKASSERT(pv
->pv_pmap
== NULL
);
2923 * Find or allocate the requested PV entry, returning a locked, held pv.
2925 * If (*isnew) is non-zero, the returned pv will have two hold counts, one
2926 * for the caller and one representing the pmap and vm_page association.
2928 * If (*isnew) is zero, the returned pv will have only one hold count.
2930 * Since both associations can only be adjusted while the pv is locked,
2931 * together they represent just one additional hold.
2935 _pv_alloc(pmap_t pmap
, vm_pindex_t pindex
, int *isnew PMAP_DEBUG_DECL
)
2938 pv_entry_t pnew
= NULL
;
2940 spin_lock(&pmap
->pm_spin
);
2942 if ((pv
= pmap
->pm_pvhint
) == NULL
|| pv
->pv_pindex
!= pindex
) {
2943 pv
= pv_entry_rb_tree_RB_LOOKUP(&pmap
->pm_pvroot
,
2948 spin_unlock(&pmap
->pm_spin
);
2949 pnew
= zalloc(pvzone
);
2950 spin_lock(&pmap
->pm_spin
);
2953 pnew
->pv_pmap
= pmap
;
2954 pnew
->pv_pindex
= pindex
;
2955 pnew
->pv_hold
= PV_HOLD_LOCKED
| 2;
2957 pnew
->pv_func
= func
;
2958 pnew
->pv_line
= lineno
;
2960 pv_entry_rb_tree_RB_INSERT(&pmap
->pm_pvroot
, pnew
);
2961 ++pmap
->pm_generation
;
2962 atomic_add_long(&pmap
->pm_stats
.resident_count
, 1);
2963 spin_unlock(&pmap
->pm_spin
);
2968 spin_unlock(&pmap
->pm_spin
);
2969 zfree(pvzone
, pnew
);
2971 spin_lock(&pmap
->pm_spin
);
2974 if (_pv_hold_try(pv PMAP_DEBUG_COPY
)) {
2975 spin_unlock(&pmap
->pm_spin
);
2977 spin_unlock(&pmap
->pm_spin
);
2978 _pv_lock(pv PMAP_DEBUG_COPY
);
2980 if (pv
->pv_pmap
== pmap
&& pv
->pv_pindex
== pindex
) {
2985 spin_lock(&pmap
->pm_spin
);
2990 * Find the requested PV entry, returning a locked+held pv or NULL
2994 _pv_get(pmap_t pmap
, vm_pindex_t pindex PMAP_DEBUG_DECL
)
2998 spin_lock(&pmap
->pm_spin
);
3003 if ((pv
= pmap
->pm_pvhint
) == NULL
|| pv
->pv_pindex
!= pindex
) {
3004 pv
= pv_entry_rb_tree_RB_LOOKUP(&pmap
->pm_pvroot
,
3008 spin_unlock(&pmap
->pm_spin
);
3011 if (_pv_hold_try(pv PMAP_DEBUG_COPY
)) {
3012 spin_unlock(&pmap
->pm_spin
);
3014 spin_unlock(&pmap
->pm_spin
);
3015 _pv_lock(pv PMAP_DEBUG_COPY
);
3017 if (pv
->pv_pmap
== pmap
&& pv
->pv_pindex
== pindex
) {
3018 pv_cache(pv
, pindex
);
3022 spin_lock(&pmap
->pm_spin
);
3027 * Lookup, hold, and attempt to lock (pmap,pindex).
3029 * If the entry does not exist NULL is returned and *errorp is set to 0
3031 * If the entry exists and could be successfully locked it is returned and
3032 * errorp is set to 0.
3034 * If the entry exists but could NOT be successfully locked it is returned
3035 * held and *errorp is set to 1.
3039 pv_get_try(pmap_t pmap
, vm_pindex_t pindex
, int *errorp
)
3043 spin_lock_shared(&pmap
->pm_spin
);
3044 if ((pv
= pmap
->pm_pvhint
) == NULL
|| pv
->pv_pindex
!= pindex
)
3045 pv
= pv_entry_rb_tree_RB_LOOKUP(&pmap
->pm_pvroot
, pindex
);
3047 spin_unlock_shared(&pmap
->pm_spin
);
3051 if (pv_hold_try(pv
)) {
3052 pv_cache(pv
, pindex
);
3053 spin_unlock_shared(&pmap
->pm_spin
);
3055 KKASSERT(pv
->pv_pmap
== pmap
&& pv
->pv_pindex
== pindex
);
3056 return(pv
); /* lock succeeded */
3058 spin_unlock_shared(&pmap
->pm_spin
);
3060 return (pv
); /* lock failed */
3064 * Find the requested PV entry, returning a held pv or NULL
3068 pv_find(pmap_t pmap
, vm_pindex_t pindex
)
3072 spin_lock_shared(&pmap
->pm_spin
);
3074 if ((pv
= pmap
->pm_pvhint
) == NULL
|| pv
->pv_pindex
!= pindex
)
3075 pv
= pv_entry_rb_tree_RB_LOOKUP(&pmap
->pm_pvroot
, pindex
);
3077 spin_unlock_shared(&pmap
->pm_spin
);
3081 pv_cache(pv
, pindex
);
3082 spin_unlock_shared(&pmap
->pm_spin
);
3087 * Lock a held pv, keeping the hold count
3091 _pv_lock(pv_entry_t pv PMAP_DEBUG_DECL
)
3096 count
= pv
->pv_hold
;
3098 if ((count
& PV_HOLD_LOCKED
) == 0) {
3099 if (atomic_cmpset_int(&pv
->pv_hold
, count
,
3100 count
| PV_HOLD_LOCKED
)) {
3103 pv
->pv_line
= lineno
;
3109 tsleep_interlock(pv
, 0);
3110 if (atomic_cmpset_int(&pv
->pv_hold
, count
,
3111 count
| PV_HOLD_WAITING
)) {
3113 kprintf("pv waiting on %s:%d\n",
3114 pv
->pv_func
, pv
->pv_line
);
3116 tsleep(pv
, PINTERLOCKED
, "pvwait", hz
);
3123 * Unlock a held and locked pv, keeping the hold count.
3127 pv_unlock(pv_entry_t pv
)
3132 count
= pv
->pv_hold
;
3134 KKASSERT((count
& (PV_HOLD_LOCKED
| PV_HOLD_MASK
)) >=
3135 (PV_HOLD_LOCKED
| 1));
3136 if (atomic_cmpset_int(&pv
->pv_hold
, count
,
3138 ~(PV_HOLD_LOCKED
| PV_HOLD_WAITING
))) {
3139 if (count
& PV_HOLD_WAITING
)
3147 * Unlock and drop a pv. If the pv is no longer associated with a pmap
3148 * and the hold count drops to zero we will free it.
3150 * Caller should not hold any spin locks. We are protected from hold races
3151 * by virtue of holds only occuring only with a pmap_spin or vm_page_spin
3152 * lock held. A pv cannot be located otherwise.
3156 pv_put(pv_entry_t pv
)
3159 if (pmap_enter_debug
> 0) {
3161 kprintf("pv_put pv=%p hold=%08x\n", pv
, pv
->pv_hold
);
3166 * Fast - shortcut most common condition
3168 if (atomic_cmpset_int(&pv
->pv_hold
, PV_HOLD_LOCKED
| 2, 1))
3179 * Remove the pmap association from a pv, require that pv_m already be removed,
3180 * then unlock and drop the pv. Any pte operations must have already been
3181 * completed. This call may result in a last-drop which will physically free
3184 * Removing the pmap association entails an additional drop.
3186 * pv must be exclusively locked on call and will be disposed of on return.
3190 pv_free(pv_entry_t pv
)
3194 KKASSERT(pv
->pv_m
== NULL
);
3195 KKASSERT((pv
->pv_hold
& PV_HOLD_MASK
) >= 2);
3196 if ((pmap
= pv
->pv_pmap
) != NULL
) {
3197 spin_lock(&pmap
->pm_spin
);
3198 pv_entry_rb_tree_RB_REMOVE(&pmap
->pm_pvroot
, pv
);
3199 ++pmap
->pm_generation
;
3200 if (pmap
->pm_pvhint
== pv
)
3201 pmap
->pm_pvhint
= NULL
;
3202 atomic_add_long(&pmap
->pm_stats
.resident_count
, -1);
3205 spin_unlock(&pmap
->pm_spin
);
3208 * Try to shortcut three atomic ops, otherwise fall through
3209 * and do it normally. Drop two refs and the lock all in
3212 if (atomic_cmpset_int(&pv
->pv_hold
, PV_HOLD_LOCKED
| 2, 0)) {
3214 if (pmap_enter_debug
> 0) {
3216 kprintf("pv_free: free pv %p\n", pv
);
3222 pv_drop(pv
); /* ref for pv_pmap */
3228 * This routine is very drastic, but can save the system
3236 static int warningdone
=0;
3238 if (pmap_pagedaemon_waken
== 0)
3240 pmap_pagedaemon_waken
= 0;
3241 if (warningdone
< 5) {
3242 kprintf("pmap_collect: collecting pv entries -- "
3243 "suggest increasing PMAP_SHPGPERPROC\n");
3247 for (i
= 0; i
< vm_page_array_size
; i
++) {
3248 m
= &vm_page_array
[i
];
3249 if (m
->wire_count
|| m
->hold_count
)
3251 if (vm_page_busy_try(m
, TRUE
) == 0) {
3252 if (m
->wire_count
== 0 && m
->hold_count
== 0) {
3261 * Scan the pmap for active page table entries and issue a callback.
3262 * The callback must dispose of pte_pv, whos PTE entry is at *ptep in
3263 * its parent page table.
3265 * pte_pv will be NULL if the page or page table is unmanaged.
3266 * pt_pv will point to the page table page containing the pte for the page.
3268 * NOTE! If we come across an unmanaged page TABLE (verses an unmanaged page),
3269 * we pass a NULL pte_pv and we pass a pt_pv pointing to the passed
3270 * process pmap's PD and page to the callback function. This can be
3271 * confusing because the pt_pv is really a pd_pv, and the target page
3272 * table page is simply aliased by the pmap and not owned by it.
3274 * It is assumed that the start and end are properly rounded to the page size.
3276 * It is assumed that PD pages and above are managed and thus in the RB tree,
3277 * allowing us to use RB_SCAN from the PD pages down for ranged scans.
3279 struct pmap_scan_info
{
3283 vm_pindex_t sva_pd_pindex
;
3284 vm_pindex_t eva_pd_pindex
;
3285 void (*func
)(pmap_t
, struct pmap_scan_info
*,
3286 pv_entry_t
, pv_entry_t
, int, vm_offset_t
,
3287 pt_entry_t
*, void *);
3289 pmap_inval_bulk_t bulk_core
;
3290 pmap_inval_bulk_t
*bulk
;
3294 static int pmap_scan_cmp(pv_entry_t pv
, void *data
);
3295 static int pmap_scan_callback(pv_entry_t pv
, void *data
);
3298 pmap_scan(struct pmap_scan_info
*info
, int smp_inval
)
3300 struct pmap
*pmap
= info
->pmap
;
3301 pv_entry_t pd_pv
; /* A page directory PV */
3302 pv_entry_t pt_pv
; /* A page table PV */
3303 pv_entry_t pte_pv
; /* A page table entry PV */
3306 struct pv_entry dummy_pv
;
3312 info
->bulk
= &info
->bulk_core
;
3313 pmap_inval_bulk_init(&info
->bulk_core
, pmap
);
3319 * Hold the token for stability; if the pmap is empty we have nothing
3322 lwkt_gettoken(&pmap
->pm_token
);
3324 if (pmap
->pm_stats
.resident_count
== 0) {
3325 lwkt_reltoken(&pmap
->pm_token
);
3334 * Special handling for scanning one page, which is a very common
3335 * operation (it is?).
3337 * NOTE: Locks must be ordered bottom-up. pte,pt,pd,pdp,pml4
3339 if (info
->sva
+ PAGE_SIZE
== info
->eva
) {
3340 generation
= pmap
->pm_generation
;
3341 if (info
->sva
>= VM_MAX_USER_ADDRESS
) {
3343 * Kernel mappings do not track wire counts on
3344 * page table pages and only maintain pd_pv and
3345 * pte_pv levels so pmap_scan() works.
3348 pte_pv
= pv_get(pmap
, pmap_pte_pindex(info
->sva
));
3349 ptep
= vtopte(info
->sva
);
3352 * User pages which are unmanaged will not have a
3353 * pte_pv. User page table pages which are unmanaged
3354 * (shared from elsewhere) will also not have a pt_pv.
3355 * The func() callback will pass both pte_pv and pt_pv
3356 * as NULL in that case.
3358 pte_pv
= pv_get(pmap
, pmap_pte_pindex(info
->sva
));
3359 pt_pv
= pv_get(pmap
, pmap_pt_pindex(info
->sva
));
3360 if (pt_pv
== NULL
) {
3361 KKASSERT(pte_pv
== NULL
);
3362 pd_pv
= pv_get(pmap
, pmap_pd_pindex(info
->sva
));
3364 ptep
= pv_pte_lookup(pd_pv
,
3365 pmap_pt_index(info
->sva
));
3367 info
->func(pmap
, info
,
3376 ptep
= pv_pte_lookup(pt_pv
, pmap_pte_index(info
->sva
));
3380 * NOTE: *ptep can't be ripped out from under us if we hold
3381 * pte_pv locked, but bits can change. However, there is
3382 * a race where another thread may be inserting pte_pv
3383 * and setting *ptep just after our pte_pv lookup fails.
3385 * In this situation we can end up with a NULL pte_pv
3386 * but find that we have a managed *ptep. We explicitly
3387 * check for this race.
3393 * Unlike the pv_find() case below we actually
3394 * acquired a locked pv in this case so any
3395 * race should have been resolved. It is expected
3398 KKASSERT(pte_pv
== NULL
);
3399 } else if (pte_pv
) {
3400 KASSERT((oldpte
& (pmap
->pmap_bits
[PG_MANAGED_IDX
] |
3401 pmap
->pmap_bits
[PG_V_IDX
])) ==
3402 (pmap
->pmap_bits
[PG_MANAGED_IDX
] |
3403 pmap
->pmap_bits
[PG_V_IDX
]),
3404 ("badA *ptep %016lx/%016lx sva %016lx pte_pv %p"
3406 *ptep
, oldpte
, info
->sva
, pte_pv
,
3407 generation
, pmap
->pm_generation
));
3408 info
->func(pmap
, info
, pte_pv
, pt_pv
, 0,
3409 info
->sva
, ptep
, info
->arg
);
3412 * Check for insertion race
3414 if ((oldpte
& pmap
->pmap_bits
[PG_MANAGED_IDX
]) &&
3416 pte_pv
= pv_find(pmap
,
3417 pmap_pte_pindex(info
->sva
));
3421 kprintf("pmap_scan: RACE1 "
3431 KASSERT((oldpte
& (pmap
->pmap_bits
[PG_MANAGED_IDX
] |
3432 pmap
->pmap_bits
[PG_V_IDX
])) ==
3433 pmap
->pmap_bits
[PG_V_IDX
],
3434 ("badB *ptep %016lx/%016lx sva %016lx pte_pv NULL"
3436 *ptep
, oldpte
, info
->sva
,
3437 generation
, pmap
->pm_generation
));
3438 info
->func(pmap
, info
, NULL
, pt_pv
, 0,
3439 info
->sva
, ptep
, info
->arg
);
3444 pmap_inval_bulk_flush(info
->bulk
);
3445 lwkt_reltoken(&pmap
->pm_token
);
3450 * Nominal scan case, RB_SCAN() for PD pages and iterate from
3453 info
->sva_pd_pindex
= pmap_pd_pindex(info
->sva
);
3454 info
->eva_pd_pindex
= pmap_pd_pindex(info
->eva
+ NBPDP
- 1);
3456 if (info
->sva
>= VM_MAX_USER_ADDRESS
) {
3458 * The kernel does not currently maintain any pv_entry's for
3459 * higher-level page tables.
3461 bzero(&dummy_pv
, sizeof(dummy_pv
));
3462 dummy_pv
.pv_pindex
= info
->sva_pd_pindex
;
3463 spin_lock(&pmap
->pm_spin
);
3464 while (dummy_pv
.pv_pindex
< info
->eva_pd_pindex
) {
3465 pmap_scan_callback(&dummy_pv
, info
);
3466 ++dummy_pv
.pv_pindex
;
3468 spin_unlock(&pmap
->pm_spin
);
3471 * User page tables maintain local PML4, PDP, and PD
3472 * pv_entry's at the very least. PT pv's might be
3473 * unmanaged and thus not exist. PTE pv's might be
3474 * unmanaged and thus not exist.
3476 spin_lock(&pmap
->pm_spin
);
3477 pv_entry_rb_tree_RB_SCAN(&pmap
->pm_pvroot
,
3478 pmap_scan_cmp
, pmap_scan_callback
, info
);
3479 spin_unlock(&pmap
->pm_spin
);
3481 pmap_inval_bulk_flush(info
->bulk
);
3482 lwkt_reltoken(&pmap
->pm_token
);
3486 * WARNING! pmap->pm_spin held
3489 pmap_scan_cmp(pv_entry_t pv
, void *data
)
3491 struct pmap_scan_info
*info
= data
;
3492 if (pv
->pv_pindex
< info
->sva_pd_pindex
)
3494 if (pv
->pv_pindex
>= info
->eva_pd_pindex
)
3500 * WARNING! pmap->pm_spin held
3503 pmap_scan_callback(pv_entry_t pv
, void *data
)
3505 struct pmap_scan_info
*info
= data
;
3506 struct pmap
*pmap
= info
->pmap
;
3507 pv_entry_t pd_pv
; /* A page directory PV */
3508 pv_entry_t pt_pv
; /* A page table PV */
3509 pv_entry_t pte_pv
; /* A page table entry PV */
3514 vm_offset_t va_next
;
3515 vm_pindex_t pd_pindex
;
3520 * Pull the PD pindex from the pv before releasing the spinlock.
3522 * WARNING: pv is faked for kernel pmap scans.
3524 pd_pindex
= pv
->pv_pindex
;
3525 spin_unlock(&pmap
->pm_spin
);
3526 pv
= NULL
; /* invalid after spinlock unlocked */
3529 * Calculate the page range within the PD. SIMPLE pmaps are
3530 * direct-mapped for the entire 2^64 address space. Normal pmaps
3531 * reflect the user and kernel address space which requires
3532 * cannonicalization w/regards to converting pd_pindex's back
3535 sva
= (pd_pindex
- NUPTE_TOTAL
- NUPT_TOTAL
) << PDPSHIFT
;
3536 if ((pmap
->pm_flags
& PMAP_FLAG_SIMPLE
) == 0 &&
3537 (sva
& PML4_SIGNMASK
)) {
3538 sva
|= PML4_SIGNMASK
;
3540 eva
= sva
+ NBPDP
; /* can overflow */
3541 if (sva
< info
->sva
)
3543 if (eva
< info
->sva
|| eva
> info
->eva
)
3547 * NOTE: kernel mappings do not track page table pages, only
3550 * NOTE: Locks must be ordered bottom-up. pte,pt,pd,pdp,pml4.
3551 * However, for the scan to be efficient we try to
3552 * cache items top-down.
3557 for (; sva
< eva
; sva
= va_next
) {
3558 if (sva
>= VM_MAX_USER_ADDRESS
) {
3567 * PD cache (degenerate case if we skip). It is possible
3568 * for the PD to not exist due to races. This is ok.
3570 if (pd_pv
== NULL
) {
3571 pd_pv
= pv_get(pmap
, pmap_pd_pindex(sva
));
3572 } else if (pd_pv
->pv_pindex
!= pmap_pd_pindex(sva
)) {
3574 pd_pv
= pv_get(pmap
, pmap_pd_pindex(sva
));
3576 if (pd_pv
== NULL
) {
3577 va_next
= (sva
+ NBPDP
) & ~PDPMASK
;
3586 if (pt_pv
== NULL
) {
3591 pt_pv
= pv_get(pmap
, pmap_pt_pindex(sva
));
3592 } else if (pt_pv
->pv_pindex
!= pmap_pt_pindex(sva
)) {
3598 pt_pv
= pv_get(pmap
, pmap_pt_pindex(sva
));
3602 * If pt_pv is NULL we either have an shared page table
3603 * page and must issue a callback specific to that case,
3604 * or there is no page table page.
3606 * Either way we can skip the page table page.
3608 if (pt_pv
== NULL
) {
3610 * Possible unmanaged (shared from another pmap)
3614 pd_pv
= pv_get(pmap
, pmap_pd_pindex(sva
));
3615 KKASSERT(pd_pv
!= NULL
);
3616 ptep
= pv_pte_lookup(pd_pv
, pmap_pt_index(sva
));
3617 if (*ptep
& pmap
->pmap_bits
[PG_V_IDX
]) {
3618 info
->func(pmap
, info
, NULL
, pd_pv
, 1,
3619 sva
, ptep
, info
->arg
);
3623 * Done, move to next page table page.
3625 va_next
= (sva
+ NBPDR
) & ~PDRMASK
;
3632 * From this point in the loop testing pt_pv for non-NULL
3633 * means we are in UVM, else if it is NULL we are in KVM.
3635 * Limit our scan to either the end of the va represented
3636 * by the current page table page, or to the end of the
3637 * range being removed.
3640 va_next
= (sva
+ NBPDR
) & ~PDRMASK
;
3647 * Scan the page table for pages. Some pages may not be
3648 * managed (might not have a pv_entry).
3650 * There is no page table management for kernel pages so
3651 * pt_pv will be NULL in that case, but otherwise pt_pv
3652 * is non-NULL, locked, and referenced.
3656 * At this point a non-NULL pt_pv means a UVA, and a NULL
3657 * pt_pv means a KVA.
3660 ptep
= pv_pte_lookup(pt_pv
, pmap_pte_index(sva
));
3664 while (sva
< va_next
) {
3666 * Yield every 64 pages.
3668 if ((++info
->count
& 63) == 0)
3672 * Acquire the related pte_pv, if any. If *ptep == 0
3673 * the related pte_pv should not exist, but if *ptep
3674 * is not zero the pte_pv may or may not exist (e.g.
3675 * will not exist for an unmanaged page).
3677 * However a multitude of races are possible here.
3679 * In addition, the (pt_pv, pte_pv) lock order is
3680 * backwards, so we have to be careful in aquiring
3681 * a properly locked pte_pv.
3683 generation
= pmap
->pm_generation
;
3685 pte_pv
= pv_get_try(pmap
, pmap_pte_pindex(sva
),
3692 pv_put(pt_pv
); /* must be non-NULL */
3694 pv_lock(pte_pv
); /* safe to block now */
3697 pt_pv
= pv_get(pmap
,
3698 pmap_pt_pindex(sva
));
3700 * pt_pv reloaded, need new ptep
3702 KKASSERT(pt_pv
!= NULL
);
3703 ptep
= pv_pte_lookup(pt_pv
,
3704 pmap_pte_index(sva
));
3708 pte_pv
= pv_get(pmap
, pmap_pte_pindex(sva
));
3712 * Ok, if *ptep == 0 we had better NOT have a pte_pv.
3717 kprintf("Unexpected non-NULL pte_pv "
3719 "*ptep = %016lx/%016lx\n",
3720 pte_pv
, pt_pv
, *ptep
, oldpte
);
3721 panic("Unexpected non-NULL pte_pv");
3729 * Ready for the callback. The locked pte_pv (if any)
3730 * is consumed by the callback. pte_pv will exist if
3731 * the page is managed, and will not exist if it
3735 KASSERT((oldpte
& (pmap
->pmap_bits
[PG_MANAGED_IDX
] | pmap
->pmap_bits
[PG_V_IDX
])) ==
3736 (pmap
->pmap_bits
[PG_MANAGED_IDX
] | pmap
->pmap_bits
[PG_V_IDX
]),
3737 ("badC *ptep %016lx/%016lx sva %016lx "
3738 "pte_pv %p pm_generation %d/%d",
3739 *ptep
, oldpte
, sva
, pte_pv
,
3740 generation
, pmap
->pm_generation
));
3741 info
->func(pmap
, info
, pte_pv
, pt_pv
, 0,
3742 sva
, ptep
, info
->arg
);
3745 * Check for insertion race. Since there is no
3746 * pte_pv to guard us it is possible for us
3747 * to race another thread doing an insertion.
3748 * Our lookup misses the pte_pv but our *ptep
3749 * check sees the inserted pte.
3751 * XXX panic case seems to occur within a
3752 * vm_fork() of /bin/sh, which frankly
3753 * shouldn't happen since no other threads
3754 * should be inserting to our pmap in that
3755 * situation. Removing, possibly. Inserting,
3758 if ((oldpte
& pmap
->pmap_bits
[PG_MANAGED_IDX
]) &&
3760 pte_pv
= pv_find(pmap
,
3761 pmap_pte_pindex(sva
));
3764 kprintf("pmap_scan: RACE2 "
3774 KASSERT((oldpte
& (pmap
->pmap_bits
[PG_MANAGED_IDX
] | pmap
->pmap_bits
[PG_V_IDX
])) ==
3775 pmap
->pmap_bits
[PG_V_IDX
],
3776 ("badD *ptep %016lx/%016lx sva %016lx "
3777 "pte_pv NULL pm_generation %d/%d",
3779 generation
, pmap
->pm_generation
));
3780 info
->func(pmap
, info
, NULL
, pt_pv
, 0,
3781 sva
, ptep
, info
->arg
);
3796 if ((++info
->count
& 7) == 0)
3800 * Relock before returning.
3802 spin_lock(&pmap
->pm_spin
);
3807 pmap_remove(struct pmap
*pmap
, vm_offset_t sva
, vm_offset_t eva
)
3809 struct pmap_scan_info info
;
3814 info
.func
= pmap_remove_callback
;
3816 pmap_scan(&info
, 1);
3820 pmap_remove_noinval(struct pmap
*pmap
, vm_offset_t sva
, vm_offset_t eva
)
3822 struct pmap_scan_info info
;
3827 info
.func
= pmap_remove_callback
;
3829 pmap_scan(&info
, 0);
3833 pmap_remove_callback(pmap_t pmap
, struct pmap_scan_info
*info
,
3834 pv_entry_t pte_pv
, pv_entry_t pt_pv
, int sharept
,
3835 vm_offset_t va
, pt_entry_t
*ptep
, void *arg __unused
)
3841 * This will also drop pt_pv's wire_count. Note that
3842 * terminal pages are not wired based on mmu presence.
3844 pmap_remove_pv_pte(pte_pv
, pt_pv
, info
->bulk
);
3845 pmap_remove_pv_page(pte_pv
);
3847 } else if (sharept
== 0) {
3849 * Unmanaged page table (pt, pd, or pdp. Not pte).
3851 * pt_pv's wire_count is still bumped by unmanaged pages
3852 * so we must decrement it manually.
3854 * We have to unwire the target page table page.
3856 * It is unclear how we can invalidate a segment so we
3857 * invalidate -1 which invlidates the tlb.
3859 pte
= pmap_inval_bulk(info
->bulk
, (vm_offset_t
)-1, ptep
, 0);
3860 if (pte
& pmap
->pmap_bits
[PG_W_IDX
])
3861 atomic_add_long(&pmap
->pm_stats
.wired_count
, -1);
3862 atomic_add_long(&pmap
->pm_stats
.resident_count
, -1);
3863 if (vm_page_unwire_quick(pt_pv
->pv_m
))
3864 panic("pmap_remove: insufficient wirecount");
3867 * Unmanaged page table (pt, pd, or pdp. Not pte) for
3868 * a shared page table.
3870 * pt_pv is actually the pd_pv for our pmap (not the shared
3873 * We have to unwire the target page table page and we
3874 * have to unwire our page directory page.
3876 * It is unclear how we can invalidate a segment so we
3877 * invalidate -1 which invlidates the tlb.
3879 pte
= pmap_inval_bulk(info
->bulk
, (vm_offset_t
)-1, ptep
, 0);
3880 atomic_add_long(&pmap
->pm_stats
.resident_count
, -1);
3881 KKASSERT((pte
& pmap
->pmap_bits
[PG_DEVICE_IDX
]) == 0);
3882 if (vm_page_unwire_quick(PHYS_TO_VM_PAGE(pte
& PG_FRAME
)))
3883 panic("pmap_remove: shared pgtable1 bad wirecount");
3884 if (vm_page_unwire_quick(pt_pv
->pv_m
))
3885 panic("pmap_remove: shared pgtable2 bad wirecount");
3890 * Removes this physical page from all physical maps in which it resides.
3891 * Reflects back modify bits to the pager.
3893 * This routine may not be called from an interrupt.
3897 pmap_remove_all(vm_page_t m
)
3900 pmap_inval_bulk_t bulk
;
3902 if (!pmap_initialized
/* || (m->flags & PG_FICTITIOUS)*/)
3905 vm_page_spin_lock(m
);
3906 while ((pv
= TAILQ_FIRST(&m
->md
.pv_list
)) != NULL
) {
3907 KKASSERT(pv
->pv_m
== m
);
3908 if (pv_hold_try(pv
)) {
3909 vm_page_spin_unlock(m
);
3911 vm_page_spin_unlock(m
);
3914 if (pv
->pv_m
!= m
) {
3916 vm_page_spin_lock(m
);
3921 * Holding no spinlocks, pv is locked.
3923 pmap_inval_bulk_init(&bulk
, pv
->pv_pmap
);
3924 pmap_remove_pv_pte(pv
, NULL
, &bulk
);
3925 pmap_inval_bulk_flush(&bulk
);
3926 pmap_remove_pv_page(pv
);
3928 vm_page_spin_lock(m
);
3930 KKASSERT((m
->flags
& (PG_MAPPED
|PG_WRITEABLE
)) == 0);
3931 vm_page_spin_unlock(m
);
3935 * Set the physical protection on the specified range of this map
3936 * as requested. This function is typically only used for debug watchpoints
3939 * This function may not be called from an interrupt if the map is
3940 * not the kernel_pmap.
3942 * NOTE! For shared page table pages we just unmap the page.
3945 pmap_protect(pmap_t pmap
, vm_offset_t sva
, vm_offset_t eva
, vm_prot_t prot
)
3947 struct pmap_scan_info info
;
3948 /* JG review for NX */
3952 if ((prot
& VM_PROT_READ
) == VM_PROT_NONE
) {
3953 pmap_remove(pmap
, sva
, eva
);
3956 if (prot
& VM_PROT_WRITE
)
3961 info
.func
= pmap_protect_callback
;
3963 pmap_scan(&info
, 1);
3968 pmap_protect_callback(pmap_t pmap
, struct pmap_scan_info
*info
,
3969 pv_entry_t pte_pv
, pv_entry_t pt_pv
, int sharept
,
3970 vm_offset_t va
, pt_entry_t
*ptep
, void *arg __unused
)
3982 if (pbits
& pmap
->pmap_bits
[PG_A_IDX
]) {
3983 if ((pbits
& pmap
->pmap_bits
[PG_DEVICE_IDX
]) == 0) {
3984 m
= PHYS_TO_VM_PAGE(pbits
& PG_FRAME
);
3985 KKASSERT(m
== pte_pv
->pv_m
);
3986 vm_page_flag_set(m
, PG_REFERENCED
);
3988 cbits
&= ~pmap
->pmap_bits
[PG_A_IDX
];
3990 if (pbits
& pmap
->pmap_bits
[PG_M_IDX
]) {
3991 if (pmap_track_modified(pte_pv
->pv_pindex
)) {
3992 if ((pbits
& pmap
->pmap_bits
[PG_DEVICE_IDX
]) == 0) {
3994 m
= PHYS_TO_VM_PAGE(pbits
&
3999 cbits
&= ~pmap
->pmap_bits
[PG_M_IDX
];
4002 } else if (sharept
) {
4004 * Unmanaged page table, pt_pv is actually the pd_pv
4005 * for our pmap (not the object's shared pmap).
4007 * When asked to protect something in a shared page table
4008 * page we just unmap the page table page. We have to
4009 * invalidate the tlb in this situation.
4011 * XXX Warning, shared page tables will not be used for
4012 * OBJT_DEVICE or OBJT_MGTDEVICE (PG_FICTITIOUS) mappings
4013 * so PHYS_TO_VM_PAGE() should be safe here.
4015 pte
= pmap_inval_smp(pmap
, (vm_offset_t
)-1, 1, ptep
, 0);
4016 if (vm_page_unwire_quick(PHYS_TO_VM_PAGE(pte
& PG_FRAME
)))
4017 panic("pmap_protect: pgtable1 pg bad wirecount");
4018 if (vm_page_unwire_quick(pt_pv
->pv_m
))
4019 panic("pmap_protect: pgtable2 pg bad wirecount");
4022 /* else unmanaged page, adjust bits, no wire changes */
4025 cbits
&= ~pmap
->pmap_bits
[PG_RW_IDX
];
4027 if (pmap_enter_debug
> 0) {
4029 kprintf("pmap_protect va=%lx ptep=%p pte_pv=%p "
4030 "pt_pv=%p cbits=%08lx\n",
4036 if (pbits
!= cbits
) {
4037 if (!pmap_inval_smp_cmpset(pmap
, (vm_offset_t
)-1,
4038 ptep
, pbits
, cbits
)) {
4048 * Insert the vm_page (m) at the virtual address (va), replacing any prior
4049 * mapping at that address. Set protection and wiring as requested.
4051 * If entry is non-NULL we check to see if the SEG_SIZE optimization is
4052 * possible. If it is we enter the page into the appropriate shared pmap
4053 * hanging off the related VM object instead of the passed pmap, then we
4054 * share the page table page from the VM object's pmap into the current pmap.
4056 * NOTE: This routine MUST insert the page into the pmap now, it cannot
4060 pmap_enter(pmap_t pmap
, vm_offset_t va
, vm_page_t m
, vm_prot_t prot
,
4061 boolean_t wired
, vm_map_entry_t entry
)
4063 pv_entry_t pt_pv
; /* page table */
4064 pv_entry_t pte_pv
; /* page table entry */
4067 pt_entry_t origpte
, newpte
;
4072 va
= trunc_page(va
);
4073 #ifdef PMAP_DIAGNOSTIC
4075 panic("pmap_enter: toobig");
4076 if ((va
>= UPT_MIN_ADDRESS
) && (va
< UPT_MAX_ADDRESS
))
4077 panic("pmap_enter: invalid to pmap_enter page table "
4078 "pages (va: 0x%lx)", va
);
4080 if (va
< UPT_MAX_ADDRESS
&& pmap
== &kernel_pmap
) {
4081 kprintf("Warning: pmap_enter called on UVA with "
4084 db_print_backtrace();
4087 if (va
>= UPT_MAX_ADDRESS
&& pmap
!= &kernel_pmap
) {
4088 kprintf("Warning: pmap_enter called on KVA without"
4091 db_print_backtrace();
4096 * Get locked PV entries for our new page table entry (pte_pv)
4097 * and for its parent page table (pt_pv). We need the parent
4098 * so we can resolve the location of the ptep.
4100 * Only hardware MMU actions can modify the ptep out from
4103 * if (m) is fictitious or unmanaged we do not create a managing
4104 * pte_pv for it. Any pre-existing page's management state must
4105 * match (avoiding code complexity).
4107 * If the pmap is still being initialized we assume existing
4110 * Kernel mapppings do not track page table pages (i.e. pt_pv).
4112 if (pmap_initialized
== FALSE
) {
4117 } else if (m
->flags
& (/*PG_FICTITIOUS |*/ PG_UNMANAGED
)) { /* XXX */
4119 if (va
>= VM_MAX_USER_ADDRESS
) {
4123 pt_pv
= pmap_allocpte_seg(pmap
, pmap_pt_pindex(va
),
4125 ptep
= pv_pte_lookup(pt_pv
, pmap_pte_index(va
));
4129 KASSERT(origpte
== 0 ||
4130 (origpte
& pmap
->pmap_bits
[PG_MANAGED_IDX
]) == 0,
4131 ("Invalid PTE 0x%016jx @ 0x%016jx\n", origpte
, va
));
4133 if (va
>= VM_MAX_USER_ADDRESS
) {
4135 * Kernel map, pv_entry-tracked.
4138 pte_pv
= pmap_allocpte(pmap
, pmap_pte_pindex(va
), NULL
);
4144 pte_pv
= pmap_allocpte_seg(pmap
, pmap_pte_pindex(va
),
4146 ptep
= pv_pte_lookup(pt_pv
, pmap_pte_index(va
));
4150 KASSERT(origpte
== 0 ||
4151 (origpte
& pmap
->pmap_bits
[PG_MANAGED_IDX
]),
4152 ("Invalid PTE 0x%016jx @ 0x%016jx\n", origpte
, va
));
4155 pa
= VM_PAGE_TO_PHYS(m
);
4156 opa
= origpte
& PG_FRAME
;
4158 newpte
= (pt_entry_t
)(pa
| pte_prot(pmap
, prot
) |
4159 pmap
->pmap_bits
[PG_V_IDX
] | pmap
->pmap_bits
[PG_A_IDX
]);
4161 newpte
|= pmap
->pmap_bits
[PG_W_IDX
];
4162 if (va
< VM_MAX_USER_ADDRESS
)
4163 newpte
|= pmap
->pmap_bits
[PG_U_IDX
];
4165 newpte
|= pmap
->pmap_bits
[PG_MANAGED_IDX
];
4166 // if (pmap == &kernel_pmap)
4167 // newpte |= pgeflag;
4168 newpte
|= pmap
->pmap_cache_bits
[m
->pat_mode
];
4169 if (m
->flags
& PG_FICTITIOUS
)
4170 newpte
|= pmap
->pmap_bits
[PG_DEVICE_IDX
];
4173 * It is possible for multiple faults to occur in threaded
4174 * environments, the existing pte might be correct.
4176 if (((origpte
^ newpte
) & ~(pt_entry_t
)(pmap
->pmap_bits
[PG_M_IDX
] |
4177 pmap
->pmap_bits
[PG_A_IDX
])) == 0)
4181 * Ok, either the address changed or the protection or wiring
4184 * Clear the current entry, interlocking the removal. For managed
4185 * pte's this will also flush the modified state to the vm_page.
4186 * Atomic ops are mandatory in order to ensure that PG_M events are
4187 * not lost during any transition.
4189 * WARNING: The caller has busied the new page but not the original
4190 * vm_page which we are trying to replace. Because we hold
4191 * the pte_pv lock, but have not busied the page, PG bits
4192 * can be cleared out from under us.
4197 * pmap_remove_pv_pte() unwires pt_pv and assumes
4198 * we will free pte_pv, but since we are reusing
4199 * pte_pv we want to retain the wire count.
4201 * pt_pv won't exist for a kernel page (managed or
4205 vm_page_wire_quick(pt_pv
->pv_m
);
4206 if (prot
& VM_PROT_NOSYNC
) {
4207 pmap_remove_pv_pte(pte_pv
, pt_pv
, NULL
);
4209 pmap_inval_bulk_t bulk
;
4211 pmap_inval_bulk_init(&bulk
, pmap
);
4212 pmap_remove_pv_pte(pte_pv
, pt_pv
, &bulk
);
4213 pmap_inval_bulk_flush(&bulk
);
4216 pmap_remove_pv_page(pte_pv
);
4217 } else if (prot
& VM_PROT_NOSYNC
) {
4219 * Unmanaged page, NOSYNC (no mmu sync) requested.
4221 * Leave wire count on PT page intact.
4223 (void)pte_load_clear(ptep
);
4224 cpu_invlpg((void *)va
);
4225 atomic_add_long(&pmap
->pm_stats
.resident_count
, -1);
4228 * Unmanaged page, normal enter.
4230 * Leave wire count on PT page intact.
4232 pmap_inval_smp(pmap
, va
, 1, ptep
, 0);
4233 atomic_add_long(&pmap
->pm_stats
.resident_count
, -1);
4235 KKASSERT(*ptep
== 0);
4239 if (pmap_enter_debug
> 0) {
4241 kprintf("pmap_enter: va=%lx m=%p origpte=%lx newpte=%lx ptep=%p"
4242 " pte_pv=%p pt_pv=%p opa=%lx prot=%02x\n",
4244 origpte
, newpte
, ptep
,
4245 pte_pv
, pt_pv
, opa
, prot
);
4251 * Enter on the PV list if part of our managed memory.
4252 * Wiring of the PT page is already handled.
4254 KKASSERT(pte_pv
->pv_m
== NULL
);
4255 vm_page_spin_lock(m
);
4257 pmap_page_stats_adding(m
);
4258 TAILQ_INSERT_TAIL(&m
->md
.pv_list
, pte_pv
, pv_list
);
4259 vm_page_flag_set(m
, PG_MAPPED
);
4260 vm_page_spin_unlock(m
);
4261 } else if (pt_pv
&& opa
== 0) {
4263 * We have to adjust the wire count on the PT page ourselves
4264 * for unmanaged entries. If opa was non-zero we retained
4265 * the existing wire count from the removal.
4267 vm_page_wire_quick(pt_pv
->pv_m
);
4271 * Kernel VMAs (pt_pv == NULL) require pmap invalidation interlocks.
4273 * User VMAs do not because those will be zero->non-zero, so no
4274 * stale entries to worry about at this point.
4276 * For KVM there appear to still be issues. Theoretically we
4277 * should be able to scrap the interlocks entirely but we
4280 if ((prot
& VM_PROT_NOSYNC
) == 0 && pt_pv
== NULL
) {
4281 pmap_inval_smp(pmap
, va
, 1, ptep
, newpte
);
4283 *(volatile pt_entry_t
*)ptep
= newpte
;
4285 cpu_invlpg((void *)va
);
4290 atomic_add_long(&pte_pv
->pv_pmap
->pm_stats
.wired_count
,
4293 atomic_add_long(&pmap
->pm_stats
.wired_count
, 1);
4296 if (newpte
& pmap
->pmap_bits
[PG_RW_IDX
])
4297 vm_page_flag_set(m
, PG_WRITEABLE
);
4300 * Unmanaged pages need manual resident_count tracking.
4302 if (pte_pv
== NULL
&& pt_pv
)
4303 atomic_add_long(&pt_pv
->pv_pmap
->pm_stats
.resident_count
, 1);
4309 KKASSERT((newpte
& pmap
->pmap_bits
[PG_MANAGED_IDX
]) == 0 ||
4310 (m
->flags
& PG_MAPPED
));
4313 * Cleanup the pv entry, allowing other accessors.
4322 * This code works like pmap_enter() but assumes VM_PROT_READ and not-wired.
4323 * This code also assumes that the pmap has no pre-existing entry for this
4326 * This code currently may only be used on user pmaps, not kernel_pmap.
4329 pmap_enter_quick(pmap_t pmap
, vm_offset_t va
, vm_page_t m
)
4331 pmap_enter(pmap
, va
, m
, VM_PROT_READ
, FALSE
, NULL
);
4335 * Make a temporary mapping for a physical address. This is only intended
4336 * to be used for panic dumps.
4338 * The caller is responsible for calling smp_invltlb().
4341 pmap_kenter_temporary(vm_paddr_t pa
, long i
)
4343 pmap_kenter_quick((vm_offset_t
)crashdumpmap
+ (i
* PAGE_SIZE
), pa
);
4344 return ((void *)crashdumpmap
);
4347 #define MAX_INIT_PT (96)
4350 * This routine preloads the ptes for a given object into the specified pmap.
4351 * This eliminates the blast of soft faults on process startup and
4352 * immediately after an mmap.
4354 static int pmap_object_init_pt_callback(vm_page_t p
, void *data
);
4357 pmap_object_init_pt(pmap_t pmap
, vm_offset_t addr
, vm_prot_t prot
,
4358 vm_object_t object
, vm_pindex_t pindex
,
4359 vm_size_t size
, int limit
)
4361 struct rb_vm_page_scan_info info
;
4366 * We can't preinit if read access isn't set or there is no pmap
4369 if ((prot
& VM_PROT_READ
) == 0 || pmap
== NULL
|| object
== NULL
)
4373 * We can't preinit if the pmap is not the current pmap
4375 lp
= curthread
->td_lwp
;
4376 if (lp
== NULL
|| pmap
!= vmspace_pmap(lp
->lwp_vmspace
))
4380 * Misc additional checks
4382 psize
= x86_64_btop(size
);
4384 if ((object
->type
!= OBJT_VNODE
) ||
4385 ((limit
& MAP_PREFAULT_PARTIAL
) && (psize
> MAX_INIT_PT
) &&
4386 (object
->resident_page_count
> MAX_INIT_PT
))) {
4390 if (pindex
+ psize
> object
->size
) {
4391 if (object
->size
< pindex
)
4393 psize
= object
->size
- pindex
;
4400 * If everything is segment-aligned do not pre-init here. Instead
4401 * allow the normal vm_fault path to pass a segment hint to
4402 * pmap_enter() which will then use an object-referenced shared
4405 if ((addr
& SEG_MASK
) == 0 &&
4406 (ctob(psize
) & SEG_MASK
) == 0 &&
4407 (ctob(pindex
) & SEG_MASK
) == 0) {
4412 * Use a red-black scan to traverse the requested range and load
4413 * any valid pages found into the pmap.
4415 * We cannot safely scan the object's memq without holding the
4418 info
.start_pindex
= pindex
;
4419 info
.end_pindex
= pindex
+ psize
- 1;
4425 vm_object_hold_shared(object
);
4426 vm_page_rb_tree_RB_SCAN(&object
->rb_memq
, rb_vm_page_scancmp
,
4427 pmap_object_init_pt_callback
, &info
);
4428 vm_object_drop(object
);
4433 pmap_object_init_pt_callback(vm_page_t p
, void *data
)
4435 struct rb_vm_page_scan_info
*info
= data
;
4436 vm_pindex_t rel_index
;
4439 * don't allow an madvise to blow away our really
4440 * free pages allocating pv entries.
4442 if ((info
->limit
& MAP_PREFAULT_MADVISE
) &&
4443 vmstats
.v_free_count
< vmstats
.v_free_reserved
) {
4448 * Ignore list markers and ignore pages we cannot instantly
4449 * busy (while holding the object token).
4451 if (p
->flags
& PG_MARKER
)
4453 if (vm_page_busy_try(p
, TRUE
))
4455 if (((p
->valid
& VM_PAGE_BITS_ALL
) == VM_PAGE_BITS_ALL
) &&
4456 (p
->flags
& PG_FICTITIOUS
) == 0) {
4457 if ((p
->queue
- p
->pc
) == PQ_CACHE
)
4458 vm_page_deactivate(p
);
4459 rel_index
= p
->pindex
- info
->start_pindex
;
4460 pmap_enter_quick(info
->pmap
,
4461 info
->addr
+ x86_64_ptob(rel_index
), p
);
4469 * Return TRUE if the pmap is in shape to trivially pre-fault the specified
4472 * Returns FALSE if it would be non-trivial or if a pte is already loaded
4475 * XXX This is safe only because page table pages are not freed.
4478 pmap_prefault_ok(pmap_t pmap
, vm_offset_t addr
)
4482 /*spin_lock(&pmap->pm_spin);*/
4483 if ((pte
= pmap_pte(pmap
, addr
)) != NULL
) {
4484 if (*pte
& pmap
->pmap_bits
[PG_V_IDX
]) {
4485 /*spin_unlock(&pmap->pm_spin);*/
4489 /*spin_unlock(&pmap->pm_spin);*/
4494 * Change the wiring attribute for a pmap/va pair. The mapping must already
4495 * exist in the pmap. The mapping may or may not be managed.
4498 pmap_change_wiring(pmap_t pmap
, vm_offset_t va
, boolean_t wired
,
4499 vm_map_entry_t entry
)
4506 lwkt_gettoken(&pmap
->pm_token
);
4507 pv
= pmap_allocpte_seg(pmap
, pmap_pt_pindex(va
), NULL
, entry
, va
);
4508 ptep
= pv_pte_lookup(pv
, pmap_pte_index(va
));
4510 if (wired
&& !pmap_pte_w(pmap
, ptep
))
4511 atomic_add_long(&pv
->pv_pmap
->pm_stats
.wired_count
, 1);
4512 else if (!wired
&& pmap_pte_w(pmap
, ptep
))
4513 atomic_add_long(&pv
->pv_pmap
->pm_stats
.wired_count
, -1);
4516 * Wiring is not a hardware characteristic so there is no need to
4517 * invalidate TLB. However, in an SMP environment we must use
4518 * a locked bus cycle to update the pte (if we are not using
4519 * the pmap_inval_*() API that is)... it's ok to do this for simple
4523 atomic_set_long(ptep
, pmap
->pmap_bits
[PG_W_IDX
]);
4525 atomic_clear_long(ptep
, pmap
->pmap_bits
[PG_W_IDX
]);
4527 lwkt_reltoken(&pmap
->pm_token
);
4533 * Copy the range specified by src_addr/len from the source map to
4534 * the range dst_addr/len in the destination map.
4536 * This routine is only advisory and need not do anything.
4539 pmap_copy(pmap_t dst_pmap
, pmap_t src_pmap
, vm_offset_t dst_addr
,
4540 vm_size_t len
, vm_offset_t src_addr
)
4547 * Zero the specified physical page.
4549 * This function may be called from an interrupt and no locking is
4553 pmap_zero_page(vm_paddr_t phys
)
4555 vm_offset_t va
= PHYS_TO_DMAP(phys
);
4557 pagezero((void *)va
);
4563 * Zero part of a physical page by mapping it into memory and clearing
4564 * its contents with bzero.
4566 * off and size may not cover an area beyond a single hardware page.
4569 pmap_zero_page_area(vm_paddr_t phys
, int off
, int size
)
4571 vm_offset_t virt
= PHYS_TO_DMAP(phys
);
4573 bzero((char *)virt
+ off
, size
);
4579 * Copy the physical page from the source PA to the target PA.
4580 * This function may be called from an interrupt. No locking
4584 pmap_copy_page(vm_paddr_t src
, vm_paddr_t dst
)
4586 vm_offset_t src_virt
, dst_virt
;
4588 src_virt
= PHYS_TO_DMAP(src
);
4589 dst_virt
= PHYS_TO_DMAP(dst
);
4590 bcopy((void *)src_virt
, (void *)dst_virt
, PAGE_SIZE
);
4594 * pmap_copy_page_frag:
4596 * Copy the physical page from the source PA to the target PA.
4597 * This function may be called from an interrupt. No locking
4601 pmap_copy_page_frag(vm_paddr_t src
, vm_paddr_t dst
, size_t bytes
)
4603 vm_offset_t src_virt
, dst_virt
;
4605 src_virt
= PHYS_TO_DMAP(src
);
4606 dst_virt
= PHYS_TO_DMAP(dst
);
4608 bcopy((char *)src_virt
+ (src
& PAGE_MASK
),
4609 (char *)dst_virt
+ (dst
& PAGE_MASK
),
4614 * Returns true if the pmap's pv is one of the first 16 pvs linked to from
4615 * this page. This count may be changed upwards or downwards in the future;
4616 * it is only necessary that true be returned for a small subset of pmaps
4617 * for proper page aging.
4620 pmap_page_exists_quick(pmap_t pmap
, vm_page_t m
)
4625 if (!pmap_initialized
|| (m
->flags
& PG_FICTITIOUS
))
4628 vm_page_spin_lock(m
);
4629 TAILQ_FOREACH(pv
, &m
->md
.pv_list
, pv_list
) {
4630 if (pv
->pv_pmap
== pmap
) {
4631 vm_page_spin_unlock(m
);
4638 vm_page_spin_unlock(m
);
4643 * Remove all pages from specified address space this aids process exit
4644 * speeds. Also, this code may be special cased for the current process
4648 pmap_remove_pages(pmap_t pmap
, vm_offset_t sva
, vm_offset_t eva
)
4650 pmap_remove_noinval(pmap
, sva
, eva
);
4655 * pmap_testbit tests bits in pte's note that the testbit/clearbit
4656 * routines are inline, and a lot of things compile-time evaluate.
4660 pmap_testbit(vm_page_t m
, int bit
)
4666 if (!pmap_initialized
|| (m
->flags
& PG_FICTITIOUS
))
4669 if (TAILQ_FIRST(&m
->md
.pv_list
) == NULL
)
4671 vm_page_spin_lock(m
);
4672 if (TAILQ_FIRST(&m
->md
.pv_list
) == NULL
) {
4673 vm_page_spin_unlock(m
);
4677 TAILQ_FOREACH(pv
, &m
->md
.pv_list
, pv_list
) {
4679 #if defined(PMAP_DIAGNOSTIC)
4680 if (pv
->pv_pmap
== NULL
) {
4681 kprintf("Null pmap (tb) at pindex: %"PRIu64
"\n",
4689 * If the bit being tested is the modified bit, then
4690 * mark clean_map and ptes as never
4693 * WARNING! Because we do not lock the pv, *pte can be in a
4694 * state of flux. Despite this the value of *pte
4695 * will still be related to the vm_page in some way
4696 * because the pv cannot be destroyed as long as we
4697 * hold the vm_page spin lock.
4699 if (bit
== PG_A_IDX
|| bit
== PG_M_IDX
) {
4700 //& (pmap->pmap_bits[PG_A_IDX] | pmap->pmap_bits[PG_M_IDX])) {
4701 if (!pmap_track_modified(pv
->pv_pindex
))
4705 pte
= pmap_pte_quick(pv
->pv_pmap
, pv
->pv_pindex
<< PAGE_SHIFT
);
4706 if (*pte
& pmap
->pmap_bits
[bit
]) {
4707 vm_page_spin_unlock(m
);
4711 vm_page_spin_unlock(m
);
4716 * This routine is used to modify bits in ptes. Only one bit should be
4717 * specified. PG_RW requires special handling.
4719 * Caller must NOT hold any spin locks
4723 pmap_clearbit(vm_page_t m
, int bit_index
)
4730 if (bit_index
== PG_RW_IDX
)
4731 vm_page_flag_clear(m
, PG_WRITEABLE
);
4732 if (!pmap_initialized
|| (m
->flags
& PG_FICTITIOUS
)) {
4739 * Loop over all current mappings setting/clearing as appropos If
4740 * setting RO do we need to clear the VAC?
4742 * NOTE: When clearing PG_M we could also (not implemented) drop
4743 * through to the PG_RW code and clear PG_RW too, forcing
4744 * a fault on write to redetect PG_M for virtual kernels, but
4745 * it isn't necessary since virtual kernels invalidate the
4746 * pte when they clear the VPTE_M bit in their virtual page
4749 * NOTE: Does not re-dirty the page when clearing only PG_M.
4751 * NOTE: Because we do not lock the pv, *pte can be in a state of
4752 * flux. Despite this the value of *pte is still somewhat
4753 * related while we hold the vm_page spin lock.
4755 * *pte can be zero due to this race. Since we are clearing
4756 * bits we basically do no harm when this race ccurs.
4758 if (bit_index
!= PG_RW_IDX
) {
4759 vm_page_spin_lock(m
);
4760 TAILQ_FOREACH(pv
, &m
->md
.pv_list
, pv_list
) {
4761 #if defined(PMAP_DIAGNOSTIC)
4762 if (pv
->pv_pmap
== NULL
) {
4763 kprintf("Null pmap (cb) at pindex: %"PRIu64
"\n",
4769 pte
= pmap_pte_quick(pv
->pv_pmap
,
4770 pv
->pv_pindex
<< PAGE_SHIFT
);
4772 if (pbits
& pmap
->pmap_bits
[bit_index
])
4773 atomic_clear_long(pte
, pmap
->pmap_bits
[bit_index
]);
4775 vm_page_spin_unlock(m
);
4780 * Clear PG_RW. Also clears PG_M and marks the page dirty if PG_M
4784 vm_page_spin_lock(m
);
4785 TAILQ_FOREACH(pv
, &m
->md
.pv_list
, pv_list
) {
4787 * don't write protect pager mappings
4789 if (!pmap_track_modified(pv
->pv_pindex
))
4792 #if defined(PMAP_DIAGNOSTIC)
4793 if (pv
->pv_pmap
== NULL
) {
4794 kprintf("Null pmap (cb) at pindex: %"PRIu64
"\n",
4801 * Skip pages which do not have PG_RW set.
4803 pte
= pmap_pte_quick(pv
->pv_pmap
, pv
->pv_pindex
<< PAGE_SHIFT
);
4804 if ((*pte
& pmap
->pmap_bits
[PG_RW_IDX
]) == 0)
4810 if (pv_hold_try(pv
)) {
4811 vm_page_spin_unlock(m
);
4813 vm_page_spin_unlock(m
);
4814 pv_lock(pv
); /* held, now do a blocking lock */
4816 if (pv
->pv_pmap
!= pmap
|| pv
->pv_m
!= m
) {
4817 pv_put(pv
); /* and release */
4818 goto restart
; /* anything could have happened */
4820 KKASSERT(pv
->pv_pmap
== pmap
);
4826 nbits
= pbits
& ~(pmap
->pmap_bits
[PG_RW_IDX
] |
4827 pmap
->pmap_bits
[PG_M_IDX
]);
4828 if (pmap_inval_smp_cmpset(pmap
,
4829 ((vm_offset_t
)pv
->pv_pindex
<< PAGE_SHIFT
),
4830 pte
, pbits
, nbits
)) {
4835 vm_page_spin_lock(m
);
4838 * If PG_M was found to be set while we were clearing PG_RW
4839 * we also clear PG_M (done above) and mark the page dirty.
4840 * Callers expect this behavior.
4842 if (pbits
& pmap
->pmap_bits
[PG_M_IDX
])
4846 vm_page_spin_unlock(m
);
4850 * Lower the permission for all mappings to a given page.
4852 * Page must be busied by caller. Because page is busied by caller this
4853 * should not be able to race a pmap_enter().
4856 pmap_page_protect(vm_page_t m
, vm_prot_t prot
)
4858 /* JG NX support? */
4859 if ((prot
& VM_PROT_WRITE
) == 0) {
4860 if (prot
& (VM_PROT_READ
| VM_PROT_EXECUTE
)) {
4862 * NOTE: pmap_clearbit(.. PG_RW) also clears
4863 * the PG_WRITEABLE flag in (m).
4865 pmap_clearbit(m
, PG_RW_IDX
);
4873 pmap_phys_address(vm_pindex_t ppn
)
4875 return (x86_64_ptob(ppn
));
4879 * Return a count of reference bits for a page, clearing those bits.
4880 * It is not necessary for every reference bit to be cleared, but it
4881 * is necessary that 0 only be returned when there are truly no
4882 * reference bits set.
4884 * XXX: The exact number of bits to check and clear is a matter that
4885 * should be tested and standardized at some point in the future for
4886 * optimal aging of shared pages.
4888 * This routine may not block.
4891 pmap_ts_referenced(vm_page_t m
)
4898 if (!pmap_initialized
|| (m
->flags
& PG_FICTITIOUS
))
4901 vm_page_spin_lock(m
);
4902 TAILQ_FOREACH(pv
, &m
->md
.pv_list
, pv_list
) {
4903 if (!pmap_track_modified(pv
->pv_pindex
))
4906 pte
= pmap_pte_quick(pv
->pv_pmap
, pv
->pv_pindex
<< PAGE_SHIFT
);
4907 if (pte
&& (*pte
& pmap
->pmap_bits
[PG_A_IDX
])) {
4908 atomic_clear_long(pte
, pmap
->pmap_bits
[PG_A_IDX
]);
4914 vm_page_spin_unlock(m
);
4921 * Return whether or not the specified physical page was modified
4922 * in any physical maps.
4925 pmap_is_modified(vm_page_t m
)
4929 res
= pmap_testbit(m
, PG_M_IDX
);
4934 * Clear the modify bits on the specified physical page.
4937 pmap_clear_modify(vm_page_t m
)
4939 pmap_clearbit(m
, PG_M_IDX
);
4943 * pmap_clear_reference:
4945 * Clear the reference bit on the specified physical page.
4948 pmap_clear_reference(vm_page_t m
)
4950 pmap_clearbit(m
, PG_A_IDX
);
4954 * Miscellaneous support routines follow
4959 i386_protection_init(void)
4963 /* JG NX support may go here; No VM_PROT_EXECUTE ==> set NX bit */
4964 kp
= protection_codes
;
4965 for (prot
= 0; prot
< PROTECTION_CODES_SIZE
; prot
++) {
4967 case VM_PROT_NONE
| VM_PROT_NONE
| VM_PROT_NONE
:
4969 * Read access is also 0. There isn't any execute bit,
4970 * so just make it readable.
4972 case VM_PROT_READ
| VM_PROT_NONE
| VM_PROT_NONE
:
4973 case VM_PROT_READ
| VM_PROT_NONE
| VM_PROT_EXECUTE
:
4974 case VM_PROT_NONE
| VM_PROT_NONE
| VM_PROT_EXECUTE
:
4977 case VM_PROT_NONE
| VM_PROT_WRITE
| VM_PROT_NONE
:
4978 case VM_PROT_NONE
| VM_PROT_WRITE
| VM_PROT_EXECUTE
:
4979 case VM_PROT_READ
| VM_PROT_WRITE
| VM_PROT_NONE
:
4980 case VM_PROT_READ
| VM_PROT_WRITE
| VM_PROT_EXECUTE
:
4981 *kp
++ = pmap_bits_default
[PG_RW_IDX
];
4988 * Map a set of physical memory pages into the kernel virtual
4989 * address space. Return a pointer to where it is mapped. This
4990 * routine is intended to be used for mapping device memory,
4993 * NOTE: We can't use pgeflag unless we invalidate the pages one at
4996 * NOTE: The PAT attributes {WRITE_BACK, WRITE_THROUGH, UNCACHED, UNCACHEABLE}
4997 * work whether the cpu supports PAT or not. The remaining PAT
4998 * attributes {WRITE_PROTECTED, WRITE_COMBINING} only work if the cpu
5002 pmap_mapdev(vm_paddr_t pa
, vm_size_t size
)
5004 return(pmap_mapdev_attr(pa
, size
, PAT_WRITE_BACK
));
5008 pmap_mapdev_uncacheable(vm_paddr_t pa
, vm_size_t size
)
5010 return(pmap_mapdev_attr(pa
, size
, PAT_UNCACHEABLE
));
5014 pmap_mapbios(vm_paddr_t pa
, vm_size_t size
)
5016 return (pmap_mapdev_attr(pa
, size
, PAT_WRITE_BACK
));
5020 * Map a set of physical memory pages into the kernel virtual
5021 * address space. Return a pointer to where it is mapped. This
5022 * routine is intended to be used for mapping device memory,
5026 pmap_mapdev_attr(vm_paddr_t pa
, vm_size_t size
, int mode
)
5028 vm_offset_t va
, tmpva
, offset
;
5032 offset
= pa
& PAGE_MASK
;
5033 size
= roundup(offset
+ size
, PAGE_SIZE
);
5035 va
= kmem_alloc_nofault(&kernel_map
, size
, VM_SUBSYS_MAPDEV
, PAGE_SIZE
);
5037 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
5039 pa
= pa
& ~PAGE_MASK
;
5040 for (tmpva
= va
, tmpsize
= size
; tmpsize
> 0;) {
5041 pte
= vtopte(tmpva
);
5043 kernel_pmap
.pmap_bits
[PG_RW_IDX
] |
5044 kernel_pmap
.pmap_bits
[PG_V_IDX
] | /* pgeflag | */
5045 kernel_pmap
.pmap_cache_bits
[mode
];
5046 tmpsize
-= PAGE_SIZE
;
5050 pmap_invalidate_range(&kernel_pmap
, va
, va
+ size
);
5051 pmap_invalidate_cache_range(va
, va
+ size
);
5053 return ((void *)(va
+ offset
));
5057 pmap_unmapdev(vm_offset_t va
, vm_size_t size
)
5059 vm_offset_t base
, offset
;
5061 base
= va
& ~PAGE_MASK
;
5062 offset
= va
& PAGE_MASK
;
5063 size
= roundup(offset
+ size
, PAGE_SIZE
);
5064 pmap_qremove(va
, size
>> PAGE_SHIFT
);
5065 kmem_free(&kernel_map
, base
, size
);
5069 * Sets the memory attribute for the specified page.
5072 pmap_page_set_memattr(vm_page_t m
, vm_memattr_t ma
)
5078 * If "m" is a normal page, update its direct mapping. This update
5079 * can be relied upon to perform any cache operations that are
5080 * required for data coherence.
5082 if ((m
->flags
& PG_FICTITIOUS
) == 0)
5083 pmap_change_attr(PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m
)), 1, m
->pat_mode
);
5087 * Change the PAT attribute on an existing kernel memory map. Caller
5088 * must ensure that the virtual memory in question is not accessed
5089 * during the adjustment.
5092 pmap_change_attr(vm_offset_t va
, vm_size_t count
, int mode
)
5099 panic("pmap_change_attr: va is NULL");
5100 base
= trunc_page(va
);
5104 *pte
= (*pte
& ~(pt_entry_t
)(kernel_pmap
.pmap_cache_mask
)) |
5105 kernel_pmap
.pmap_cache_bits
[mode
];
5110 changed
= 1; /* XXX: not optimal */
5113 * Flush CPU caches if required to make sure any data isn't cached that
5114 * shouldn't be, etc.
5117 pmap_invalidate_range(&kernel_pmap
, base
, va
);
5118 pmap_invalidate_cache_range(base
, va
);
5123 * perform the pmap work for mincore
5126 pmap_mincore(pmap_t pmap
, vm_offset_t addr
)
5128 pt_entry_t
*ptep
, pte
;
5132 lwkt_gettoken(&pmap
->pm_token
);
5133 ptep
= pmap_pte(pmap
, addr
);
5135 if (ptep
&& (pte
= *ptep
) != 0) {
5138 val
= MINCORE_INCORE
;
5139 if ((pte
& pmap
->pmap_bits
[PG_MANAGED_IDX
]) == 0)
5142 pa
= pte
& PG_FRAME
;
5144 if (pte
& pmap
->pmap_bits
[PG_DEVICE_IDX
])
5147 m
= PHYS_TO_VM_PAGE(pa
);
5152 if (pte
& pmap
->pmap_bits
[PG_M_IDX
])
5153 val
|= MINCORE_MODIFIED
|MINCORE_MODIFIED_OTHER
;
5155 * Modified by someone
5157 else if (m
&& (m
->dirty
|| pmap_is_modified(m
)))
5158 val
|= MINCORE_MODIFIED_OTHER
;
5162 if (pte
& pmap
->pmap_bits
[PG_A_IDX
])
5163 val
|= MINCORE_REFERENCED
|MINCORE_REFERENCED_OTHER
;
5166 * Referenced by someone
5168 else if (m
&& ((m
->flags
& PG_REFERENCED
) ||
5169 pmap_ts_referenced(m
))) {
5170 val
|= MINCORE_REFERENCED_OTHER
;
5171 vm_page_flag_set(m
, PG_REFERENCED
);
5175 lwkt_reltoken(&pmap
->pm_token
);
5181 * Replace p->p_vmspace with a new one. If adjrefs is non-zero the new
5182 * vmspace will be ref'd and the old one will be deref'd.
5184 * The vmspace for all lwps associated with the process will be adjusted
5185 * and cr3 will be reloaded if any lwp is the current lwp.
5187 * The process must hold the vmspace->vm_map.token for oldvm and newvm
5190 pmap_replacevm(struct proc
*p
, struct vmspace
*newvm
, int adjrefs
)
5192 struct vmspace
*oldvm
;
5195 oldvm
= p
->p_vmspace
;
5196 if (oldvm
!= newvm
) {
5199 p
->p_vmspace
= newvm
;
5200 KKASSERT(p
->p_nthreads
== 1);
5201 lp
= RB_ROOT(&p
->p_lwp_tree
);
5202 pmap_setlwpvm(lp
, newvm
);
5209 * Set the vmspace for a LWP. The vmspace is almost universally set the
5210 * same as the process vmspace, but virtual kernels need to swap out contexts
5211 * on a per-lwp basis.
5213 * Caller does not necessarily hold any vmspace tokens. Caller must control
5214 * the lwp (typically be in the context of the lwp). We use a critical
5215 * section to protect against statclock and hardclock (statistics collection).
5218 pmap_setlwpvm(struct lwp
*lp
, struct vmspace
*newvm
)
5220 struct vmspace
*oldvm
;
5223 oldvm
= lp
->lwp_vmspace
;
5225 if (oldvm
!= newvm
) {
5227 lp
->lwp_vmspace
= newvm
;
5228 if (curthread
->td_lwp
== lp
) {
5229 pmap
= vmspace_pmap(newvm
);
5230 ATOMIC_CPUMASK_ORBIT(pmap
->pm_active
, mycpu
->gd_cpuid
);
5231 if (pmap
->pm_active_lock
& CPULOCK_EXCL
)
5232 pmap_interlock_wait(newvm
);
5233 #if defined(SWTCH_OPTIM_STATS)
5236 if (pmap
->pmap_bits
[TYPE_IDX
] == REGULAR_PMAP
) {
5237 curthread
->td_pcb
->pcb_cr3
= vtophys(pmap
->pm_pml4
);
5238 } else if (pmap
->pmap_bits
[TYPE_IDX
] == EPT_PMAP
) {
5239 curthread
->td_pcb
->pcb_cr3
= KPML4phys
;
5241 panic("pmap_setlwpvm: unknown pmap type\n");
5243 load_cr3(curthread
->td_pcb
->pcb_cr3
);
5244 pmap
= vmspace_pmap(oldvm
);
5245 ATOMIC_CPUMASK_NANDBIT(pmap
->pm_active
,
5253 * Called when switching to a locked pmap, used to interlock against pmaps
5254 * undergoing modifications to prevent us from activating the MMU for the
5255 * target pmap until all such modifications have completed. We have to do
5256 * this because the thread making the modifications has already set up its
5257 * SMP synchronization mask.
5259 * This function cannot sleep!
5264 pmap_interlock_wait(struct vmspace
*vm
)
5266 struct pmap
*pmap
= &vm
->vm_pmap
;
5268 if (pmap
->pm_active_lock
& CPULOCK_EXCL
) {
5270 KKASSERT(curthread
->td_critcount
>= 2);
5271 DEBUG_PUSH_INFO("pmap_interlock_wait");
5272 while (pmap
->pm_active_lock
& CPULOCK_EXCL
) {
5274 lwkt_process_ipiq();
5282 pmap_addr_hint(vm_object_t obj
, vm_offset_t addr
, vm_size_t size
)
5285 if ((obj
== NULL
) || (size
< NBPDR
) ||
5286 ((obj
->type
!= OBJT_DEVICE
) && (obj
->type
!= OBJT_MGTDEVICE
))) {
5290 addr
= roundup2(addr
, NBPDR
);
5295 * Used by kmalloc/kfree, page already exists at va
5298 pmap_kvtom(vm_offset_t va
)
5300 pt_entry_t
*ptep
= vtopte(va
);
5302 KKASSERT((*ptep
& kernel_pmap
.pmap_bits
[PG_DEVICE_IDX
]) == 0);
5303 return(PHYS_TO_VM_PAGE(*ptep
& PG_FRAME
));
5307 * Initialize machine-specific shared page directory support. This
5308 * is executed when a VM object is created.
5311 pmap_object_init(vm_object_t object
)
5313 object
->md
.pmap_rw
= NULL
;
5314 object
->md
.pmap_ro
= NULL
;
5318 * Clean up machine-specific shared page directory support. This
5319 * is executed when a VM object is destroyed.
5322 pmap_object_free(vm_object_t object
)
5326 if ((pmap
= object
->md
.pmap_rw
) != NULL
) {
5327 object
->md
.pmap_rw
= NULL
;
5328 pmap_remove_noinval(pmap
,
5329 VM_MIN_USER_ADDRESS
, VM_MAX_USER_ADDRESS
);
5330 CPUMASK_ASSZERO(pmap
->pm_active
);
5333 kfree(pmap
, M_OBJPMAP
);
5335 if ((pmap
= object
->md
.pmap_ro
) != NULL
) {
5336 object
->md
.pmap_ro
= NULL
;
5337 pmap_remove_noinval(pmap
,
5338 VM_MIN_USER_ADDRESS
, VM_MAX_USER_ADDRESS
);
5339 CPUMASK_ASSZERO(pmap
->pm_active
);
5342 kfree(pmap
, M_OBJPMAP
);