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 pmap_invalidate_page(pmap, va);
3454 vm_page_aflag_set(om, PGA_REFERENCED);
3456 CHANGE_PV_LIST_LOCK_TO_PHYS(&lock, opa);
3457 pv = pmap_pvh_remove(&om->md, pmap, va);
3458 if ((m->oflags & VPO_UNMANAGED) != 0)
3459 free_pv_entry(pmap, pv);
3460 if ((om->a.flags & PGA_WRITEABLE) != 0 &&
3461 TAILQ_EMPTY(&om->md.pv_list) &&
3462 ((om->flags & PG_FICTITIOUS) != 0 ||
3463 TAILQ_EMPTY(&pa_to_pvh(opa)->pv_list)))
3464 vm_page_aflag_clear(om, PGA_WRITEABLE);
3466 KASSERT((orig_l3 & ATTR_AF) != 0,
3467 ("pmap_enter: unmanaged mapping lacks ATTR_AF"));
3468 pmap_invalidate_page(pmap, va);
3473 * Increment the counters.
3475 if ((new_l3 & ATTR_SW_WIRED) != 0)
3476 pmap->pm_stats.wired_count++;
3477 pmap_resident_count_inc(pmap, 1);
3480 * Enter on the PV list if part of our managed memory.
3482 if ((m->oflags & VPO_UNMANAGED) == 0) {
3484 pv = get_pv_entry(pmap, &lock);
3487 CHANGE_PV_LIST_LOCK_TO_PHYS(&lock, pa);
3488 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_next);
3490 if ((new_l3 & ATTR_SW_DBM) != 0)
3491 vm_page_aflag_set(m, PGA_WRITEABLE);
3496 * Sync icache if exec permission and attribute VM_MEMATTR_WRITE_BACK
3497 * is set. Do it now, before the mapping is stored and made
3498 * valid for hardware table walk. If done later, then other can
3499 * access this page before caches are properly synced.
3500 * Don't do it for kernel memory which is mapped with exec
3501 * permission even if the memory isn't going to hold executable
3502 * code. The only time when icache sync is needed is after
3503 * kernel module is loaded and the relocation info is processed.
3504 * And it's done in elf_cpu_load_file().
3506 if ((prot & VM_PROT_EXECUTE) && pmap != kernel_pmap &&
3507 m->md.pv_memattr == VM_MEMATTR_WRITE_BACK &&
3508 (opa != pa || (orig_l3 & ATTR_XN)))
3509 cpu_icache_sync_range(PHYS_TO_DMAP(pa), PAGE_SIZE);
3512 * Update the L3 entry
3514 if (pmap_l3_valid(orig_l3)) {
3515 KASSERT(opa == pa, ("pmap_enter: invalid update"));
3516 if ((orig_l3 & ~ATTR_AF) != (new_l3 & ~ATTR_AF)) {
3517 /* same PA, different attributes */
3518 orig_l3 = pmap_load_store(l3, new_l3);
3519 pmap_invalidate_page(pmap, va);
3520 if ((orig_l3 & ATTR_SW_MANAGED) != 0 &&
3521 pmap_pte_dirty(orig_l3))
3526 * This can happens if multiple threads simultaneously
3527 * access not yet mapped page. This bad for performance
3528 * since this can cause full demotion-NOP-promotion
3530 * Another possible reasons are:
3531 * - VM and pmap memory layout are diverged
3532 * - tlb flush is missing somewhere and CPU doesn't see
3535 CTR4(KTR_PMAP, "%s: already mapped page - "
3536 "pmap %p va 0x%#lx pte 0x%lx",
3537 __func__, pmap, va, new_l3);
3541 pmap_store(l3, new_l3);
3545 #if VM_NRESERVLEVEL > 0
3546 if ((mpte == NULL || mpte->ref_count == NL3PG) &&
3547 pmap_ps_enabled(pmap) &&
3548 (m->flags & PG_FICTITIOUS) == 0 &&
3549 vm_reserv_level_iffullpop(m) == 0) {
3550 pmap_promote_l2(pmap, pde, va, &lock);
3563 * Tries to create a read- and/or execute-only 2MB page mapping. Returns true
3564 * if successful. Returns false if (1) a page table page cannot be allocated
3565 * without sleeping, (2) a mapping already exists at the specified virtual
3566 * address, or (3) a PV entry cannot be allocated without reclaiming another
3570 pmap_enter_2mpage(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
3571 struct rwlock **lockp)
3575 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3577 new_l2 = (pd_entry_t)(VM_PAGE_TO_PHYS(m) | ATTR_DEFAULT |
3578 ATTR_IDX(m->md.pv_memattr) | ATTR_AP(ATTR_AP_RO) | L2_BLOCK);
3579 if ((m->oflags & VPO_UNMANAGED) == 0) {
3580 new_l2 |= ATTR_SW_MANAGED;
3583 if ((prot & VM_PROT_EXECUTE) == 0 ||
3584 m->md.pv_memattr == VM_MEMATTR_DEVICE)
3586 if (va < VM_MAXUSER_ADDRESS)
3587 new_l2 |= ATTR_AP(ATTR_AP_USER) | ATTR_PXN;
3590 if (pmap != kernel_pmap)
3592 return (pmap_enter_l2(pmap, va, new_l2, PMAP_ENTER_NOSLEEP |
3593 PMAP_ENTER_NOREPLACE | PMAP_ENTER_NORECLAIM, NULL, lockp) ==
3598 * Returns true if every page table entry in the specified page table is
3602 pmap_every_pte_zero(vm_paddr_t pa)
3604 pt_entry_t *pt_end, *pte;
3606 KASSERT((pa & PAGE_MASK) == 0, ("pa is misaligned"));
3607 pte = (pt_entry_t *)PHYS_TO_DMAP(pa);
3608 for (pt_end = pte + Ln_ENTRIES; pte < pt_end; pte++) {
3616 * Tries to create the specified 2MB page mapping. Returns KERN_SUCCESS if
3617 * the mapping was created, and either KERN_FAILURE or KERN_RESOURCE_SHORTAGE
3618 * otherwise. Returns KERN_FAILURE if PMAP_ENTER_NOREPLACE was specified and
3619 * a mapping already exists at the specified virtual address. Returns
3620 * KERN_RESOURCE_SHORTAGE if PMAP_ENTER_NOSLEEP was specified and a page table
3621 * page allocation failed. Returns KERN_RESOURCE_SHORTAGE if
3622 * PMAP_ENTER_NORECLAIM was specified and a PV entry allocation failed.
3624 * The parameter "m" is only used when creating a managed, writeable mapping.
3627 pmap_enter_l2(pmap_t pmap, vm_offset_t va, pd_entry_t new_l2, u_int flags,
3628 vm_page_t m, struct rwlock **lockp)
3630 struct spglist free;
3631 pd_entry_t *l2, old_l2;
3634 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3636 if ((l2 = pmap_alloc_l2(pmap, va, &l2pg, (flags &
3637 PMAP_ENTER_NOSLEEP) != 0 ? NULL : lockp)) == NULL) {
3638 CTR2(KTR_PMAP, "pmap_enter_l2: failure for va %#lx in pmap %p",
3640 return (KERN_RESOURCE_SHORTAGE);
3644 * If there are existing mappings, either abort or remove them.
3646 if ((old_l2 = pmap_load(l2)) != 0) {
3647 KASSERT(l2pg == NULL || l2pg->ref_count > 1,
3648 ("pmap_enter_l2: l2pg's ref count is too low"));
3649 if ((flags & PMAP_ENTER_NOREPLACE) != 0 && (va <
3650 VM_MAXUSER_ADDRESS || (old_l2 & ATTR_DESCR_MASK) ==
3651 L2_BLOCK || !pmap_every_pte_zero(old_l2 & ~ATTR_MASK))) {
3654 CTR2(KTR_PMAP, "pmap_enter_l2: failure for va %#lx"
3655 " in pmap %p", va, pmap);
3656 return (KERN_FAILURE);
3659 if ((old_l2 & ATTR_DESCR_MASK) == L2_BLOCK)
3660 (void)pmap_remove_l2(pmap, l2, va,
3661 pmap_load(pmap_l1(pmap, va)), &free, lockp);
3663 pmap_remove_l3_range(pmap, old_l2, va, va + L2_SIZE,
3665 if (va < VM_MAXUSER_ADDRESS) {
3666 vm_page_free_pages_toq(&free, true);
3667 KASSERT(pmap_load(l2) == 0,
3668 ("pmap_enter_l2: non-zero L2 entry %p", l2));
3670 KASSERT(SLIST_EMPTY(&free),
3671 ("pmap_enter_l2: freed kernel page table page"));
3674 * Both pmap_remove_l2() and pmap_remove_l3_range()
3675 * will leave the kernel page table page zero filled.
3676 * Nonetheless, the TLB could have an intermediate
3677 * entry for the kernel page table page.
3679 mt = PHYS_TO_VM_PAGE(pmap_load(l2) & ~ATTR_MASK);
3680 if (pmap_insert_pt_page(pmap, mt, false))
3681 panic("pmap_enter_l2: trie insert failed");
3683 pmap_invalidate_page(pmap, va);
3687 if ((new_l2 & ATTR_SW_MANAGED) != 0) {
3689 * Abort this mapping if its PV entry could not be created.
3691 if (!pmap_pv_insert_l2(pmap, va, new_l2, flags, lockp)) {
3693 pmap_abort_ptp(pmap, va, l2pg);
3695 "pmap_enter_l2: failure for va %#lx in pmap %p",
3697 return (KERN_RESOURCE_SHORTAGE);
3699 if ((new_l2 & ATTR_SW_DBM) != 0)
3700 for (mt = m; mt < &m[L2_SIZE / PAGE_SIZE]; mt++)
3701 vm_page_aflag_set(mt, PGA_WRITEABLE);
3705 * Increment counters.
3707 if ((new_l2 & ATTR_SW_WIRED) != 0)
3708 pmap->pm_stats.wired_count += L2_SIZE / PAGE_SIZE;
3709 pmap->pm_stats.resident_count += L2_SIZE / PAGE_SIZE;
3712 * Map the superpage.
3714 pmap_store(l2, new_l2);
3717 atomic_add_long(&pmap_l2_mappings, 1);
3718 CTR2(KTR_PMAP, "pmap_enter_l2: success for va %#lx in pmap %p",
3721 return (KERN_SUCCESS);
3725 * Maps a sequence of resident pages belonging to the same object.
3726 * The sequence begins with the given page m_start. This page is
3727 * mapped at the given virtual address start. Each subsequent page is
3728 * mapped at a virtual address that is offset from start by the same
3729 * amount as the page is offset from m_start within the object. The
3730 * last page in the sequence is the page with the largest offset from
3731 * m_start that can be mapped at a virtual address less than the given
3732 * virtual address end. Not every virtual page between start and end
3733 * is mapped; only those for which a resident page exists with the
3734 * corresponding offset from m_start are mapped.
3737 pmap_enter_object(pmap_t pmap, vm_offset_t start, vm_offset_t end,
3738 vm_page_t m_start, vm_prot_t prot)
3740 struct rwlock *lock;
3743 vm_pindex_t diff, psize;
3745 VM_OBJECT_ASSERT_LOCKED(m_start->object);
3747 psize = atop(end - start);
3752 while (m != NULL && (diff = m->pindex - m_start->pindex) < psize) {
3753 va = start + ptoa(diff);
3754 if ((va & L2_OFFSET) == 0 && va + L2_SIZE <= end &&
3755 m->psind == 1 && pmap_ps_enabled(pmap) &&
3756 pmap_enter_2mpage(pmap, va, m, prot, &lock))
3757 m = &m[L2_SIZE / PAGE_SIZE - 1];
3759 mpte = pmap_enter_quick_locked(pmap, va, m, prot, mpte,
3761 m = TAILQ_NEXT(m, listq);
3769 * this code makes some *MAJOR* assumptions:
3770 * 1. Current pmap & pmap exists.
3773 * 4. No page table pages.
3774 * but is *MUCH* faster than pmap_enter...
3778 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot)
3780 struct rwlock *lock;
3784 (void)pmap_enter_quick_locked(pmap, va, m, prot, NULL, &lock);
3791 pmap_enter_quick_locked(pmap_t pmap, vm_offset_t va, vm_page_t m,
3792 vm_prot_t prot, vm_page_t mpte, struct rwlock **lockp)
3795 pt_entry_t *l2, *l3, l3_val;
3799 KASSERT(va < kmi.clean_sva || va >= kmi.clean_eva ||
3800 (m->oflags & VPO_UNMANAGED) != 0,
3801 ("pmap_enter_quick_locked: managed mapping within the clean submap"));
3802 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3804 CTR2(KTR_PMAP, "pmap_enter_quick_locked: %p %lx", pmap, va);
3806 * In the case that a page table page is not
3807 * resident, we are creating it here.
3809 if (va < VM_MAXUSER_ADDRESS) {
3810 vm_pindex_t l2pindex;
3813 * Calculate pagetable page index
3815 l2pindex = pmap_l2_pindex(va);
3816 if (mpte && (mpte->pindex == l2pindex)) {
3822 pde = pmap_pde(pmap, va, &lvl);
3825 * If the page table page is mapped, we just increment
3826 * the hold count, and activate it. Otherwise, we
3827 * attempt to allocate a page table page. If this
3828 * attempt fails, we don't retry. Instead, we give up.
3831 l2 = pmap_l1_to_l2(pde, va);
3832 if ((pmap_load(l2) & ATTR_DESCR_MASK) ==
3836 if (lvl == 2 && pmap_load(pde) != 0) {
3838 PHYS_TO_VM_PAGE(pmap_load(pde) & ~ATTR_MASK);
3842 * Pass NULL instead of the PV list lock
3843 * pointer, because we don't intend to sleep.
3845 mpte = _pmap_alloc_l3(pmap, l2pindex, NULL);
3850 l3 = (pt_entry_t *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(mpte));
3851 l3 = &l3[pmap_l3_index(va)];
3854 pde = pmap_pde(kernel_pmap, va, &lvl);
3855 KASSERT(pde != NULL,
3856 ("pmap_enter_quick_locked: Invalid page entry, va: 0x%lx",
3859 ("pmap_enter_quick_locked: Invalid level %d", lvl));
3860 l3 = pmap_l2_to_l3(pde, va);
3864 * Abort if a mapping already exists.
3866 if (pmap_load(l3) != 0) {
3873 * Enter on the PV list if part of our managed memory.
3875 if ((m->oflags & VPO_UNMANAGED) == 0 &&
3876 !pmap_try_insert_pv_entry(pmap, va, m, lockp)) {
3878 pmap_abort_ptp(pmap, va, mpte);
3883 * Increment counters
3885 pmap_resident_count_inc(pmap, 1);
3887 pa = VM_PAGE_TO_PHYS(m);
3888 l3_val = pa | ATTR_DEFAULT | ATTR_IDX(m->md.pv_memattr) |
3889 ATTR_AP(ATTR_AP_RO) | L3_PAGE;
3890 if ((prot & VM_PROT_EXECUTE) == 0 ||
3891 m->md.pv_memattr == VM_MEMATTR_DEVICE)
3893 if (va < VM_MAXUSER_ADDRESS)
3894 l3_val |= ATTR_AP(ATTR_AP_USER) | ATTR_PXN;
3897 if (pmap != kernel_pmap)
3901 * Now validate mapping with RO protection
3903 if ((m->oflags & VPO_UNMANAGED) == 0) {
3904 l3_val |= ATTR_SW_MANAGED;
3908 /* Sync icache before the mapping is stored to PTE */
3909 if ((prot & VM_PROT_EXECUTE) && pmap != kernel_pmap &&
3910 m->md.pv_memattr == VM_MEMATTR_WRITE_BACK)
3911 cpu_icache_sync_range(PHYS_TO_DMAP(pa), PAGE_SIZE);
3913 pmap_store(l3, l3_val);
3920 * This code maps large physical mmap regions into the
3921 * processor address space. Note that some shortcuts
3922 * are taken, but the code works.
3925 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr, vm_object_t object,
3926 vm_pindex_t pindex, vm_size_t size)
3929 VM_OBJECT_ASSERT_WLOCKED(object);
3930 KASSERT(object->type == OBJT_DEVICE || object->type == OBJT_SG,
3931 ("pmap_object_init_pt: non-device object"));
3935 * Clear the wired attribute from the mappings for the specified range of
3936 * addresses in the given pmap. Every valid mapping within that range
3937 * must have the wired attribute set. In contrast, invalid mappings
3938 * cannot have the wired attribute set, so they are ignored.
3940 * The wired attribute of the page table entry is not a hardware feature,
3941 * so there is no need to invalidate any TLB entries.
3944 pmap_unwire(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
3946 vm_offset_t va_next;
3947 pd_entry_t *l0, *l1, *l2;
3951 for (; sva < eva; sva = va_next) {
3952 l0 = pmap_l0(pmap, sva);
3953 if (pmap_load(l0) == 0) {
3954 va_next = (sva + L0_SIZE) & ~L0_OFFSET;
3960 l1 = pmap_l0_to_l1(l0, sva);
3961 if (pmap_load(l1) == 0) {
3962 va_next = (sva + L1_SIZE) & ~L1_OFFSET;
3968 va_next = (sva + L2_SIZE) & ~L2_OFFSET;
3972 l2 = pmap_l1_to_l2(l1, sva);
3973 if (pmap_load(l2) == 0)
3976 if ((pmap_load(l2) & ATTR_DESCR_MASK) == L2_BLOCK) {
3977 if ((pmap_load(l2) & ATTR_SW_WIRED) == 0)
3978 panic("pmap_unwire: l2 %#jx is missing "
3979 "ATTR_SW_WIRED", (uintmax_t)pmap_load(l2));
3982 * Are we unwiring the entire large page? If not,
3983 * demote the mapping and fall through.
3985 if (sva + L2_SIZE == va_next && eva >= va_next) {
3986 pmap_clear_bits(l2, ATTR_SW_WIRED);
3987 pmap->pm_stats.wired_count -= L2_SIZE /
3990 } else if (pmap_demote_l2(pmap, l2, sva) == NULL)
3991 panic("pmap_unwire: demotion failed");
3993 KASSERT((pmap_load(l2) & ATTR_DESCR_MASK) == L2_TABLE,
3994 ("pmap_unwire: Invalid l2 entry after demotion"));
3998 for (l3 = pmap_l2_to_l3(l2, sva); sva != va_next; l3++,
4000 if (pmap_load(l3) == 0)
4002 if ((pmap_load(l3) & ATTR_SW_WIRED) == 0)
4003 panic("pmap_unwire: l3 %#jx is missing "
4004 "ATTR_SW_WIRED", (uintmax_t)pmap_load(l3));
4007 * ATTR_SW_WIRED must be cleared atomically. Although
4008 * the pmap lock synchronizes access to ATTR_SW_WIRED,
4009 * the System MMU may write to the entry concurrently.
4011 pmap_clear_bits(l3, ATTR_SW_WIRED);
4012 pmap->pm_stats.wired_count--;
4019 * Copy the range specified by src_addr/len
4020 * from the source map to the range dst_addr/len
4021 * in the destination map.
4023 * This routine is only advisory and need not do anything.
4025 * Because the executable mappings created by this routine are copied,
4026 * it should not have to flush the instruction cache.
4029 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr, vm_size_t len,
4030 vm_offset_t src_addr)
4032 struct rwlock *lock;
4033 pd_entry_t *l0, *l1, *l2, srcptepaddr;
4034 pt_entry_t *dst_pte, mask, nbits, ptetemp, *src_pte;
4035 vm_offset_t addr, end_addr, va_next;
4036 vm_page_t dst_l2pg, dstmpte, srcmpte;
4038 if (dst_addr != src_addr)
4040 end_addr = src_addr + len;
4042 if (dst_pmap < src_pmap) {
4043 PMAP_LOCK(dst_pmap);
4044 PMAP_LOCK(src_pmap);
4046 PMAP_LOCK(src_pmap);
4047 PMAP_LOCK(dst_pmap);
4049 for (addr = src_addr; addr < end_addr; addr = va_next) {
4050 l0 = pmap_l0(src_pmap, addr);
4051 if (pmap_load(l0) == 0) {
4052 va_next = (addr + L0_SIZE) & ~L0_OFFSET;
4057 l1 = pmap_l0_to_l1(l0, addr);
4058 if (pmap_load(l1) == 0) {
4059 va_next = (addr + L1_SIZE) & ~L1_OFFSET;
4064 va_next = (addr + L2_SIZE) & ~L2_OFFSET;
4067 l2 = pmap_l1_to_l2(l1, addr);
4068 srcptepaddr = pmap_load(l2);
4069 if (srcptepaddr == 0)
4071 if ((srcptepaddr & ATTR_DESCR_MASK) == L2_BLOCK) {
4072 if ((addr & L2_OFFSET) != 0 ||
4073 addr + L2_SIZE > end_addr)
4075 l2 = pmap_alloc_l2(dst_pmap, addr, &dst_l2pg, NULL);
4078 if (pmap_load(l2) == 0 &&
4079 ((srcptepaddr & ATTR_SW_MANAGED) == 0 ||
4080 pmap_pv_insert_l2(dst_pmap, addr, srcptepaddr,
4081 PMAP_ENTER_NORECLAIM, &lock))) {
4082 mask = ATTR_AF | ATTR_SW_WIRED;
4084 if ((srcptepaddr & ATTR_SW_DBM) != 0)
4085 nbits |= ATTR_AP_RW_BIT;
4086 pmap_store(l2, (srcptepaddr & ~mask) | nbits);
4087 pmap_resident_count_inc(dst_pmap, L2_SIZE /
4089 atomic_add_long(&pmap_l2_mappings, 1);
4091 pmap_abort_ptp(dst_pmap, addr, dst_l2pg);
4094 KASSERT((srcptepaddr & ATTR_DESCR_MASK) == L2_TABLE,
4095 ("pmap_copy: invalid L2 entry"));
4096 srcptepaddr &= ~ATTR_MASK;
4097 srcmpte = PHYS_TO_VM_PAGE(srcptepaddr);
4098 KASSERT(srcmpte->ref_count > 0,
4099 ("pmap_copy: source page table page is unused"));
4100 if (va_next > end_addr)
4102 src_pte = (pt_entry_t *)PHYS_TO_DMAP(srcptepaddr);
4103 src_pte = &src_pte[pmap_l3_index(addr)];
4105 for (; addr < va_next; addr += PAGE_SIZE, src_pte++) {
4106 ptetemp = pmap_load(src_pte);
4109 * We only virtual copy managed pages.
4111 if ((ptetemp & ATTR_SW_MANAGED) == 0)
4114 if (dstmpte != NULL) {
4115 KASSERT(dstmpte->pindex == pmap_l2_pindex(addr),
4116 ("dstmpte pindex/addr mismatch"));
4117 dstmpte->ref_count++;
4118 } else if ((dstmpte = pmap_alloc_l3(dst_pmap, addr,
4121 dst_pte = (pt_entry_t *)
4122 PHYS_TO_DMAP(VM_PAGE_TO_PHYS(dstmpte));
4123 dst_pte = &dst_pte[pmap_l3_index(addr)];
4124 if (pmap_load(dst_pte) == 0 &&
4125 pmap_try_insert_pv_entry(dst_pmap, addr,
4126 PHYS_TO_VM_PAGE(ptetemp & ~ATTR_MASK), &lock)) {
4128 * Clear the wired, modified, and accessed
4129 * (referenced) bits during the copy.
4131 mask = ATTR_AF | ATTR_SW_WIRED;
4133 if ((ptetemp & ATTR_SW_DBM) != 0)
4134 nbits |= ATTR_AP_RW_BIT;
4135 pmap_store(dst_pte, (ptetemp & ~mask) | nbits);
4136 pmap_resident_count_inc(dst_pmap, 1);
4138 pmap_abort_ptp(dst_pmap, addr, dstmpte);
4141 /* Have we copied all of the valid mappings? */
4142 if (dstmpte->ref_count >= srcmpte->ref_count)
4148 * XXX This barrier may not be needed because the destination pmap is
4155 PMAP_UNLOCK(src_pmap);
4156 PMAP_UNLOCK(dst_pmap);
4160 * pmap_zero_page zeros the specified hardware page by mapping
4161 * the page into KVM and using bzero to clear its contents.
4164 pmap_zero_page(vm_page_t m)
4166 vm_offset_t va = PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m));
4168 pagezero((void *)va);
4172 * pmap_zero_page_area zeros the specified hardware page by mapping
4173 * the page into KVM and using bzero to clear its contents.
4175 * off and size may not cover an area beyond a single hardware page.
4178 pmap_zero_page_area(vm_page_t m, int off, int size)
4180 vm_offset_t va = PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m));
4182 if (off == 0 && size == PAGE_SIZE)
4183 pagezero((void *)va);
4185 bzero((char *)va + off, size);
4189 * pmap_copy_page copies the specified (machine independent)
4190 * page by mapping the page into virtual memory and using
4191 * bcopy to copy the page, one machine dependent page at a
4195 pmap_copy_page(vm_page_t msrc, vm_page_t mdst)
4197 vm_offset_t src = PHYS_TO_DMAP(VM_PAGE_TO_PHYS(msrc));
4198 vm_offset_t dst = PHYS_TO_DMAP(VM_PAGE_TO_PHYS(mdst));
4200 pagecopy((void *)src, (void *)dst);
4203 int unmapped_buf_allowed = 1;
4206 pmap_copy_pages(vm_page_t ma[], vm_offset_t a_offset, vm_page_t mb[],
4207 vm_offset_t b_offset, int xfersize)
4211 vm_paddr_t p_a, p_b;
4212 vm_offset_t a_pg_offset, b_pg_offset;
4215 while (xfersize > 0) {
4216 a_pg_offset = a_offset & PAGE_MASK;
4217 m_a = ma[a_offset >> PAGE_SHIFT];
4218 p_a = m_a->phys_addr;
4219 b_pg_offset = b_offset & PAGE_MASK;
4220 m_b = mb[b_offset >> PAGE_SHIFT];
4221 p_b = m_b->phys_addr;
4222 cnt = min(xfersize, PAGE_SIZE - a_pg_offset);
4223 cnt = min(cnt, PAGE_SIZE - b_pg_offset);
4224 if (__predict_false(!PHYS_IN_DMAP(p_a))) {
4225 panic("!DMAP a %lx", p_a);
4227 a_cp = (char *)PHYS_TO_DMAP(p_a) + a_pg_offset;
4229 if (__predict_false(!PHYS_IN_DMAP(p_b))) {
4230 panic("!DMAP b %lx", p_b);
4232 b_cp = (char *)PHYS_TO_DMAP(p_b) + b_pg_offset;
4234 bcopy(a_cp, b_cp, cnt);
4242 pmap_quick_enter_page(vm_page_t m)
4245 return (PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m)));
4249 pmap_quick_remove_page(vm_offset_t addr)
4254 * Returns true if the pmap's pv is one of the first
4255 * 16 pvs linked to from this page. This count may
4256 * be changed upwards or downwards in the future; it
4257 * is only necessary that true be returned for a small
4258 * subset of pmaps for proper page aging.
4261 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
4263 struct md_page *pvh;
4264 struct rwlock *lock;
4269 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4270 ("pmap_page_exists_quick: page %p is not managed", m));
4272 lock = VM_PAGE_TO_PV_LIST_LOCK(m);
4274 TAILQ_FOREACH(pv, &m->md.pv_list, pv_next) {
4275 if (PV_PMAP(pv) == pmap) {
4283 if (!rv && loops < 16 && (m->flags & PG_FICTITIOUS) == 0) {
4284 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
4285 TAILQ_FOREACH(pv, &pvh->pv_list, pv_next) {
4286 if (PV_PMAP(pv) == pmap) {
4300 * pmap_page_wired_mappings:
4302 * Return the number of managed mappings to the given physical page
4306 pmap_page_wired_mappings(vm_page_t m)
4308 struct rwlock *lock;
4309 struct md_page *pvh;
4313 int count, lvl, md_gen, pvh_gen;
4315 if ((m->oflags & VPO_UNMANAGED) != 0)
4317 lock = VM_PAGE_TO_PV_LIST_LOCK(m);
4321 TAILQ_FOREACH(pv, &m->md.pv_list, pv_next) {
4323 if (!PMAP_TRYLOCK(pmap)) {
4324 md_gen = m->md.pv_gen;
4328 if (md_gen != m->md.pv_gen) {
4333 pte = pmap_pte(pmap, pv->pv_va, &lvl);
4334 if (pte != NULL && (pmap_load(pte) & ATTR_SW_WIRED) != 0)
4338 if ((m->flags & PG_FICTITIOUS) == 0) {
4339 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
4340 TAILQ_FOREACH(pv, &pvh->pv_list, pv_next) {
4342 if (!PMAP_TRYLOCK(pmap)) {
4343 md_gen = m->md.pv_gen;
4344 pvh_gen = pvh->pv_gen;
4348 if (md_gen != m->md.pv_gen ||
4349 pvh_gen != pvh->pv_gen) {
4354 pte = pmap_pte(pmap, pv->pv_va, &lvl);
4356 (pmap_load(pte) & ATTR_SW_WIRED) != 0)
4366 * Returns true if the given page is mapped individually or as part of
4367 * a 2mpage. Otherwise, returns false.
4370 pmap_page_is_mapped(vm_page_t m)
4372 struct rwlock *lock;
4375 if ((m->oflags & VPO_UNMANAGED) != 0)
4377 lock = VM_PAGE_TO_PV_LIST_LOCK(m);
4379 rv = !TAILQ_EMPTY(&m->md.pv_list) ||
4380 ((m->flags & PG_FICTITIOUS) == 0 &&
4381 !TAILQ_EMPTY(&pa_to_pvh(VM_PAGE_TO_PHYS(m))->pv_list));
4387 * Destroy all managed, non-wired mappings in the given user-space
4388 * pmap. This pmap cannot be active on any processor besides the
4391 * This function cannot be applied to the kernel pmap. Moreover, it
4392 * is not intended for general use. It is only to be used during
4393 * process termination. Consequently, it can be implemented in ways
4394 * that make it faster than pmap_remove(). First, it can more quickly
4395 * destroy mappings by iterating over the pmap's collection of PV
4396 * entries, rather than searching the page table. Second, it doesn't
4397 * have to test and clear the page table entries atomically, because
4398 * no processor is currently accessing the user address space. In
4399 * particular, a page table entry's dirty bit won't change state once
4400 * this function starts.
4403 pmap_remove_pages(pmap_t pmap)
4406 pt_entry_t *pte, tpte;
4407 struct spglist free;
4408 vm_page_t m, ml3, mt;
4410 struct md_page *pvh;
4411 struct pv_chunk *pc, *npc;
4412 struct rwlock *lock;
4414 uint64_t inuse, bitmask;
4415 int allfree, field, freed, idx, lvl;
4418 KASSERT(pmap == PCPU_GET(curpmap), ("non-current pmap %p", pmap));
4424 TAILQ_FOREACH_SAFE(pc, &pmap->pm_pvchunk, pc_list, npc) {
4427 for (field = 0; field < _NPCM; field++) {
4428 inuse = ~pc->pc_map[field] & pc_freemask[field];
4429 while (inuse != 0) {
4430 bit = ffsl(inuse) - 1;
4431 bitmask = 1UL << bit;
4432 idx = field * 64 + bit;
4433 pv = &pc->pc_pventry[idx];
4436 pde = pmap_pde(pmap, pv->pv_va, &lvl);
4437 KASSERT(pde != NULL,
4438 ("Attempting to remove an unmapped page"));
4442 pte = pmap_l1_to_l2(pde, pv->pv_va);
4443 tpte = pmap_load(pte);
4444 KASSERT((tpte & ATTR_DESCR_MASK) ==
4446 ("Attempting to remove an invalid "
4447 "block: %lx", tpte));
4450 pte = pmap_l2_to_l3(pde, pv->pv_va);
4451 tpte = pmap_load(pte);
4452 KASSERT((tpte & ATTR_DESCR_MASK) ==
4454 ("Attempting to remove an invalid "
4455 "page: %lx", tpte));
4459 "Invalid page directory level: %d",
4464 * We cannot remove wired pages from a process' mapping at this time
4466 if (tpte & ATTR_SW_WIRED) {
4471 pa = tpte & ~ATTR_MASK;
4473 m = PHYS_TO_VM_PAGE(pa);
4474 KASSERT(m->phys_addr == pa,
4475 ("vm_page_t %p phys_addr mismatch %016jx %016jx",
4476 m, (uintmax_t)m->phys_addr,
4479 KASSERT((m->flags & PG_FICTITIOUS) != 0 ||
4480 m < &vm_page_array[vm_page_array_size],
4481 ("pmap_remove_pages: bad pte %#jx",
4485 * Because this pmap is not active on other
4486 * processors, the dirty bit cannot have
4487 * changed state since we last loaded pte.
4492 * Update the vm_page_t clean/reference bits.
4494 if (pmap_pte_dirty(tpte)) {
4497 for (mt = m; mt < &m[L2_SIZE / PAGE_SIZE]; mt++)
4506 CHANGE_PV_LIST_LOCK_TO_VM_PAGE(&lock, m);
4509 pc->pc_map[field] |= bitmask;
4512 pmap_resident_count_dec(pmap,
4513 L2_SIZE / PAGE_SIZE);
4514 pvh = pa_to_pvh(tpte & ~ATTR_MASK);
4515 TAILQ_REMOVE(&pvh->pv_list, pv,pv_next);
4517 if (TAILQ_EMPTY(&pvh->pv_list)) {
4518 for (mt = m; mt < &m[L2_SIZE / PAGE_SIZE]; mt++)
4519 if ((mt->a.flags & PGA_WRITEABLE) != 0 &&
4520 TAILQ_EMPTY(&mt->md.pv_list))
4521 vm_page_aflag_clear(mt, PGA_WRITEABLE);
4523 ml3 = pmap_remove_pt_page(pmap,
4526 KASSERT(ml3->valid == VM_PAGE_BITS_ALL,
4527 ("pmap_remove_pages: l3 page not promoted"));
4528 pmap_resident_count_dec(pmap,1);
4529 KASSERT(ml3->ref_count == NL3PG,
4530 ("pmap_remove_pages: l3 page ref count error"));
4532 pmap_add_delayed_free_list(ml3,
4537 pmap_resident_count_dec(pmap, 1);
4538 TAILQ_REMOVE(&m->md.pv_list, pv,
4541 if ((m->a.flags & PGA_WRITEABLE) != 0 &&
4542 TAILQ_EMPTY(&m->md.pv_list) &&
4543 (m->flags & PG_FICTITIOUS) == 0) {
4545 VM_PAGE_TO_PHYS(m));
4546 if (TAILQ_EMPTY(&pvh->pv_list))
4547 vm_page_aflag_clear(m,
4552 pmap_unuse_pt(pmap, pv->pv_va, pmap_load(pde),
4557 PV_STAT(atomic_add_long(&pv_entry_frees, freed));
4558 PV_STAT(atomic_add_int(&pv_entry_spare, freed));
4559 PV_STAT(atomic_subtract_long(&pv_entry_count, freed));
4561 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
4567 pmap_invalidate_all(pmap);
4569 vm_page_free_pages_toq(&free, true);
4573 * This is used to check if a page has been accessed or modified.
4576 pmap_page_test_mappings(vm_page_t m, boolean_t accessed, boolean_t modified)
4578 struct rwlock *lock;
4580 struct md_page *pvh;
4581 pt_entry_t *pte, mask, value;
4583 int lvl, md_gen, pvh_gen;
4587 lock = VM_PAGE_TO_PV_LIST_LOCK(m);
4590 TAILQ_FOREACH(pv, &m->md.pv_list, pv_next) {
4592 if (!PMAP_TRYLOCK(pmap)) {
4593 md_gen = m->md.pv_gen;
4597 if (md_gen != m->md.pv_gen) {
4602 pte = pmap_pte(pmap, pv->pv_va, &lvl);
4604 ("pmap_page_test_mappings: Invalid level %d", lvl));
4608 mask |= ATTR_AP_RW_BIT;
4609 value |= ATTR_AP(ATTR_AP_RW);
4612 mask |= ATTR_AF | ATTR_DESCR_MASK;
4613 value |= ATTR_AF | L3_PAGE;
4615 rv = (pmap_load(pte) & mask) == value;
4620 if ((m->flags & PG_FICTITIOUS) == 0) {
4621 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
4622 TAILQ_FOREACH(pv, &pvh->pv_list, pv_next) {
4624 if (!PMAP_TRYLOCK(pmap)) {
4625 md_gen = m->md.pv_gen;
4626 pvh_gen = pvh->pv_gen;
4630 if (md_gen != m->md.pv_gen ||
4631 pvh_gen != pvh->pv_gen) {
4636 pte = pmap_pte(pmap, pv->pv_va, &lvl);
4638 ("pmap_page_test_mappings: Invalid level %d", lvl));
4642 mask |= ATTR_AP_RW_BIT;
4643 value |= ATTR_AP(ATTR_AP_RW);
4646 mask |= ATTR_AF | ATTR_DESCR_MASK;
4647 value |= ATTR_AF | L2_BLOCK;
4649 rv = (pmap_load(pte) & mask) == value;
4663 * Return whether or not the specified physical page was modified
4664 * in any physical maps.
4667 pmap_is_modified(vm_page_t m)
4670 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4671 ("pmap_is_modified: page %p is not managed", m));
4674 * If the page is not busied then this check is racy.
4676 if (!pmap_page_is_write_mapped(m))
4678 return (pmap_page_test_mappings(m, FALSE, TRUE));
4682 * pmap_is_prefaultable:
4684 * Return whether or not the specified virtual address is eligible
4688 pmap_is_prefaultable(pmap_t pmap, vm_offset_t addr)
4696 pte = pmap_pte(pmap, addr, &lvl);
4697 if (pte != NULL && pmap_load(pte) != 0) {
4705 * pmap_is_referenced:
4707 * Return whether or not the specified physical page was referenced
4708 * in any physical maps.
4711 pmap_is_referenced(vm_page_t m)
4714 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4715 ("pmap_is_referenced: page %p is not managed", m));
4716 return (pmap_page_test_mappings(m, TRUE, FALSE));
4720 * Clear the write and modified bits in each of the given page's mappings.
4723 pmap_remove_write(vm_page_t m)
4725 struct md_page *pvh;
4727 struct rwlock *lock;
4728 pv_entry_t next_pv, pv;
4729 pt_entry_t oldpte, *pte;
4731 int lvl, md_gen, pvh_gen;
4733 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4734 ("pmap_remove_write: page %p is not managed", m));
4735 vm_page_assert_busied(m);
4737 if (!pmap_page_is_write_mapped(m))
4739 lock = VM_PAGE_TO_PV_LIST_LOCK(m);
4740 pvh = (m->flags & PG_FICTITIOUS) != 0 ? &pv_dummy :
4741 pa_to_pvh(VM_PAGE_TO_PHYS(m));
4744 TAILQ_FOREACH_SAFE(pv, &pvh->pv_list, pv_next, next_pv) {
4746 if (!PMAP_TRYLOCK(pmap)) {
4747 pvh_gen = pvh->pv_gen;
4751 if (pvh_gen != pvh->pv_gen) {
4758 pte = pmap_pte(pmap, pv->pv_va, &lvl);
4759 if ((pmap_load(pte) & ATTR_SW_DBM) != 0)
4760 (void)pmap_demote_l2_locked(pmap, pte, va, &lock);
4761 KASSERT(lock == VM_PAGE_TO_PV_LIST_LOCK(m),
4762 ("inconsistent pv lock %p %p for page %p",
4763 lock, VM_PAGE_TO_PV_LIST_LOCK(m), m));
4766 TAILQ_FOREACH(pv, &m->md.pv_list, pv_next) {
4768 if (!PMAP_TRYLOCK(pmap)) {
4769 pvh_gen = pvh->pv_gen;
4770 md_gen = m->md.pv_gen;
4774 if (pvh_gen != pvh->pv_gen ||
4775 md_gen != m->md.pv_gen) {
4781 pte = pmap_pte(pmap, pv->pv_va, &lvl);
4782 oldpte = pmap_load(pte);
4784 if ((oldpte & ATTR_SW_DBM) != 0) {
4785 if (!atomic_fcmpset_long(pte, &oldpte,
4786 (oldpte | ATTR_AP_RW_BIT) & ~ATTR_SW_DBM))
4788 if ((oldpte & ATTR_AP_RW_BIT) ==
4789 ATTR_AP(ATTR_AP_RW))
4791 pmap_invalidate_page(pmap, pv->pv_va);
4796 vm_page_aflag_clear(m, PGA_WRITEABLE);
4800 * pmap_ts_referenced:
4802 * Return a count of reference bits for a page, clearing those bits.
4803 * It is not necessary for every reference bit to be cleared, but it
4804 * is necessary that 0 only be returned when there are truly no
4805 * reference bits set.
4807 * As an optimization, update the page's dirty field if a modified bit is
4808 * found while counting reference bits. This opportunistic update can be
4809 * performed at low cost and can eliminate the need for some future calls
4810 * to pmap_is_modified(). However, since this function stops after
4811 * finding PMAP_TS_REFERENCED_MAX reference bits, it may not detect some
4812 * dirty pages. Those dirty pages will only be detected by a future call
4813 * to pmap_is_modified().
4816 pmap_ts_referenced(vm_page_t m)
4818 struct md_page *pvh;
4821 struct rwlock *lock;
4822 pd_entry_t *pde, tpde;
4823 pt_entry_t *pte, tpte;
4826 int cleared, lvl, md_gen, not_cleared, pvh_gen;
4827 struct spglist free;
4829 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4830 ("pmap_ts_referenced: page %p is not managed", m));
4833 pa = VM_PAGE_TO_PHYS(m);
4834 lock = PHYS_TO_PV_LIST_LOCK(pa);
4835 pvh = (m->flags & PG_FICTITIOUS) != 0 ? &pv_dummy : pa_to_pvh(pa);
4839 if ((pvf = TAILQ_FIRST(&pvh->pv_list)) == NULL)
4840 goto small_mappings;
4846 if (!PMAP_TRYLOCK(pmap)) {
4847 pvh_gen = pvh->pv_gen;
4851 if (pvh_gen != pvh->pv_gen) {
4857 pde = pmap_pde(pmap, pv->pv_va, &lvl);
4858 KASSERT(pde != NULL, ("pmap_ts_referenced: no l1 table found"));
4860 ("pmap_ts_referenced: invalid pde level %d", lvl));
4861 tpde = pmap_load(pde);
4862 KASSERT((tpde & ATTR_DESCR_MASK) == L1_TABLE,
4863 ("pmap_ts_referenced: found an invalid l1 table"));
4864 pte = pmap_l1_to_l2(pde, pv->pv_va);
4865 tpte = pmap_load(pte);
4866 if (pmap_pte_dirty(tpte)) {
4868 * Although "tpte" is mapping a 2MB page, because
4869 * this function is called at a 4KB page granularity,
4870 * we only update the 4KB page under test.
4875 if ((tpte & ATTR_AF) != 0) {
4877 * Since this reference bit is shared by 512 4KB pages,
4878 * it should not be cleared every time it is tested.
4879 * Apply a simple "hash" function on the physical page
4880 * number, the virtual superpage number, and the pmap
4881 * address to select one 4KB page out of the 512 on
4882 * which testing the reference bit will result in
4883 * clearing that reference bit. This function is
4884 * designed to avoid the selection of the same 4KB page
4885 * for every 2MB page mapping.
4887 * On demotion, a mapping that hasn't been referenced
4888 * is simply destroyed. To avoid the possibility of a
4889 * subsequent page fault on a demoted wired mapping,
4890 * always leave its reference bit set. Moreover,
4891 * since the superpage is wired, the current state of
4892 * its reference bit won't affect page replacement.
4894 if ((((pa >> PAGE_SHIFT) ^ (pv->pv_va >> L2_SHIFT) ^
4895 (uintptr_t)pmap) & (Ln_ENTRIES - 1)) == 0 &&
4896 (tpte & ATTR_SW_WIRED) == 0) {
4897 pmap_clear_bits(pte, ATTR_AF);
4898 pmap_invalidate_page(pmap, pv->pv_va);
4904 /* Rotate the PV list if it has more than one entry. */
4905 if (pv != NULL && TAILQ_NEXT(pv, pv_next) != NULL) {
4906 TAILQ_REMOVE(&pvh->pv_list, pv, pv_next);
4907 TAILQ_INSERT_TAIL(&pvh->pv_list, pv, pv_next);
4910 if (cleared + not_cleared >= PMAP_TS_REFERENCED_MAX)
4912 } while ((pv = TAILQ_FIRST(&pvh->pv_list)) != pvf);
4914 if ((pvf = TAILQ_FIRST(&m->md.pv_list)) == NULL)
4921 if (!PMAP_TRYLOCK(pmap)) {
4922 pvh_gen = pvh->pv_gen;
4923 md_gen = m->md.pv_gen;
4927 if (pvh_gen != pvh->pv_gen || md_gen != m->md.pv_gen) {
4932 pde = pmap_pde(pmap, pv->pv_va, &lvl);
4933 KASSERT(pde != NULL, ("pmap_ts_referenced: no l2 table found"));
4935 ("pmap_ts_referenced: invalid pde level %d", lvl));
4936 tpde = pmap_load(pde);
4937 KASSERT((tpde & ATTR_DESCR_MASK) == L2_TABLE,
4938 ("pmap_ts_referenced: found an invalid l2 table"));
4939 pte = pmap_l2_to_l3(pde, pv->pv_va);
4940 tpte = pmap_load(pte);
4941 if (pmap_pte_dirty(tpte))
4943 if ((tpte & ATTR_AF) != 0) {
4944 if ((tpte & ATTR_SW_WIRED) == 0) {
4945 pmap_clear_bits(pte, ATTR_AF);
4946 pmap_invalidate_page(pmap, pv->pv_va);
4952 /* Rotate the PV list if it has more than one entry. */
4953 if (pv != NULL && TAILQ_NEXT(pv, pv_next) != NULL) {
4954 TAILQ_REMOVE(&m->md.pv_list, pv, pv_next);
4955 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_next);
4958 } while ((pv = TAILQ_FIRST(&m->md.pv_list)) != pvf && cleared +
4959 not_cleared < PMAP_TS_REFERENCED_MAX);
4962 vm_page_free_pages_toq(&free, true);
4963 return (cleared + not_cleared);
4967 * Apply the given advice to the specified range of addresses within the
4968 * given pmap. Depending on the advice, clear the referenced and/or
4969 * modified flags in each mapping and set the mapped page's dirty field.
4972 pmap_advise(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, int advice)
4974 struct rwlock *lock;
4975 vm_offset_t va, va_next;
4977 pd_entry_t *l0, *l1, *l2, oldl2;
4978 pt_entry_t *l3, oldl3;
4980 if (advice != MADV_DONTNEED && advice != MADV_FREE)
4984 for (; sva < eva; sva = va_next) {
4985 l0 = pmap_l0(pmap, sva);
4986 if (pmap_load(l0) == 0) {
4987 va_next = (sva + L0_SIZE) & ~L0_OFFSET;
4992 l1 = pmap_l0_to_l1(l0, sva);
4993 if (pmap_load(l1) == 0) {
4994 va_next = (sva + L1_SIZE) & ~L1_OFFSET;
4999 va_next = (sva + L2_SIZE) & ~L2_OFFSET;
5002 l2 = pmap_l1_to_l2(l1, sva);
5003 oldl2 = pmap_load(l2);
5006 if ((oldl2 & ATTR_DESCR_MASK) == L2_BLOCK) {
5007 if ((oldl2 & ATTR_SW_MANAGED) == 0)
5010 if (!pmap_demote_l2_locked(pmap, l2, sva, &lock)) {
5015 * The 2MB page mapping was destroyed.
5021 * Unless the page mappings are wired, remove the
5022 * mapping to a single page so that a subsequent
5023 * access may repromote. Choosing the last page
5024 * within the address range [sva, min(va_next, eva))
5025 * generally results in more repromotions. Since the
5026 * underlying page table page is fully populated, this
5027 * removal never frees a page table page.
5029 if ((oldl2 & ATTR_SW_WIRED) == 0) {
5035 ("pmap_advise: no address gap"));
5036 l3 = pmap_l2_to_l3(l2, va);
5037 KASSERT(pmap_load(l3) != 0,
5038 ("pmap_advise: invalid PTE"));
5039 pmap_remove_l3(pmap, l3, va, pmap_load(l2),
5045 KASSERT((pmap_load(l2) & ATTR_DESCR_MASK) == L2_TABLE,
5046 ("pmap_advise: invalid L2 entry after demotion"));
5050 for (l3 = pmap_l2_to_l3(l2, sva); sva != va_next; l3++,
5052 oldl3 = pmap_load(l3);
5053 if ((oldl3 & (ATTR_SW_MANAGED | ATTR_DESCR_MASK)) !=
5054 (ATTR_SW_MANAGED | L3_PAGE))
5056 else if (pmap_pte_dirty(oldl3)) {
5057 if (advice == MADV_DONTNEED) {
5059 * Future calls to pmap_is_modified()
5060 * can be avoided by making the page
5063 m = PHYS_TO_VM_PAGE(oldl3 & ~ATTR_MASK);
5066 while (!atomic_fcmpset_long(l3, &oldl3,
5067 (oldl3 & ~ATTR_AF) | ATTR_AP(ATTR_AP_RO)))
5069 } else if ((oldl3 & ATTR_AF) != 0)
5070 pmap_clear_bits(l3, ATTR_AF);
5077 if (va != va_next) {
5078 pmap_invalidate_range(pmap, va, sva);
5083 pmap_invalidate_range(pmap, va, sva);
5089 * Clear the modify bits on the specified physical page.
5092 pmap_clear_modify(vm_page_t m)
5094 struct md_page *pvh;
5095 struct rwlock *lock;
5097 pv_entry_t next_pv, pv;
5098 pd_entry_t *l2, oldl2;
5099 pt_entry_t *l3, oldl3;
5101 int md_gen, pvh_gen;
5103 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
5104 ("pmap_clear_modify: page %p is not managed", m));
5105 vm_page_assert_busied(m);
5107 if (!pmap_page_is_write_mapped(m))
5109 pvh = (m->flags & PG_FICTITIOUS) != 0 ? &pv_dummy :
5110 pa_to_pvh(VM_PAGE_TO_PHYS(m));
5111 lock = VM_PAGE_TO_PV_LIST_LOCK(m);
5114 TAILQ_FOREACH_SAFE(pv, &pvh->pv_list, pv_next, next_pv) {
5116 if (!PMAP_TRYLOCK(pmap)) {
5117 pvh_gen = pvh->pv_gen;
5121 if (pvh_gen != pvh->pv_gen) {
5127 l2 = pmap_l2(pmap, va);
5128 oldl2 = pmap_load(l2);
5129 /* If oldl2 has ATTR_SW_DBM set, then it is also dirty. */
5130 if ((oldl2 & ATTR_SW_DBM) != 0 &&
5131 pmap_demote_l2_locked(pmap, l2, va, &lock) &&
5132 (oldl2 & ATTR_SW_WIRED) == 0) {
5134 * Write protect the mapping to a single page so that
5135 * a subsequent write access may repromote.
5137 va += VM_PAGE_TO_PHYS(m) - (oldl2 & ~ATTR_MASK);
5138 l3 = pmap_l2_to_l3(l2, va);
5139 oldl3 = pmap_load(l3);
5140 while (!atomic_fcmpset_long(l3, &oldl3,
5141 (oldl3 & ~ATTR_SW_DBM) | ATTR_AP(ATTR_AP_RO)))
5144 pmap_invalidate_page(pmap, va);
5148 TAILQ_FOREACH(pv, &m->md.pv_list, pv_next) {
5150 if (!PMAP_TRYLOCK(pmap)) {
5151 md_gen = m->md.pv_gen;
5152 pvh_gen = pvh->pv_gen;
5156 if (pvh_gen != pvh->pv_gen || md_gen != m->md.pv_gen) {
5161 l2 = pmap_l2(pmap, pv->pv_va);
5162 l3 = pmap_l2_to_l3(l2, pv->pv_va);
5163 oldl3 = pmap_load(l3);
5164 if (pmap_l3_valid(oldl3) &&
5165 (oldl3 & (ATTR_AP_RW_BIT | ATTR_SW_DBM)) == ATTR_SW_DBM) {
5166 pmap_set_bits(l3, ATTR_AP(ATTR_AP_RO));
5167 pmap_invalidate_page(pmap, pv->pv_va);
5175 pmap_mapbios(vm_paddr_t pa, vm_size_t size)
5177 struct pmap_preinit_mapping *ppim;
5178 vm_offset_t va, offset;
5181 int i, lvl, l2_blocks, free_l2_count, start_idx;
5183 if (!vm_initialized) {
5185 * No L3 ptables so map entire L2 blocks where start VA is:
5186 * preinit_map_va + start_idx * L2_SIZE
5187 * There may be duplicate mappings (multiple VA -> same PA) but
5188 * ARM64 dcache is always PIPT so that's acceptable.
5193 /* Calculate how many L2 blocks are needed for the mapping */
5194 l2_blocks = (roundup2(pa + size, L2_SIZE) -
5195 rounddown2(pa, L2_SIZE)) >> L2_SHIFT;
5197 offset = pa & L2_OFFSET;
5199 if (preinit_map_va == 0)
5202 /* Map 2MiB L2 blocks from reserved VA space */
5206 /* Find enough free contiguous VA space */
5207 for (i = 0; i < PMAP_PREINIT_MAPPING_COUNT; i++) {
5208 ppim = pmap_preinit_mapping + i;
5209 if (free_l2_count > 0 && ppim->pa != 0) {
5210 /* Not enough space here */
5216 if (ppim->pa == 0) {
5218 if (start_idx == -1)
5221 if (free_l2_count == l2_blocks)
5225 if (free_l2_count != l2_blocks)
5226 panic("%s: too many preinit mappings", __func__);
5228 va = preinit_map_va + (start_idx * L2_SIZE);
5229 for (i = start_idx; i < start_idx + l2_blocks; i++) {
5230 /* Mark entries as allocated */
5231 ppim = pmap_preinit_mapping + i;
5233 ppim->va = va + offset;
5238 pa = rounddown2(pa, L2_SIZE);
5239 for (i = 0; i < l2_blocks; i++) {
5240 pde = pmap_pde(kernel_pmap, va, &lvl);
5241 KASSERT(pde != NULL,
5242 ("pmap_mapbios: Invalid page entry, va: 0x%lx",
5245 ("pmap_mapbios: Invalid level %d", lvl));
5247 /* Insert L2_BLOCK */
5248 l2 = pmap_l1_to_l2(pde, va);
5250 pa | ATTR_DEFAULT | ATTR_XN |
5251 ATTR_IDX(VM_MEMATTR_WRITE_BACK) | L2_BLOCK);
5256 pmap_invalidate_all(kernel_pmap);
5258 va = preinit_map_va + (start_idx * L2_SIZE);
5261 /* kva_alloc may be used to map the pages */
5262 offset = pa & PAGE_MASK;
5263 size = round_page(offset + size);
5265 va = kva_alloc(size);
5267 panic("%s: Couldn't allocate KVA", __func__);
5269 pde = pmap_pde(kernel_pmap, va, &lvl);
5270 KASSERT(lvl == 2, ("pmap_mapbios: Invalid level %d", lvl));
5272 /* L3 table is linked */
5273 va = trunc_page(va);
5274 pa = trunc_page(pa);
5275 pmap_kenter(va, size, pa, VM_MEMATTR_WRITE_BACK);
5278 return ((void *)(va + offset));
5282 pmap_unmapbios(vm_offset_t va, vm_size_t size)
5284 struct pmap_preinit_mapping *ppim;
5285 vm_offset_t offset, tmpsize, va_trunc;
5288 int i, lvl, l2_blocks, block;
5292 (roundup2(va + size, L2_SIZE) - rounddown2(va, L2_SIZE)) >> L2_SHIFT;
5293 KASSERT(l2_blocks > 0, ("pmap_unmapbios: invalid size %lx", size));
5295 /* Remove preinit mapping */
5296 preinit_map = false;
5298 for (i = 0; i < PMAP_PREINIT_MAPPING_COUNT; i++) {
5299 ppim = pmap_preinit_mapping + i;
5300 if (ppim->va == va) {
5301 KASSERT(ppim->size == size,
5302 ("pmap_unmapbios: size mismatch"));
5307 offset = block * L2_SIZE;
5308 va_trunc = rounddown2(va, L2_SIZE) + offset;
5310 /* Remove L2_BLOCK */
5311 pde = pmap_pde(kernel_pmap, va_trunc, &lvl);
5312 KASSERT(pde != NULL,
5313 ("pmap_unmapbios: Invalid page entry, va: 0x%lx",
5315 l2 = pmap_l1_to_l2(pde, va_trunc);
5318 if (block == (l2_blocks - 1))
5324 pmap_invalidate_all(kernel_pmap);
5328 /* Unmap the pages reserved with kva_alloc. */
5329 if (vm_initialized) {
5330 offset = va & PAGE_MASK;
5331 size = round_page(offset + size);
5332 va = trunc_page(va);
5334 pde = pmap_pde(kernel_pmap, va, &lvl);
5335 KASSERT(pde != NULL,
5336 ("pmap_unmapbios: Invalid page entry, va: 0x%lx", va));
5337 KASSERT(lvl == 2, ("pmap_unmapbios: Invalid level %d", lvl));
5339 /* Unmap and invalidate the pages */
5340 for (tmpsize = 0; tmpsize < size; tmpsize += PAGE_SIZE)
5341 pmap_kremove(va + tmpsize);
5348 * Sets the memory attribute for the specified page.
5351 pmap_page_set_memattr(vm_page_t m, vm_memattr_t ma)
5354 m->md.pv_memattr = ma;
5357 * If "m" is a normal page, update its direct mapping. This update
5358 * can be relied upon to perform any cache operations that are
5359 * required for data coherence.
5361 if ((m->flags & PG_FICTITIOUS) == 0 &&
5362 pmap_change_attr(PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m)), PAGE_SIZE,
5363 m->md.pv_memattr) != 0)
5364 panic("memory attribute change on the direct map failed");
5368 * Changes the specified virtual address range's memory type to that given by
5369 * the parameter "mode". The specified virtual address range must be
5370 * completely contained within either the direct map or the kernel map. If
5371 * the virtual address range is contained within the kernel map, then the
5372 * memory type for each of the corresponding ranges of the direct map is also
5373 * changed. (The corresponding ranges of the direct map are those ranges that
5374 * map the same physical pages as the specified virtual address range.) These
5375 * changes to the direct map are necessary because Intel describes the
5376 * behavior of their processors as "undefined" if two or more mappings to the
5377 * same physical page have different memory types.
5379 * Returns zero if the change completed successfully, and either EINVAL or
5380 * ENOMEM if the change failed. Specifically, EINVAL is returned if some part
5381 * of the virtual address range was not mapped, and ENOMEM is returned if
5382 * there was insufficient memory available to complete the change. In the
5383 * latter case, the memory type may have been changed on some part of the
5384 * virtual address range or the direct map.
5387 pmap_change_attr(vm_offset_t va, vm_size_t size, int mode)
5391 PMAP_LOCK(kernel_pmap);
5392 error = pmap_change_attr_locked(va, size, mode);
5393 PMAP_UNLOCK(kernel_pmap);
5398 pmap_change_attr_locked(vm_offset_t va, vm_size_t size, int mode)
5400 vm_offset_t base, offset, tmpva;
5401 pt_entry_t l3, *pte, *newpte;
5404 PMAP_LOCK_ASSERT(kernel_pmap, MA_OWNED);
5405 base = trunc_page(va);
5406 offset = va & PAGE_MASK;
5407 size = round_page(offset + size);
5409 if (!VIRT_IN_DMAP(base) &&
5410 !(base >= VM_MIN_KERNEL_ADDRESS && base < VM_MAX_KERNEL_ADDRESS))
5413 for (tmpva = base; tmpva < base + size; ) {
5414 pte = pmap_pte(kernel_pmap, tmpva, &lvl);
5418 if ((pmap_load(pte) & ATTR_IDX_MASK) == ATTR_IDX(mode)) {
5420 * We already have the correct attribute,
5421 * ignore this entry.
5425 panic("Invalid DMAP table level: %d\n", lvl);
5427 tmpva = (tmpva & ~L1_OFFSET) + L1_SIZE;
5430 tmpva = (tmpva & ~L2_OFFSET) + L2_SIZE;
5438 * Split the entry to an level 3 table, then
5439 * set the new attribute.
5443 panic("Invalid DMAP table level: %d\n", lvl);
5445 newpte = pmap_demote_l1(kernel_pmap, pte,
5446 tmpva & ~L1_OFFSET);
5449 pte = pmap_l1_to_l2(pte, tmpva);
5451 newpte = pmap_demote_l2(kernel_pmap, pte,
5455 pte = pmap_l2_to_l3(pte, tmpva);
5457 /* Update the entry */
5458 l3 = pmap_load(pte);
5459 l3 &= ~ATTR_IDX_MASK;
5460 l3 |= ATTR_IDX(mode);
5461 if (mode == VM_MEMATTR_DEVICE)
5464 pmap_update_entry(kernel_pmap, pte, l3, tmpva,
5468 * If moving to a non-cacheable entry flush
5471 if (mode == VM_MEMATTR_UNCACHEABLE)
5472 cpu_dcache_wbinv_range(tmpva, L3_SIZE);
5484 * Create an L2 table to map all addresses within an L1 mapping.
5487 pmap_demote_l1(pmap_t pmap, pt_entry_t *l1, vm_offset_t va)
5489 pt_entry_t *l2, newl2, oldl1;
5491 vm_paddr_t l2phys, phys;
5495 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
5496 oldl1 = pmap_load(l1);
5497 KASSERT((oldl1 & ATTR_DESCR_MASK) == L1_BLOCK,
5498 ("pmap_demote_l1: Demoting a non-block entry"));
5499 KASSERT((va & L1_OFFSET) == 0,
5500 ("pmap_demote_l1: Invalid virtual address %#lx", va));
5501 KASSERT((oldl1 & ATTR_SW_MANAGED) == 0,
5502 ("pmap_demote_l1: Level 1 table shouldn't be managed"));
5505 if (va <= (vm_offset_t)l1 && va + L1_SIZE > (vm_offset_t)l1) {
5506 tmpl1 = kva_alloc(PAGE_SIZE);
5511 if ((ml2 = vm_page_alloc(NULL, 0, VM_ALLOC_INTERRUPT |
5512 VM_ALLOC_NOOBJ | VM_ALLOC_WIRED)) == NULL) {
5513 CTR2(KTR_PMAP, "pmap_demote_l1: failure for va %#lx"
5514 " in pmap %p", va, pmap);
5518 l2phys = VM_PAGE_TO_PHYS(ml2);
5519 l2 = (pt_entry_t *)PHYS_TO_DMAP(l2phys);
5521 /* Address the range points at */
5522 phys = oldl1 & ~ATTR_MASK;
5523 /* The attributed from the old l1 table to be copied */
5524 newl2 = oldl1 & ATTR_MASK;
5526 /* Create the new entries */
5527 for (i = 0; i < Ln_ENTRIES; i++) {
5528 l2[i] = newl2 | phys;
5531 KASSERT(l2[0] == ((oldl1 & ~ATTR_DESCR_MASK) | L2_BLOCK),
5532 ("Invalid l2 page (%lx != %lx)", l2[0],
5533 (oldl1 & ~ATTR_DESCR_MASK) | L2_BLOCK));
5536 pmap_kenter(tmpl1, PAGE_SIZE,
5537 DMAP_TO_PHYS((vm_offset_t)l1) & ~L3_OFFSET,
5538 VM_MEMATTR_WRITE_BACK);
5539 l1 = (pt_entry_t *)(tmpl1 + ((vm_offset_t)l1 & PAGE_MASK));
5542 pmap_update_entry(pmap, l1, l2phys | L1_TABLE, va, PAGE_SIZE);
5545 pmap_kremove(tmpl1);
5546 kva_free(tmpl1, PAGE_SIZE);
5553 pmap_fill_l3(pt_entry_t *firstl3, pt_entry_t newl3)
5557 for (l3 = firstl3; l3 - firstl3 < Ln_ENTRIES; l3++) {
5564 pmap_demote_l2_abort(pmap_t pmap, vm_offset_t va, pt_entry_t *l2,
5565 struct rwlock **lockp)
5567 struct spglist free;
5570 (void)pmap_remove_l2(pmap, l2, va, pmap_load(pmap_l1(pmap, va)), &free,
5572 vm_page_free_pages_toq(&free, true);
5576 * Create an L3 table to map all addresses within an L2 mapping.
5579 pmap_demote_l2_locked(pmap_t pmap, pt_entry_t *l2, vm_offset_t va,
5580 struct rwlock **lockp)
5582 pt_entry_t *l3, newl3, oldl2;
5587 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
5589 oldl2 = pmap_load(l2);
5590 KASSERT((oldl2 & ATTR_DESCR_MASK) == L2_BLOCK,
5591 ("pmap_demote_l2: Demoting a non-block entry"));
5595 if (va <= (vm_offset_t)l2 && va + L2_SIZE > (vm_offset_t)l2) {
5596 tmpl2 = kva_alloc(PAGE_SIZE);
5602 * Invalidate the 2MB page mapping and return "failure" if the
5603 * mapping was never accessed.
5605 if ((oldl2 & ATTR_AF) == 0) {
5606 KASSERT((oldl2 & ATTR_SW_WIRED) == 0,
5607 ("pmap_demote_l2: a wired mapping is missing ATTR_AF"));
5608 pmap_demote_l2_abort(pmap, va, l2, lockp);
5609 CTR2(KTR_PMAP, "pmap_demote_l2: failure for va %#lx in pmap %p",
5614 if ((ml3 = pmap_remove_pt_page(pmap, va)) == NULL) {
5615 KASSERT((oldl2 & ATTR_SW_WIRED) == 0,
5616 ("pmap_demote_l2: page table page for a wired mapping"
5620 * If the page table page is missing and the mapping
5621 * is for a kernel address, the mapping must belong to
5622 * the direct map. Page table pages are preallocated
5623 * for every other part of the kernel address space,
5624 * so the direct map region is the only part of the
5625 * kernel address space that must be handled here.
5627 KASSERT(va < VM_MAXUSER_ADDRESS || VIRT_IN_DMAP(va),
5628 ("pmap_demote_l2: No saved mpte for va %#lx", va));
5631 * If the 2MB page mapping belongs to the direct map
5632 * region of the kernel's address space, then the page
5633 * allocation request specifies the highest possible
5634 * priority (VM_ALLOC_INTERRUPT). Otherwise, the
5635 * priority is normal.
5637 ml3 = vm_page_alloc(NULL, pmap_l2_pindex(va),
5638 (VIRT_IN_DMAP(va) ? VM_ALLOC_INTERRUPT : VM_ALLOC_NORMAL) |
5639 VM_ALLOC_NOOBJ | VM_ALLOC_WIRED);
5642 * If the allocation of the new page table page fails,
5643 * invalidate the 2MB page mapping and return "failure".
5646 pmap_demote_l2_abort(pmap, va, l2, lockp);
5647 CTR2(KTR_PMAP, "pmap_demote_l2: failure for va %#lx"
5648 " in pmap %p", va, pmap);
5652 if (va < VM_MAXUSER_ADDRESS) {
5653 ml3->ref_count = NL3PG;
5654 pmap_resident_count_inc(pmap, 1);
5657 l3phys = VM_PAGE_TO_PHYS(ml3);
5658 l3 = (pt_entry_t *)PHYS_TO_DMAP(l3phys);
5659 newl3 = (oldl2 & ~ATTR_DESCR_MASK) | L3_PAGE;
5660 KASSERT((oldl2 & (ATTR_AP_RW_BIT | ATTR_SW_DBM)) !=
5661 (ATTR_AP(ATTR_AP_RO) | ATTR_SW_DBM),
5662 ("pmap_demote_l2: L2 entry is writeable but not dirty"));
5665 * If the page table page is not leftover from an earlier promotion,
5666 * or the mapping attributes have changed, (re)initialize the L3 table.
5668 * When pmap_update_entry() clears the old L2 mapping, it (indirectly)
5669 * performs a dsb(). That dsb() ensures that the stores for filling
5670 * "l3" are visible before "l3" is added to the page table.
5672 if (ml3->valid == 0 || (l3[0] & ATTR_MASK) != (newl3 & ATTR_MASK))
5673 pmap_fill_l3(l3, newl3);
5676 * Map the temporary page so we don't lose access to the l2 table.
5679 pmap_kenter(tmpl2, PAGE_SIZE,
5680 DMAP_TO_PHYS((vm_offset_t)l2) & ~L3_OFFSET,
5681 VM_MEMATTR_WRITE_BACK);
5682 l2 = (pt_entry_t *)(tmpl2 + ((vm_offset_t)l2 & PAGE_MASK));
5686 * The spare PV entries must be reserved prior to demoting the
5687 * mapping, that is, prior to changing the PDE. Otherwise, the state
5688 * of the L2 and the PV lists will be inconsistent, which can result
5689 * in reclaim_pv_chunk() attempting to remove a PV entry from the
5690 * wrong PV list and pmap_pv_demote_l2() failing to find the expected
5691 * PV entry for the 2MB page mapping that is being demoted.
5693 if ((oldl2 & ATTR_SW_MANAGED) != 0)
5694 reserve_pv_entries(pmap, Ln_ENTRIES - 1, lockp);
5697 * Pass PAGE_SIZE so that a single TLB invalidation is performed on
5698 * the 2MB page mapping.
5700 pmap_update_entry(pmap, l2, l3phys | L2_TABLE, va, PAGE_SIZE);
5703 * Demote the PV entry.
5705 if ((oldl2 & ATTR_SW_MANAGED) != 0)
5706 pmap_pv_demote_l2(pmap, va, oldl2 & ~ATTR_MASK, lockp);
5708 atomic_add_long(&pmap_l2_demotions, 1);
5709 CTR3(KTR_PMAP, "pmap_demote_l2: success for va %#lx"
5710 " in pmap %p %lx", va, pmap, l3[0]);
5714 pmap_kremove(tmpl2);
5715 kva_free(tmpl2, PAGE_SIZE);
5723 pmap_demote_l2(pmap_t pmap, pt_entry_t *l2, vm_offset_t va)
5725 struct rwlock *lock;
5729 l3 = pmap_demote_l2_locked(pmap, l2, va, &lock);
5736 * Perform the pmap work for mincore(2). If the page is not both referenced and
5737 * modified by this pmap, returns its physical address so that the caller can
5738 * find other mappings.
5741 pmap_mincore(pmap_t pmap, vm_offset_t addr, vm_paddr_t *pap)
5743 pt_entry_t *pte, tpte;
5744 vm_paddr_t mask, pa;
5749 pte = pmap_pte(pmap, addr, &lvl);
5751 tpte = pmap_load(pte);
5764 panic("pmap_mincore: invalid level %d", lvl);
5767 managed = (tpte & ATTR_SW_MANAGED) != 0;
5768 val = MINCORE_INCORE;
5770 val |= MINCORE_SUPER;
5771 if ((managed && pmap_pte_dirty(tpte)) || (!managed &&
5772 (tpte & ATTR_AP_RW_BIT) == ATTR_AP(ATTR_AP_RW)))
5773 val |= MINCORE_MODIFIED | MINCORE_MODIFIED_OTHER;
5774 if ((tpte & ATTR_AF) == ATTR_AF)
5775 val |= MINCORE_REFERENCED | MINCORE_REFERENCED_OTHER;
5777 pa = (tpte & ~ATTR_MASK) | (addr & mask);
5783 if ((val & (MINCORE_MODIFIED_OTHER | MINCORE_REFERENCED_OTHER)) !=
5784 (MINCORE_MODIFIED_OTHER | MINCORE_REFERENCED_OTHER) && managed) {
5792 * Garbage collect every ASID that is neither active on a processor nor
5796 pmap_reset_asid_set(void)
5799 int asid, cpuid, epoch;
5801 mtx_assert(&asid_set_mutex, MA_OWNED);
5804 * Ensure that the store to asid_epoch is globally visible before the
5805 * loads from pc_curpmap are performed.
5807 epoch = asid_epoch + 1;
5808 if (epoch == INT_MAX)
5812 __asm __volatile("tlbi vmalle1is");
5814 bit_nclear(asid_set, ASID_FIRST_AVAILABLE, asid_set_size - 1);
5815 CPU_FOREACH(cpuid) {
5816 if (cpuid == curcpu)
5818 pmap = pcpu_find(cpuid)->pc_curpmap;
5819 asid = COOKIE_TO_ASID(pmap->pm_cookie);
5822 bit_set(asid_set, asid);
5823 pmap->pm_cookie = COOKIE_FROM(asid, epoch);
5828 * Allocate a new ASID for the specified pmap.
5831 pmap_alloc_asid(pmap_t pmap)
5835 mtx_lock_spin(&asid_set_mutex);
5838 * While this processor was waiting to acquire the asid set mutex,
5839 * pmap_reset_asid_set() running on another processor might have
5840 * updated this pmap's cookie to the current epoch. In which case, we
5841 * don't need to allocate a new ASID.
5843 if (COOKIE_TO_EPOCH(pmap->pm_cookie) == asid_epoch)
5846 bit_ffc_at(asid_set, asid_next, asid_set_size, &new_asid);
5847 if (new_asid == -1) {
5848 bit_ffc_at(asid_set, ASID_FIRST_AVAILABLE, asid_next,
5850 if (new_asid == -1) {
5851 pmap_reset_asid_set();
5852 bit_ffc_at(asid_set, ASID_FIRST_AVAILABLE,
5853 asid_set_size, &new_asid);
5854 KASSERT(new_asid != -1, ("ASID allocation failure"));
5857 bit_set(asid_set, new_asid);
5858 asid_next = new_asid + 1;
5859 pmap->pm_cookie = COOKIE_FROM(new_asid, asid_epoch);
5861 mtx_unlock_spin(&asid_set_mutex);
5865 * Compute the value that should be stored in ttbr0 to activate the specified
5866 * pmap. This value may change from time to time.
5869 pmap_to_ttbr0(pmap_t pmap)
5872 return (ASID_TO_OPERAND(COOKIE_TO_ASID(pmap->pm_cookie)) |
5877 pmap_activate_int(pmap_t pmap)
5881 KASSERT(PCPU_GET(curpmap) != NULL, ("no active pmap"));
5882 KASSERT(pmap != kernel_pmap, ("kernel pmap activation"));
5883 if (pmap == PCPU_GET(curpmap)) {
5885 * Handle the possibility that the old thread was preempted
5886 * after an "ic" or "tlbi" instruction but before it performed
5887 * a "dsb" instruction. If the old thread migrates to a new
5888 * processor, its completion of a "dsb" instruction on that
5889 * new processor does not guarantee that the "ic" or "tlbi"
5890 * instructions performed on the old processor have completed.
5897 * Ensure that the store to curpmap is globally visible before the
5898 * load from asid_epoch is performed.
5900 PCPU_SET(curpmap, pmap);
5902 epoch = COOKIE_TO_EPOCH(pmap->pm_cookie);
5903 if (epoch >= 0 && epoch != asid_epoch)
5904 pmap_alloc_asid(pmap);
5906 set_ttbr0(pmap_to_ttbr0(pmap));
5907 if (PCPU_GET(bcast_tlbi_workaround) != 0)
5908 invalidate_local_icache();
5913 pmap_activate(struct thread *td)
5917 pmap = vmspace_pmap(td->td_proc->p_vmspace);
5919 (void)pmap_activate_int(pmap);
5924 * To eliminate the unused parameter "old", we would have to add an instruction
5928 pmap_switch(struct thread *old __unused, struct thread *new)
5930 pcpu_bp_harden bp_harden;
5933 /* Store the new curthread */
5934 PCPU_SET(curthread, new);
5936 /* And the new pcb */
5938 PCPU_SET(curpcb, pcb);
5941 * TODO: We may need to flush the cache here if switching
5942 * to a user process.
5945 if (pmap_activate_int(vmspace_pmap(new->td_proc->p_vmspace))) {
5947 * Stop userspace from training the branch predictor against
5948 * other processes. This will call into a CPU specific
5949 * function that clears the branch predictor state.
5951 bp_harden = PCPU_GET(bp_harden);
5952 if (bp_harden != NULL)
5960 pmap_sync_icache(pmap_t pmap, vm_offset_t va, vm_size_t sz)
5963 if (va >= VM_MIN_KERNEL_ADDRESS) {
5964 cpu_icache_sync_range(va, sz);
5969 /* Find the length of data in this page to flush */
5970 offset = va & PAGE_MASK;
5971 len = imin(PAGE_SIZE - offset, sz);
5974 /* Extract the physical address & find it in the DMAP */
5975 pa = pmap_extract(pmap, va);
5977 cpu_icache_sync_range(PHYS_TO_DMAP(pa), len);
5979 /* Move to the next page */
5982 /* Set the length for the next iteration */
5983 len = imin(PAGE_SIZE, sz);
5989 pmap_fault(pmap_t pmap, uint64_t esr, uint64_t far)
5991 pt_entry_t pte, *ptep;
5998 ec = ESR_ELx_EXCEPTION(esr);
6000 case EXCP_INSN_ABORT_L:
6001 case EXCP_INSN_ABORT:
6002 case EXCP_DATA_ABORT_L:
6003 case EXCP_DATA_ABORT:
6009 /* Data and insn aborts use same encoding for FSC field. */
6010 switch (esr & ISS_DATA_DFSC_MASK) {
6011 case ISS_DATA_DFSC_AFF_L1:
6012 case ISS_DATA_DFSC_AFF_L2:
6013 case ISS_DATA_DFSC_AFF_L3:
6015 ptep = pmap_pte(pmap, far, &lvl);
6017 pmap_set_bits(ptep, ATTR_AF);
6020 * XXXMJ as an optimization we could mark the entry
6021 * dirty if this is a write fault.
6026 case ISS_DATA_DFSC_PF_L1:
6027 case ISS_DATA_DFSC_PF_L2:
6028 case ISS_DATA_DFSC_PF_L3:
6029 if ((ec != EXCP_DATA_ABORT_L && ec != EXCP_DATA_ABORT) ||
6030 (esr & ISS_DATA_WnR) == 0)
6033 ptep = pmap_pte(pmap, far, &lvl);
6035 ((pte = pmap_load(ptep)) & ATTR_SW_DBM) != 0) {
6036 if ((pte & ATTR_AP_RW_BIT) == ATTR_AP(ATTR_AP_RO)) {
6037 pmap_clear_bits(ptep, ATTR_AP_RW_BIT);
6038 pmap_invalidate_page(pmap, far);
6044 case ISS_DATA_DFSC_TF_L0:
6045 case ISS_DATA_DFSC_TF_L1:
6046 case ISS_DATA_DFSC_TF_L2:
6047 case ISS_DATA_DFSC_TF_L3:
6049 * Retry the translation. A break-before-make sequence can
6050 * produce a transient fault.
6052 if (pmap == kernel_pmap) {
6054 * The translation fault may have occurred within a
6055 * critical section. Therefore, we must check the
6056 * address without acquiring the kernel pmap's lock.
6058 if (pmap_kextract(far) != 0)
6062 /* Ask the MMU to check the address. */
6063 intr = intr_disable();
6064 par = arm64_address_translate_s1e0r(far);
6069 * If the translation was successful, then we can
6070 * return success to the trap handler.
6072 if (PAR_SUCCESS(par))
6082 * Increase the starting virtual address of the given mapping if a
6083 * different alignment might result in more superpage mappings.
6086 pmap_align_superpage(vm_object_t object, vm_ooffset_t offset,
6087 vm_offset_t *addr, vm_size_t size)
6089 vm_offset_t superpage_offset;
6093 if (object != NULL && (object->flags & OBJ_COLORED) != 0)
6094 offset += ptoa(object->pg_color);
6095 superpage_offset = offset & L2_OFFSET;
6096 if (size - ((L2_SIZE - superpage_offset) & L2_OFFSET) < L2_SIZE ||
6097 (*addr & L2_OFFSET) == superpage_offset)
6099 if ((*addr & L2_OFFSET) < superpage_offset)
6100 *addr = (*addr & ~L2_OFFSET) + superpage_offset;
6102 *addr = ((*addr + L2_OFFSET) & ~L2_OFFSET) + superpage_offset;
6106 * Get the kernel virtual address of a set of physical pages. If there are
6107 * physical addresses not covered by the DMAP perform a transient mapping
6108 * that will be removed when calling pmap_unmap_io_transient.
6110 * \param page The pages the caller wishes to obtain the virtual
6111 * address on the kernel memory map.
6112 * \param vaddr On return contains the kernel virtual memory address
6113 * of the pages passed in the page parameter.
6114 * \param count Number of pages passed in.
6115 * \param can_fault TRUE if the thread using the mapped pages can take
6116 * page faults, FALSE otherwise.
6118 * \returns TRUE if the caller must call pmap_unmap_io_transient when
6119 * finished or FALSE otherwise.
6123 pmap_map_io_transient(vm_page_t page[], vm_offset_t vaddr[], int count,
6124 boolean_t can_fault)
6127 boolean_t needs_mapping;
6131 * Allocate any KVA space that we need, this is done in a separate
6132 * loop to prevent calling vmem_alloc while pinned.
6134 needs_mapping = FALSE;
6135 for (i = 0; i < count; i++) {
6136 paddr = VM_PAGE_TO_PHYS(page[i]);
6137 if (__predict_false(!PHYS_IN_DMAP(paddr))) {
6138 error = vmem_alloc(kernel_arena, PAGE_SIZE,
6139 M_BESTFIT | M_WAITOK, &vaddr[i]);
6140 KASSERT(error == 0, ("vmem_alloc failed: %d", error));
6141 needs_mapping = TRUE;
6143 vaddr[i] = PHYS_TO_DMAP(paddr);
6147 /* Exit early if everything is covered by the DMAP */
6153 for (i = 0; i < count; i++) {
6154 paddr = VM_PAGE_TO_PHYS(page[i]);
6155 if (!PHYS_IN_DMAP(paddr)) {
6157 "pmap_map_io_transient: TODO: Map out of DMAP data");
6161 return (needs_mapping);
6165 pmap_unmap_io_transient(vm_page_t page[], vm_offset_t vaddr[], int count,
6166 boolean_t can_fault)
6173 for (i = 0; i < count; i++) {
6174 paddr = VM_PAGE_TO_PHYS(page[i]);
6175 if (!PHYS_IN_DMAP(paddr)) {
6176 panic("ARM64TODO: pmap_unmap_io_transient: Unmap data");
6182 pmap_is_valid_memattr(pmap_t pmap __unused, vm_memattr_t mode)
6185 return (mode >= VM_MEMATTR_DEVICE && mode <= VM_MEMATTR_WRITE_THROUGH);
6189 * Track a range of the kernel's virtual address space that is contiguous
6190 * in various mapping attributes.
6192 struct pmap_kernel_map_range {
6202 sysctl_kmaps_dump(struct sbuf *sb, struct pmap_kernel_map_range *range,
6208 if (eva <= range->sva)
6211 index = range->attrs & ATTR_IDX_MASK;
6213 case ATTR_IDX(VM_MEMATTR_DEVICE):
6216 case ATTR_IDX(VM_MEMATTR_UNCACHEABLE):
6219 case ATTR_IDX(VM_MEMATTR_WRITE_BACK):
6222 case ATTR_IDX(VM_MEMATTR_WRITE_THROUGH):
6227 "%s: unknown memory type %x for range 0x%016lx-0x%016lx\n",
6228 __func__, index, range->sva, eva);
6233 sbuf_printf(sb, "0x%016lx-0x%016lx r%c%c%c %3s %d %d %d %d\n",
6235 (range->attrs & ATTR_AP_RW_BIT) == ATTR_AP_RW ? 'w' : '-',
6236 (range->attrs & ATTR_PXN) != 0 ? '-' : 'x',
6237 (range->attrs & ATTR_AP_USER) != 0 ? 'u' : 's',
6238 mode, range->l1blocks, range->l2blocks, range->l3contig,
6241 /* Reset to sentinel value. */
6242 range->sva = 0xfffffffffffffffful;
6246 * Determine whether the attributes specified by a page table entry match those
6247 * being tracked by the current range.
6250 sysctl_kmaps_match(struct pmap_kernel_map_range *range, pt_entry_t attrs)
6253 return (range->attrs == attrs);
6257 sysctl_kmaps_reinit(struct pmap_kernel_map_range *range, vm_offset_t va,
6261 memset(range, 0, sizeof(*range));
6263 range->attrs = attrs;
6267 * Given a leaf PTE, derive the mapping's attributes. If they do not match
6268 * those of the current run, dump the address range and its attributes, and
6272 sysctl_kmaps_check(struct sbuf *sb, struct pmap_kernel_map_range *range,
6273 vm_offset_t va, pd_entry_t l0e, pd_entry_t l1e, pd_entry_t l2e,
6278 attrs = l0e & (ATTR_AP_MASK | ATTR_XN);
6279 attrs |= l1e & (ATTR_AP_MASK | ATTR_XN);
6280 if ((l1e & ATTR_DESCR_MASK) == L1_BLOCK)
6281 attrs |= l1e & ATTR_IDX_MASK;
6282 attrs |= l2e & (ATTR_AP_MASK | ATTR_XN);
6283 if ((l2e & ATTR_DESCR_MASK) == L2_BLOCK)
6284 attrs |= l2e & ATTR_IDX_MASK;
6285 attrs |= l3e & (ATTR_AP_MASK | ATTR_XN | ATTR_IDX_MASK);
6287 if (range->sva > va || !sysctl_kmaps_match(range, attrs)) {
6288 sysctl_kmaps_dump(sb, range, va);
6289 sysctl_kmaps_reinit(range, va, attrs);
6294 sysctl_kmaps(SYSCTL_HANDLER_ARGS)
6296 struct pmap_kernel_map_range range;
6297 struct sbuf sbuf, *sb;
6298 pd_entry_t l0e, *l1, l1e, *l2, l2e;
6299 pt_entry_t *l3, l3e;
6302 int error, i, j, k, l;
6304 error = sysctl_wire_old_buffer(req, 0);
6308 sbuf_new_for_sysctl(sb, NULL, PAGE_SIZE, req);
6310 /* Sentinel value. */
6311 range.sva = 0xfffffffffffffffful;
6314 * Iterate over the kernel page tables without holding the kernel pmap
6315 * lock. Kernel page table pages are never freed, so at worst we will
6316 * observe inconsistencies in the output.
6318 for (sva = 0xffff000000000000ul, i = pmap_l0_index(sva); i < Ln_ENTRIES;
6320 if (i == pmap_l0_index(DMAP_MIN_ADDRESS))
6321 sbuf_printf(sb, "\nDirect map:\n");
6322 else if (i == pmap_l0_index(VM_MIN_KERNEL_ADDRESS))
6323 sbuf_printf(sb, "\nKernel map:\n");
6325 l0e = kernel_pmap->pm_l0[i];
6326 if ((l0e & ATTR_DESCR_VALID) == 0) {
6327 sysctl_kmaps_dump(sb, &range, sva);
6331 pa = l0e & ~ATTR_MASK;
6332 l1 = (pd_entry_t *)PHYS_TO_DMAP(pa);
6334 for (j = pmap_l1_index(sva); j < Ln_ENTRIES; j++) {
6336 if ((l1e & ATTR_DESCR_VALID) == 0) {
6337 sysctl_kmaps_dump(sb, &range, sva);
6341 if ((l1e & ATTR_DESCR_MASK) == L1_BLOCK) {
6342 sysctl_kmaps_check(sb, &range, sva, l0e, l1e,
6348 pa = l1e & ~ATTR_MASK;
6349 l2 = (pd_entry_t *)PHYS_TO_DMAP(pa);
6351 for (k = pmap_l2_index(sva); k < Ln_ENTRIES; k++) {
6353 if ((l2e & ATTR_DESCR_VALID) == 0) {
6354 sysctl_kmaps_dump(sb, &range, sva);
6358 if ((l2e & ATTR_DESCR_MASK) == L2_BLOCK) {
6359 sysctl_kmaps_check(sb, &range, sva,
6365 pa = l2e & ~ATTR_MASK;
6366 l3 = (pt_entry_t *)PHYS_TO_DMAP(pa);
6368 for (l = pmap_l3_index(sva); l < Ln_ENTRIES;
6369 l++, sva += L3_SIZE) {
6371 if ((l3e & ATTR_DESCR_VALID) == 0) {
6372 sysctl_kmaps_dump(sb, &range,
6376 sysctl_kmaps_check(sb, &range, sva,
6377 l0e, l1e, l2e, l3e);
6378 if ((l3e & ATTR_CONTIGUOUS) != 0)
6379 range.l3contig += l % 16 == 0 ?
6388 error = sbuf_finish(sb);
6392 SYSCTL_OID(_vm_pmap, OID_AUTO, kernel_maps,
6393 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
6394 NULL, 0, sysctl_kmaps, "A",
6395 "Dump kernel address layout");