2 * Copyright (c) 1991 Regents of the University of California.
4 * Copyright (c) 1994 John S. Dyson
6 * Copyright (c) 1994 David Greenman
8 * Copyright (c) 2003 Peter Wemm
10 * Copyright (c) 2005-2010 Alan L. Cox <alc@cs.rice.edu>
11 * All rights reserved.
12 * Copyright (c) 2014 Andrew Turner
13 * All rights reserved.
14 * Copyright (c) 2014-2016 The FreeBSD Foundation
15 * All rights reserved.
17 * This code is derived from software contributed to Berkeley by
18 * the Systems Programming Group of the University of Utah Computer
19 * Science Department and William Jolitz of UUNET Technologies Inc.
21 * This software was developed by Andrew Turner under sponsorship from
22 * the FreeBSD Foundation.
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
27 * 1. Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * 2. Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in the
31 * documentation and/or other materials provided with the distribution.
32 * 3. All advertising materials mentioning features or use of this software
33 * must display the following acknowledgement:
34 * This product includes software developed by the University of
35 * California, Berkeley and its contributors.
36 * 4. Neither the name of the University nor the names of its contributors
37 * may be used to endorse or promote products derived from this software
38 * without specific prior written permission.
40 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * from: @(#)pmap.c 7.7 (Berkeley) 5/12/91
55 * Copyright (c) 2003 Networks Associates Technology, Inc.
56 * All rights reserved.
58 * This software was developed for the FreeBSD Project by Jake Burkholder,
59 * Safeport Network Services, and Network Associates Laboratories, the
60 * Security Research Division of Network Associates, Inc. under
61 * DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA
62 * CHATS research program.
64 * Redistribution and use in source and binary forms, with or without
65 * modification, are permitted provided that the following conditions
67 * 1. Redistributions of source code must retain the above copyright
68 * notice, this list of conditions and the following disclaimer.
69 * 2. Redistributions in binary form must reproduce the above copyright
70 * notice, this list of conditions and the following disclaimer in the
71 * documentation and/or other materials provided with the distribution.
73 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
74 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
75 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
76 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
77 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
78 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
79 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
80 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
81 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
82 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
86 #include <sys/cdefs.h>
87 __FBSDID("$FreeBSD$");
90 * Manages physical address maps.
92 * Since the information managed by this module is
93 * also stored by the logical address mapping module,
94 * this module may throw away valid virtual-to-physical
95 * mappings at almost any time. However, invalidations
96 * of virtual-to-physical mappings must be done as
99 * In order to cope with hardware architectures which
100 * make virtual-to-physical map invalidates expensive,
101 * this module may delay invalidate or reduced protection
102 * operations until such time as they are actually
103 * necessary. This module is given full information as
104 * to which processors are currently using which maps,
105 * and to when physical maps must be made correct.
110 #include <sys/param.h>
111 #include <sys/bitstring.h>
113 #include <sys/systm.h>
114 #include <sys/kernel.h>
116 #include <sys/limits.h>
117 #include <sys/lock.h>
118 #include <sys/malloc.h>
119 #include <sys/mman.h>
120 #include <sys/msgbuf.h>
121 #include <sys/mutex.h>
122 #include <sys/proc.h>
123 #include <sys/rwlock.h>
124 #include <sys/sbuf.h>
126 #include <sys/vmem.h>
127 #include <sys/vmmeter.h>
128 #include <sys/sched.h>
129 #include <sys/sysctl.h>
130 #include <sys/_unrhdr.h>
134 #include <vm/vm_param.h>
135 #include <vm/vm_kern.h>
136 #include <vm/vm_page.h>
137 #include <vm/vm_map.h>
138 #include <vm/vm_object.h>
139 #include <vm/vm_extern.h>
140 #include <vm/vm_pageout.h>
141 #include <vm/vm_pager.h>
142 #include <vm/vm_phys.h>
143 #include <vm/vm_radix.h>
144 #include <vm/vm_reserv.h>
147 #include <machine/machdep.h>
148 #include <machine/md_var.h>
149 #include <machine/pcb.h>
151 #include <arm/include/physmem.h>
153 #define NL0PG (PAGE_SIZE/(sizeof (pd_entry_t)))
154 #define NL1PG (PAGE_SIZE/(sizeof (pd_entry_t)))
155 #define NL2PG (PAGE_SIZE/(sizeof (pd_entry_t)))
156 #define NL3PG (PAGE_SIZE/(sizeof (pt_entry_t)))
158 #define NUL0E L0_ENTRIES
159 #define NUL1E (NUL0E * NL1PG)
160 #define NUL2E (NUL1E * NL2PG)
162 #if !defined(DIAGNOSTIC)
163 #ifdef __GNUC_GNU_INLINE__
164 #define PMAP_INLINE __attribute__((__gnu_inline__)) inline
166 #define PMAP_INLINE extern inline
173 #define PV_STAT(x) do { x ; } while (0)
175 #define PV_STAT(x) do { } while (0)
178 #define pmap_l2_pindex(v) ((v) >> L2_SHIFT)
179 #define pa_to_pvh(pa) (&pv_table[pmap_l2_pindex(pa)])
181 #define NPV_LIST_LOCKS MAXCPU
183 #define PHYS_TO_PV_LIST_LOCK(pa) \
184 (&pv_list_locks[pa_index(pa) % NPV_LIST_LOCKS])
186 #define CHANGE_PV_LIST_LOCK_TO_PHYS(lockp, pa) do { \
187 struct rwlock **_lockp = (lockp); \
188 struct rwlock *_new_lock; \
190 _new_lock = PHYS_TO_PV_LIST_LOCK(pa); \
191 if (_new_lock != *_lockp) { \
192 if (*_lockp != NULL) \
193 rw_wunlock(*_lockp); \
194 *_lockp = _new_lock; \
199 #define CHANGE_PV_LIST_LOCK_TO_VM_PAGE(lockp, m) \
200 CHANGE_PV_LIST_LOCK_TO_PHYS(lockp, VM_PAGE_TO_PHYS(m))
202 #define RELEASE_PV_LIST_LOCK(lockp) do { \
203 struct rwlock **_lockp = (lockp); \
205 if (*_lockp != NULL) { \
206 rw_wunlock(*_lockp); \
211 #define VM_PAGE_TO_PV_LIST_LOCK(m) \
212 PHYS_TO_PV_LIST_LOCK(VM_PAGE_TO_PHYS(m))
215 * The presence of this flag indicates that the mapping is writeable.
216 * If the ATTR_AP_RO bit is also set, then the mapping is clean, otherwise it is
217 * dirty. This flag may only be set on managed mappings.
219 * The DBM bit is reserved on ARMv8.0 but it seems we can safely treat it
220 * as a software managed bit.
222 #define ATTR_SW_DBM ATTR_DBM
224 struct pmap kernel_pmap_store;
226 /* Used for mapping ACPI memory before VM is initialized */
227 #define PMAP_PREINIT_MAPPING_COUNT 32
228 #define PMAP_PREINIT_MAPPING_SIZE (PMAP_PREINIT_MAPPING_COUNT * L2_SIZE)
229 static vm_offset_t preinit_map_va; /* Start VA of pre-init mapping space */
230 static int vm_initialized = 0; /* No need to use pre-init maps when set */
233 * Reserve a few L2 blocks starting from 'preinit_map_va' pointer.
234 * Always map entire L2 block for simplicity.
235 * VA of L2 block = preinit_map_va + i * L2_SIZE
237 static struct pmap_preinit_mapping {
241 } pmap_preinit_mapping[PMAP_PREINIT_MAPPING_COUNT];
243 vm_offset_t virtual_avail; /* VA of first avail page (after kernel bss) */
244 vm_offset_t virtual_end; /* VA of last avail page (end of kernel AS) */
245 vm_offset_t kernel_vm_end = 0;
248 * Data for the pv entry allocation mechanism.
250 static TAILQ_HEAD(pch, pv_chunk) pv_chunks = TAILQ_HEAD_INITIALIZER(pv_chunks);
251 static struct mtx pv_chunks_mutex;
252 static struct rwlock pv_list_locks[NPV_LIST_LOCKS];
253 static struct md_page *pv_table;
254 static struct md_page pv_dummy;
256 vm_paddr_t dmap_phys_base; /* The start of the dmap region */
257 vm_paddr_t dmap_phys_max; /* The limit of the dmap region */
258 vm_offset_t dmap_max_addr; /* The virtual address limit of the dmap */
260 /* This code assumes all L1 DMAP entries will be used */
261 CTASSERT((DMAP_MIN_ADDRESS & ~L0_OFFSET) == DMAP_MIN_ADDRESS);
262 CTASSERT((DMAP_MAX_ADDRESS & ~L0_OFFSET) == DMAP_MAX_ADDRESS);
264 #define DMAP_TABLES ((DMAP_MAX_ADDRESS - DMAP_MIN_ADDRESS) >> L0_SHIFT)
265 extern pt_entry_t pagetable_dmap[];
267 #define PHYSMAP_SIZE (2 * (VM_PHYSSEG_MAX - 1))
268 static vm_paddr_t physmap[PHYSMAP_SIZE];
269 static u_int physmap_idx;
271 static SYSCTL_NODE(_vm, OID_AUTO, pmap, CTLFLAG_RD, 0, "VM/pmap parameters");
274 * This ASID allocator uses a bit vector ("asid_set") to remember which ASIDs
275 * that it has currently allocated to a pmap, a cursor ("asid_next") to
276 * optimize its search for a free ASID in the bit vector, and an epoch number
277 * ("asid_epoch") to indicate when it has reclaimed all previously allocated
278 * ASIDs that are not currently active on a processor.
280 * The current epoch number is always in the range [0, INT_MAX). Negative
281 * numbers and INT_MAX are reserved for special cases that are described
284 static SYSCTL_NODE(_vm_pmap, OID_AUTO, asid, CTLFLAG_RD, 0, "ASID allocator");
285 static int asid_bits;
286 SYSCTL_INT(_vm_pmap_asid, OID_AUTO, bits, CTLFLAG_RD, &asid_bits, 0,
287 "The number of bits in an ASID");
288 static bitstr_t *asid_set;
289 static int asid_set_size;
290 static int asid_next;
291 SYSCTL_INT(_vm_pmap_asid, OID_AUTO, next, CTLFLAG_RD, &asid_next, 0,
292 "The last allocated ASID plus one");
293 static int asid_epoch;
294 SYSCTL_INT(_vm_pmap_asid, OID_AUTO, epoch, CTLFLAG_RD, &asid_epoch, 0,
295 "The current epoch number");
296 static struct mtx asid_set_mutex;
299 * A pmap's cookie encodes an ASID and epoch number. Cookies for reserved
300 * ASIDs have a negative epoch number, specifically, INT_MIN. Cookies for
301 * dynamically allocated ASIDs have a non-negative epoch number.
303 * An invalid ASID is represented by -1.
305 * There are two special-case cookie values: (1) COOKIE_FROM(-1, INT_MIN),
306 * which indicates that an ASID should never be allocated to the pmap, and
307 * (2) COOKIE_FROM(-1, INT_MAX), which indicates that an ASID should be
308 * allocated when the pmap is next activated.
310 #define COOKIE_FROM(asid, epoch) ((long)((u_int)(asid) | \
311 ((u_long)(epoch) << 32)))
312 #define COOKIE_TO_ASID(cookie) ((int)(cookie))
313 #define COOKIE_TO_EPOCH(cookie) ((int)((u_long)(cookie) >> 32))
315 static int superpages_enabled = 1;
316 SYSCTL_INT(_vm_pmap, OID_AUTO, superpages_enabled,
317 CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &superpages_enabled, 0,
318 "Are large page mappings enabled?");
321 * Internal flags for pmap_enter()'s helper functions.
323 #define PMAP_ENTER_NORECLAIM 0x1000000 /* Don't reclaim PV entries. */
324 #define PMAP_ENTER_NOREPLACE 0x2000000 /* Don't replace mappings. */
326 static void free_pv_chunk(struct pv_chunk *pc);
327 static void free_pv_entry(pmap_t pmap, pv_entry_t pv);
328 static pv_entry_t get_pv_entry(pmap_t pmap, struct rwlock **lockp);
329 static vm_page_t reclaim_pv_chunk(pmap_t locked_pmap, struct rwlock **lockp);
330 static void pmap_pvh_free(struct md_page *pvh, pmap_t pmap, vm_offset_t va);
331 static pv_entry_t pmap_pvh_remove(struct md_page *pvh, pmap_t pmap,
334 static void pmap_abort_ptp(pmap_t pmap, vm_offset_t va, vm_page_t mpte);
335 static bool pmap_activate_int(pmap_t pmap);
336 static void pmap_alloc_asid(pmap_t pmap);
337 static int pmap_change_attr_locked(vm_offset_t va, vm_size_t size, int mode);
338 static pt_entry_t *pmap_demote_l1(pmap_t pmap, pt_entry_t *l1, vm_offset_t va);
339 static pt_entry_t *pmap_demote_l2_locked(pmap_t pmap, pt_entry_t *l2,
340 vm_offset_t va, struct rwlock **lockp);
341 static pt_entry_t *pmap_demote_l2(pmap_t pmap, pt_entry_t *l2, vm_offset_t va);
342 static vm_page_t pmap_enter_quick_locked(pmap_t pmap, vm_offset_t va,
343 vm_page_t m, vm_prot_t prot, vm_page_t mpte, struct rwlock **lockp);
344 static int pmap_enter_l2(pmap_t pmap, vm_offset_t va, pd_entry_t new_l2,
345 u_int flags, vm_page_t m, struct rwlock **lockp);
346 static int pmap_remove_l2(pmap_t pmap, pt_entry_t *l2, vm_offset_t sva,
347 pd_entry_t l1e, struct spglist *free, struct rwlock **lockp);
348 static int pmap_remove_l3(pmap_t pmap, pt_entry_t *l3, vm_offset_t sva,
349 pd_entry_t l2e, struct spglist *free, struct rwlock **lockp);
350 static void pmap_reset_asid_set(void);
351 static boolean_t pmap_try_insert_pv_entry(pmap_t pmap, vm_offset_t va,
352 vm_page_t m, struct rwlock **lockp);
354 static vm_page_t _pmap_alloc_l3(pmap_t pmap, vm_pindex_t ptepindex,
355 struct rwlock **lockp);
357 static void _pmap_unwire_l3(pmap_t pmap, vm_offset_t va, vm_page_t m,
358 struct spglist *free);
359 static int pmap_unuse_pt(pmap_t, vm_offset_t, pd_entry_t, struct spglist *);
360 static __inline vm_page_t pmap_remove_pt_page(pmap_t pmap, vm_offset_t va);
363 * These load the old table data and store the new value.
364 * They need to be atomic as the System MMU may write to the table at
365 * the same time as the CPU.
367 #define pmap_clear(table) atomic_store_64(table, 0)
368 #define pmap_clear_bits(table, bits) atomic_clear_64(table, bits)
369 #define pmap_load(table) (*table)
370 #define pmap_load_clear(table) atomic_swap_64(table, 0)
371 #define pmap_load_store(table, entry) atomic_swap_64(table, entry)
372 #define pmap_set_bits(table, bits) atomic_set_64(table, bits)
373 #define pmap_store(table, entry) atomic_store_64(table, entry)
375 /********************/
376 /* Inline functions */
377 /********************/
380 pagecopy(void *s, void *d)
383 memcpy(d, s, PAGE_SIZE);
386 static __inline pd_entry_t *
387 pmap_l0(pmap_t pmap, vm_offset_t va)
390 return (&pmap->pm_l0[pmap_l0_index(va)]);
393 static __inline pd_entry_t *
394 pmap_l0_to_l1(pd_entry_t *l0, vm_offset_t va)
398 l1 = (pd_entry_t *)PHYS_TO_DMAP(pmap_load(l0) & ~ATTR_MASK);
399 return (&l1[pmap_l1_index(va)]);
402 static __inline pd_entry_t *
403 pmap_l1(pmap_t pmap, vm_offset_t va)
407 l0 = pmap_l0(pmap, va);
408 if ((pmap_load(l0) & ATTR_DESCR_MASK) != L0_TABLE)
411 return (pmap_l0_to_l1(l0, va));
414 static __inline pd_entry_t *
415 pmap_l1_to_l2(pd_entry_t *l1, vm_offset_t va)
419 l2 = (pd_entry_t *)PHYS_TO_DMAP(pmap_load(l1) & ~ATTR_MASK);
420 return (&l2[pmap_l2_index(va)]);
423 static __inline pd_entry_t *
424 pmap_l2(pmap_t pmap, vm_offset_t va)
428 l1 = pmap_l1(pmap, va);
429 if ((pmap_load(l1) & ATTR_DESCR_MASK) != L1_TABLE)
432 return (pmap_l1_to_l2(l1, va));
435 static __inline pt_entry_t *
436 pmap_l2_to_l3(pd_entry_t *l2, vm_offset_t va)
440 l3 = (pd_entry_t *)PHYS_TO_DMAP(pmap_load(l2) & ~ATTR_MASK);
441 return (&l3[pmap_l3_index(va)]);
445 * Returns the lowest valid pde for a given virtual address.
446 * The next level may or may not point to a valid page or block.
448 static __inline pd_entry_t *
449 pmap_pde(pmap_t pmap, vm_offset_t va, int *level)
451 pd_entry_t *l0, *l1, *l2, desc;
453 l0 = pmap_l0(pmap, va);
454 desc = pmap_load(l0) & ATTR_DESCR_MASK;
455 if (desc != L0_TABLE) {
460 l1 = pmap_l0_to_l1(l0, va);
461 desc = pmap_load(l1) & ATTR_DESCR_MASK;
462 if (desc != L1_TABLE) {
467 l2 = pmap_l1_to_l2(l1, va);
468 desc = pmap_load(l2) & ATTR_DESCR_MASK;
469 if (desc != L2_TABLE) {
479 * Returns the lowest valid pte block or table entry for a given virtual
480 * address. If there are no valid entries return NULL and set the level to
481 * the first invalid level.
483 static __inline pt_entry_t *
484 pmap_pte(pmap_t pmap, vm_offset_t va, int *level)
486 pd_entry_t *l1, *l2, desc;
489 l1 = pmap_l1(pmap, va);
494 desc = pmap_load(l1) & ATTR_DESCR_MASK;
495 if (desc == L1_BLOCK) {
500 if (desc != L1_TABLE) {
505 l2 = pmap_l1_to_l2(l1, va);
506 desc = pmap_load(l2) & ATTR_DESCR_MASK;
507 if (desc == L2_BLOCK) {
512 if (desc != L2_TABLE) {
518 l3 = pmap_l2_to_l3(l2, va);
519 if ((pmap_load(l3) & ATTR_DESCR_MASK) != L3_PAGE)
526 pmap_ps_enabled(pmap_t pmap __unused)
529 return (superpages_enabled != 0);
533 pmap_get_tables(pmap_t pmap, vm_offset_t va, pd_entry_t **l0, pd_entry_t **l1,
534 pd_entry_t **l2, pt_entry_t **l3)
536 pd_entry_t *l0p, *l1p, *l2p;
538 if (pmap->pm_l0 == NULL)
541 l0p = pmap_l0(pmap, va);
544 if ((pmap_load(l0p) & ATTR_DESCR_MASK) != L0_TABLE)
547 l1p = pmap_l0_to_l1(l0p, va);
550 if ((pmap_load(l1p) & ATTR_DESCR_MASK) == L1_BLOCK) {
556 if ((pmap_load(l1p) & ATTR_DESCR_MASK) != L1_TABLE)
559 l2p = pmap_l1_to_l2(l1p, va);
562 if ((pmap_load(l2p) & ATTR_DESCR_MASK) == L2_BLOCK) {
567 if ((pmap_load(l2p) & ATTR_DESCR_MASK) != L2_TABLE)
570 *l3 = pmap_l2_to_l3(l2p, va);
576 pmap_l3_valid(pt_entry_t l3)
579 return ((l3 & ATTR_DESCR_MASK) == L3_PAGE);
583 CTASSERT(L1_BLOCK == L2_BLOCK);
586 * Checks if the PTE is dirty.
589 pmap_pte_dirty(pt_entry_t pte)
592 KASSERT((pte & ATTR_SW_MANAGED) != 0, ("pte %#lx is unmanaged", pte));
593 KASSERT((pte & (ATTR_AP_RW_BIT | ATTR_SW_DBM)) != 0,
594 ("pte %#lx is writeable and missing ATTR_SW_DBM", pte));
596 return ((pte & (ATTR_AP_RW_BIT | ATTR_SW_DBM)) ==
597 (ATTR_AP(ATTR_AP_RW) | ATTR_SW_DBM));
601 pmap_resident_count_inc(pmap_t pmap, int count)
604 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
605 pmap->pm_stats.resident_count += count;
609 pmap_resident_count_dec(pmap_t pmap, int count)
612 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
613 KASSERT(pmap->pm_stats.resident_count >= count,
614 ("pmap %p resident count underflow %ld %d", pmap,
615 pmap->pm_stats.resident_count, count));
616 pmap->pm_stats.resident_count -= count;
620 pmap_early_page_idx(vm_offset_t l1pt, vm_offset_t va, u_int *l1_slot,
626 l1 = (pd_entry_t *)l1pt;
627 *l1_slot = (va >> L1_SHIFT) & Ln_ADDR_MASK;
629 /* Check locore has used a table L1 map */
630 KASSERT((l1[*l1_slot] & ATTR_DESCR_MASK) == L1_TABLE,
631 ("Invalid bootstrap L1 table"));
632 /* Find the address of the L2 table */
633 l2 = (pt_entry_t *)init_pt_va;
634 *l2_slot = pmap_l2_index(va);
640 pmap_early_vtophys(vm_offset_t l1pt, vm_offset_t va)
642 u_int l1_slot, l2_slot;
645 l2 = pmap_early_page_idx(l1pt, va, &l1_slot, &l2_slot);
647 return ((l2[l2_slot] & ~ATTR_MASK) + (va & L2_OFFSET));
651 pmap_bootstrap_dmap(vm_offset_t kern_l1, vm_paddr_t min_pa,
652 vm_offset_t freemempos)
656 vm_paddr_t l2_pa, pa;
657 u_int l1_slot, l2_slot, prev_l1_slot;
660 dmap_phys_base = min_pa & ~L1_OFFSET;
666 #define DMAP_TABLES ((DMAP_MAX_ADDRESS - DMAP_MIN_ADDRESS) >> L0_SHIFT)
667 memset(pagetable_dmap, 0, PAGE_SIZE * DMAP_TABLES);
669 for (i = 0; i < (physmap_idx * 2); i += 2) {
670 pa = physmap[i] & ~L2_OFFSET;
671 va = pa - dmap_phys_base + DMAP_MIN_ADDRESS;
673 /* Create L2 mappings at the start of the region */
674 if ((pa & L1_OFFSET) != 0) {
675 l1_slot = ((va - DMAP_MIN_ADDRESS) >> L1_SHIFT);
676 if (l1_slot != prev_l1_slot) {
677 prev_l1_slot = l1_slot;
678 l2 = (pt_entry_t *)freemempos;
679 l2_pa = pmap_early_vtophys(kern_l1,
681 freemempos += PAGE_SIZE;
683 pmap_store(&pagetable_dmap[l1_slot],
684 (l2_pa & ~Ln_TABLE_MASK) | L1_TABLE);
686 memset(l2, 0, PAGE_SIZE);
689 ("pmap_bootstrap_dmap: NULL l2 map"));
690 for (; va < DMAP_MAX_ADDRESS && pa < physmap[i + 1];
691 pa += L2_SIZE, va += L2_SIZE) {
693 * We are on a boundary, stop to
694 * create a level 1 block
696 if ((pa & L1_OFFSET) == 0)
699 l2_slot = pmap_l2_index(va);
700 KASSERT(l2_slot != 0, ("..."));
701 pmap_store(&l2[l2_slot],
702 (pa & ~L2_OFFSET) | ATTR_DEFAULT | ATTR_XN |
703 ATTR_IDX(VM_MEMATTR_WRITE_BACK) | L2_BLOCK);
705 KASSERT(va == (pa - dmap_phys_base + DMAP_MIN_ADDRESS),
709 for (; va < DMAP_MAX_ADDRESS && pa < physmap[i + 1] &&
710 (physmap[i + 1] - pa) >= L1_SIZE;
711 pa += L1_SIZE, va += L1_SIZE) {
712 l1_slot = ((va - DMAP_MIN_ADDRESS) >> L1_SHIFT);
713 pmap_store(&pagetable_dmap[l1_slot],
714 (pa & ~L1_OFFSET) | ATTR_DEFAULT | ATTR_XN |
715 ATTR_IDX(VM_MEMATTR_WRITE_BACK) | L1_BLOCK);
718 /* Create L2 mappings at the end of the region */
719 if (pa < physmap[i + 1]) {
720 l1_slot = ((va - DMAP_MIN_ADDRESS) >> L1_SHIFT);
721 if (l1_slot != prev_l1_slot) {
722 prev_l1_slot = l1_slot;
723 l2 = (pt_entry_t *)freemempos;
724 l2_pa = pmap_early_vtophys(kern_l1,
726 freemempos += PAGE_SIZE;
728 pmap_store(&pagetable_dmap[l1_slot],
729 (l2_pa & ~Ln_TABLE_MASK) | L1_TABLE);
731 memset(l2, 0, PAGE_SIZE);
734 ("pmap_bootstrap_dmap: NULL l2 map"));
735 for (; va < DMAP_MAX_ADDRESS && pa < physmap[i + 1];
736 pa += L2_SIZE, va += L2_SIZE) {
737 l2_slot = pmap_l2_index(va);
738 pmap_store(&l2[l2_slot],
739 (pa & ~L2_OFFSET) | ATTR_DEFAULT | ATTR_XN |
740 ATTR_IDX(VM_MEMATTR_WRITE_BACK) | L2_BLOCK);
744 if (pa > dmap_phys_max) {
756 pmap_bootstrap_l2(vm_offset_t l1pt, vm_offset_t va, vm_offset_t l2_start)
763 KASSERT((va & L1_OFFSET) == 0, ("Invalid virtual address"));
765 l1 = (pd_entry_t *)l1pt;
766 l1_slot = pmap_l1_index(va);
769 for (; va < VM_MAX_KERNEL_ADDRESS; l1_slot++, va += L1_SIZE) {
770 KASSERT(l1_slot < Ln_ENTRIES, ("Invalid L1 index"));
772 pa = pmap_early_vtophys(l1pt, l2pt);
773 pmap_store(&l1[l1_slot],
774 (pa & ~Ln_TABLE_MASK) | L1_TABLE);
778 /* Clean the L2 page table */
779 memset((void *)l2_start, 0, l2pt - l2_start);
785 pmap_bootstrap_l3(vm_offset_t l1pt, vm_offset_t va, vm_offset_t l3_start)
792 KASSERT((va & L2_OFFSET) == 0, ("Invalid virtual address"));
794 l2 = pmap_l2(kernel_pmap, va);
795 l2 = (pd_entry_t *)rounddown2((uintptr_t)l2, PAGE_SIZE);
796 l2_slot = pmap_l2_index(va);
799 for (; va < VM_MAX_KERNEL_ADDRESS; l2_slot++, va += L2_SIZE) {
800 KASSERT(l2_slot < Ln_ENTRIES, ("Invalid L2 index"));
802 pa = pmap_early_vtophys(l1pt, l3pt);
803 pmap_store(&l2[l2_slot],
804 (pa & ~Ln_TABLE_MASK) | ATTR_UXN | L2_TABLE);
808 /* Clean the L2 page table */
809 memset((void *)l3_start, 0, l3pt - l3_start);
815 * Bootstrap the system enough to run with virtual memory.
818 pmap_bootstrap(vm_offset_t l0pt, vm_offset_t l1pt, vm_paddr_t kernstart,
821 u_int l1_slot, l2_slot;
823 vm_offset_t va, freemempos;
824 vm_offset_t dpcpu, msgbufpv;
825 vm_paddr_t start_pa, pa, min_pa;
829 /* Verify that the ASID is set through TTBR0. */
830 KASSERT((READ_SPECIALREG(tcr_el1) & TCR_A1) == 0,
831 ("pmap_bootstrap: TCR_EL1.A1 != 0"));
833 kern_delta = KERNBASE - kernstart;
835 printf("pmap_bootstrap %lx %lx %lx\n", l1pt, kernstart, kernlen);
836 printf("%lx\n", l1pt);
837 printf("%lx\n", (KERNBASE >> L1_SHIFT) & Ln_ADDR_MASK);
839 /* Set this early so we can use the pagetable walking functions */
840 kernel_pmap_store.pm_l0 = (pd_entry_t *)l0pt;
841 PMAP_LOCK_INIT(kernel_pmap);
842 kernel_pmap->pm_l0_paddr = l0pt - kern_delta;
843 kernel_pmap->pm_cookie = COOKIE_FROM(-1, INT_MIN);
845 /* Assume the address we were loaded to is a valid physical address */
846 min_pa = KERNBASE - kern_delta;
848 physmap_idx = arm_physmem_avail(physmap, nitems(physmap));
852 * Find the minimum physical address. physmap is sorted,
853 * but may contain empty ranges.
855 for (i = 0; i < (physmap_idx * 2); i += 2) {
856 if (physmap[i] == physmap[i + 1])
858 if (physmap[i] <= min_pa)
862 freemempos = KERNBASE + kernlen;
863 freemempos = roundup2(freemempos, PAGE_SIZE);
865 /* Create a direct map region early so we can use it for pa -> va */
866 freemempos = pmap_bootstrap_dmap(l1pt, min_pa, freemempos);
869 start_pa = pa = KERNBASE - kern_delta;
872 * Read the page table to find out what is already mapped.
873 * This assumes we have mapped a block of memory from KERNBASE
874 * using a single L1 entry.
876 l2 = pmap_early_page_idx(l1pt, KERNBASE, &l1_slot, &l2_slot);
878 /* Sanity check the index, KERNBASE should be the first VA */
879 KASSERT(l2_slot == 0, ("The L2 index is non-zero"));
881 /* Find how many pages we have mapped */
882 for (; l2_slot < Ln_ENTRIES; l2_slot++) {
883 if ((l2[l2_slot] & ATTR_DESCR_MASK) == 0)
886 /* Check locore used L2 blocks */
887 KASSERT((l2[l2_slot] & ATTR_DESCR_MASK) == L2_BLOCK,
888 ("Invalid bootstrap L2 table"));
889 KASSERT((l2[l2_slot] & ~ATTR_MASK) == pa,
890 ("Incorrect PA in L2 table"));
896 va = roundup2(va, L1_SIZE);
898 /* Create the l2 tables up to VM_MAX_KERNEL_ADDRESS */
899 freemempos = pmap_bootstrap_l2(l1pt, va, freemempos);
900 /* And the l3 tables for the early devmap */
901 freemempos = pmap_bootstrap_l3(l1pt,
902 VM_MAX_KERNEL_ADDRESS - (PMAP_MAPDEV_EARLY_SIZE), freemempos);
906 #define alloc_pages(var, np) \
907 (var) = freemempos; \
908 freemempos += (np * PAGE_SIZE); \
909 memset((char *)(var), 0, ((np) * PAGE_SIZE));
911 /* Allocate dynamic per-cpu area. */
912 alloc_pages(dpcpu, DPCPU_SIZE / PAGE_SIZE);
913 dpcpu_init((void *)dpcpu, 0);
915 /* Allocate memory for the msgbuf, e.g. for /sbin/dmesg */
916 alloc_pages(msgbufpv, round_page(msgbufsize) / PAGE_SIZE);
917 msgbufp = (void *)msgbufpv;
919 /* Reserve some VA space for early BIOS/ACPI mapping */
920 preinit_map_va = roundup2(freemempos, L2_SIZE);
922 virtual_avail = preinit_map_va + PMAP_PREINIT_MAPPING_SIZE;
923 virtual_avail = roundup2(virtual_avail, L1_SIZE);
924 virtual_end = VM_MAX_KERNEL_ADDRESS - (PMAP_MAPDEV_EARLY_SIZE);
925 kernel_vm_end = virtual_avail;
927 pa = pmap_early_vtophys(l1pt, freemempos);
929 arm_physmem_exclude_region(start_pa, pa - start_pa, EXFLAG_NOALLOC);
935 * Initialize a vm_page's machine-dependent fields.
938 pmap_page_init(vm_page_t m)
941 TAILQ_INIT(&m->md.pv_list);
942 m->md.pv_memattr = VM_MEMATTR_WRITE_BACK;
946 * Initialize the pmap module.
947 * Called by vm_init, to initialize any structures that the pmap
948 * system needs to map virtual memory.
957 * Determine whether an ASID is 8 or 16 bits in size.
959 asid_bits = (READ_SPECIALREG(tcr_el1) & TCR_ASID_16) != 0 ? 16 : 8;
962 * Are large page mappings enabled?
964 TUNABLE_INT_FETCH("vm.pmap.superpages_enabled", &superpages_enabled);
965 if (superpages_enabled) {
966 KASSERT(MAXPAGESIZES > 1 && pagesizes[1] == 0,
967 ("pmap_init: can't assign to pagesizes[1]"));
968 pagesizes[1] = L2_SIZE;
972 * Initialize the ASID allocator. At this point, we are still too
973 * early in the overall initialization process to use bit_alloc().
975 asid_set_size = 1 << asid_bits;
976 asid_set = (bitstr_t *)kmem_malloc(bitstr_size(asid_set_size),
978 for (i = 0; i < ASID_FIRST_AVAILABLE; i++)
979 bit_set(asid_set, i);
980 asid_next = ASID_FIRST_AVAILABLE;
981 mtx_init(&asid_set_mutex, "asid set", NULL, MTX_SPIN);
984 * Initialize the pv chunk list mutex.
986 mtx_init(&pv_chunks_mutex, "pmap pv chunk list", NULL, MTX_DEF);
989 * Initialize the pool of pv list locks.
991 for (i = 0; i < NPV_LIST_LOCKS; i++)
992 rw_init(&pv_list_locks[i], "pmap pv list");
995 * Calculate the size of the pv head table for superpages.
997 pv_npg = howmany(vm_phys_segs[vm_phys_nsegs - 1].end, L2_SIZE);
1000 * Allocate memory for the pv head table for superpages.
1002 s = (vm_size_t)(pv_npg * sizeof(struct md_page));
1004 pv_table = (struct md_page *)kmem_malloc(s, M_WAITOK | M_ZERO);
1005 for (i = 0; i < pv_npg; i++)
1006 TAILQ_INIT(&pv_table[i].pv_list);
1007 TAILQ_INIT(&pv_dummy.pv_list);
1012 static SYSCTL_NODE(_vm_pmap, OID_AUTO, l2, CTLFLAG_RD, 0,
1013 "2MB page mapping counters");
1015 static u_long pmap_l2_demotions;
1016 SYSCTL_ULONG(_vm_pmap_l2, OID_AUTO, demotions, CTLFLAG_RD,
1017 &pmap_l2_demotions, 0, "2MB page demotions");
1019 static u_long pmap_l2_mappings;
1020 SYSCTL_ULONG(_vm_pmap_l2, OID_AUTO, mappings, CTLFLAG_RD,
1021 &pmap_l2_mappings, 0, "2MB page mappings");
1023 static u_long pmap_l2_p_failures;
1024 SYSCTL_ULONG(_vm_pmap_l2, OID_AUTO, p_failures, CTLFLAG_RD,
1025 &pmap_l2_p_failures, 0, "2MB page promotion failures");
1027 static u_long pmap_l2_promotions;
1028 SYSCTL_ULONG(_vm_pmap_l2, OID_AUTO, promotions, CTLFLAG_RD,
1029 &pmap_l2_promotions, 0, "2MB page promotions");
1032 * Invalidate a single TLB entry.
1034 static __inline void
1035 pmap_invalidate_page(pmap_t pmap, vm_offset_t va)
1040 if (pmap == kernel_pmap) {
1042 __asm __volatile("tlbi vaae1is, %0" : : "r" (r));
1044 r = ASID_TO_OPERAND(COOKIE_TO_ASID(pmap->pm_cookie)) | atop(va);
1045 __asm __volatile("tlbi vae1is, %0" : : "r" (r));
1051 static __inline void
1052 pmap_invalidate_range(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
1054 uint64_t end, r, start;
1057 if (pmap == kernel_pmap) {
1060 for (r = start; r < end; r++)
1061 __asm __volatile("tlbi vaae1is, %0" : : "r" (r));
1063 start = end = ASID_TO_OPERAND(COOKIE_TO_ASID(pmap->pm_cookie));
1066 for (r = start; r < end; r++)
1067 __asm __volatile("tlbi vae1is, %0" : : "r" (r));
1073 static __inline void
1074 pmap_invalidate_all(pmap_t pmap)
1079 if (pmap == kernel_pmap) {
1080 __asm __volatile("tlbi vmalle1is");
1082 r = ASID_TO_OPERAND(COOKIE_TO_ASID(pmap->pm_cookie));
1083 __asm __volatile("tlbi aside1is, %0" : : "r" (r));
1090 * Routine: pmap_extract
1092 * Extract the physical page address associated
1093 * with the given map/virtual_address pair.
1096 pmap_extract(pmap_t pmap, vm_offset_t va)
1098 pt_entry_t *pte, tpte;
1105 * Find the block or page map for this virtual address. pmap_pte
1106 * will return either a valid block/page entry, or NULL.
1108 pte = pmap_pte(pmap, va, &lvl);
1110 tpte = pmap_load(pte);
1111 pa = tpte & ~ATTR_MASK;
1114 KASSERT((tpte & ATTR_DESCR_MASK) == L1_BLOCK,
1115 ("pmap_extract: Invalid L1 pte found: %lx",
1116 tpte & ATTR_DESCR_MASK));
1117 pa |= (va & L1_OFFSET);
1120 KASSERT((tpte & ATTR_DESCR_MASK) == L2_BLOCK,
1121 ("pmap_extract: Invalid L2 pte found: %lx",
1122 tpte & ATTR_DESCR_MASK));
1123 pa |= (va & L2_OFFSET);
1126 KASSERT((tpte & ATTR_DESCR_MASK) == L3_PAGE,
1127 ("pmap_extract: Invalid L3 pte found: %lx",
1128 tpte & ATTR_DESCR_MASK));
1129 pa |= (va & L3_OFFSET);
1138 * Routine: pmap_extract_and_hold
1140 * Atomically extract and hold the physical page
1141 * with the given pmap and virtual address pair
1142 * if that mapping permits the given protection.
1145 pmap_extract_and_hold(pmap_t pmap, vm_offset_t va, vm_prot_t prot)
1147 pt_entry_t *pte, tpte;
1154 pte = pmap_pte(pmap, va, &lvl);
1156 tpte = pmap_load(pte);
1158 KASSERT(lvl > 0 && lvl <= 3,
1159 ("pmap_extract_and_hold: Invalid level %d", lvl));
1160 CTASSERT(L1_BLOCK == L2_BLOCK);
1161 KASSERT((lvl == 3 && (tpte & ATTR_DESCR_MASK) == L3_PAGE) ||
1162 (lvl < 3 && (tpte & ATTR_DESCR_MASK) == L1_BLOCK),
1163 ("pmap_extract_and_hold: Invalid pte at L%d: %lx", lvl,
1164 tpte & ATTR_DESCR_MASK));
1165 if (((tpte & ATTR_AP_RW_BIT) == ATTR_AP(ATTR_AP_RW)) ||
1166 ((prot & VM_PROT_WRITE) == 0)) {
1169 off = va & L1_OFFSET;
1172 off = va & L2_OFFSET;
1178 m = PHYS_TO_VM_PAGE((tpte & ~ATTR_MASK) | off);
1179 if (!vm_page_wire_mapped(m))
1188 pmap_kextract(vm_offset_t va)
1190 pt_entry_t *pte, tpte;
1192 if (va >= DMAP_MIN_ADDRESS && va < DMAP_MAX_ADDRESS)
1193 return (DMAP_TO_PHYS(va));
1194 pte = pmap_l1(kernel_pmap, va);
1199 * A concurrent pmap_update_entry() will clear the entry's valid bit
1200 * but leave the rest of the entry unchanged. Therefore, we treat a
1201 * non-zero entry as being valid, and we ignore the valid bit when
1202 * determining whether the entry maps a block, page, or table.
1204 tpte = pmap_load(pte);
1207 if ((tpte & ATTR_DESCR_TYPE_MASK) == ATTR_DESCR_TYPE_BLOCK)
1208 return ((tpte & ~ATTR_MASK) | (va & L1_OFFSET));
1209 pte = pmap_l1_to_l2(&tpte, va);
1210 tpte = pmap_load(pte);
1213 if ((tpte & ATTR_DESCR_TYPE_MASK) == ATTR_DESCR_TYPE_BLOCK)
1214 return ((tpte & ~ATTR_MASK) | (va & L2_OFFSET));
1215 pte = pmap_l2_to_l3(&tpte, va);
1216 tpte = pmap_load(pte);
1219 return ((tpte & ~ATTR_MASK) | (va & L3_OFFSET));
1222 /***************************************************
1223 * Low level mapping routines.....
1224 ***************************************************/
1227 pmap_kenter(vm_offset_t sva, vm_size_t size, vm_paddr_t pa, int mode)
1230 pt_entry_t *pte, attr;
1234 KASSERT((pa & L3_OFFSET) == 0,
1235 ("pmap_kenter: Invalid physical address"));
1236 KASSERT((sva & L3_OFFSET) == 0,
1237 ("pmap_kenter: Invalid virtual address"));
1238 KASSERT((size & PAGE_MASK) == 0,
1239 ("pmap_kenter: Mapping is not page-sized"));
1241 attr = ATTR_DEFAULT | ATTR_AP(ATTR_AP_RW) | ATTR_XN | ATTR_IDX(mode) |
1245 pde = pmap_pde(kernel_pmap, va, &lvl);
1246 KASSERT(pde != NULL,
1247 ("pmap_kenter: Invalid page entry, va: 0x%lx", va));
1248 KASSERT(lvl == 2, ("pmap_kenter: Invalid level %d", lvl));
1250 pte = pmap_l2_to_l3(pde, va);
1251 pmap_load_store(pte, (pa & ~L3_OFFSET) | attr);
1257 pmap_invalidate_range(kernel_pmap, sva, va);
1261 pmap_kenter_device(vm_offset_t sva, vm_size_t size, vm_paddr_t pa)
1264 pmap_kenter(sva, size, pa, VM_MEMATTR_DEVICE);
1268 * Remove a page from the kernel pagetables.
1271 pmap_kremove(vm_offset_t va)
1276 pte = pmap_pte(kernel_pmap, va, &lvl);
1277 KASSERT(pte != NULL, ("pmap_kremove: Invalid address"));
1278 KASSERT(lvl == 3, ("pmap_kremove: Invalid pte level %d", lvl));
1281 pmap_invalidate_page(kernel_pmap, va);
1285 pmap_kremove_device(vm_offset_t sva, vm_size_t size)
1291 KASSERT((sva & L3_OFFSET) == 0,
1292 ("pmap_kremove_device: Invalid virtual address"));
1293 KASSERT((size & PAGE_MASK) == 0,
1294 ("pmap_kremove_device: Mapping is not page-sized"));
1298 pte = pmap_pte(kernel_pmap, va, &lvl);
1299 KASSERT(pte != NULL, ("Invalid page table, va: 0x%lx", va));
1301 ("Invalid device pagetable level: %d != 3", lvl));
1307 pmap_invalidate_range(kernel_pmap, sva, va);
1311 * Used to map a range of physical addresses into kernel
1312 * virtual address space.
1314 * The value passed in '*virt' is a suggested virtual address for
1315 * the mapping. Architectures which can support a direct-mapped
1316 * physical to virtual region can return the appropriate address
1317 * within that region, leaving '*virt' unchanged. Other
1318 * architectures should map the pages starting at '*virt' and
1319 * update '*virt' with the first usable address after the mapped
1323 pmap_map(vm_offset_t *virt, vm_paddr_t start, vm_paddr_t end, int prot)
1325 return PHYS_TO_DMAP(start);
1330 * Add a list of wired pages to the kva
1331 * this routine is only used for temporary
1332 * kernel mappings that do not need to have
1333 * page modification or references recorded.
1334 * Note that old mappings are simply written
1335 * over. The page *must* be wired.
1336 * Note: SMP coherent. Uses a ranged shootdown IPI.
1339 pmap_qenter(vm_offset_t sva, vm_page_t *ma, int count)
1342 pt_entry_t *pte, pa;
1348 for (i = 0; i < count; i++) {
1349 pde = pmap_pde(kernel_pmap, va, &lvl);
1350 KASSERT(pde != NULL,
1351 ("pmap_qenter: Invalid page entry, va: 0x%lx", va));
1353 ("pmap_qenter: Invalid level %d", lvl));
1356 pa = VM_PAGE_TO_PHYS(m) | ATTR_DEFAULT | ATTR_AP(ATTR_AP_RW) |
1357 ATTR_XN | ATTR_IDX(m->md.pv_memattr) | L3_PAGE;
1358 pte = pmap_l2_to_l3(pde, va);
1359 pmap_load_store(pte, pa);
1363 pmap_invalidate_range(kernel_pmap, sva, va);
1367 * This routine tears out page mappings from the
1368 * kernel -- it is meant only for temporary mappings.
1371 pmap_qremove(vm_offset_t sva, int count)
1377 KASSERT(sva >= VM_MIN_KERNEL_ADDRESS, ("usermode va %lx", sva));
1380 while (count-- > 0) {
1381 pte = pmap_pte(kernel_pmap, va, &lvl);
1383 ("Invalid device pagetable level: %d != 3", lvl));
1390 pmap_invalidate_range(kernel_pmap, sva, va);
1393 /***************************************************
1394 * Page table page management routines.....
1395 ***************************************************/
1397 * Schedule the specified unused page table page to be freed. Specifically,
1398 * add the page to the specified list of pages that will be released to the
1399 * physical memory manager after the TLB has been updated.
1401 static __inline void
1402 pmap_add_delayed_free_list(vm_page_t m, struct spglist *free,
1403 boolean_t set_PG_ZERO)
1407 m->flags |= PG_ZERO;
1409 m->flags &= ~PG_ZERO;
1410 SLIST_INSERT_HEAD(free, m, plinks.s.ss);
1414 * Decrements a page table page's reference count, which is used to record the
1415 * number of valid page table entries within the page. If the reference count
1416 * drops to zero, then the page table page is unmapped. Returns TRUE if the
1417 * page table page was unmapped and FALSE otherwise.
1419 static inline boolean_t
1420 pmap_unwire_l3(pmap_t pmap, vm_offset_t va, vm_page_t m, struct spglist *free)
1424 if (m->ref_count == 0) {
1425 _pmap_unwire_l3(pmap, va, m, free);
1432 _pmap_unwire_l3(pmap_t pmap, vm_offset_t va, vm_page_t m, struct spglist *free)
1435 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1437 * unmap the page table page
1439 if (m->pindex >= (NUL2E + NUL1E)) {
1443 l0 = pmap_l0(pmap, va);
1445 } else if (m->pindex >= NUL2E) {
1449 l1 = pmap_l1(pmap, va);
1455 l2 = pmap_l2(pmap, va);
1458 pmap_resident_count_dec(pmap, 1);
1459 if (m->pindex < NUL2E) {
1460 /* We just released an l3, unhold the matching l2 */
1461 pd_entry_t *l1, tl1;
1464 l1 = pmap_l1(pmap, va);
1465 tl1 = pmap_load(l1);
1466 l2pg = PHYS_TO_VM_PAGE(tl1 & ~ATTR_MASK);
1467 pmap_unwire_l3(pmap, va, l2pg, free);
1468 } else if (m->pindex < (NUL2E + NUL1E)) {
1469 /* We just released an l2, unhold the matching l1 */
1470 pd_entry_t *l0, tl0;
1473 l0 = pmap_l0(pmap, va);
1474 tl0 = pmap_load(l0);
1475 l1pg = PHYS_TO_VM_PAGE(tl0 & ~ATTR_MASK);
1476 pmap_unwire_l3(pmap, va, l1pg, free);
1478 pmap_invalidate_page(pmap, va);
1481 * Put page on a list so that it is released after
1482 * *ALL* TLB shootdown is done
1484 pmap_add_delayed_free_list(m, free, TRUE);
1488 * After removing a page table entry, this routine is used to
1489 * conditionally free the page, and manage the reference count.
1492 pmap_unuse_pt(pmap_t pmap, vm_offset_t va, pd_entry_t ptepde,
1493 struct spglist *free)
1497 if (va >= VM_MAXUSER_ADDRESS)
1499 KASSERT(ptepde != 0, ("pmap_unuse_pt: ptepde != 0"));
1500 mpte = PHYS_TO_VM_PAGE(ptepde & ~ATTR_MASK);
1501 return (pmap_unwire_l3(pmap, va, mpte, free));
1505 * Release a page table page reference after a failed attempt to create a
1509 pmap_abort_ptp(pmap_t pmap, vm_offset_t va, vm_page_t mpte)
1511 struct spglist free;
1514 if (pmap_unwire_l3(pmap, va, mpte, &free)) {
1516 * Although "va" was never mapped, the TLB could nonetheless
1517 * have intermediate entries that refer to the freed page
1518 * table pages. Invalidate those entries.
1520 * XXX redundant invalidation (See _pmap_unwire_l3().)
1522 pmap_invalidate_page(pmap, va);
1523 vm_page_free_pages_toq(&free, true);
1528 pmap_pinit0(pmap_t pmap)
1531 PMAP_LOCK_INIT(pmap);
1532 bzero(&pmap->pm_stats, sizeof(pmap->pm_stats));
1533 pmap->pm_l0_paddr = READ_SPECIALREG(ttbr0_el1);
1534 pmap->pm_l0 = (pd_entry_t *)PHYS_TO_DMAP(pmap->pm_l0_paddr);
1535 pmap->pm_root.rt_root = 0;
1536 pmap->pm_cookie = COOKIE_FROM(ASID_RESERVED_FOR_PID_0, INT_MIN);
1538 PCPU_SET(curpmap, pmap);
1542 pmap_pinit(pmap_t pmap)
1547 * allocate the l0 page
1549 while ((l0pt = vm_page_alloc(NULL, 0, VM_ALLOC_NORMAL |
1550 VM_ALLOC_NOOBJ | VM_ALLOC_WIRED | VM_ALLOC_ZERO)) == NULL)
1553 pmap->pm_l0_paddr = VM_PAGE_TO_PHYS(l0pt);
1554 pmap->pm_l0 = (pd_entry_t *)PHYS_TO_DMAP(pmap->pm_l0_paddr);
1556 if ((l0pt->flags & PG_ZERO) == 0)
1557 pagezero(pmap->pm_l0);
1559 pmap->pm_root.rt_root = 0;
1560 bzero(&pmap->pm_stats, sizeof(pmap->pm_stats));
1561 pmap->pm_cookie = COOKIE_FROM(-1, INT_MAX);
1562 /* XXX Temporarily disable deferred ASID allocation. */
1563 pmap_alloc_asid(pmap);
1569 * This routine is called if the desired page table page does not exist.
1571 * If page table page allocation fails, this routine may sleep before
1572 * returning NULL. It sleeps only if a lock pointer was given.
1574 * Note: If a page allocation fails at page table level two or three,
1575 * one or two pages may be held during the wait, only to be released
1576 * afterwards. This conservative approach is easily argued to avoid
1580 _pmap_alloc_l3(pmap_t pmap, vm_pindex_t ptepindex, struct rwlock **lockp)
1582 vm_page_t m, l1pg, l2pg;
1584 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1587 * Allocate a page table page.
1589 if ((m = vm_page_alloc(NULL, ptepindex, VM_ALLOC_NOOBJ |
1590 VM_ALLOC_WIRED | VM_ALLOC_ZERO)) == NULL) {
1591 if (lockp != NULL) {
1592 RELEASE_PV_LIST_LOCK(lockp);
1599 * Indicate the need to retry. While waiting, the page table
1600 * page may have been allocated.
1604 if ((m->flags & PG_ZERO) == 0)
1608 * Because of AArch64's weak memory consistency model, we must have a
1609 * barrier here to ensure that the stores for zeroing "m", whether by
1610 * pmap_zero_page() or an earlier function, are visible before adding
1611 * "m" to the page table. Otherwise, a page table walk by another
1612 * processor's MMU could see the mapping to "m" and a stale, non-zero
1618 * Map the pagetable page into the process address space, if
1619 * it isn't already there.
1622 if (ptepindex >= (NUL2E + NUL1E)) {
1624 vm_pindex_t l0index;
1626 l0index = ptepindex - (NUL2E + NUL1E);
1627 l0 = &pmap->pm_l0[l0index];
1628 pmap_store(l0, VM_PAGE_TO_PHYS(m) | L0_TABLE);
1629 } else if (ptepindex >= NUL2E) {
1630 vm_pindex_t l0index, l1index;
1631 pd_entry_t *l0, *l1;
1634 l1index = ptepindex - NUL2E;
1635 l0index = l1index >> L0_ENTRIES_SHIFT;
1637 l0 = &pmap->pm_l0[l0index];
1638 tl0 = pmap_load(l0);
1640 /* recurse for allocating page dir */
1641 if (_pmap_alloc_l3(pmap, NUL2E + NUL1E + l0index,
1643 vm_page_unwire_noq(m);
1644 vm_page_free_zero(m);
1648 l1pg = PHYS_TO_VM_PAGE(tl0 & ~ATTR_MASK);
1652 l1 = (pd_entry_t *)PHYS_TO_DMAP(pmap_load(l0) & ~ATTR_MASK);
1653 l1 = &l1[ptepindex & Ln_ADDR_MASK];
1654 pmap_store(l1, VM_PAGE_TO_PHYS(m) | L1_TABLE);
1656 vm_pindex_t l0index, l1index;
1657 pd_entry_t *l0, *l1, *l2;
1658 pd_entry_t tl0, tl1;
1660 l1index = ptepindex >> Ln_ENTRIES_SHIFT;
1661 l0index = l1index >> L0_ENTRIES_SHIFT;
1663 l0 = &pmap->pm_l0[l0index];
1664 tl0 = pmap_load(l0);
1666 /* recurse for allocating page dir */
1667 if (_pmap_alloc_l3(pmap, NUL2E + l1index,
1669 vm_page_unwire_noq(m);
1670 vm_page_free_zero(m);
1673 tl0 = pmap_load(l0);
1674 l1 = (pd_entry_t *)PHYS_TO_DMAP(tl0 & ~ATTR_MASK);
1675 l1 = &l1[l1index & Ln_ADDR_MASK];
1677 l1 = (pd_entry_t *)PHYS_TO_DMAP(tl0 & ~ATTR_MASK);
1678 l1 = &l1[l1index & Ln_ADDR_MASK];
1679 tl1 = pmap_load(l1);
1681 /* recurse for allocating page dir */
1682 if (_pmap_alloc_l3(pmap, NUL2E + l1index,
1684 vm_page_unwire_noq(m);
1685 vm_page_free_zero(m);
1689 l2pg = PHYS_TO_VM_PAGE(tl1 & ~ATTR_MASK);
1694 l2 = (pd_entry_t *)PHYS_TO_DMAP(pmap_load(l1) & ~ATTR_MASK);
1695 l2 = &l2[ptepindex & Ln_ADDR_MASK];
1696 pmap_store(l2, VM_PAGE_TO_PHYS(m) | L2_TABLE);
1699 pmap_resident_count_inc(pmap, 1);
1705 pmap_alloc_l2(pmap_t pmap, vm_offset_t va, vm_page_t *l2pgp,
1706 struct rwlock **lockp)
1708 pd_entry_t *l1, *l2;
1710 vm_pindex_t l2pindex;
1713 l1 = pmap_l1(pmap, va);
1714 if (l1 != NULL && (pmap_load(l1) & ATTR_DESCR_MASK) == L1_TABLE) {
1715 l2 = pmap_l1_to_l2(l1, va);
1716 if (va < VM_MAXUSER_ADDRESS) {
1717 /* Add a reference to the L2 page. */
1718 l2pg = PHYS_TO_VM_PAGE(pmap_load(l1) & ~ATTR_MASK);
1722 } else if (va < VM_MAXUSER_ADDRESS) {
1723 /* Allocate a L2 page. */
1724 l2pindex = pmap_l2_pindex(va) >> Ln_ENTRIES_SHIFT;
1725 l2pg = _pmap_alloc_l3(pmap, NUL2E + l2pindex, lockp);
1732 l2 = (pd_entry_t *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(l2pg));
1733 l2 = &l2[pmap_l2_index(va)];
1735 panic("pmap_alloc_l2: missing page table page for va %#lx",
1742 pmap_alloc_l3(pmap_t pmap, vm_offset_t va, struct rwlock **lockp)
1744 vm_pindex_t ptepindex;
1745 pd_entry_t *pde, tpde;
1753 * Calculate pagetable page index
1755 ptepindex = pmap_l2_pindex(va);
1758 * Get the page directory entry
1760 pde = pmap_pde(pmap, va, &lvl);
1763 * If the page table page is mapped, we just increment the hold count,
1764 * and activate it. If we get a level 2 pde it will point to a level 3
1772 pte = pmap_l0_to_l1(pde, va);
1773 KASSERT(pmap_load(pte) == 0,
1774 ("pmap_alloc_l3: TODO: l0 superpages"));
1779 pte = pmap_l1_to_l2(pde, va);
1780 KASSERT(pmap_load(pte) == 0,
1781 ("pmap_alloc_l3: TODO: l1 superpages"));
1785 tpde = pmap_load(pde);
1787 m = PHYS_TO_VM_PAGE(tpde & ~ATTR_MASK);
1793 panic("pmap_alloc_l3: Invalid level %d", lvl);
1797 * Here if the pte page isn't mapped, or if it has been deallocated.
1799 m = _pmap_alloc_l3(pmap, ptepindex, lockp);
1800 if (m == NULL && lockp != NULL)
1806 /***************************************************
1807 * Pmap allocation/deallocation routines.
1808 ***************************************************/
1811 * Release any resources held by the given physical map.
1812 * Called when a pmap initialized by pmap_pinit is being released.
1813 * Should only be called if the map contains no valid mappings.
1816 pmap_release(pmap_t pmap)
1821 KASSERT(pmap->pm_stats.resident_count == 0,
1822 ("pmap_release: pmap resident count %ld != 0",
1823 pmap->pm_stats.resident_count));
1824 KASSERT(vm_radix_is_empty(&pmap->pm_root),
1825 ("pmap_release: pmap has reserved page table page(s)"));
1827 mtx_lock_spin(&asid_set_mutex);
1828 if (COOKIE_TO_EPOCH(pmap->pm_cookie) == asid_epoch) {
1829 asid = COOKIE_TO_ASID(pmap->pm_cookie);
1830 KASSERT(asid >= ASID_FIRST_AVAILABLE && asid < asid_set_size,
1831 ("pmap_release: pmap cookie has out-of-range asid"));
1832 bit_clear(asid_set, asid);
1834 mtx_unlock_spin(&asid_set_mutex);
1836 m = PHYS_TO_VM_PAGE(pmap->pm_l0_paddr);
1837 vm_page_unwire_noq(m);
1838 vm_page_free_zero(m);
1842 kvm_size(SYSCTL_HANDLER_ARGS)
1844 unsigned long ksize = VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS;
1846 return sysctl_handle_long(oidp, &ksize, 0, req);
1848 SYSCTL_PROC(_vm, OID_AUTO, kvm_size, CTLTYPE_LONG|CTLFLAG_RD,
1849 0, 0, kvm_size, "LU", "Size of KVM");
1852 kvm_free(SYSCTL_HANDLER_ARGS)
1854 unsigned long kfree = VM_MAX_KERNEL_ADDRESS - kernel_vm_end;
1856 return sysctl_handle_long(oidp, &kfree, 0, req);
1858 SYSCTL_PROC(_vm, OID_AUTO, kvm_free, CTLTYPE_LONG|CTLFLAG_RD,
1859 0, 0, kvm_free, "LU", "Amount of KVM free");
1862 * grow the number of kernel page table entries, if needed
1865 pmap_growkernel(vm_offset_t addr)
1869 pd_entry_t *l0, *l1, *l2;
1871 mtx_assert(&kernel_map->system_mtx, MA_OWNED);
1873 addr = roundup2(addr, L2_SIZE);
1874 if (addr - 1 >= vm_map_max(kernel_map))
1875 addr = vm_map_max(kernel_map);
1876 while (kernel_vm_end < addr) {
1877 l0 = pmap_l0(kernel_pmap, kernel_vm_end);
1878 KASSERT(pmap_load(l0) != 0,
1879 ("pmap_growkernel: No level 0 kernel entry"));
1881 l1 = pmap_l0_to_l1(l0, kernel_vm_end);
1882 if (pmap_load(l1) == 0) {
1883 /* We need a new PDP entry */
1884 nkpg = vm_page_alloc(NULL, kernel_vm_end >> L1_SHIFT,
1885 VM_ALLOC_INTERRUPT | VM_ALLOC_NOOBJ |
1886 VM_ALLOC_WIRED | VM_ALLOC_ZERO);
1888 panic("pmap_growkernel: no memory to grow kernel");
1889 if ((nkpg->flags & PG_ZERO) == 0)
1890 pmap_zero_page(nkpg);
1891 /* See the dmb() in _pmap_alloc_l3(). */
1893 paddr = VM_PAGE_TO_PHYS(nkpg);
1894 pmap_store(l1, paddr | L1_TABLE);
1895 continue; /* try again */
1897 l2 = pmap_l1_to_l2(l1, kernel_vm_end);
1898 if (pmap_load(l2) != 0) {
1899 kernel_vm_end = (kernel_vm_end + L2_SIZE) & ~L2_OFFSET;
1900 if (kernel_vm_end - 1 >= vm_map_max(kernel_map)) {
1901 kernel_vm_end = vm_map_max(kernel_map);
1907 nkpg = vm_page_alloc(NULL, kernel_vm_end >> L2_SHIFT,
1908 VM_ALLOC_INTERRUPT | VM_ALLOC_NOOBJ | VM_ALLOC_WIRED |
1911 panic("pmap_growkernel: no memory to grow kernel");
1912 if ((nkpg->flags & PG_ZERO) == 0)
1913 pmap_zero_page(nkpg);
1914 /* See the dmb() in _pmap_alloc_l3(). */
1916 paddr = VM_PAGE_TO_PHYS(nkpg);
1917 pmap_store(l2, paddr | L2_TABLE);
1919 kernel_vm_end = (kernel_vm_end + L2_SIZE) & ~L2_OFFSET;
1920 if (kernel_vm_end - 1 >= vm_map_max(kernel_map)) {
1921 kernel_vm_end = vm_map_max(kernel_map);
1928 /***************************************************
1929 * page management routines.
1930 ***************************************************/
1932 CTASSERT(sizeof(struct pv_chunk) == PAGE_SIZE);
1933 CTASSERT(_NPCM == 3);
1934 CTASSERT(_NPCPV == 168);
1936 static __inline struct pv_chunk *
1937 pv_to_chunk(pv_entry_t pv)
1940 return ((struct pv_chunk *)((uintptr_t)pv & ~(uintptr_t)PAGE_MASK));
1943 #define PV_PMAP(pv) (pv_to_chunk(pv)->pc_pmap)
1945 #define PC_FREE0 0xfffffffffffffffful
1946 #define PC_FREE1 0xfffffffffffffffful
1947 #define PC_FREE2 0x000000fffffffffful
1949 static const uint64_t pc_freemask[_NPCM] = { PC_FREE0, PC_FREE1, PC_FREE2 };
1953 static int pc_chunk_count, pc_chunk_allocs, pc_chunk_frees, pc_chunk_tryfail;
1955 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_count, CTLFLAG_RD, &pc_chunk_count, 0,
1956 "Current number of pv entry chunks");
1957 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_allocs, CTLFLAG_RD, &pc_chunk_allocs, 0,
1958 "Current number of pv entry chunks allocated");
1959 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_frees, CTLFLAG_RD, &pc_chunk_frees, 0,
1960 "Current number of pv entry chunks frees");
1961 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_tryfail, CTLFLAG_RD, &pc_chunk_tryfail, 0,
1962 "Number of times tried to get a chunk page but failed.");
1964 static long pv_entry_frees, pv_entry_allocs, pv_entry_count;
1965 static int pv_entry_spare;
1967 SYSCTL_LONG(_vm_pmap, OID_AUTO, pv_entry_frees, CTLFLAG_RD, &pv_entry_frees, 0,
1968 "Current number of pv entry frees");
1969 SYSCTL_LONG(_vm_pmap, OID_AUTO, pv_entry_allocs, CTLFLAG_RD, &pv_entry_allocs, 0,
1970 "Current number of pv entry allocs");
1971 SYSCTL_LONG(_vm_pmap, OID_AUTO, pv_entry_count, CTLFLAG_RD, &pv_entry_count, 0,
1972 "Current number of pv entries");
1973 SYSCTL_INT(_vm_pmap, OID_AUTO, pv_entry_spare, CTLFLAG_RD, &pv_entry_spare, 0,
1974 "Current number of spare pv entries");
1979 * We are in a serious low memory condition. Resort to
1980 * drastic measures to free some pages so we can allocate
1981 * another pv entry chunk.
1983 * Returns NULL if PV entries were reclaimed from the specified pmap.
1985 * We do not, however, unmap 2mpages because subsequent accesses will
1986 * allocate per-page pv entries until repromotion occurs, thereby
1987 * exacerbating the shortage of free pv entries.
1990 reclaim_pv_chunk(pmap_t locked_pmap, struct rwlock **lockp)
1992 struct pv_chunk *pc, *pc_marker, *pc_marker_end;
1993 struct pv_chunk_header pc_marker_b, pc_marker_end_b;
1994 struct md_page *pvh;
1996 pmap_t next_pmap, pmap;
1997 pt_entry_t *pte, tpte;
2001 struct spglist free;
2003 int bit, field, freed, lvl;
2004 static int active_reclaims = 0;
2006 PMAP_LOCK_ASSERT(locked_pmap, MA_OWNED);
2007 KASSERT(lockp != NULL, ("reclaim_pv_chunk: lockp is NULL"));
2012 bzero(&pc_marker_b, sizeof(pc_marker_b));
2013 bzero(&pc_marker_end_b, sizeof(pc_marker_end_b));
2014 pc_marker = (struct pv_chunk *)&pc_marker_b;
2015 pc_marker_end = (struct pv_chunk *)&pc_marker_end_b;
2017 mtx_lock(&pv_chunks_mutex);
2019 TAILQ_INSERT_HEAD(&pv_chunks, pc_marker, pc_lru);
2020 TAILQ_INSERT_TAIL(&pv_chunks, pc_marker_end, pc_lru);
2021 while ((pc = TAILQ_NEXT(pc_marker, pc_lru)) != pc_marker_end &&
2022 SLIST_EMPTY(&free)) {
2023 next_pmap = pc->pc_pmap;
2024 if (next_pmap == NULL) {
2026 * The next chunk is a marker. However, it is
2027 * not our marker, so active_reclaims must be
2028 * > 1. Consequently, the next_chunk code
2029 * will not rotate the pv_chunks list.
2033 mtx_unlock(&pv_chunks_mutex);
2036 * A pv_chunk can only be removed from the pc_lru list
2037 * when both pv_chunks_mutex is owned and the
2038 * corresponding pmap is locked.
2040 if (pmap != next_pmap) {
2041 if (pmap != NULL && pmap != locked_pmap)
2044 /* Avoid deadlock and lock recursion. */
2045 if (pmap > locked_pmap) {
2046 RELEASE_PV_LIST_LOCK(lockp);
2048 mtx_lock(&pv_chunks_mutex);
2050 } else if (pmap != locked_pmap) {
2051 if (PMAP_TRYLOCK(pmap)) {
2052 mtx_lock(&pv_chunks_mutex);
2055 pmap = NULL; /* pmap is not locked */
2056 mtx_lock(&pv_chunks_mutex);
2057 pc = TAILQ_NEXT(pc_marker, pc_lru);
2059 pc->pc_pmap != next_pmap)
2067 * Destroy every non-wired, 4 KB page mapping in the chunk.
2070 for (field = 0; field < _NPCM; field++) {
2071 for (inuse = ~pc->pc_map[field] & pc_freemask[field];
2072 inuse != 0; inuse &= ~(1UL << bit)) {
2073 bit = ffsl(inuse) - 1;
2074 pv = &pc->pc_pventry[field * 64 + bit];
2076 pde = pmap_pde(pmap, va, &lvl);
2079 pte = pmap_l2_to_l3(pde, va);
2080 tpte = pmap_load(pte);
2081 if ((tpte & ATTR_SW_WIRED) != 0)
2083 tpte = pmap_load_clear(pte);
2084 m = PHYS_TO_VM_PAGE(tpte & ~ATTR_MASK);
2085 if (pmap_pte_dirty(tpte))
2087 if ((tpte & ATTR_AF) != 0) {
2088 pmap_invalidate_page(pmap, va);
2089 vm_page_aflag_set(m, PGA_REFERENCED);
2091 CHANGE_PV_LIST_LOCK_TO_VM_PAGE(lockp, m);
2092 TAILQ_REMOVE(&m->md.pv_list, pv, pv_next);
2094 if (TAILQ_EMPTY(&m->md.pv_list) &&
2095 (m->flags & PG_FICTITIOUS) == 0) {
2096 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
2097 if (TAILQ_EMPTY(&pvh->pv_list)) {
2098 vm_page_aflag_clear(m,
2102 pc->pc_map[field] |= 1UL << bit;
2103 pmap_unuse_pt(pmap, va, pmap_load(pde), &free);
2108 mtx_lock(&pv_chunks_mutex);
2111 /* Every freed mapping is for a 4 KB page. */
2112 pmap_resident_count_dec(pmap, freed);
2113 PV_STAT(atomic_add_long(&pv_entry_frees, freed));
2114 PV_STAT(atomic_add_int(&pv_entry_spare, freed));
2115 PV_STAT(atomic_subtract_long(&pv_entry_count, freed));
2116 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
2117 if (pc->pc_map[0] == PC_FREE0 && pc->pc_map[1] == PC_FREE1 &&
2118 pc->pc_map[2] == PC_FREE2) {
2119 PV_STAT(atomic_subtract_int(&pv_entry_spare, _NPCPV));
2120 PV_STAT(atomic_subtract_int(&pc_chunk_count, 1));
2121 PV_STAT(atomic_add_int(&pc_chunk_frees, 1));
2122 /* Entire chunk is free; return it. */
2123 m_pc = PHYS_TO_VM_PAGE(DMAP_TO_PHYS((vm_offset_t)pc));
2124 dump_drop_page(m_pc->phys_addr);
2125 mtx_lock(&pv_chunks_mutex);
2126 TAILQ_REMOVE(&pv_chunks, pc, pc_lru);
2129 TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc, pc_list);
2130 mtx_lock(&pv_chunks_mutex);
2131 /* One freed pv entry in locked_pmap is sufficient. */
2132 if (pmap == locked_pmap)
2136 TAILQ_REMOVE(&pv_chunks, pc_marker, pc_lru);
2137 TAILQ_INSERT_AFTER(&pv_chunks, pc, pc_marker, pc_lru);
2138 if (active_reclaims == 1 && pmap != NULL) {
2140 * Rotate the pv chunks list so that we do not
2141 * scan the same pv chunks that could not be
2142 * freed (because they contained a wired
2143 * and/or superpage mapping) on every
2144 * invocation of reclaim_pv_chunk().
2146 while ((pc = TAILQ_FIRST(&pv_chunks)) != pc_marker) {
2147 MPASS(pc->pc_pmap != NULL);
2148 TAILQ_REMOVE(&pv_chunks, pc, pc_lru);
2149 TAILQ_INSERT_TAIL(&pv_chunks, pc, pc_lru);
2153 TAILQ_REMOVE(&pv_chunks, pc_marker, pc_lru);
2154 TAILQ_REMOVE(&pv_chunks, pc_marker_end, pc_lru);
2156 mtx_unlock(&pv_chunks_mutex);
2157 if (pmap != NULL && pmap != locked_pmap)
2159 if (m_pc == NULL && !SLIST_EMPTY(&free)) {
2160 m_pc = SLIST_FIRST(&free);
2161 SLIST_REMOVE_HEAD(&free, plinks.s.ss);
2162 /* Recycle a freed page table page. */
2163 m_pc->ref_count = 1;
2165 vm_page_free_pages_toq(&free, true);
2170 * free the pv_entry back to the free list
2173 free_pv_entry(pmap_t pmap, pv_entry_t pv)
2175 struct pv_chunk *pc;
2176 int idx, field, bit;
2178 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2179 PV_STAT(atomic_add_long(&pv_entry_frees, 1));
2180 PV_STAT(atomic_add_int(&pv_entry_spare, 1));
2181 PV_STAT(atomic_subtract_long(&pv_entry_count, 1));
2182 pc = pv_to_chunk(pv);
2183 idx = pv - &pc->pc_pventry[0];
2186 pc->pc_map[field] |= 1ul << bit;
2187 if (pc->pc_map[0] != PC_FREE0 || pc->pc_map[1] != PC_FREE1 ||
2188 pc->pc_map[2] != PC_FREE2) {
2189 /* 98% of the time, pc is already at the head of the list. */
2190 if (__predict_false(pc != TAILQ_FIRST(&pmap->pm_pvchunk))) {
2191 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
2192 TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc, pc_list);
2196 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
2201 free_pv_chunk(struct pv_chunk *pc)
2205 mtx_lock(&pv_chunks_mutex);
2206 TAILQ_REMOVE(&pv_chunks, pc, pc_lru);
2207 mtx_unlock(&pv_chunks_mutex);
2208 PV_STAT(atomic_subtract_int(&pv_entry_spare, _NPCPV));
2209 PV_STAT(atomic_subtract_int(&pc_chunk_count, 1));
2210 PV_STAT(atomic_add_int(&pc_chunk_frees, 1));
2211 /* entire chunk is free, return it */
2212 m = PHYS_TO_VM_PAGE(DMAP_TO_PHYS((vm_offset_t)pc));
2213 dump_drop_page(m->phys_addr);
2214 vm_page_unwire_noq(m);
2219 * Returns a new PV entry, allocating a new PV chunk from the system when
2220 * needed. If this PV chunk allocation fails and a PV list lock pointer was
2221 * given, a PV chunk is reclaimed from an arbitrary pmap. Otherwise, NULL is
2224 * The given PV list lock may be released.
2227 get_pv_entry(pmap_t pmap, struct rwlock **lockp)
2231 struct pv_chunk *pc;
2234 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2235 PV_STAT(atomic_add_long(&pv_entry_allocs, 1));
2237 pc = TAILQ_FIRST(&pmap->pm_pvchunk);
2239 for (field = 0; field < _NPCM; field++) {
2240 if (pc->pc_map[field]) {
2241 bit = ffsl(pc->pc_map[field]) - 1;
2245 if (field < _NPCM) {
2246 pv = &pc->pc_pventry[field * 64 + bit];
2247 pc->pc_map[field] &= ~(1ul << bit);
2248 /* If this was the last item, move it to tail */
2249 if (pc->pc_map[0] == 0 && pc->pc_map[1] == 0 &&
2250 pc->pc_map[2] == 0) {
2251 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
2252 TAILQ_INSERT_TAIL(&pmap->pm_pvchunk, pc,
2255 PV_STAT(atomic_add_long(&pv_entry_count, 1));
2256 PV_STAT(atomic_subtract_int(&pv_entry_spare, 1));
2260 /* No free items, allocate another chunk */
2261 m = vm_page_alloc(NULL, 0, VM_ALLOC_NORMAL | VM_ALLOC_NOOBJ |
2264 if (lockp == NULL) {
2265 PV_STAT(pc_chunk_tryfail++);
2268 m = reclaim_pv_chunk(pmap, lockp);
2272 PV_STAT(atomic_add_int(&pc_chunk_count, 1));
2273 PV_STAT(atomic_add_int(&pc_chunk_allocs, 1));
2274 dump_add_page(m->phys_addr);
2275 pc = (void *)PHYS_TO_DMAP(m->phys_addr);
2277 pc->pc_map[0] = PC_FREE0 & ~1ul; /* preallocated bit 0 */
2278 pc->pc_map[1] = PC_FREE1;
2279 pc->pc_map[2] = PC_FREE2;
2280 mtx_lock(&pv_chunks_mutex);
2281 TAILQ_INSERT_TAIL(&pv_chunks, pc, pc_lru);
2282 mtx_unlock(&pv_chunks_mutex);
2283 pv = &pc->pc_pventry[0];
2284 TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc, pc_list);
2285 PV_STAT(atomic_add_long(&pv_entry_count, 1));
2286 PV_STAT(atomic_add_int(&pv_entry_spare, _NPCPV - 1));
2291 * Ensure that the number of spare PV entries in the specified pmap meets or
2292 * exceeds the given count, "needed".
2294 * The given PV list lock may be released.
2297 reserve_pv_entries(pmap_t pmap, int needed, struct rwlock **lockp)
2299 struct pch new_tail;
2300 struct pv_chunk *pc;
2305 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2306 KASSERT(lockp != NULL, ("reserve_pv_entries: lockp is NULL"));
2309 * Newly allocated PV chunks must be stored in a private list until
2310 * the required number of PV chunks have been allocated. Otherwise,
2311 * reclaim_pv_chunk() could recycle one of these chunks. In
2312 * contrast, these chunks must be added to the pmap upon allocation.
2314 TAILQ_INIT(&new_tail);
2317 TAILQ_FOREACH(pc, &pmap->pm_pvchunk, pc_list) {
2318 bit_count((bitstr_t *)pc->pc_map, 0,
2319 sizeof(pc->pc_map) * NBBY, &free);
2323 if (avail >= needed)
2326 for (reclaimed = false; avail < needed; avail += _NPCPV) {
2327 m = vm_page_alloc(NULL, 0, VM_ALLOC_NORMAL | VM_ALLOC_NOOBJ |
2330 m = reclaim_pv_chunk(pmap, lockp);
2335 PV_STAT(atomic_add_int(&pc_chunk_count, 1));
2336 PV_STAT(atomic_add_int(&pc_chunk_allocs, 1));
2337 dump_add_page(m->phys_addr);
2338 pc = (void *)PHYS_TO_DMAP(m->phys_addr);
2340 pc->pc_map[0] = PC_FREE0;
2341 pc->pc_map[1] = PC_FREE1;
2342 pc->pc_map[2] = PC_FREE2;
2343 TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc, pc_list);
2344 TAILQ_INSERT_TAIL(&new_tail, pc, pc_lru);
2345 PV_STAT(atomic_add_int(&pv_entry_spare, _NPCPV));
2348 * The reclaim might have freed a chunk from the current pmap.
2349 * If that chunk contained available entries, we need to
2350 * re-count the number of available entries.
2355 if (!TAILQ_EMPTY(&new_tail)) {
2356 mtx_lock(&pv_chunks_mutex);
2357 TAILQ_CONCAT(&pv_chunks, &new_tail, pc_lru);
2358 mtx_unlock(&pv_chunks_mutex);
2363 * First find and then remove the pv entry for the specified pmap and virtual
2364 * address from the specified pv list. Returns the pv entry if found and NULL
2365 * otherwise. This operation can be performed on pv lists for either 4KB or
2366 * 2MB page mappings.
2368 static __inline pv_entry_t
2369 pmap_pvh_remove(struct md_page *pvh, pmap_t pmap, vm_offset_t va)
2373 TAILQ_FOREACH(pv, &pvh->pv_list, pv_next) {
2374 if (pmap == PV_PMAP(pv) && va == pv->pv_va) {
2375 TAILQ_REMOVE(&pvh->pv_list, pv, pv_next);
2384 * After demotion from a 2MB page mapping to 512 4KB page mappings,
2385 * destroy the pv entry for the 2MB page mapping and reinstantiate the pv
2386 * entries for each of the 4KB page mappings.
2389 pmap_pv_demote_l2(pmap_t pmap, vm_offset_t va, vm_paddr_t pa,
2390 struct rwlock **lockp)
2392 struct md_page *pvh;
2393 struct pv_chunk *pc;
2395 vm_offset_t va_last;
2399 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2400 KASSERT((va & L2_OFFSET) == 0,
2401 ("pmap_pv_demote_l2: va is not 2mpage aligned"));
2402 KASSERT((pa & L2_OFFSET) == 0,
2403 ("pmap_pv_demote_l2: pa is not 2mpage aligned"));
2404 CHANGE_PV_LIST_LOCK_TO_PHYS(lockp, pa);
2407 * Transfer the 2mpage's pv entry for this mapping to the first
2408 * page's pv list. Once this transfer begins, the pv list lock
2409 * must not be released until the last pv entry is reinstantiated.
2411 pvh = pa_to_pvh(pa);
2412 pv = pmap_pvh_remove(pvh, pmap, va);
2413 KASSERT(pv != NULL, ("pmap_pv_demote_l2: pv not found"));
2414 m = PHYS_TO_VM_PAGE(pa);
2415 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_next);
2417 /* Instantiate the remaining Ln_ENTRIES - 1 pv entries. */
2418 PV_STAT(atomic_add_long(&pv_entry_allocs, Ln_ENTRIES - 1));
2419 va_last = va + L2_SIZE - PAGE_SIZE;
2421 pc = TAILQ_FIRST(&pmap->pm_pvchunk);
2422 KASSERT(pc->pc_map[0] != 0 || pc->pc_map[1] != 0 ||
2423 pc->pc_map[2] != 0, ("pmap_pv_demote_l2: missing spare"));
2424 for (field = 0; field < _NPCM; field++) {
2425 while (pc->pc_map[field]) {
2426 bit = ffsl(pc->pc_map[field]) - 1;
2427 pc->pc_map[field] &= ~(1ul << bit);
2428 pv = &pc->pc_pventry[field * 64 + bit];
2432 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2433 ("pmap_pv_demote_l2: page %p is not managed", m));
2434 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_next);
2440 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
2441 TAILQ_INSERT_TAIL(&pmap->pm_pvchunk, pc, pc_list);
2444 if (pc->pc_map[0] == 0 && pc->pc_map[1] == 0 && pc->pc_map[2] == 0) {
2445 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
2446 TAILQ_INSERT_TAIL(&pmap->pm_pvchunk, pc, pc_list);
2448 PV_STAT(atomic_add_long(&pv_entry_count, Ln_ENTRIES - 1));
2449 PV_STAT(atomic_subtract_int(&pv_entry_spare, Ln_ENTRIES - 1));
2453 * First find and then destroy the pv entry for the specified pmap and virtual
2454 * address. This operation can be performed on pv lists for either 4KB or 2MB
2458 pmap_pvh_free(struct md_page *pvh, pmap_t pmap, vm_offset_t va)
2462 pv = pmap_pvh_remove(pvh, pmap, va);
2463 KASSERT(pv != NULL, ("pmap_pvh_free: pv not found"));
2464 free_pv_entry(pmap, pv);
2468 * Conditionally create the PV entry for a 4KB page mapping if the required
2469 * memory can be allocated without resorting to reclamation.
2472 pmap_try_insert_pv_entry(pmap_t pmap, vm_offset_t va, vm_page_t m,
2473 struct rwlock **lockp)
2477 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2478 /* Pass NULL instead of the lock pointer to disable reclamation. */
2479 if ((pv = get_pv_entry(pmap, NULL)) != NULL) {
2481 CHANGE_PV_LIST_LOCK_TO_VM_PAGE(lockp, m);
2482 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_next);
2490 * Create the PV entry for a 2MB page mapping. Always returns true unless the
2491 * flag PMAP_ENTER_NORECLAIM is specified. If that flag is specified, returns
2492 * false if the PV entry cannot be allocated without resorting to reclamation.
2495 pmap_pv_insert_l2(pmap_t pmap, vm_offset_t va, pd_entry_t l2e, u_int flags,
2496 struct rwlock **lockp)
2498 struct md_page *pvh;
2502 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2503 /* Pass NULL instead of the lock pointer to disable reclamation. */
2504 if ((pv = get_pv_entry(pmap, (flags & PMAP_ENTER_NORECLAIM) != 0 ?
2505 NULL : lockp)) == NULL)
2508 pa = l2e & ~ATTR_MASK;
2509 CHANGE_PV_LIST_LOCK_TO_PHYS(lockp, pa);
2510 pvh = pa_to_pvh(pa);
2511 TAILQ_INSERT_TAIL(&pvh->pv_list, pv, pv_next);
2517 pmap_remove_kernel_l2(pmap_t pmap, pt_entry_t *l2, vm_offset_t va)
2519 pt_entry_t newl2, oldl2;
2523 KASSERT(!VIRT_IN_DMAP(va), ("removing direct mapping of %#lx", va));
2524 KASSERT(pmap == kernel_pmap, ("pmap %p is not kernel_pmap", pmap));
2525 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2527 ml3 = pmap_remove_pt_page(pmap, va);
2529 panic("pmap_remove_kernel_l2: Missing pt page");
2531 ml3pa = VM_PAGE_TO_PHYS(ml3);
2532 newl2 = ml3pa | L2_TABLE;
2535 * If this page table page was unmapped by a promotion, then it
2536 * contains valid mappings. Zero it to invalidate those mappings.
2538 if (ml3->valid != 0)
2539 pagezero((void *)PHYS_TO_DMAP(ml3pa));
2542 * Demote the mapping. The caller must have already invalidated the
2543 * mapping (i.e., the "break" in break-before-make).
2545 oldl2 = pmap_load_store(l2, newl2);
2546 KASSERT(oldl2 == 0, ("%s: found existing mapping at %p: %#lx",
2547 __func__, l2, oldl2));
2551 * pmap_remove_l2: Do the things to unmap a level 2 superpage.
2554 pmap_remove_l2(pmap_t pmap, pt_entry_t *l2, vm_offset_t sva,
2555 pd_entry_t l1e, struct spglist *free, struct rwlock **lockp)
2557 struct md_page *pvh;
2559 vm_offset_t eva, va;
2562 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2563 KASSERT((sva & L2_OFFSET) == 0, ("pmap_remove_l2: sva is not aligned"));
2564 old_l2 = pmap_load_clear(l2);
2565 KASSERT((old_l2 & ATTR_DESCR_MASK) == L2_BLOCK,
2566 ("pmap_remove_l2: L2e %lx is not a block mapping", old_l2));
2569 * Since a promotion must break the 4KB page mappings before making
2570 * the 2MB page mapping, a pmap_invalidate_page() suffices.
2572 pmap_invalidate_page(pmap, sva);
2574 if (old_l2 & ATTR_SW_WIRED)
2575 pmap->pm_stats.wired_count -= L2_SIZE / PAGE_SIZE;
2576 pmap_resident_count_dec(pmap, L2_SIZE / PAGE_SIZE);
2577 if (old_l2 & ATTR_SW_MANAGED) {
2578 CHANGE_PV_LIST_LOCK_TO_PHYS(lockp, old_l2 & ~ATTR_MASK);
2579 pvh = pa_to_pvh(old_l2 & ~ATTR_MASK);
2580 pmap_pvh_free(pvh, pmap, sva);
2581 eva = sva + L2_SIZE;
2582 for (va = sva, m = PHYS_TO_VM_PAGE(old_l2 & ~ATTR_MASK);
2583 va < eva; va += PAGE_SIZE, m++) {
2584 if (pmap_pte_dirty(old_l2))
2586 if (old_l2 & ATTR_AF)
2587 vm_page_aflag_set(m, PGA_REFERENCED);
2588 if (TAILQ_EMPTY(&m->md.pv_list) &&
2589 TAILQ_EMPTY(&pvh->pv_list))
2590 vm_page_aflag_clear(m, PGA_WRITEABLE);
2593 if (pmap == kernel_pmap) {
2594 pmap_remove_kernel_l2(pmap, l2, sva);
2596 ml3 = pmap_remove_pt_page(pmap, sva);
2598 KASSERT(ml3->valid == VM_PAGE_BITS_ALL,
2599 ("pmap_remove_l2: l3 page not promoted"));
2600 pmap_resident_count_dec(pmap, 1);
2601 KASSERT(ml3->ref_count == NL3PG,
2602 ("pmap_remove_l2: l3 page ref count error"));
2604 pmap_add_delayed_free_list(ml3, free, FALSE);
2607 return (pmap_unuse_pt(pmap, sva, l1e, free));
2611 * pmap_remove_l3: do the things to unmap a page in a process
2614 pmap_remove_l3(pmap_t pmap, pt_entry_t *l3, vm_offset_t va,
2615 pd_entry_t l2e, struct spglist *free, struct rwlock **lockp)
2617 struct md_page *pvh;
2621 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2622 old_l3 = pmap_load_clear(l3);
2623 pmap_invalidate_page(pmap, va);
2624 if (old_l3 & ATTR_SW_WIRED)
2625 pmap->pm_stats.wired_count -= 1;
2626 pmap_resident_count_dec(pmap, 1);
2627 if (old_l3 & ATTR_SW_MANAGED) {
2628 m = PHYS_TO_VM_PAGE(old_l3 & ~ATTR_MASK);
2629 if (pmap_pte_dirty(old_l3))
2631 if (old_l3 & ATTR_AF)
2632 vm_page_aflag_set(m, PGA_REFERENCED);
2633 CHANGE_PV_LIST_LOCK_TO_VM_PAGE(lockp, m);
2634 pmap_pvh_free(&m->md, pmap, va);
2635 if (TAILQ_EMPTY(&m->md.pv_list) &&
2636 (m->flags & PG_FICTITIOUS) == 0) {
2637 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
2638 if (TAILQ_EMPTY(&pvh->pv_list))
2639 vm_page_aflag_clear(m, PGA_WRITEABLE);
2642 return (pmap_unuse_pt(pmap, va, l2e, free));
2646 * Remove the specified range of addresses from the L3 page table that is
2647 * identified by the given L2 entry.
2650 pmap_remove_l3_range(pmap_t pmap, pd_entry_t l2e, vm_offset_t sva,
2651 vm_offset_t eva, struct spglist *free, struct rwlock **lockp)
2653 struct md_page *pvh;
2654 struct rwlock *new_lock;
2655 pt_entry_t *l3, old_l3;
2659 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2660 KASSERT(rounddown2(sva, L2_SIZE) + L2_SIZE == roundup2(eva, L2_SIZE),
2661 ("pmap_remove_l3_range: range crosses an L3 page table boundary"));
2662 l3pg = sva < VM_MAXUSER_ADDRESS ? PHYS_TO_VM_PAGE(l2e & ~ATTR_MASK) :
2665 for (l3 = pmap_l2_to_l3(&l2e, sva); sva != eva; l3++, sva += L3_SIZE) {
2666 if (!pmap_l3_valid(pmap_load(l3))) {
2668 pmap_invalidate_range(pmap, va, sva);
2673 old_l3 = pmap_load_clear(l3);
2674 if ((old_l3 & ATTR_SW_WIRED) != 0)
2675 pmap->pm_stats.wired_count--;
2676 pmap_resident_count_dec(pmap, 1);
2677 if ((old_l3 & ATTR_SW_MANAGED) != 0) {
2678 m = PHYS_TO_VM_PAGE(old_l3 & ~ATTR_MASK);
2679 if (pmap_pte_dirty(old_l3))
2681 if ((old_l3 & ATTR_AF) != 0)
2682 vm_page_aflag_set(m, PGA_REFERENCED);
2683 new_lock = PHYS_TO_PV_LIST_LOCK(VM_PAGE_TO_PHYS(m));
2684 if (new_lock != *lockp) {
2685 if (*lockp != NULL) {
2687 * Pending TLB invalidations must be
2688 * performed before the PV list lock is
2689 * released. Otherwise, a concurrent
2690 * pmap_remove_all() on a physical page
2691 * could return while a stale TLB entry
2692 * still provides access to that page.
2695 pmap_invalidate_range(pmap, va,
2704 pmap_pvh_free(&m->md, pmap, sva);
2705 if (TAILQ_EMPTY(&m->md.pv_list) &&
2706 (m->flags & PG_FICTITIOUS) == 0) {
2707 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
2708 if (TAILQ_EMPTY(&pvh->pv_list))
2709 vm_page_aflag_clear(m, PGA_WRITEABLE);
2714 if (l3pg != NULL && pmap_unwire_l3(pmap, sva, l3pg, free)) {
2720 pmap_invalidate_range(pmap, va, sva);
2724 * Remove the given range of addresses from the specified map.
2726 * It is assumed that the start and end are properly
2727 * rounded to the page size.
2730 pmap_remove(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
2732 struct rwlock *lock;
2733 vm_offset_t va_next;
2734 pd_entry_t *l0, *l1, *l2;
2735 pt_entry_t l3_paddr;
2736 struct spglist free;
2739 * Perform an unsynchronized read. This is, however, safe.
2741 if (pmap->pm_stats.resident_count == 0)
2749 for (; sva < eva; sva = va_next) {
2751 if (pmap->pm_stats.resident_count == 0)
2754 l0 = pmap_l0(pmap, sva);
2755 if (pmap_load(l0) == 0) {
2756 va_next = (sva + L0_SIZE) & ~L0_OFFSET;
2762 l1 = pmap_l0_to_l1(l0, sva);
2763 if (pmap_load(l1) == 0) {
2764 va_next = (sva + L1_SIZE) & ~L1_OFFSET;
2771 * Calculate index for next page table.
2773 va_next = (sva + L2_SIZE) & ~L2_OFFSET;
2777 l2 = pmap_l1_to_l2(l1, sva);
2781 l3_paddr = pmap_load(l2);
2783 if ((l3_paddr & ATTR_DESCR_MASK) == L2_BLOCK) {
2784 if (sva + L2_SIZE == va_next && eva >= va_next) {
2785 pmap_remove_l2(pmap, l2, sva, pmap_load(l1),
2788 } else if (pmap_demote_l2_locked(pmap, l2, sva,
2791 l3_paddr = pmap_load(l2);
2795 * Weed out invalid mappings.
2797 if ((l3_paddr & ATTR_DESCR_MASK) != L2_TABLE)
2801 * Limit our scan to either the end of the va represented
2802 * by the current page table page, or to the end of the
2803 * range being removed.
2808 pmap_remove_l3_range(pmap, l3_paddr, sva, va_next, &free,
2814 vm_page_free_pages_toq(&free, true);
2818 * Routine: pmap_remove_all
2820 * Removes this physical page from
2821 * all physical maps in which it resides.
2822 * Reflects back modify bits to the pager.
2825 * Original versions of this routine were very
2826 * inefficient because they iteratively called
2827 * pmap_remove (slow...)
2831 pmap_remove_all(vm_page_t m)
2833 struct md_page *pvh;
2836 struct rwlock *lock;
2837 pd_entry_t *pde, tpde;
2838 pt_entry_t *pte, tpte;
2840 struct spglist free;
2841 int lvl, pvh_gen, md_gen;
2843 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2844 ("pmap_remove_all: page %p is not managed", m));
2846 lock = VM_PAGE_TO_PV_LIST_LOCK(m);
2847 pvh = (m->flags & PG_FICTITIOUS) != 0 ? &pv_dummy :
2848 pa_to_pvh(VM_PAGE_TO_PHYS(m));
2851 while ((pv = TAILQ_FIRST(&pvh->pv_list)) != NULL) {
2853 if (!PMAP_TRYLOCK(pmap)) {
2854 pvh_gen = pvh->pv_gen;
2858 if (pvh_gen != pvh->pv_gen) {
2865 pte = pmap_pte(pmap, va, &lvl);
2866 KASSERT(pte != NULL,
2867 ("pmap_remove_all: no page table entry found"));
2869 ("pmap_remove_all: invalid pte level %d", lvl));
2871 pmap_demote_l2_locked(pmap, pte, va, &lock);
2874 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
2876 if (!PMAP_TRYLOCK(pmap)) {
2877 pvh_gen = pvh->pv_gen;
2878 md_gen = m->md.pv_gen;
2882 if (pvh_gen != pvh->pv_gen || md_gen != m->md.pv_gen) {
2888 pmap_resident_count_dec(pmap, 1);
2890 pde = pmap_pde(pmap, pv->pv_va, &lvl);
2891 KASSERT(pde != NULL,
2892 ("pmap_remove_all: no page directory entry found"));
2894 ("pmap_remove_all: invalid pde level %d", lvl));
2895 tpde = pmap_load(pde);
2897 pte = pmap_l2_to_l3(pde, pv->pv_va);
2898 tpte = pmap_load_clear(pte);
2899 if (tpte & ATTR_SW_WIRED)
2900 pmap->pm_stats.wired_count--;
2901 if ((tpte & ATTR_AF) != 0) {
2902 pmap_invalidate_page(pmap, pv->pv_va);
2903 vm_page_aflag_set(m, PGA_REFERENCED);
2907 * Update the vm_page_t clean and reference bits.
2909 if (pmap_pte_dirty(tpte))
2911 pmap_unuse_pt(pmap, pv->pv_va, tpde, &free);
2912 TAILQ_REMOVE(&m->md.pv_list, pv, pv_next);
2914 free_pv_entry(pmap, pv);
2917 vm_page_aflag_clear(m, PGA_WRITEABLE);
2919 vm_page_free_pages_toq(&free, true);
2923 * pmap_protect_l2: do the things to protect a 2MB page in a pmap
2926 pmap_protect_l2(pmap_t pmap, pt_entry_t *l2, vm_offset_t sva, pt_entry_t mask,
2932 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2933 KASSERT((sva & L2_OFFSET) == 0,
2934 ("pmap_protect_l2: sva is not 2mpage aligned"));
2935 old_l2 = pmap_load(l2);
2936 KASSERT((old_l2 & ATTR_DESCR_MASK) == L2_BLOCK,
2937 ("pmap_protect_l2: L2e %lx is not a block mapping", old_l2));
2940 * Return if the L2 entry already has the desired access restrictions
2944 if ((old_l2 & mask) == nbits)
2948 * When a dirty read/write superpage mapping is write protected,
2949 * update the dirty field of each of the superpage's constituent 4KB
2952 if ((old_l2 & ATTR_SW_MANAGED) != 0 &&
2953 (nbits & ATTR_AP(ATTR_AP_RO)) != 0 && pmap_pte_dirty(old_l2)) {
2954 m = PHYS_TO_VM_PAGE(old_l2 & ~ATTR_MASK);
2955 for (mt = m; mt < &m[L2_SIZE / PAGE_SIZE]; mt++)
2959 if (!atomic_fcmpset_64(l2, &old_l2, (old_l2 & ~mask) | nbits))
2963 * Since a promotion must break the 4KB page mappings before making
2964 * the 2MB page mapping, a pmap_invalidate_page() suffices.
2966 pmap_invalidate_page(pmap, sva);
2970 * Set the physical protection on the
2971 * specified range of this map as requested.
2974 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
2976 vm_offset_t va, va_next;
2977 pd_entry_t *l0, *l1, *l2;
2978 pt_entry_t *l3p, l3, mask, nbits;
2980 KASSERT((prot & ~VM_PROT_ALL) == 0, ("invalid prot %x", prot));
2981 if (prot == VM_PROT_NONE) {
2982 pmap_remove(pmap, sva, eva);
2987 if ((prot & VM_PROT_WRITE) == 0) {
2988 mask |= ATTR_AP_RW_BIT | ATTR_SW_DBM;
2989 nbits |= ATTR_AP(ATTR_AP_RO);
2991 if ((prot & VM_PROT_EXECUTE) == 0) {
2999 for (; sva < eva; sva = va_next) {
3001 l0 = pmap_l0(pmap, sva);
3002 if (pmap_load(l0) == 0) {
3003 va_next = (sva + L0_SIZE) & ~L0_OFFSET;
3009 l1 = pmap_l0_to_l1(l0, sva);
3010 if (pmap_load(l1) == 0) {
3011 va_next = (sva + L1_SIZE) & ~L1_OFFSET;
3017 va_next = (sva + L2_SIZE) & ~L2_OFFSET;
3021 l2 = pmap_l1_to_l2(l1, sva);
3022 if (pmap_load(l2) == 0)
3025 if ((pmap_load(l2) & ATTR_DESCR_MASK) == L2_BLOCK) {
3026 if (sva + L2_SIZE == va_next && eva >= va_next) {
3027 pmap_protect_l2(pmap, l2, sva, mask, nbits);
3029 } else if (pmap_demote_l2(pmap, l2, sva) == NULL)
3032 KASSERT((pmap_load(l2) & ATTR_DESCR_MASK) == L2_TABLE,
3033 ("pmap_protect: Invalid L2 entry after demotion"));
3039 for (l3p = pmap_l2_to_l3(l2, sva); sva != va_next; l3p++,
3041 l3 = pmap_load(l3p);
3044 * Go to the next L3 entry if the current one is
3045 * invalid or already has the desired access
3046 * restrictions in place. (The latter case occurs
3047 * frequently. For example, in a "buildworld"
3048 * workload, almost 1 out of 4 L3 entries already
3049 * have the desired restrictions.)
3051 if (!pmap_l3_valid(l3) || (l3 & mask) == nbits) {
3052 if (va != va_next) {
3053 pmap_invalidate_range(pmap, va, sva);
3060 * When a dirty read/write mapping is write protected,
3061 * update the page's dirty field.
3063 if ((l3 & ATTR_SW_MANAGED) != 0 &&
3064 (nbits & ATTR_AP(ATTR_AP_RO)) != 0 &&
3066 vm_page_dirty(PHYS_TO_VM_PAGE(l3 & ~ATTR_MASK));
3068 if (!atomic_fcmpset_64(l3p, &l3, (l3 & ~mask) | nbits))
3074 pmap_invalidate_range(pmap, va, sva);
3080 * Inserts the specified page table page into the specified pmap's collection
3081 * of idle page table pages. Each of a pmap's page table pages is responsible
3082 * for mapping a distinct range of virtual addresses. The pmap's collection is
3083 * ordered by this virtual address range.
3085 * If "promoted" is false, then the page table page "mpte" must be zero filled.
3088 pmap_insert_pt_page(pmap_t pmap, vm_page_t mpte, bool promoted)
3091 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3092 mpte->valid = promoted ? VM_PAGE_BITS_ALL : 0;
3093 return (vm_radix_insert(&pmap->pm_root, mpte));
3097 * Removes the page table page mapping the specified virtual address from the
3098 * specified pmap's collection of idle page table pages, and returns it.
3099 * Otherwise, returns NULL if there is no page table page corresponding to the
3100 * specified virtual address.
3102 static __inline vm_page_t
3103 pmap_remove_pt_page(pmap_t pmap, vm_offset_t va)
3106 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3107 return (vm_radix_remove(&pmap->pm_root, pmap_l2_pindex(va)));
3111 * Performs a break-before-make update of a pmap entry. This is needed when
3112 * either promoting or demoting pages to ensure the TLB doesn't get into an
3113 * inconsistent state.
3116 pmap_update_entry(pmap_t pmap, pd_entry_t *pte, pd_entry_t newpte,
3117 vm_offset_t va, vm_size_t size)
3121 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3124 * Ensure we don't get switched out with the page table in an
3125 * inconsistent state. We also need to ensure no interrupts fire
3126 * as they may make use of an address we are about to invalidate.
3128 intr = intr_disable();
3131 * Clear the old mapping's valid bit, but leave the rest of the entry
3132 * unchanged, so that a lockless, concurrent pmap_kextract() can still
3133 * lookup the physical address.
3135 pmap_clear_bits(pte, ATTR_DESCR_VALID);
3136 pmap_invalidate_range(pmap, va, va + size);
3138 /* Create the new mapping */
3139 pmap_store(pte, newpte);
3145 #if VM_NRESERVLEVEL > 0
3147 * After promotion from 512 4KB page mappings to a single 2MB page mapping,
3148 * replace the many pv entries for the 4KB page mappings by a single pv entry
3149 * for the 2MB page mapping.
3152 pmap_pv_promote_l2(pmap_t pmap, vm_offset_t va, vm_paddr_t pa,
3153 struct rwlock **lockp)
3155 struct md_page *pvh;
3157 vm_offset_t va_last;
3160 KASSERT((pa & L2_OFFSET) == 0,
3161 ("pmap_pv_promote_l2: pa is not 2mpage aligned"));
3162 CHANGE_PV_LIST_LOCK_TO_PHYS(lockp, pa);
3165 * Transfer the first page's pv entry for this mapping to the 2mpage's
3166 * pv list. Aside from avoiding the cost of a call to get_pv_entry(),
3167 * a transfer avoids the possibility that get_pv_entry() calls
3168 * reclaim_pv_chunk() and that reclaim_pv_chunk() removes one of the
3169 * mappings that is being promoted.
3171 m = PHYS_TO_VM_PAGE(pa);
3172 va = va & ~L2_OFFSET;
3173 pv = pmap_pvh_remove(&m->md, pmap, va);
3174 KASSERT(pv != NULL, ("pmap_pv_promote_l2: pv not found"));
3175 pvh = pa_to_pvh(pa);
3176 TAILQ_INSERT_TAIL(&pvh->pv_list, pv, pv_next);
3178 /* Free the remaining NPTEPG - 1 pv entries. */
3179 va_last = va + L2_SIZE - PAGE_SIZE;
3183 pmap_pvh_free(&m->md, pmap, va);
3184 } while (va < va_last);
3188 * Tries to promote the 512, contiguous 4KB page mappings that are within a
3189 * single level 2 table entry to a single 2MB page mapping. For promotion
3190 * to occur, two conditions must be met: (1) the 4KB page mappings must map
3191 * aligned, contiguous physical memory and (2) the 4KB page mappings must have
3192 * identical characteristics.
3195 pmap_promote_l2(pmap_t pmap, pd_entry_t *l2, vm_offset_t va,
3196 struct rwlock **lockp)
3198 pt_entry_t *firstl3, *l3, newl2, oldl3, pa;
3202 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3204 sva = va & ~L2_OFFSET;
3205 firstl3 = pmap_l2_to_l3(l2, sva);
3206 newl2 = pmap_load(firstl3);
3209 if (((newl2 & (~ATTR_MASK | ATTR_AF)) & L2_OFFSET) != ATTR_AF) {
3210 atomic_add_long(&pmap_l2_p_failures, 1);
3211 CTR2(KTR_PMAP, "pmap_promote_l2: failure for va %#lx"
3212 " in pmap %p", va, pmap);
3216 if ((newl2 & (ATTR_AP_RW_BIT | ATTR_SW_DBM)) ==
3217 (ATTR_AP(ATTR_AP_RO) | ATTR_SW_DBM)) {
3218 if (!atomic_fcmpset_64(l2, &newl2, newl2 & ~ATTR_SW_DBM))
3220 newl2 &= ~ATTR_SW_DBM;
3223 pa = newl2 + L2_SIZE - PAGE_SIZE;
3224 for (l3 = firstl3 + NL3PG - 1; l3 > firstl3; l3--) {
3225 oldl3 = pmap_load(l3);
3227 if ((oldl3 & (ATTR_AP_RW_BIT | ATTR_SW_DBM)) ==
3228 (ATTR_AP(ATTR_AP_RO) | ATTR_SW_DBM)) {
3229 if (!atomic_fcmpset_64(l3, &oldl3, oldl3 &
3232 oldl3 &= ~ATTR_SW_DBM;
3235 atomic_add_long(&pmap_l2_p_failures, 1);
3236 CTR2(KTR_PMAP, "pmap_promote_l2: failure for va %#lx"
3237 " in pmap %p", va, pmap);
3244 * Save the page table page in its current state until the L2
3245 * mapping the superpage is demoted by pmap_demote_l2() or
3246 * destroyed by pmap_remove_l3().
3248 mpte = PHYS_TO_VM_PAGE(pmap_load(l2) & ~ATTR_MASK);
3249 KASSERT(mpte >= vm_page_array &&
3250 mpte < &vm_page_array[vm_page_array_size],
3251 ("pmap_promote_l2: page table page is out of range"));
3252 KASSERT(mpte->pindex == pmap_l2_pindex(va),
3253 ("pmap_promote_l2: page table page's pindex is wrong"));
3254 if (pmap_insert_pt_page(pmap, mpte, true)) {
3255 atomic_add_long(&pmap_l2_p_failures, 1);
3257 "pmap_promote_l2: failure for va %#lx in pmap %p", va,
3262 if ((newl2 & ATTR_SW_MANAGED) != 0)
3263 pmap_pv_promote_l2(pmap, va, newl2 & ~ATTR_MASK, lockp);
3265 newl2 &= ~ATTR_DESCR_MASK;
3268 pmap_update_entry(pmap, l2, newl2, sva, L2_SIZE);
3270 atomic_add_long(&pmap_l2_promotions, 1);
3271 CTR2(KTR_PMAP, "pmap_promote_l2: success for va %#lx in pmap %p", va,
3274 #endif /* VM_NRESERVLEVEL > 0 */
3277 * Insert the given physical page (p) at
3278 * the specified virtual address (v) in the
3279 * target physical map with the protection requested.
3281 * If specified, the page will be wired down, meaning
3282 * that the related pte can not be reclaimed.
3284 * NB: This is the only routine which MAY NOT lazy-evaluate
3285 * or lose information. That is, this routine must actually
3286 * insert this page into the given map NOW.
3289 pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
3290 u_int flags, int8_t psind)
3292 struct rwlock *lock;
3294 pt_entry_t new_l3, orig_l3;
3295 pt_entry_t *l2, *l3;
3302 va = trunc_page(va);
3303 if ((m->oflags & VPO_UNMANAGED) == 0)
3304 VM_PAGE_OBJECT_BUSY_ASSERT(m);
3305 pa = VM_PAGE_TO_PHYS(m);
3306 new_l3 = (pt_entry_t)(pa | ATTR_DEFAULT | ATTR_IDX(m->md.pv_memattr) |
3308 if ((prot & VM_PROT_WRITE) == 0)
3309 new_l3 |= ATTR_AP(ATTR_AP_RO);
3310 if ((prot & VM_PROT_EXECUTE) == 0 ||
3311 m->md.pv_memattr == VM_MEMATTR_DEVICE)
3313 if ((flags & PMAP_ENTER_WIRED) != 0)
3314 new_l3 |= ATTR_SW_WIRED;
3315 if (va < VM_MAXUSER_ADDRESS)
3316 new_l3 |= ATTR_AP(ATTR_AP_USER) | ATTR_PXN;
3319 if (pmap != kernel_pmap)
3321 if ((m->oflags & VPO_UNMANAGED) == 0) {
3322 new_l3 |= ATTR_SW_MANAGED;
3323 if ((prot & VM_PROT_WRITE) != 0) {
3324 new_l3 |= ATTR_SW_DBM;
3325 if ((flags & VM_PROT_WRITE) == 0)
3326 new_l3 |= ATTR_AP(ATTR_AP_RO);
3330 CTR2(KTR_PMAP, "pmap_enter: %.16lx -> %.16lx", va, pa);
3335 /* Assert the required virtual and physical alignment. */
3336 KASSERT((va & L2_OFFSET) == 0, ("pmap_enter: va unaligned"));
3337 KASSERT(m->psind > 0, ("pmap_enter: m->psind < psind"));
3338 rv = pmap_enter_l2(pmap, va, (new_l3 & ~L3_PAGE) | L2_BLOCK,
3345 * In the case that a page table page is not
3346 * resident, we are creating it here.
3349 pde = pmap_pde(pmap, va, &lvl);
3350 if (pde != NULL && lvl == 2) {
3351 l3 = pmap_l2_to_l3(pde, va);
3352 if (va < VM_MAXUSER_ADDRESS && mpte == NULL) {
3353 mpte = PHYS_TO_VM_PAGE(pmap_load(pde) & ~ATTR_MASK);
3357 } else if (pde != NULL && lvl == 1) {
3358 l2 = pmap_l1_to_l2(pde, va);
3359 if ((pmap_load(l2) & ATTR_DESCR_MASK) == L2_BLOCK &&
3360 (l3 = pmap_demote_l2_locked(pmap, l2, va, &lock)) != NULL) {
3361 l3 = &l3[pmap_l3_index(va)];
3362 if (va < VM_MAXUSER_ADDRESS) {
3363 mpte = PHYS_TO_VM_PAGE(
3364 pmap_load(l2) & ~ATTR_MASK);
3369 /* We need to allocate an L3 table. */
3371 if (va < VM_MAXUSER_ADDRESS) {
3372 nosleep = (flags & PMAP_ENTER_NOSLEEP) != 0;
3375 * We use _pmap_alloc_l3() instead of pmap_alloc_l3() in order
3376 * to handle the possibility that a superpage mapping for "va"
3377 * was created while we slept.
3379 mpte = _pmap_alloc_l3(pmap, pmap_l2_pindex(va),
3380 nosleep ? NULL : &lock);
3381 if (mpte == NULL && nosleep) {
3382 CTR0(KTR_PMAP, "pmap_enter: mpte == NULL");
3383 rv = KERN_RESOURCE_SHORTAGE;
3388 panic("pmap_enter: missing L3 table for kernel va %#lx", va);
3391 orig_l3 = pmap_load(l3);
3392 opa = orig_l3 & ~ATTR_MASK;
3396 * Is the specified virtual address already mapped?
3398 if (pmap_l3_valid(orig_l3)) {
3400 * Wiring change, just update stats. We don't worry about
3401 * wiring PT pages as they remain resident as long as there
3402 * are valid mappings in them. Hence, if a user page is wired,
3403 * the PT page will be also.
3405 if ((flags & PMAP_ENTER_WIRED) != 0 &&
3406 (orig_l3 & ATTR_SW_WIRED) == 0)
3407 pmap->pm_stats.wired_count++;
3408 else if ((flags & PMAP_ENTER_WIRED) == 0 &&
3409 (orig_l3 & ATTR_SW_WIRED) != 0)
3410 pmap->pm_stats.wired_count--;
3413 * Remove the extra PT page reference.
3417 KASSERT(mpte->ref_count > 0,
3418 ("pmap_enter: missing reference to page table page,"
3423 * Has the physical page changed?
3427 * No, might be a protection or wiring change.
3429 if ((orig_l3 & ATTR_SW_MANAGED) != 0 &&
3430 (new_l3 & ATTR_SW_DBM) != 0)
3431 vm_page_aflag_set(m, PGA_WRITEABLE);
3436 * The physical page has changed. Temporarily invalidate
3439 orig_l3 = pmap_load_clear(l3);
3440 KASSERT((orig_l3 & ~ATTR_MASK) == opa,
3441 ("pmap_enter: unexpected pa update for %#lx", va));
3442 if ((orig_l3 & ATTR_SW_MANAGED) != 0) {
3443 om = PHYS_TO_VM_PAGE(opa);
3446 * The pmap lock is sufficient to synchronize with
3447 * concurrent calls to pmap_page_test_mappings() and
3448 * pmap_ts_referenced().
3450 if (pmap_pte_dirty(orig_l3))
3452 if ((orig_l3 & ATTR_AF) != 0)
3453 vm_page_aflag_set(om, PGA_REFERENCED);
3454 CHANGE_PV_LIST_LOCK_TO_PHYS(&lock, opa);
3455 pv = pmap_pvh_remove(&om->md, pmap, va);
3456 if ((m->oflags & VPO_UNMANAGED) != 0)
3457 free_pv_entry(pmap, pv);
3458 if ((om->a.flags & PGA_WRITEABLE) != 0 &&
3459 TAILQ_EMPTY(&om->md.pv_list) &&
3460 ((om->flags & PG_FICTITIOUS) != 0 ||
3461 TAILQ_EMPTY(&pa_to_pvh(opa)->pv_list)))
3462 vm_page_aflag_clear(om, PGA_WRITEABLE);
3464 pmap_invalidate_page(pmap, va);
3468 * Increment the counters.
3470 if ((new_l3 & ATTR_SW_WIRED) != 0)
3471 pmap->pm_stats.wired_count++;
3472 pmap_resident_count_inc(pmap, 1);
3475 * Enter on the PV list if part of our managed memory.
3477 if ((m->oflags & VPO_UNMANAGED) == 0) {
3479 pv = get_pv_entry(pmap, &lock);
3482 CHANGE_PV_LIST_LOCK_TO_PHYS(&lock, pa);
3483 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_next);
3485 if ((new_l3 & ATTR_SW_DBM) != 0)
3486 vm_page_aflag_set(m, PGA_WRITEABLE);
3491 * Sync icache if exec permission and attribute VM_MEMATTR_WRITE_BACK
3492 * is set. Do it now, before the mapping is stored and made
3493 * valid for hardware table walk. If done later, then other can
3494 * access this page before caches are properly synced.
3495 * Don't do it for kernel memory which is mapped with exec
3496 * permission even if the memory isn't going to hold executable
3497 * code. The only time when icache sync is needed is after
3498 * kernel module is loaded and the relocation info is processed.
3499 * And it's done in elf_cpu_load_file().
3501 if ((prot & VM_PROT_EXECUTE) && pmap != kernel_pmap &&
3502 m->md.pv_memattr == VM_MEMATTR_WRITE_BACK &&
3503 (opa != pa || (orig_l3 & ATTR_XN)))
3504 cpu_icache_sync_range(PHYS_TO_DMAP(pa), PAGE_SIZE);
3507 * Update the L3 entry
3509 if (pmap_l3_valid(orig_l3)) {
3510 KASSERT(opa == pa, ("pmap_enter: invalid update"));
3511 if ((orig_l3 & ~ATTR_AF) != (new_l3 & ~ATTR_AF)) {
3512 /* same PA, different attributes */
3513 /* XXXMJ need to reload orig_l3 for hardware DBM. */
3514 pmap_load_store(l3, new_l3);
3515 pmap_invalidate_page(pmap, va);
3516 if ((orig_l3 & ATTR_SW_MANAGED) != 0 &&
3517 pmap_pte_dirty(orig_l3))
3522 * This can happens if multiple threads simultaneously
3523 * access not yet mapped page. This bad for performance
3524 * since this can cause full demotion-NOP-promotion
3526 * Another possible reasons are:
3527 * - VM and pmap memory layout are diverged
3528 * - tlb flush is missing somewhere and CPU doesn't see
3531 CTR4(KTR_PMAP, "%s: already mapped page - "
3532 "pmap %p va 0x%#lx pte 0x%lx",
3533 __func__, pmap, va, new_l3);
3537 pmap_store(l3, new_l3);
3541 #if VM_NRESERVLEVEL > 0
3542 if ((mpte == NULL || mpte->ref_count == NL3PG) &&
3543 pmap_ps_enabled(pmap) &&
3544 (m->flags & PG_FICTITIOUS) == 0 &&
3545 vm_reserv_level_iffullpop(m) == 0) {
3546 pmap_promote_l2(pmap, pde, va, &lock);
3559 * Tries to create a read- and/or execute-only 2MB page mapping. Returns true
3560 * if successful. Returns false if (1) a page table page cannot be allocated
3561 * without sleeping, (2) a mapping already exists at the specified virtual
3562 * address, or (3) a PV entry cannot be allocated without reclaiming another
3566 pmap_enter_2mpage(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
3567 struct rwlock **lockp)
3571 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3573 new_l2 = (pd_entry_t)(VM_PAGE_TO_PHYS(m) | ATTR_DEFAULT |
3574 ATTR_IDX(m->md.pv_memattr) | ATTR_AP(ATTR_AP_RO) | L2_BLOCK);
3575 if ((m->oflags & VPO_UNMANAGED) == 0) {
3576 new_l2 |= ATTR_SW_MANAGED;
3579 if ((prot & VM_PROT_EXECUTE) == 0 ||
3580 m->md.pv_memattr == VM_MEMATTR_DEVICE)
3582 if (va < VM_MAXUSER_ADDRESS)
3583 new_l2 |= ATTR_AP(ATTR_AP_USER) | ATTR_PXN;
3586 if (pmap != kernel_pmap)
3588 return (pmap_enter_l2(pmap, va, new_l2, PMAP_ENTER_NOSLEEP |
3589 PMAP_ENTER_NOREPLACE | PMAP_ENTER_NORECLAIM, NULL, lockp) ==
3594 * Returns true if every page table entry in the specified page table is
3598 pmap_every_pte_zero(vm_paddr_t pa)
3600 pt_entry_t *pt_end, *pte;
3602 KASSERT((pa & PAGE_MASK) == 0, ("pa is misaligned"));
3603 pte = (pt_entry_t *)PHYS_TO_DMAP(pa);
3604 for (pt_end = pte + Ln_ENTRIES; pte < pt_end; pte++) {
3612 * Tries to create the specified 2MB page mapping. Returns KERN_SUCCESS if
3613 * the mapping was created, and either KERN_FAILURE or KERN_RESOURCE_SHORTAGE
3614 * otherwise. Returns KERN_FAILURE if PMAP_ENTER_NOREPLACE was specified and
3615 * a mapping already exists at the specified virtual address. Returns
3616 * KERN_RESOURCE_SHORTAGE if PMAP_ENTER_NOSLEEP was specified and a page table
3617 * page allocation failed. Returns KERN_RESOURCE_SHORTAGE if
3618 * PMAP_ENTER_NORECLAIM was specified and a PV entry allocation failed.
3620 * The parameter "m" is only used when creating a managed, writeable mapping.
3623 pmap_enter_l2(pmap_t pmap, vm_offset_t va, pd_entry_t new_l2, u_int flags,
3624 vm_page_t m, struct rwlock **lockp)
3626 struct spglist free;
3627 pd_entry_t *l2, old_l2;
3630 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3632 if ((l2 = pmap_alloc_l2(pmap, va, &l2pg, (flags &
3633 PMAP_ENTER_NOSLEEP) != 0 ? NULL : lockp)) == NULL) {
3634 CTR2(KTR_PMAP, "pmap_enter_l2: failure for va %#lx in pmap %p",
3636 return (KERN_RESOURCE_SHORTAGE);
3640 * If there are existing mappings, either abort or remove them.
3642 if ((old_l2 = pmap_load(l2)) != 0) {
3643 KASSERT(l2pg == NULL || l2pg->ref_count > 1,
3644 ("pmap_enter_l2: l2pg's ref count is too low"));
3645 if ((flags & PMAP_ENTER_NOREPLACE) != 0 && (va <
3646 VM_MAXUSER_ADDRESS || (old_l2 & ATTR_DESCR_MASK) ==
3647 L2_BLOCK || !pmap_every_pte_zero(old_l2 & ~ATTR_MASK))) {
3650 CTR2(KTR_PMAP, "pmap_enter_l2: failure for va %#lx"
3651 " in pmap %p", va, pmap);
3652 return (KERN_FAILURE);
3655 if ((old_l2 & ATTR_DESCR_MASK) == L2_BLOCK)
3656 (void)pmap_remove_l2(pmap, l2, va,
3657 pmap_load(pmap_l1(pmap, va)), &free, lockp);
3659 pmap_remove_l3_range(pmap, old_l2, va, va + L2_SIZE,
3661 if (va < VM_MAXUSER_ADDRESS) {
3662 vm_page_free_pages_toq(&free, true);
3663 KASSERT(pmap_load(l2) == 0,
3664 ("pmap_enter_l2: non-zero L2 entry %p", l2));
3666 KASSERT(SLIST_EMPTY(&free),
3667 ("pmap_enter_l2: freed kernel page table page"));
3670 * Both pmap_remove_l2() and pmap_remove_l3_range()
3671 * will leave the kernel page table page zero filled.
3672 * Nonetheless, the TLB could have an intermediate
3673 * entry for the kernel page table page.
3675 mt = PHYS_TO_VM_PAGE(pmap_load(l2) & ~ATTR_MASK);
3676 if (pmap_insert_pt_page(pmap, mt, false))
3677 panic("pmap_enter_l2: trie insert failed");
3679 pmap_invalidate_page(pmap, va);
3683 if ((new_l2 & ATTR_SW_MANAGED) != 0) {
3685 * Abort this mapping if its PV entry could not be created.
3687 if (!pmap_pv_insert_l2(pmap, va, new_l2, flags, lockp)) {
3689 pmap_abort_ptp(pmap, va, l2pg);
3691 "pmap_enter_l2: failure for va %#lx in pmap %p",
3693 return (KERN_RESOURCE_SHORTAGE);
3695 if ((new_l2 & ATTR_SW_DBM) != 0)
3696 for (mt = m; mt < &m[L2_SIZE / PAGE_SIZE]; mt++)
3697 vm_page_aflag_set(mt, PGA_WRITEABLE);
3701 * Increment counters.
3703 if ((new_l2 & ATTR_SW_WIRED) != 0)
3704 pmap->pm_stats.wired_count += L2_SIZE / PAGE_SIZE;
3705 pmap->pm_stats.resident_count += L2_SIZE / PAGE_SIZE;
3708 * Map the superpage.
3710 pmap_store(l2, new_l2);
3713 atomic_add_long(&pmap_l2_mappings, 1);
3714 CTR2(KTR_PMAP, "pmap_enter_l2: success for va %#lx in pmap %p",
3717 return (KERN_SUCCESS);
3721 * Maps a sequence of resident pages belonging to the same object.
3722 * The sequence begins with the given page m_start. This page is
3723 * mapped at the given virtual address start. Each subsequent page is
3724 * mapped at a virtual address that is offset from start by the same
3725 * amount as the page is offset from m_start within the object. The
3726 * last page in the sequence is the page with the largest offset from
3727 * m_start that can be mapped at a virtual address less than the given
3728 * virtual address end. Not every virtual page between start and end
3729 * is mapped; only those for which a resident page exists with the
3730 * corresponding offset from m_start are mapped.
3733 pmap_enter_object(pmap_t pmap, vm_offset_t start, vm_offset_t end,
3734 vm_page_t m_start, vm_prot_t prot)
3736 struct rwlock *lock;
3739 vm_pindex_t diff, psize;
3741 VM_OBJECT_ASSERT_LOCKED(m_start->object);
3743 psize = atop(end - start);
3748 while (m != NULL && (diff = m->pindex - m_start->pindex) < psize) {
3749 va = start + ptoa(diff);
3750 if ((va & L2_OFFSET) == 0 && va + L2_SIZE <= end &&
3751 m->psind == 1 && pmap_ps_enabled(pmap) &&
3752 pmap_enter_2mpage(pmap, va, m, prot, &lock))
3753 m = &m[L2_SIZE / PAGE_SIZE - 1];
3755 mpte = pmap_enter_quick_locked(pmap, va, m, prot, mpte,
3757 m = TAILQ_NEXT(m, listq);
3765 * this code makes some *MAJOR* assumptions:
3766 * 1. Current pmap & pmap exists.
3769 * 4. No page table pages.
3770 * but is *MUCH* faster than pmap_enter...
3774 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot)
3776 struct rwlock *lock;
3780 (void)pmap_enter_quick_locked(pmap, va, m, prot, NULL, &lock);
3787 pmap_enter_quick_locked(pmap_t pmap, vm_offset_t va, vm_page_t m,
3788 vm_prot_t prot, vm_page_t mpte, struct rwlock **lockp)
3791 pt_entry_t *l2, *l3, l3_val;
3795 KASSERT(va < kmi.clean_sva || va >= kmi.clean_eva ||
3796 (m->oflags & VPO_UNMANAGED) != 0,
3797 ("pmap_enter_quick_locked: managed mapping within the clean submap"));
3798 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3800 CTR2(KTR_PMAP, "pmap_enter_quick_locked: %p %lx", pmap, va);
3802 * In the case that a page table page is not
3803 * resident, we are creating it here.
3805 if (va < VM_MAXUSER_ADDRESS) {
3806 vm_pindex_t l2pindex;
3809 * Calculate pagetable page index
3811 l2pindex = pmap_l2_pindex(va);
3812 if (mpte && (mpte->pindex == l2pindex)) {
3818 pde = pmap_pde(pmap, va, &lvl);
3821 * If the page table page is mapped, we just increment
3822 * the hold count, and activate it. Otherwise, we
3823 * attempt to allocate a page table page. If this
3824 * attempt fails, we don't retry. Instead, we give up.
3827 l2 = pmap_l1_to_l2(pde, va);
3828 if ((pmap_load(l2) & ATTR_DESCR_MASK) ==
3832 if (lvl == 2 && pmap_load(pde) != 0) {
3834 PHYS_TO_VM_PAGE(pmap_load(pde) & ~ATTR_MASK);
3838 * Pass NULL instead of the PV list lock
3839 * pointer, because we don't intend to sleep.
3841 mpte = _pmap_alloc_l3(pmap, l2pindex, NULL);
3846 l3 = (pt_entry_t *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(mpte));
3847 l3 = &l3[pmap_l3_index(va)];
3850 pde = pmap_pde(kernel_pmap, va, &lvl);
3851 KASSERT(pde != NULL,
3852 ("pmap_enter_quick_locked: Invalid page entry, va: 0x%lx",
3855 ("pmap_enter_quick_locked: Invalid level %d", lvl));
3856 l3 = pmap_l2_to_l3(pde, va);
3860 * Abort if a mapping already exists.
3862 if (pmap_load(l3) != 0) {
3869 * Enter on the PV list if part of our managed memory.
3871 if ((m->oflags & VPO_UNMANAGED) == 0 &&
3872 !pmap_try_insert_pv_entry(pmap, va, m, lockp)) {
3874 pmap_abort_ptp(pmap, va, mpte);
3879 * Increment counters
3881 pmap_resident_count_inc(pmap, 1);
3883 pa = VM_PAGE_TO_PHYS(m);
3884 l3_val = pa | ATTR_DEFAULT | ATTR_IDX(m->md.pv_memattr) |
3885 ATTR_AP(ATTR_AP_RO) | L3_PAGE;
3886 if ((prot & VM_PROT_EXECUTE) == 0 ||
3887 m->md.pv_memattr == VM_MEMATTR_DEVICE)
3889 if (va < VM_MAXUSER_ADDRESS)
3890 l3_val |= ATTR_AP(ATTR_AP_USER) | ATTR_PXN;
3893 if (pmap != kernel_pmap)
3897 * Now validate mapping with RO protection
3899 if ((m->oflags & VPO_UNMANAGED) == 0) {
3900 l3_val |= ATTR_SW_MANAGED;
3904 /* Sync icache before the mapping is stored to PTE */
3905 if ((prot & VM_PROT_EXECUTE) && pmap != kernel_pmap &&
3906 m->md.pv_memattr == VM_MEMATTR_WRITE_BACK)
3907 cpu_icache_sync_range(PHYS_TO_DMAP(pa), PAGE_SIZE);
3909 pmap_store(l3, l3_val);
3916 * This code maps large physical mmap regions into the
3917 * processor address space. Note that some shortcuts
3918 * are taken, but the code works.
3921 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr, vm_object_t object,
3922 vm_pindex_t pindex, vm_size_t size)
3925 VM_OBJECT_ASSERT_WLOCKED(object);
3926 KASSERT(object->type == OBJT_DEVICE || object->type == OBJT_SG,
3927 ("pmap_object_init_pt: non-device object"));
3931 * Clear the wired attribute from the mappings for the specified range of
3932 * addresses in the given pmap. Every valid mapping within that range
3933 * must have the wired attribute set. In contrast, invalid mappings
3934 * cannot have the wired attribute set, so they are ignored.
3936 * The wired attribute of the page table entry is not a hardware feature,
3937 * so there is no need to invalidate any TLB entries.
3940 pmap_unwire(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
3942 vm_offset_t va_next;
3943 pd_entry_t *l0, *l1, *l2;
3947 for (; sva < eva; sva = va_next) {
3948 l0 = pmap_l0(pmap, sva);
3949 if (pmap_load(l0) == 0) {
3950 va_next = (sva + L0_SIZE) & ~L0_OFFSET;
3956 l1 = pmap_l0_to_l1(l0, sva);
3957 if (pmap_load(l1) == 0) {
3958 va_next = (sva + L1_SIZE) & ~L1_OFFSET;
3964 va_next = (sva + L2_SIZE) & ~L2_OFFSET;
3968 l2 = pmap_l1_to_l2(l1, sva);
3969 if (pmap_load(l2) == 0)
3972 if ((pmap_load(l2) & ATTR_DESCR_MASK) == L2_BLOCK) {
3973 if ((pmap_load(l2) & ATTR_SW_WIRED) == 0)
3974 panic("pmap_unwire: l2 %#jx is missing "
3975 "ATTR_SW_WIRED", (uintmax_t)pmap_load(l2));
3978 * Are we unwiring the entire large page? If not,
3979 * demote the mapping and fall through.
3981 if (sva + L2_SIZE == va_next && eva >= va_next) {
3982 pmap_clear_bits(l2, ATTR_SW_WIRED);
3983 pmap->pm_stats.wired_count -= L2_SIZE /
3986 } else if (pmap_demote_l2(pmap, l2, sva) == NULL)
3987 panic("pmap_unwire: demotion failed");
3989 KASSERT((pmap_load(l2) & ATTR_DESCR_MASK) == L2_TABLE,
3990 ("pmap_unwire: Invalid l2 entry after demotion"));
3994 for (l3 = pmap_l2_to_l3(l2, sva); sva != va_next; l3++,
3996 if (pmap_load(l3) == 0)
3998 if ((pmap_load(l3) & ATTR_SW_WIRED) == 0)
3999 panic("pmap_unwire: l3 %#jx is missing "
4000 "ATTR_SW_WIRED", (uintmax_t)pmap_load(l3));
4003 * ATTR_SW_WIRED must be cleared atomically. Although
4004 * the pmap lock synchronizes access to ATTR_SW_WIRED,
4005 * the System MMU may write to the entry concurrently.
4007 pmap_clear_bits(l3, ATTR_SW_WIRED);
4008 pmap->pm_stats.wired_count--;
4015 * Copy the range specified by src_addr/len
4016 * from the source map to the range dst_addr/len
4017 * in the destination map.
4019 * This routine is only advisory and need not do anything.
4021 * Because the executable mappings created by this routine are copied,
4022 * it should not have to flush the instruction cache.
4025 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr, vm_size_t len,
4026 vm_offset_t src_addr)
4028 struct rwlock *lock;
4029 pd_entry_t *l0, *l1, *l2, srcptepaddr;
4030 pt_entry_t *dst_pte, mask, nbits, ptetemp, *src_pte;
4031 vm_offset_t addr, end_addr, va_next;
4032 vm_page_t dst_l2pg, dstmpte, srcmpte;
4034 if (dst_addr != src_addr)
4036 end_addr = src_addr + len;
4038 if (dst_pmap < src_pmap) {
4039 PMAP_LOCK(dst_pmap);
4040 PMAP_LOCK(src_pmap);
4042 PMAP_LOCK(src_pmap);
4043 PMAP_LOCK(dst_pmap);
4045 for (addr = src_addr; addr < end_addr; addr = va_next) {
4046 l0 = pmap_l0(src_pmap, addr);
4047 if (pmap_load(l0) == 0) {
4048 va_next = (addr + L0_SIZE) & ~L0_OFFSET;
4053 l1 = pmap_l0_to_l1(l0, addr);
4054 if (pmap_load(l1) == 0) {
4055 va_next = (addr + L1_SIZE) & ~L1_OFFSET;
4060 va_next = (addr + L2_SIZE) & ~L2_OFFSET;
4063 l2 = pmap_l1_to_l2(l1, addr);
4064 srcptepaddr = pmap_load(l2);
4065 if (srcptepaddr == 0)
4067 if ((srcptepaddr & ATTR_DESCR_MASK) == L2_BLOCK) {
4068 if ((addr & L2_OFFSET) != 0 ||
4069 addr + L2_SIZE > end_addr)
4071 l2 = pmap_alloc_l2(dst_pmap, addr, &dst_l2pg, NULL);
4074 if (pmap_load(l2) == 0 &&
4075 ((srcptepaddr & ATTR_SW_MANAGED) == 0 ||
4076 pmap_pv_insert_l2(dst_pmap, addr, srcptepaddr,
4077 PMAP_ENTER_NORECLAIM, &lock))) {
4078 mask = ATTR_AF | ATTR_SW_WIRED;
4080 if ((srcptepaddr & ATTR_SW_DBM) != 0)
4081 nbits |= ATTR_AP_RW_BIT;
4082 pmap_store(l2, (srcptepaddr & ~mask) | nbits);
4083 pmap_resident_count_inc(dst_pmap, L2_SIZE /
4085 atomic_add_long(&pmap_l2_mappings, 1);
4087 pmap_abort_ptp(dst_pmap, addr, dst_l2pg);
4090 KASSERT((srcptepaddr & ATTR_DESCR_MASK) == L2_TABLE,
4091 ("pmap_copy: invalid L2 entry"));
4092 srcptepaddr &= ~ATTR_MASK;
4093 srcmpte = PHYS_TO_VM_PAGE(srcptepaddr);
4094 KASSERT(srcmpte->ref_count > 0,
4095 ("pmap_copy: source page table page is unused"));
4096 if (va_next > end_addr)
4098 src_pte = (pt_entry_t *)PHYS_TO_DMAP(srcptepaddr);
4099 src_pte = &src_pte[pmap_l3_index(addr)];
4101 for (; addr < va_next; addr += PAGE_SIZE, src_pte++) {
4102 ptetemp = pmap_load(src_pte);
4105 * We only virtual copy managed pages.
4107 if ((ptetemp & ATTR_SW_MANAGED) == 0)
4110 if (dstmpte != NULL) {
4111 KASSERT(dstmpte->pindex == pmap_l2_pindex(addr),
4112 ("dstmpte pindex/addr mismatch"));
4113 dstmpte->ref_count++;
4114 } else if ((dstmpte = pmap_alloc_l3(dst_pmap, addr,
4117 dst_pte = (pt_entry_t *)
4118 PHYS_TO_DMAP(VM_PAGE_TO_PHYS(dstmpte));
4119 dst_pte = &dst_pte[pmap_l3_index(addr)];
4120 if (pmap_load(dst_pte) == 0 &&
4121 pmap_try_insert_pv_entry(dst_pmap, addr,
4122 PHYS_TO_VM_PAGE(ptetemp & ~ATTR_MASK), &lock)) {
4124 * Clear the wired, modified, and accessed
4125 * (referenced) bits during the copy.
4127 mask = ATTR_AF | ATTR_SW_WIRED;
4129 if ((ptetemp & ATTR_SW_DBM) != 0)
4130 nbits |= ATTR_AP_RW_BIT;
4131 pmap_store(dst_pte, (ptetemp & ~mask) | nbits);
4132 pmap_resident_count_inc(dst_pmap, 1);
4134 pmap_abort_ptp(dst_pmap, addr, dstmpte);
4137 /* Have we copied all of the valid mappings? */
4138 if (dstmpte->ref_count >= srcmpte->ref_count)
4144 * XXX This barrier may not be needed because the destination pmap is
4151 PMAP_UNLOCK(src_pmap);
4152 PMAP_UNLOCK(dst_pmap);
4156 * pmap_zero_page zeros the specified hardware page by mapping
4157 * the page into KVM and using bzero to clear its contents.
4160 pmap_zero_page(vm_page_t m)
4162 vm_offset_t va = PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m));
4164 pagezero((void *)va);
4168 * pmap_zero_page_area zeros the specified hardware page by mapping
4169 * the page into KVM and using bzero to clear its contents.
4171 * off and size may not cover an area beyond a single hardware page.
4174 pmap_zero_page_area(vm_page_t m, int off, int size)
4176 vm_offset_t va = PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m));
4178 if (off == 0 && size == PAGE_SIZE)
4179 pagezero((void *)va);
4181 bzero((char *)va + off, size);
4185 * pmap_copy_page copies the specified (machine independent)
4186 * page by mapping the page into virtual memory and using
4187 * bcopy to copy the page, one machine dependent page at a
4191 pmap_copy_page(vm_page_t msrc, vm_page_t mdst)
4193 vm_offset_t src = PHYS_TO_DMAP(VM_PAGE_TO_PHYS(msrc));
4194 vm_offset_t dst = PHYS_TO_DMAP(VM_PAGE_TO_PHYS(mdst));
4196 pagecopy((void *)src, (void *)dst);
4199 int unmapped_buf_allowed = 1;
4202 pmap_copy_pages(vm_page_t ma[], vm_offset_t a_offset, vm_page_t mb[],
4203 vm_offset_t b_offset, int xfersize)
4207 vm_paddr_t p_a, p_b;
4208 vm_offset_t a_pg_offset, b_pg_offset;
4211 while (xfersize > 0) {
4212 a_pg_offset = a_offset & PAGE_MASK;
4213 m_a = ma[a_offset >> PAGE_SHIFT];
4214 p_a = m_a->phys_addr;
4215 b_pg_offset = b_offset & PAGE_MASK;
4216 m_b = mb[b_offset >> PAGE_SHIFT];
4217 p_b = m_b->phys_addr;
4218 cnt = min(xfersize, PAGE_SIZE - a_pg_offset);
4219 cnt = min(cnt, PAGE_SIZE - b_pg_offset);
4220 if (__predict_false(!PHYS_IN_DMAP(p_a))) {
4221 panic("!DMAP a %lx", p_a);
4223 a_cp = (char *)PHYS_TO_DMAP(p_a) + a_pg_offset;
4225 if (__predict_false(!PHYS_IN_DMAP(p_b))) {
4226 panic("!DMAP b %lx", p_b);
4228 b_cp = (char *)PHYS_TO_DMAP(p_b) + b_pg_offset;
4230 bcopy(a_cp, b_cp, cnt);
4238 pmap_quick_enter_page(vm_page_t m)
4241 return (PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m)));
4245 pmap_quick_remove_page(vm_offset_t addr)
4250 * Returns true if the pmap's pv is one of the first
4251 * 16 pvs linked to from this page. This count may
4252 * be changed upwards or downwards in the future; it
4253 * is only necessary that true be returned for a small
4254 * subset of pmaps for proper page aging.
4257 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
4259 struct md_page *pvh;
4260 struct rwlock *lock;
4265 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4266 ("pmap_page_exists_quick: page %p is not managed", m));
4268 lock = VM_PAGE_TO_PV_LIST_LOCK(m);
4270 TAILQ_FOREACH(pv, &m->md.pv_list, pv_next) {
4271 if (PV_PMAP(pv) == pmap) {
4279 if (!rv && loops < 16 && (m->flags & PG_FICTITIOUS) == 0) {
4280 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
4281 TAILQ_FOREACH(pv, &pvh->pv_list, pv_next) {
4282 if (PV_PMAP(pv) == pmap) {
4296 * pmap_page_wired_mappings:
4298 * Return the number of managed mappings to the given physical page
4302 pmap_page_wired_mappings(vm_page_t m)
4304 struct rwlock *lock;
4305 struct md_page *pvh;
4309 int count, lvl, md_gen, pvh_gen;
4311 if ((m->oflags & VPO_UNMANAGED) != 0)
4313 lock = VM_PAGE_TO_PV_LIST_LOCK(m);
4317 TAILQ_FOREACH(pv, &m->md.pv_list, pv_next) {
4319 if (!PMAP_TRYLOCK(pmap)) {
4320 md_gen = m->md.pv_gen;
4324 if (md_gen != m->md.pv_gen) {
4329 pte = pmap_pte(pmap, pv->pv_va, &lvl);
4330 if (pte != NULL && (pmap_load(pte) & ATTR_SW_WIRED) != 0)
4334 if ((m->flags & PG_FICTITIOUS) == 0) {
4335 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
4336 TAILQ_FOREACH(pv, &pvh->pv_list, pv_next) {
4338 if (!PMAP_TRYLOCK(pmap)) {
4339 md_gen = m->md.pv_gen;
4340 pvh_gen = pvh->pv_gen;
4344 if (md_gen != m->md.pv_gen ||
4345 pvh_gen != pvh->pv_gen) {
4350 pte = pmap_pte(pmap, pv->pv_va, &lvl);
4352 (pmap_load(pte) & ATTR_SW_WIRED) != 0)
4362 * Returns true if the given page is mapped individually or as part of
4363 * a 2mpage. Otherwise, returns false.
4366 pmap_page_is_mapped(vm_page_t m)
4368 struct rwlock *lock;
4371 if ((m->oflags & VPO_UNMANAGED) != 0)
4373 lock = VM_PAGE_TO_PV_LIST_LOCK(m);
4375 rv = !TAILQ_EMPTY(&m->md.pv_list) ||
4376 ((m->flags & PG_FICTITIOUS) == 0 &&
4377 !TAILQ_EMPTY(&pa_to_pvh(VM_PAGE_TO_PHYS(m))->pv_list));
4383 * Destroy all managed, non-wired mappings in the given user-space
4384 * pmap. This pmap cannot be active on any processor besides the
4387 * This function cannot be applied to the kernel pmap. Moreover, it
4388 * is not intended for general use. It is only to be used during
4389 * process termination. Consequently, it can be implemented in ways
4390 * that make it faster than pmap_remove(). First, it can more quickly
4391 * destroy mappings by iterating over the pmap's collection of PV
4392 * entries, rather than searching the page table. Second, it doesn't
4393 * have to test and clear the page table entries atomically, because
4394 * no processor is currently accessing the user address space. In
4395 * particular, a page table entry's dirty bit won't change state once
4396 * this function starts.
4399 pmap_remove_pages(pmap_t pmap)
4402 pt_entry_t *pte, tpte;
4403 struct spglist free;
4404 vm_page_t m, ml3, mt;
4406 struct md_page *pvh;
4407 struct pv_chunk *pc, *npc;
4408 struct rwlock *lock;
4410 uint64_t inuse, bitmask;
4411 int allfree, field, freed, idx, lvl;
4414 KASSERT(pmap == PCPU_GET(curpmap), ("non-current pmap %p", pmap));
4420 TAILQ_FOREACH_SAFE(pc, &pmap->pm_pvchunk, pc_list, npc) {
4423 for (field = 0; field < _NPCM; field++) {
4424 inuse = ~pc->pc_map[field] & pc_freemask[field];
4425 while (inuse != 0) {
4426 bit = ffsl(inuse) - 1;
4427 bitmask = 1UL << bit;
4428 idx = field * 64 + bit;
4429 pv = &pc->pc_pventry[idx];
4432 pde = pmap_pde(pmap, pv->pv_va, &lvl);
4433 KASSERT(pde != NULL,
4434 ("Attempting to remove an unmapped page"));
4438 pte = pmap_l1_to_l2(pde, pv->pv_va);
4439 tpte = pmap_load(pte);
4440 KASSERT((tpte & ATTR_DESCR_MASK) ==
4442 ("Attempting to remove an invalid "
4443 "block: %lx", tpte));
4446 pte = pmap_l2_to_l3(pde, pv->pv_va);
4447 tpte = pmap_load(pte);
4448 KASSERT((tpte & ATTR_DESCR_MASK) ==
4450 ("Attempting to remove an invalid "
4451 "page: %lx", tpte));
4455 "Invalid page directory level: %d",
4460 * We cannot remove wired pages from a process' mapping at this time
4462 if (tpte & ATTR_SW_WIRED) {
4467 pa = tpte & ~ATTR_MASK;
4469 m = PHYS_TO_VM_PAGE(pa);
4470 KASSERT(m->phys_addr == pa,
4471 ("vm_page_t %p phys_addr mismatch %016jx %016jx",
4472 m, (uintmax_t)m->phys_addr,
4475 KASSERT((m->flags & PG_FICTITIOUS) != 0 ||
4476 m < &vm_page_array[vm_page_array_size],
4477 ("pmap_remove_pages: bad pte %#jx",
4481 * Because this pmap is not active on other
4482 * processors, the dirty bit cannot have
4483 * changed state since we last loaded pte.
4488 * Update the vm_page_t clean/reference bits.
4490 if (pmap_pte_dirty(tpte)) {
4493 for (mt = m; mt < &m[L2_SIZE / PAGE_SIZE]; mt++)
4502 CHANGE_PV_LIST_LOCK_TO_VM_PAGE(&lock, m);
4505 pc->pc_map[field] |= bitmask;
4508 pmap_resident_count_dec(pmap,
4509 L2_SIZE / PAGE_SIZE);
4510 pvh = pa_to_pvh(tpte & ~ATTR_MASK);
4511 TAILQ_REMOVE(&pvh->pv_list, pv,pv_next);
4513 if (TAILQ_EMPTY(&pvh->pv_list)) {
4514 for (mt = m; mt < &m[L2_SIZE / PAGE_SIZE]; mt++)
4515 if ((mt->a.flags & PGA_WRITEABLE) != 0 &&
4516 TAILQ_EMPTY(&mt->md.pv_list))
4517 vm_page_aflag_clear(mt, PGA_WRITEABLE);
4519 ml3 = pmap_remove_pt_page(pmap,
4522 KASSERT(ml3->valid == VM_PAGE_BITS_ALL,
4523 ("pmap_remove_pages: l3 page not promoted"));
4524 pmap_resident_count_dec(pmap,1);
4525 KASSERT(ml3->ref_count == NL3PG,
4526 ("pmap_remove_pages: l3 page ref count error"));
4528 pmap_add_delayed_free_list(ml3,
4533 pmap_resident_count_dec(pmap, 1);
4534 TAILQ_REMOVE(&m->md.pv_list, pv,
4537 if ((m->a.flags & PGA_WRITEABLE) != 0 &&
4538 TAILQ_EMPTY(&m->md.pv_list) &&
4539 (m->flags & PG_FICTITIOUS) == 0) {
4541 VM_PAGE_TO_PHYS(m));
4542 if (TAILQ_EMPTY(&pvh->pv_list))
4543 vm_page_aflag_clear(m,
4548 pmap_unuse_pt(pmap, pv->pv_va, pmap_load(pde),
4553 PV_STAT(atomic_add_long(&pv_entry_frees, freed));
4554 PV_STAT(atomic_add_int(&pv_entry_spare, freed));
4555 PV_STAT(atomic_subtract_long(&pv_entry_count, freed));
4557 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
4563 pmap_invalidate_all(pmap);
4565 vm_page_free_pages_toq(&free, true);
4569 * This is used to check if a page has been accessed or modified.
4572 pmap_page_test_mappings(vm_page_t m, boolean_t accessed, boolean_t modified)
4574 struct rwlock *lock;
4576 struct md_page *pvh;
4577 pt_entry_t *pte, mask, value;
4579 int lvl, md_gen, pvh_gen;
4583 lock = VM_PAGE_TO_PV_LIST_LOCK(m);
4586 TAILQ_FOREACH(pv, &m->md.pv_list, pv_next) {
4588 if (!PMAP_TRYLOCK(pmap)) {
4589 md_gen = m->md.pv_gen;
4593 if (md_gen != m->md.pv_gen) {
4598 pte = pmap_pte(pmap, pv->pv_va, &lvl);
4600 ("pmap_page_test_mappings: Invalid level %d", lvl));
4604 mask |= ATTR_AP_RW_BIT;
4605 value |= ATTR_AP(ATTR_AP_RW);
4608 mask |= ATTR_AF | ATTR_DESCR_MASK;
4609 value |= ATTR_AF | L3_PAGE;
4611 rv = (pmap_load(pte) & mask) == value;
4616 if ((m->flags & PG_FICTITIOUS) == 0) {
4617 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
4618 TAILQ_FOREACH(pv, &pvh->pv_list, pv_next) {
4620 if (!PMAP_TRYLOCK(pmap)) {
4621 md_gen = m->md.pv_gen;
4622 pvh_gen = pvh->pv_gen;
4626 if (md_gen != m->md.pv_gen ||
4627 pvh_gen != pvh->pv_gen) {
4632 pte = pmap_pte(pmap, pv->pv_va, &lvl);
4634 ("pmap_page_test_mappings: Invalid level %d", lvl));
4638 mask |= ATTR_AP_RW_BIT;
4639 value |= ATTR_AP(ATTR_AP_RW);
4642 mask |= ATTR_AF | ATTR_DESCR_MASK;
4643 value |= ATTR_AF | L2_BLOCK;
4645 rv = (pmap_load(pte) & mask) == value;
4659 * Return whether or not the specified physical page was modified
4660 * in any physical maps.
4663 pmap_is_modified(vm_page_t m)
4666 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4667 ("pmap_is_modified: page %p is not managed", m));
4670 * If the page is not busied then this check is racy.
4672 if (!pmap_page_is_write_mapped(m))
4674 return (pmap_page_test_mappings(m, FALSE, TRUE));
4678 * pmap_is_prefaultable:
4680 * Return whether or not the specified virtual address is eligible
4684 pmap_is_prefaultable(pmap_t pmap, vm_offset_t addr)
4692 pte = pmap_pte(pmap, addr, &lvl);
4693 if (pte != NULL && pmap_load(pte) != 0) {
4701 * pmap_is_referenced:
4703 * Return whether or not the specified physical page was referenced
4704 * in any physical maps.
4707 pmap_is_referenced(vm_page_t m)
4710 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4711 ("pmap_is_referenced: page %p is not managed", m));
4712 return (pmap_page_test_mappings(m, TRUE, FALSE));
4716 * Clear the write and modified bits in each of the given page's mappings.
4719 pmap_remove_write(vm_page_t m)
4721 struct md_page *pvh;
4723 struct rwlock *lock;
4724 pv_entry_t next_pv, pv;
4725 pt_entry_t oldpte, *pte;
4727 int lvl, md_gen, pvh_gen;
4729 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4730 ("pmap_remove_write: page %p is not managed", m));
4731 vm_page_assert_busied(m);
4733 if (!pmap_page_is_write_mapped(m))
4735 lock = VM_PAGE_TO_PV_LIST_LOCK(m);
4736 pvh = (m->flags & PG_FICTITIOUS) != 0 ? &pv_dummy :
4737 pa_to_pvh(VM_PAGE_TO_PHYS(m));
4740 TAILQ_FOREACH_SAFE(pv, &pvh->pv_list, pv_next, next_pv) {
4742 if (!PMAP_TRYLOCK(pmap)) {
4743 pvh_gen = pvh->pv_gen;
4747 if (pvh_gen != pvh->pv_gen) {
4754 pte = pmap_pte(pmap, pv->pv_va, &lvl);
4755 if ((pmap_load(pte) & ATTR_SW_DBM) != 0)
4756 (void)pmap_demote_l2_locked(pmap, pte, va, &lock);
4757 KASSERT(lock == VM_PAGE_TO_PV_LIST_LOCK(m),
4758 ("inconsistent pv lock %p %p for page %p",
4759 lock, VM_PAGE_TO_PV_LIST_LOCK(m), m));
4762 TAILQ_FOREACH(pv, &m->md.pv_list, pv_next) {
4764 if (!PMAP_TRYLOCK(pmap)) {
4765 pvh_gen = pvh->pv_gen;
4766 md_gen = m->md.pv_gen;
4770 if (pvh_gen != pvh->pv_gen ||
4771 md_gen != m->md.pv_gen) {
4777 pte = pmap_pte(pmap, pv->pv_va, &lvl);
4778 oldpte = pmap_load(pte);
4780 if ((oldpte & ATTR_SW_DBM) != 0) {
4781 if (!atomic_fcmpset_long(pte, &oldpte,
4782 (oldpte | ATTR_AP_RW_BIT) & ~ATTR_SW_DBM))
4784 if ((oldpte & ATTR_AP_RW_BIT) ==
4785 ATTR_AP(ATTR_AP_RW))
4787 pmap_invalidate_page(pmap, pv->pv_va);
4792 vm_page_aflag_clear(m, PGA_WRITEABLE);
4796 * pmap_ts_referenced:
4798 * Return a count of reference bits for a page, clearing those bits.
4799 * It is not necessary for every reference bit to be cleared, but it
4800 * is necessary that 0 only be returned when there are truly no
4801 * reference bits set.
4803 * As an optimization, update the page's dirty field if a modified bit is
4804 * found while counting reference bits. This opportunistic update can be
4805 * performed at low cost and can eliminate the need for some future calls
4806 * to pmap_is_modified(). However, since this function stops after
4807 * finding PMAP_TS_REFERENCED_MAX reference bits, it may not detect some
4808 * dirty pages. Those dirty pages will only be detected by a future call
4809 * to pmap_is_modified().
4812 pmap_ts_referenced(vm_page_t m)
4814 struct md_page *pvh;
4817 struct rwlock *lock;
4818 pd_entry_t *pde, tpde;
4819 pt_entry_t *pte, tpte;
4822 int cleared, lvl, md_gen, not_cleared, pvh_gen;
4823 struct spglist free;
4825 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4826 ("pmap_ts_referenced: page %p is not managed", m));
4829 pa = VM_PAGE_TO_PHYS(m);
4830 lock = PHYS_TO_PV_LIST_LOCK(pa);
4831 pvh = (m->flags & PG_FICTITIOUS) != 0 ? &pv_dummy : pa_to_pvh(pa);
4835 if ((pvf = TAILQ_FIRST(&pvh->pv_list)) == NULL)
4836 goto small_mappings;
4842 if (!PMAP_TRYLOCK(pmap)) {
4843 pvh_gen = pvh->pv_gen;
4847 if (pvh_gen != pvh->pv_gen) {
4853 pde = pmap_pde(pmap, pv->pv_va, &lvl);
4854 KASSERT(pde != NULL, ("pmap_ts_referenced: no l1 table found"));
4856 ("pmap_ts_referenced: invalid pde level %d", lvl));
4857 tpde = pmap_load(pde);
4858 KASSERT((tpde & ATTR_DESCR_MASK) == L1_TABLE,
4859 ("pmap_ts_referenced: found an invalid l1 table"));
4860 pte = pmap_l1_to_l2(pde, pv->pv_va);
4861 tpte = pmap_load(pte);
4862 if (pmap_pte_dirty(tpte)) {
4864 * Although "tpte" is mapping a 2MB page, because
4865 * this function is called at a 4KB page granularity,
4866 * we only update the 4KB page under test.
4871 if ((tpte & ATTR_AF) != 0) {
4873 * Since this reference bit is shared by 512 4KB pages,
4874 * it should not be cleared every time it is tested.
4875 * Apply a simple "hash" function on the physical page
4876 * number, the virtual superpage number, and the pmap
4877 * address to select one 4KB page out of the 512 on
4878 * which testing the reference bit will result in
4879 * clearing that reference bit. This function is
4880 * designed to avoid the selection of the same 4KB page
4881 * for every 2MB page mapping.
4883 * On demotion, a mapping that hasn't been referenced
4884 * is simply destroyed. To avoid the possibility of a
4885 * subsequent page fault on a demoted wired mapping,
4886 * always leave its reference bit set. Moreover,
4887 * since the superpage is wired, the current state of
4888 * its reference bit won't affect page replacement.
4890 if ((((pa >> PAGE_SHIFT) ^ (pv->pv_va >> L2_SHIFT) ^
4891 (uintptr_t)pmap) & (Ln_ENTRIES - 1)) == 0 &&
4892 (tpte & ATTR_SW_WIRED) == 0) {
4893 pmap_clear_bits(pte, ATTR_AF);
4894 pmap_invalidate_page(pmap, pv->pv_va);
4900 /* Rotate the PV list if it has more than one entry. */
4901 if (pv != NULL && TAILQ_NEXT(pv, pv_next) != NULL) {
4902 TAILQ_REMOVE(&pvh->pv_list, pv, pv_next);
4903 TAILQ_INSERT_TAIL(&pvh->pv_list, pv, pv_next);
4906 if (cleared + not_cleared >= PMAP_TS_REFERENCED_MAX)
4908 } while ((pv = TAILQ_FIRST(&pvh->pv_list)) != pvf);
4910 if ((pvf = TAILQ_FIRST(&m->md.pv_list)) == NULL)
4917 if (!PMAP_TRYLOCK(pmap)) {
4918 pvh_gen = pvh->pv_gen;
4919 md_gen = m->md.pv_gen;
4923 if (pvh_gen != pvh->pv_gen || md_gen != m->md.pv_gen) {
4928 pde = pmap_pde(pmap, pv->pv_va, &lvl);
4929 KASSERT(pde != NULL, ("pmap_ts_referenced: no l2 table found"));
4931 ("pmap_ts_referenced: invalid pde level %d", lvl));
4932 tpde = pmap_load(pde);
4933 KASSERT((tpde & ATTR_DESCR_MASK) == L2_TABLE,
4934 ("pmap_ts_referenced: found an invalid l2 table"));
4935 pte = pmap_l2_to_l3(pde, pv->pv_va);
4936 tpte = pmap_load(pte);
4937 if (pmap_pte_dirty(tpte))
4939 if ((tpte & ATTR_AF) != 0) {
4940 if ((tpte & ATTR_SW_WIRED) == 0) {
4941 pmap_clear_bits(pte, ATTR_AF);
4942 pmap_invalidate_page(pmap, pv->pv_va);
4948 /* Rotate the PV list if it has more than one entry. */
4949 if (pv != NULL && TAILQ_NEXT(pv, pv_next) != NULL) {
4950 TAILQ_REMOVE(&m->md.pv_list, pv, pv_next);
4951 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_next);
4954 } while ((pv = TAILQ_FIRST(&m->md.pv_list)) != pvf && cleared +
4955 not_cleared < PMAP_TS_REFERENCED_MAX);
4958 vm_page_free_pages_toq(&free, true);
4959 return (cleared + not_cleared);
4963 * Apply the given advice to the specified range of addresses within the
4964 * given pmap. Depending on the advice, clear the referenced and/or
4965 * modified flags in each mapping and set the mapped page's dirty field.
4968 pmap_advise(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, int advice)
4970 struct rwlock *lock;
4971 vm_offset_t va, va_next;
4973 pd_entry_t *l0, *l1, *l2, oldl2;
4974 pt_entry_t *l3, oldl3;
4976 if (advice != MADV_DONTNEED && advice != MADV_FREE)
4980 for (; sva < eva; sva = va_next) {
4981 l0 = pmap_l0(pmap, sva);
4982 if (pmap_load(l0) == 0) {
4983 va_next = (sva + L0_SIZE) & ~L0_OFFSET;
4988 l1 = pmap_l0_to_l1(l0, sva);
4989 if (pmap_load(l1) == 0) {
4990 va_next = (sva + L1_SIZE) & ~L1_OFFSET;
4995 va_next = (sva + L2_SIZE) & ~L2_OFFSET;
4998 l2 = pmap_l1_to_l2(l1, sva);
4999 oldl2 = pmap_load(l2);
5002 if ((oldl2 & ATTR_DESCR_MASK) == L2_BLOCK) {
5003 if ((oldl2 & ATTR_SW_MANAGED) == 0)
5006 if (!pmap_demote_l2_locked(pmap, l2, sva, &lock)) {
5011 * The 2MB page mapping was destroyed.
5017 * Unless the page mappings are wired, remove the
5018 * mapping to a single page so that a subsequent
5019 * access may repromote. Choosing the last page
5020 * within the address range [sva, min(va_next, eva))
5021 * generally results in more repromotions. Since the
5022 * underlying page table page is fully populated, this
5023 * removal never frees a page table page.
5025 if ((oldl2 & ATTR_SW_WIRED) == 0) {
5031 ("pmap_advise: no address gap"));
5032 l3 = pmap_l2_to_l3(l2, va);
5033 KASSERT(pmap_load(l3) != 0,
5034 ("pmap_advise: invalid PTE"));
5035 pmap_remove_l3(pmap, l3, va, pmap_load(l2),
5041 KASSERT((pmap_load(l2) & ATTR_DESCR_MASK) == L2_TABLE,
5042 ("pmap_advise: invalid L2 entry after demotion"));
5046 for (l3 = pmap_l2_to_l3(l2, sva); sva != va_next; l3++,
5048 oldl3 = pmap_load(l3);
5049 if ((oldl3 & (ATTR_SW_MANAGED | ATTR_DESCR_MASK)) !=
5050 (ATTR_SW_MANAGED | L3_PAGE))
5052 else if (pmap_pte_dirty(oldl3)) {
5053 if (advice == MADV_DONTNEED) {
5055 * Future calls to pmap_is_modified()
5056 * can be avoided by making the page
5059 m = PHYS_TO_VM_PAGE(oldl3 & ~ATTR_MASK);
5062 while (!atomic_fcmpset_long(l3, &oldl3,
5063 (oldl3 & ~ATTR_AF) | ATTR_AP(ATTR_AP_RO)))
5065 } else if ((oldl3 & ATTR_AF) != 0)
5066 pmap_clear_bits(l3, ATTR_AF);
5073 if (va != va_next) {
5074 pmap_invalidate_range(pmap, va, sva);
5079 pmap_invalidate_range(pmap, va, sva);
5085 * Clear the modify bits on the specified physical page.
5088 pmap_clear_modify(vm_page_t m)
5090 struct md_page *pvh;
5091 struct rwlock *lock;
5093 pv_entry_t next_pv, pv;
5094 pd_entry_t *l2, oldl2;
5095 pt_entry_t *l3, oldl3;
5097 int md_gen, pvh_gen;
5099 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
5100 ("pmap_clear_modify: page %p is not managed", m));
5101 vm_page_assert_busied(m);
5103 if (!pmap_page_is_write_mapped(m))
5105 pvh = (m->flags & PG_FICTITIOUS) != 0 ? &pv_dummy :
5106 pa_to_pvh(VM_PAGE_TO_PHYS(m));
5107 lock = VM_PAGE_TO_PV_LIST_LOCK(m);
5110 TAILQ_FOREACH_SAFE(pv, &pvh->pv_list, pv_next, next_pv) {
5112 if (!PMAP_TRYLOCK(pmap)) {
5113 pvh_gen = pvh->pv_gen;
5117 if (pvh_gen != pvh->pv_gen) {
5123 l2 = pmap_l2(pmap, va);
5124 oldl2 = pmap_load(l2);
5125 /* If oldl2 has ATTR_SW_DBM set, then it is also dirty. */
5126 if ((oldl2 & ATTR_SW_DBM) != 0 &&
5127 pmap_demote_l2_locked(pmap, l2, va, &lock) &&
5128 (oldl2 & ATTR_SW_WIRED) == 0) {
5130 * Write protect the mapping to a single page so that
5131 * a subsequent write access may repromote.
5133 va += VM_PAGE_TO_PHYS(m) - (oldl2 & ~ATTR_MASK);
5134 l3 = pmap_l2_to_l3(l2, va);
5135 oldl3 = pmap_load(l3);
5136 while (!atomic_fcmpset_long(l3, &oldl3,
5137 (oldl3 & ~ATTR_SW_DBM) | ATTR_AP(ATTR_AP_RO)))
5140 pmap_invalidate_page(pmap, va);
5144 TAILQ_FOREACH(pv, &m->md.pv_list, pv_next) {
5146 if (!PMAP_TRYLOCK(pmap)) {
5147 md_gen = m->md.pv_gen;
5148 pvh_gen = pvh->pv_gen;
5152 if (pvh_gen != pvh->pv_gen || md_gen != m->md.pv_gen) {
5157 l2 = pmap_l2(pmap, pv->pv_va);
5158 l3 = pmap_l2_to_l3(l2, pv->pv_va);
5159 oldl3 = pmap_load(l3);
5160 if (pmap_l3_valid(oldl3) &&
5161 (oldl3 & (ATTR_AP_RW_BIT | ATTR_SW_DBM)) == ATTR_SW_DBM) {
5162 pmap_set_bits(l3, ATTR_AP(ATTR_AP_RO));
5163 pmap_invalidate_page(pmap, pv->pv_va);
5171 pmap_mapbios(vm_paddr_t pa, vm_size_t size)
5173 struct pmap_preinit_mapping *ppim;
5174 vm_offset_t va, offset;
5177 int i, lvl, l2_blocks, free_l2_count, start_idx;
5179 if (!vm_initialized) {
5181 * No L3 ptables so map entire L2 blocks where start VA is:
5182 * preinit_map_va + start_idx * L2_SIZE
5183 * There may be duplicate mappings (multiple VA -> same PA) but
5184 * ARM64 dcache is always PIPT so that's acceptable.
5189 /* Calculate how many L2 blocks are needed for the mapping */
5190 l2_blocks = (roundup2(pa + size, L2_SIZE) -
5191 rounddown2(pa, L2_SIZE)) >> L2_SHIFT;
5193 offset = pa & L2_OFFSET;
5195 if (preinit_map_va == 0)
5198 /* Map 2MiB L2 blocks from reserved VA space */
5202 /* Find enough free contiguous VA space */
5203 for (i = 0; i < PMAP_PREINIT_MAPPING_COUNT; i++) {
5204 ppim = pmap_preinit_mapping + i;
5205 if (free_l2_count > 0 && ppim->pa != 0) {
5206 /* Not enough space here */
5212 if (ppim->pa == 0) {
5214 if (start_idx == -1)
5217 if (free_l2_count == l2_blocks)
5221 if (free_l2_count != l2_blocks)
5222 panic("%s: too many preinit mappings", __func__);
5224 va = preinit_map_va + (start_idx * L2_SIZE);
5225 for (i = start_idx; i < start_idx + l2_blocks; i++) {
5226 /* Mark entries as allocated */
5227 ppim = pmap_preinit_mapping + i;
5229 ppim->va = va + offset;
5234 pa = rounddown2(pa, L2_SIZE);
5235 for (i = 0; i < l2_blocks; i++) {
5236 pde = pmap_pde(kernel_pmap, va, &lvl);
5237 KASSERT(pde != NULL,
5238 ("pmap_mapbios: Invalid page entry, va: 0x%lx",
5241 ("pmap_mapbios: Invalid level %d", lvl));
5243 /* Insert L2_BLOCK */
5244 l2 = pmap_l1_to_l2(pde, va);
5246 pa | ATTR_DEFAULT | ATTR_XN |
5247 ATTR_IDX(VM_MEMATTR_WRITE_BACK) | L2_BLOCK);
5252 pmap_invalidate_all(kernel_pmap);
5254 va = preinit_map_va + (start_idx * L2_SIZE);
5257 /* kva_alloc may be used to map the pages */
5258 offset = pa & PAGE_MASK;
5259 size = round_page(offset + size);
5261 va = kva_alloc(size);
5263 panic("%s: Couldn't allocate KVA", __func__);
5265 pde = pmap_pde(kernel_pmap, va, &lvl);
5266 KASSERT(lvl == 2, ("pmap_mapbios: Invalid level %d", lvl));
5268 /* L3 table is linked */
5269 va = trunc_page(va);
5270 pa = trunc_page(pa);
5271 pmap_kenter(va, size, pa, VM_MEMATTR_WRITE_BACK);
5274 return ((void *)(va + offset));
5278 pmap_unmapbios(vm_offset_t va, vm_size_t size)
5280 struct pmap_preinit_mapping *ppim;
5281 vm_offset_t offset, tmpsize, va_trunc;
5284 int i, lvl, l2_blocks, block;
5288 (roundup2(va + size, L2_SIZE) - rounddown2(va, L2_SIZE)) >> L2_SHIFT;
5289 KASSERT(l2_blocks > 0, ("pmap_unmapbios: invalid size %lx", size));
5291 /* Remove preinit mapping */
5292 preinit_map = false;
5294 for (i = 0; i < PMAP_PREINIT_MAPPING_COUNT; i++) {
5295 ppim = pmap_preinit_mapping + i;
5296 if (ppim->va == va) {
5297 KASSERT(ppim->size == size,
5298 ("pmap_unmapbios: size mismatch"));
5303 offset = block * L2_SIZE;
5304 va_trunc = rounddown2(va, L2_SIZE) + offset;
5306 /* Remove L2_BLOCK */
5307 pde = pmap_pde(kernel_pmap, va_trunc, &lvl);
5308 KASSERT(pde != NULL,
5309 ("pmap_unmapbios: Invalid page entry, va: 0x%lx",
5311 l2 = pmap_l1_to_l2(pde, va_trunc);
5314 if (block == (l2_blocks - 1))
5320 pmap_invalidate_all(kernel_pmap);
5324 /* Unmap the pages reserved with kva_alloc. */
5325 if (vm_initialized) {
5326 offset = va & PAGE_MASK;
5327 size = round_page(offset + size);
5328 va = trunc_page(va);
5330 pde = pmap_pde(kernel_pmap, va, &lvl);
5331 KASSERT(pde != NULL,
5332 ("pmap_unmapbios: Invalid page entry, va: 0x%lx", va));
5333 KASSERT(lvl == 2, ("pmap_unmapbios: Invalid level %d", lvl));
5335 /* Unmap and invalidate the pages */
5336 for (tmpsize = 0; tmpsize < size; tmpsize += PAGE_SIZE)
5337 pmap_kremove(va + tmpsize);
5344 * Sets the memory attribute for the specified page.
5347 pmap_page_set_memattr(vm_page_t m, vm_memattr_t ma)
5350 m->md.pv_memattr = ma;
5353 * If "m" is a normal page, update its direct mapping. This update
5354 * can be relied upon to perform any cache operations that are
5355 * required for data coherence.
5357 if ((m->flags & PG_FICTITIOUS) == 0 &&
5358 pmap_change_attr(PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m)), PAGE_SIZE,
5359 m->md.pv_memattr) != 0)
5360 panic("memory attribute change on the direct map failed");
5364 * Changes the specified virtual address range's memory type to that given by
5365 * the parameter "mode". The specified virtual address range must be
5366 * completely contained within either the direct map or the kernel map. If
5367 * the virtual address range is contained within the kernel map, then the
5368 * memory type for each of the corresponding ranges of the direct map is also
5369 * changed. (The corresponding ranges of the direct map are those ranges that
5370 * map the same physical pages as the specified virtual address range.) These
5371 * changes to the direct map are necessary because Intel describes the
5372 * behavior of their processors as "undefined" if two or more mappings to the
5373 * same physical page have different memory types.
5375 * Returns zero if the change completed successfully, and either EINVAL or
5376 * ENOMEM if the change failed. Specifically, EINVAL is returned if some part
5377 * of the virtual address range was not mapped, and ENOMEM is returned if
5378 * there was insufficient memory available to complete the change. In the
5379 * latter case, the memory type may have been changed on some part of the
5380 * virtual address range or the direct map.
5383 pmap_change_attr(vm_offset_t va, vm_size_t size, int mode)
5387 PMAP_LOCK(kernel_pmap);
5388 error = pmap_change_attr_locked(va, size, mode);
5389 PMAP_UNLOCK(kernel_pmap);
5394 pmap_change_attr_locked(vm_offset_t va, vm_size_t size, int mode)
5396 vm_offset_t base, offset, tmpva;
5397 pt_entry_t l3, *pte, *newpte;
5400 PMAP_LOCK_ASSERT(kernel_pmap, MA_OWNED);
5401 base = trunc_page(va);
5402 offset = va & PAGE_MASK;
5403 size = round_page(offset + size);
5405 if (!VIRT_IN_DMAP(base) &&
5406 !(base >= VM_MIN_KERNEL_ADDRESS && base < VM_MAX_KERNEL_ADDRESS))
5409 for (tmpva = base; tmpva < base + size; ) {
5410 pte = pmap_pte(kernel_pmap, tmpva, &lvl);
5414 if ((pmap_load(pte) & ATTR_IDX_MASK) == ATTR_IDX(mode)) {
5416 * We already have the correct attribute,
5417 * ignore this entry.
5421 panic("Invalid DMAP table level: %d\n", lvl);
5423 tmpva = (tmpva & ~L1_OFFSET) + L1_SIZE;
5426 tmpva = (tmpva & ~L2_OFFSET) + L2_SIZE;
5434 * Split the entry to an level 3 table, then
5435 * set the new attribute.
5439 panic("Invalid DMAP table level: %d\n", lvl);
5441 newpte = pmap_demote_l1(kernel_pmap, pte,
5442 tmpva & ~L1_OFFSET);
5445 pte = pmap_l1_to_l2(pte, tmpva);
5447 newpte = pmap_demote_l2(kernel_pmap, pte,
5451 pte = pmap_l2_to_l3(pte, tmpva);
5453 /* Update the entry */
5454 l3 = pmap_load(pte);
5455 l3 &= ~ATTR_IDX_MASK;
5456 l3 |= ATTR_IDX(mode);
5457 if (mode == VM_MEMATTR_DEVICE)
5460 pmap_update_entry(kernel_pmap, pte, l3, tmpva,
5464 * If moving to a non-cacheable entry flush
5467 if (mode == VM_MEMATTR_UNCACHEABLE)
5468 cpu_dcache_wbinv_range(tmpva, L3_SIZE);
5480 * Create an L2 table to map all addresses within an L1 mapping.
5483 pmap_demote_l1(pmap_t pmap, pt_entry_t *l1, vm_offset_t va)
5485 pt_entry_t *l2, newl2, oldl1;
5487 vm_paddr_t l2phys, phys;
5491 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
5492 oldl1 = pmap_load(l1);
5493 KASSERT((oldl1 & ATTR_DESCR_MASK) == L1_BLOCK,
5494 ("pmap_demote_l1: Demoting a non-block entry"));
5495 KASSERT((va & L1_OFFSET) == 0,
5496 ("pmap_demote_l1: Invalid virtual address %#lx", va));
5497 KASSERT((oldl1 & ATTR_SW_MANAGED) == 0,
5498 ("pmap_demote_l1: Level 1 table shouldn't be managed"));
5501 if (va <= (vm_offset_t)l1 && va + L1_SIZE > (vm_offset_t)l1) {
5502 tmpl1 = kva_alloc(PAGE_SIZE);
5507 if ((ml2 = vm_page_alloc(NULL, 0, VM_ALLOC_INTERRUPT |
5508 VM_ALLOC_NOOBJ | VM_ALLOC_WIRED)) == NULL) {
5509 CTR2(KTR_PMAP, "pmap_demote_l1: failure for va %#lx"
5510 " in pmap %p", va, pmap);
5514 l2phys = VM_PAGE_TO_PHYS(ml2);
5515 l2 = (pt_entry_t *)PHYS_TO_DMAP(l2phys);
5517 /* Address the range points at */
5518 phys = oldl1 & ~ATTR_MASK;
5519 /* The attributed from the old l1 table to be copied */
5520 newl2 = oldl1 & ATTR_MASK;
5522 /* Create the new entries */
5523 for (i = 0; i < Ln_ENTRIES; i++) {
5524 l2[i] = newl2 | phys;
5527 KASSERT(l2[0] == ((oldl1 & ~ATTR_DESCR_MASK) | L2_BLOCK),
5528 ("Invalid l2 page (%lx != %lx)", l2[0],
5529 (oldl1 & ~ATTR_DESCR_MASK) | L2_BLOCK));
5532 pmap_kenter(tmpl1, PAGE_SIZE,
5533 DMAP_TO_PHYS((vm_offset_t)l1) & ~L3_OFFSET,
5534 VM_MEMATTR_WRITE_BACK);
5535 l1 = (pt_entry_t *)(tmpl1 + ((vm_offset_t)l1 & PAGE_MASK));
5538 pmap_update_entry(pmap, l1, l2phys | L1_TABLE, va, PAGE_SIZE);
5541 pmap_kremove(tmpl1);
5542 kva_free(tmpl1, PAGE_SIZE);
5549 pmap_fill_l3(pt_entry_t *firstl3, pt_entry_t newl3)
5553 for (l3 = firstl3; l3 - firstl3 < Ln_ENTRIES; l3++) {
5560 pmap_demote_l2_abort(pmap_t pmap, vm_offset_t va, pt_entry_t *l2,
5561 struct rwlock **lockp)
5563 struct spglist free;
5566 (void)pmap_remove_l2(pmap, l2, va, pmap_load(pmap_l1(pmap, va)), &free,
5568 vm_page_free_pages_toq(&free, true);
5572 * Create an L3 table to map all addresses within an L2 mapping.
5575 pmap_demote_l2_locked(pmap_t pmap, pt_entry_t *l2, vm_offset_t va,
5576 struct rwlock **lockp)
5578 pt_entry_t *l3, newl3, oldl2;
5583 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
5585 oldl2 = pmap_load(l2);
5586 KASSERT((oldl2 & ATTR_DESCR_MASK) == L2_BLOCK,
5587 ("pmap_demote_l2: Demoting a non-block entry"));
5591 if (va <= (vm_offset_t)l2 && va + L2_SIZE > (vm_offset_t)l2) {
5592 tmpl2 = kva_alloc(PAGE_SIZE);
5598 * Invalidate the 2MB page mapping and return "failure" if the
5599 * mapping was never accessed.
5601 if ((oldl2 & ATTR_AF) == 0) {
5602 KASSERT((oldl2 & ATTR_SW_WIRED) == 0,
5603 ("pmap_demote_l2: a wired mapping is missing ATTR_AF"));
5604 pmap_demote_l2_abort(pmap, va, l2, lockp);
5605 CTR2(KTR_PMAP, "pmap_demote_l2: failure for va %#lx in pmap %p",
5610 if ((ml3 = pmap_remove_pt_page(pmap, va)) == NULL) {
5611 KASSERT((oldl2 & ATTR_SW_WIRED) == 0,
5612 ("pmap_demote_l2: page table page for a wired mapping"
5616 * If the page table page is missing and the mapping
5617 * is for a kernel address, the mapping must belong to
5618 * the direct map. Page table pages are preallocated
5619 * for every other part of the kernel address space,
5620 * so the direct map region is the only part of the
5621 * kernel address space that must be handled here.
5623 KASSERT(va < VM_MAXUSER_ADDRESS || VIRT_IN_DMAP(va),
5624 ("pmap_demote_l2: No saved mpte for va %#lx", va));
5627 * If the 2MB page mapping belongs to the direct map
5628 * region of the kernel's address space, then the page
5629 * allocation request specifies the highest possible
5630 * priority (VM_ALLOC_INTERRUPT). Otherwise, the
5631 * priority is normal.
5633 ml3 = vm_page_alloc(NULL, pmap_l2_pindex(va),
5634 (VIRT_IN_DMAP(va) ? VM_ALLOC_INTERRUPT : VM_ALLOC_NORMAL) |
5635 VM_ALLOC_NOOBJ | VM_ALLOC_WIRED);
5638 * If the allocation of the new page table page fails,
5639 * invalidate the 2MB page mapping and return "failure".
5642 pmap_demote_l2_abort(pmap, va, l2, lockp);
5643 CTR2(KTR_PMAP, "pmap_demote_l2: failure for va %#lx"
5644 " in pmap %p", va, pmap);
5648 if (va < VM_MAXUSER_ADDRESS) {
5649 ml3->ref_count = NL3PG;
5650 pmap_resident_count_inc(pmap, 1);
5653 l3phys = VM_PAGE_TO_PHYS(ml3);
5654 l3 = (pt_entry_t *)PHYS_TO_DMAP(l3phys);
5655 newl3 = (oldl2 & ~ATTR_DESCR_MASK) | L3_PAGE;
5656 KASSERT((oldl2 & (ATTR_AP_RW_BIT | ATTR_SW_DBM)) !=
5657 (ATTR_AP(ATTR_AP_RO) | ATTR_SW_DBM),
5658 ("pmap_demote_l2: L2 entry is writeable but not dirty"));
5661 * If the page table page is not leftover from an earlier promotion,
5662 * or the mapping attributes have changed, (re)initialize the L3 table.
5664 * When pmap_update_entry() clears the old L2 mapping, it (indirectly)
5665 * performs a dsb(). That dsb() ensures that the stores for filling
5666 * "l3" are visible before "l3" is added to the page table.
5668 if (ml3->valid == 0 || (l3[0] & ATTR_MASK) != (newl3 & ATTR_MASK))
5669 pmap_fill_l3(l3, newl3);
5672 * Map the temporary page so we don't lose access to the l2 table.
5675 pmap_kenter(tmpl2, PAGE_SIZE,
5676 DMAP_TO_PHYS((vm_offset_t)l2) & ~L3_OFFSET,
5677 VM_MEMATTR_WRITE_BACK);
5678 l2 = (pt_entry_t *)(tmpl2 + ((vm_offset_t)l2 & PAGE_MASK));
5682 * The spare PV entries must be reserved prior to demoting the
5683 * mapping, that is, prior to changing the PDE. Otherwise, the state
5684 * of the L2 and the PV lists will be inconsistent, which can result
5685 * in reclaim_pv_chunk() attempting to remove a PV entry from the
5686 * wrong PV list and pmap_pv_demote_l2() failing to find the expected
5687 * PV entry for the 2MB page mapping that is being demoted.
5689 if ((oldl2 & ATTR_SW_MANAGED) != 0)
5690 reserve_pv_entries(pmap, Ln_ENTRIES - 1, lockp);
5693 * Pass PAGE_SIZE so that a single TLB invalidation is performed on
5694 * the 2MB page mapping.
5696 pmap_update_entry(pmap, l2, l3phys | L2_TABLE, va, PAGE_SIZE);
5699 * Demote the PV entry.
5701 if ((oldl2 & ATTR_SW_MANAGED) != 0)
5702 pmap_pv_demote_l2(pmap, va, oldl2 & ~ATTR_MASK, lockp);
5704 atomic_add_long(&pmap_l2_demotions, 1);
5705 CTR3(KTR_PMAP, "pmap_demote_l2: success for va %#lx"
5706 " in pmap %p %lx", va, pmap, l3[0]);
5710 pmap_kremove(tmpl2);
5711 kva_free(tmpl2, PAGE_SIZE);
5719 pmap_demote_l2(pmap_t pmap, pt_entry_t *l2, vm_offset_t va)
5721 struct rwlock *lock;
5725 l3 = pmap_demote_l2_locked(pmap, l2, va, &lock);
5732 * Perform the pmap work for mincore(2). If the page is not both referenced and
5733 * modified by this pmap, returns its physical address so that the caller can
5734 * find other mappings.
5737 pmap_mincore(pmap_t pmap, vm_offset_t addr, vm_paddr_t *pap)
5739 pt_entry_t *pte, tpte;
5740 vm_paddr_t mask, pa;
5745 pte = pmap_pte(pmap, addr, &lvl);
5747 tpte = pmap_load(pte);
5760 panic("pmap_mincore: invalid level %d", lvl);
5763 managed = (tpte & ATTR_SW_MANAGED) != 0;
5764 val = MINCORE_INCORE;
5766 val |= MINCORE_SUPER;
5767 if ((managed && pmap_pte_dirty(tpte)) || (!managed &&
5768 (tpte & ATTR_AP_RW_BIT) == ATTR_AP(ATTR_AP_RW)))
5769 val |= MINCORE_MODIFIED | MINCORE_MODIFIED_OTHER;
5770 if ((tpte & ATTR_AF) == ATTR_AF)
5771 val |= MINCORE_REFERENCED | MINCORE_REFERENCED_OTHER;
5773 pa = (tpte & ~ATTR_MASK) | (addr & mask);
5779 if ((val & (MINCORE_MODIFIED_OTHER | MINCORE_REFERENCED_OTHER)) !=
5780 (MINCORE_MODIFIED_OTHER | MINCORE_REFERENCED_OTHER) && managed) {
5788 * Garbage collect every ASID that is neither active on a processor nor
5792 pmap_reset_asid_set(void)
5795 int asid, cpuid, epoch;
5797 mtx_assert(&asid_set_mutex, MA_OWNED);
5800 * Ensure that the store to asid_epoch is globally visible before the
5801 * loads from pc_curpmap are performed.
5803 epoch = asid_epoch + 1;
5804 if (epoch == INT_MAX)
5808 __asm __volatile("tlbi vmalle1is");
5810 bit_nclear(asid_set, ASID_FIRST_AVAILABLE, asid_set_size - 1);
5811 CPU_FOREACH(cpuid) {
5812 if (cpuid == curcpu)
5814 pmap = pcpu_find(cpuid)->pc_curpmap;
5815 asid = COOKIE_TO_ASID(pmap->pm_cookie);
5818 bit_set(asid_set, asid);
5819 pmap->pm_cookie = COOKIE_FROM(asid, epoch);
5824 * Allocate a new ASID for the specified pmap.
5827 pmap_alloc_asid(pmap_t pmap)
5831 mtx_lock_spin(&asid_set_mutex);
5834 * While this processor was waiting to acquire the asid set mutex,
5835 * pmap_reset_asid_set() running on another processor might have
5836 * updated this pmap's cookie to the current epoch. In which case, we
5837 * don't need to allocate a new ASID.
5839 if (COOKIE_TO_EPOCH(pmap->pm_cookie) == asid_epoch)
5842 bit_ffc_at(asid_set, asid_next, asid_set_size, &new_asid);
5843 if (new_asid == -1) {
5844 bit_ffc_at(asid_set, ASID_FIRST_AVAILABLE, asid_next,
5846 if (new_asid == -1) {
5847 pmap_reset_asid_set();
5848 bit_ffc_at(asid_set, ASID_FIRST_AVAILABLE,
5849 asid_set_size, &new_asid);
5850 KASSERT(new_asid != -1, ("ASID allocation failure"));
5853 bit_set(asid_set, new_asid);
5854 asid_next = new_asid + 1;
5855 pmap->pm_cookie = COOKIE_FROM(new_asid, asid_epoch);
5857 mtx_unlock_spin(&asid_set_mutex);
5861 * Compute the value that should be stored in ttbr0 to activate the specified
5862 * pmap. This value may change from time to time.
5865 pmap_to_ttbr0(pmap_t pmap)
5868 return (ASID_TO_OPERAND(COOKIE_TO_ASID(pmap->pm_cookie)) |
5873 pmap_activate_int(pmap_t pmap)
5877 KASSERT(PCPU_GET(curpmap) != NULL, ("no active pmap"));
5878 KASSERT(pmap != kernel_pmap, ("kernel pmap activation"));
5879 if (pmap == PCPU_GET(curpmap)) {
5881 * Handle the possibility that the old thread was preempted
5882 * after an "ic" or "tlbi" instruction but before it performed
5883 * a "dsb" instruction. If the old thread migrates to a new
5884 * processor, its completion of a "dsb" instruction on that
5885 * new processor does not guarantee that the "ic" or "tlbi"
5886 * instructions performed on the old processor have completed.
5893 * Ensure that the store to curpmap is globally visible before the
5894 * load from asid_epoch is performed.
5896 PCPU_SET(curpmap, pmap);
5898 epoch = COOKIE_TO_EPOCH(pmap->pm_cookie);
5899 if (epoch >= 0 && epoch != asid_epoch)
5900 pmap_alloc_asid(pmap);
5902 set_ttbr0(pmap_to_ttbr0(pmap));
5903 if (PCPU_GET(bcast_tlbi_workaround) != 0)
5904 invalidate_local_icache();
5909 pmap_activate(struct thread *td)
5913 pmap = vmspace_pmap(td->td_proc->p_vmspace);
5915 (void)pmap_activate_int(pmap);
5920 * To eliminate the unused parameter "old", we would have to add an instruction
5924 pmap_switch(struct thread *old __unused, struct thread *new)
5926 pcpu_bp_harden bp_harden;
5929 /* Store the new curthread */
5930 PCPU_SET(curthread, new);
5932 /* And the new pcb */
5934 PCPU_SET(curpcb, pcb);
5937 * TODO: We may need to flush the cache here if switching
5938 * to a user process.
5941 if (pmap_activate_int(vmspace_pmap(new->td_proc->p_vmspace))) {
5943 * Stop userspace from training the branch predictor against
5944 * other processes. This will call into a CPU specific
5945 * function that clears the branch predictor state.
5947 bp_harden = PCPU_GET(bp_harden);
5948 if (bp_harden != NULL)
5956 pmap_sync_icache(pmap_t pmap, vm_offset_t va, vm_size_t sz)
5959 if (va >= VM_MIN_KERNEL_ADDRESS) {
5960 cpu_icache_sync_range(va, sz);
5965 /* Find the length of data in this page to flush */
5966 offset = va & PAGE_MASK;
5967 len = imin(PAGE_SIZE - offset, sz);
5970 /* Extract the physical address & find it in the DMAP */
5971 pa = pmap_extract(pmap, va);
5973 cpu_icache_sync_range(PHYS_TO_DMAP(pa), len);
5975 /* Move to the next page */
5978 /* Set the length for the next iteration */
5979 len = imin(PAGE_SIZE, sz);
5985 pmap_fault(pmap_t pmap, uint64_t esr, uint64_t far)
5987 pt_entry_t pte, *ptep;
5994 ec = ESR_ELx_EXCEPTION(esr);
5996 case EXCP_INSN_ABORT_L:
5997 case EXCP_INSN_ABORT:
5998 case EXCP_DATA_ABORT_L:
5999 case EXCP_DATA_ABORT:
6005 /* Data and insn aborts use same encoding for FSC field. */
6006 switch (esr & ISS_DATA_DFSC_MASK) {
6007 case ISS_DATA_DFSC_AFF_L1:
6008 case ISS_DATA_DFSC_AFF_L2:
6009 case ISS_DATA_DFSC_AFF_L3:
6011 ptep = pmap_pte(pmap, far, &lvl);
6013 pmap_set_bits(ptep, ATTR_AF);
6016 * XXXMJ as an optimization we could mark the entry
6017 * dirty if this is a write fault.
6022 case ISS_DATA_DFSC_PF_L1:
6023 case ISS_DATA_DFSC_PF_L2:
6024 case ISS_DATA_DFSC_PF_L3:
6025 if ((ec != EXCP_DATA_ABORT_L && ec != EXCP_DATA_ABORT) ||
6026 (esr & ISS_DATA_WnR) == 0)
6029 ptep = pmap_pte(pmap, far, &lvl);
6031 ((pte = pmap_load(ptep)) & ATTR_SW_DBM) != 0) {
6032 if ((pte & ATTR_AP_RW_BIT) == ATTR_AP(ATTR_AP_RO)) {
6033 pmap_clear_bits(ptep, ATTR_AP_RW_BIT);
6034 pmap_invalidate_page(pmap, far);
6040 case ISS_DATA_DFSC_TF_L0:
6041 case ISS_DATA_DFSC_TF_L1:
6042 case ISS_DATA_DFSC_TF_L2:
6043 case ISS_DATA_DFSC_TF_L3:
6045 * Retry the translation. A break-before-make sequence can
6046 * produce a transient fault.
6048 if (pmap == kernel_pmap) {
6050 * The translation fault may have occurred within a
6051 * critical section. Therefore, we must check the
6052 * address without acquiring the kernel pmap's lock.
6054 if (pmap_kextract(far) != 0)
6058 /* Ask the MMU to check the address. */
6059 intr = intr_disable();
6060 par = arm64_address_translate_s1e0r(far);
6065 * If the translation was successful, then we can
6066 * return success to the trap handler.
6068 if (PAR_SUCCESS(par))
6078 * Increase the starting virtual address of the given mapping if a
6079 * different alignment might result in more superpage mappings.
6082 pmap_align_superpage(vm_object_t object, vm_ooffset_t offset,
6083 vm_offset_t *addr, vm_size_t size)
6085 vm_offset_t superpage_offset;
6089 if (object != NULL && (object->flags & OBJ_COLORED) != 0)
6090 offset += ptoa(object->pg_color);
6091 superpage_offset = offset & L2_OFFSET;
6092 if (size - ((L2_SIZE - superpage_offset) & L2_OFFSET) < L2_SIZE ||
6093 (*addr & L2_OFFSET) == superpage_offset)
6095 if ((*addr & L2_OFFSET) < superpage_offset)
6096 *addr = (*addr & ~L2_OFFSET) + superpage_offset;
6098 *addr = ((*addr + L2_OFFSET) & ~L2_OFFSET) + superpage_offset;
6102 * Get the kernel virtual address of a set of physical pages. If there are
6103 * physical addresses not covered by the DMAP perform a transient mapping
6104 * that will be removed when calling pmap_unmap_io_transient.
6106 * \param page The pages the caller wishes to obtain the virtual
6107 * address on the kernel memory map.
6108 * \param vaddr On return contains the kernel virtual memory address
6109 * of the pages passed in the page parameter.
6110 * \param count Number of pages passed in.
6111 * \param can_fault TRUE if the thread using the mapped pages can take
6112 * page faults, FALSE otherwise.
6114 * \returns TRUE if the caller must call pmap_unmap_io_transient when
6115 * finished or FALSE otherwise.
6119 pmap_map_io_transient(vm_page_t page[], vm_offset_t vaddr[], int count,
6120 boolean_t can_fault)
6123 boolean_t needs_mapping;
6127 * Allocate any KVA space that we need, this is done in a separate
6128 * loop to prevent calling vmem_alloc while pinned.
6130 needs_mapping = FALSE;
6131 for (i = 0; i < count; i++) {
6132 paddr = VM_PAGE_TO_PHYS(page[i]);
6133 if (__predict_false(!PHYS_IN_DMAP(paddr))) {
6134 error = vmem_alloc(kernel_arena, PAGE_SIZE,
6135 M_BESTFIT | M_WAITOK, &vaddr[i]);
6136 KASSERT(error == 0, ("vmem_alloc failed: %d", error));
6137 needs_mapping = TRUE;
6139 vaddr[i] = PHYS_TO_DMAP(paddr);
6143 /* Exit early if everything is covered by the DMAP */
6149 for (i = 0; i < count; i++) {
6150 paddr = VM_PAGE_TO_PHYS(page[i]);
6151 if (!PHYS_IN_DMAP(paddr)) {
6153 "pmap_map_io_transient: TODO: Map out of DMAP data");
6157 return (needs_mapping);
6161 pmap_unmap_io_transient(vm_page_t page[], vm_offset_t vaddr[], int count,
6162 boolean_t can_fault)
6169 for (i = 0; i < count; i++) {
6170 paddr = VM_PAGE_TO_PHYS(page[i]);
6171 if (!PHYS_IN_DMAP(paddr)) {
6172 panic("ARM64TODO: pmap_unmap_io_transient: Unmap data");
6178 pmap_is_valid_memattr(pmap_t pmap __unused, vm_memattr_t mode)
6181 return (mode >= VM_MEMATTR_DEVICE && mode <= VM_MEMATTR_WRITE_THROUGH);
6185 * Track a range of the kernel's virtual address space that is contiguous
6186 * in various mapping attributes.
6188 struct pmap_kernel_map_range {
6198 sysctl_kmaps_dump(struct sbuf *sb, struct pmap_kernel_map_range *range,
6204 if (eva <= range->sva)
6207 index = range->attrs & ATTR_IDX_MASK;
6209 case ATTR_IDX(VM_MEMATTR_DEVICE):
6212 case ATTR_IDX(VM_MEMATTR_UNCACHEABLE):
6215 case ATTR_IDX(VM_MEMATTR_WRITE_BACK):
6218 case ATTR_IDX(VM_MEMATTR_WRITE_THROUGH):
6223 "%s: unknown memory type %x for range 0x%016lx-0x%016lx\n",
6224 __func__, index, range->sva, eva);
6229 sbuf_printf(sb, "0x%016lx-0x%016lx r%c%c%c %3s %d %d %d %d\n",
6231 (range->attrs & ATTR_AP_RW_BIT) == ATTR_AP_RW ? 'w' : '-',
6232 (range->attrs & ATTR_PXN) != 0 ? '-' : 'x',
6233 (range->attrs & ATTR_AP_USER) != 0 ? 'u' : 's',
6234 mode, range->l1blocks, range->l2blocks, range->l3contig,
6237 /* Reset to sentinel value. */
6238 range->sva = 0xfffffffffffffffful;
6242 * Determine whether the attributes specified by a page table entry match those
6243 * being tracked by the current range.
6246 sysctl_kmaps_match(struct pmap_kernel_map_range *range, pt_entry_t attrs)
6249 return (range->attrs == attrs);
6253 sysctl_kmaps_reinit(struct pmap_kernel_map_range *range, vm_offset_t va,
6257 memset(range, 0, sizeof(*range));
6259 range->attrs = attrs;
6263 * Given a leaf PTE, derive the mapping's attributes. If they do not match
6264 * those of the current run, dump the address range and its attributes, and
6268 sysctl_kmaps_check(struct sbuf *sb, struct pmap_kernel_map_range *range,
6269 vm_offset_t va, pd_entry_t l0e, pd_entry_t l1e, pd_entry_t l2e,
6274 attrs = l0e & (ATTR_AP_MASK | ATTR_XN);
6275 attrs |= l1e & (ATTR_AP_MASK | ATTR_XN);
6276 if ((l1e & ATTR_DESCR_MASK) == L1_BLOCK)
6277 attrs |= l1e & ATTR_IDX_MASK;
6278 attrs |= l2e & (ATTR_AP_MASK | ATTR_XN);
6279 if ((l2e & ATTR_DESCR_MASK) == L2_BLOCK)
6280 attrs |= l2e & ATTR_IDX_MASK;
6281 attrs |= l3e & (ATTR_AP_MASK | ATTR_XN | ATTR_IDX_MASK);
6283 if (range->sva > va || !sysctl_kmaps_match(range, attrs)) {
6284 sysctl_kmaps_dump(sb, range, va);
6285 sysctl_kmaps_reinit(range, va, attrs);
6290 sysctl_kmaps(SYSCTL_HANDLER_ARGS)
6292 struct pmap_kernel_map_range range;
6293 struct sbuf sbuf, *sb;
6294 pd_entry_t l0e, *l1, l1e, *l2, l2e;
6295 pt_entry_t *l3, l3e;
6298 int error, i, j, k, l;
6300 error = sysctl_wire_old_buffer(req, 0);
6304 sbuf_new_for_sysctl(sb, NULL, PAGE_SIZE, req);
6306 /* Sentinel value. */
6307 range.sva = 0xfffffffffffffffful;
6310 * Iterate over the kernel page tables without holding the kernel pmap
6311 * lock. Kernel page table pages are never freed, so at worst we will
6312 * observe inconsistencies in the output.
6314 for (sva = 0xffff000000000000ul, i = pmap_l0_index(sva); i < Ln_ENTRIES;
6316 if (i == pmap_l0_index(DMAP_MIN_ADDRESS))
6317 sbuf_printf(sb, "\nDirect map:\n");
6318 else if (i == pmap_l0_index(VM_MIN_KERNEL_ADDRESS))
6319 sbuf_printf(sb, "\nKernel map:\n");
6321 l0e = kernel_pmap->pm_l0[i];
6322 if ((l0e & ATTR_DESCR_VALID) == 0) {
6323 sysctl_kmaps_dump(sb, &range, sva);
6327 pa = l0e & ~ATTR_MASK;
6328 l1 = (pd_entry_t *)PHYS_TO_DMAP(pa);
6330 for (j = pmap_l1_index(sva); j < Ln_ENTRIES; j++) {
6332 if ((l1e & ATTR_DESCR_VALID) == 0) {
6333 sysctl_kmaps_dump(sb, &range, sva);
6337 if ((l1e & ATTR_DESCR_MASK) == L1_BLOCK) {
6338 sysctl_kmaps_check(sb, &range, sva, l0e, l1e,
6344 pa = l1e & ~ATTR_MASK;
6345 l2 = (pd_entry_t *)PHYS_TO_DMAP(pa);
6347 for (k = pmap_l2_index(sva); k < Ln_ENTRIES; k++) {
6349 if ((l2e & ATTR_DESCR_VALID) == 0) {
6350 sysctl_kmaps_dump(sb, &range, sva);
6354 if ((l2e & ATTR_DESCR_MASK) == L2_BLOCK) {
6355 sysctl_kmaps_check(sb, &range, sva,
6361 pa = l2e & ~ATTR_MASK;
6362 l3 = (pt_entry_t *)PHYS_TO_DMAP(pa);
6364 for (l = pmap_l3_index(sva); l < Ln_ENTRIES;
6365 l++, sva += L3_SIZE) {
6367 if ((l3e & ATTR_DESCR_VALID) == 0) {
6368 sysctl_kmaps_dump(sb, &range,
6372 sysctl_kmaps_check(sb, &range, sva,
6373 l0e, l1e, l2e, l3e);
6374 if ((l3e & ATTR_CONTIGUOUS) != 0)
6375 range.l3contig += l % 16 == 0 ?
6384 error = sbuf_finish(sb);
6388 SYSCTL_OID(_vm_pmap, OID_AUTO, kernel_maps,
6389 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
6390 NULL, 0, sysctl_kmaps, "A",
6391 "Dump kernel address layout");