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/asan.h>
112 #include <sys/bitstring.h>
114 #include <sys/systm.h>
115 #include <sys/kernel.h>
117 #include <sys/limits.h>
118 #include <sys/lock.h>
119 #include <sys/malloc.h>
120 #include <sys/mman.h>
121 #include <sys/msgbuf.h>
122 #include <sys/mutex.h>
123 #include <sys/physmem.h>
124 #include <sys/proc.h>
125 #include <sys/rwlock.h>
126 #include <sys/sbuf.h>
128 #include <sys/vmem.h>
129 #include <sys/vmmeter.h>
130 #include <sys/sched.h>
131 #include <sys/sysctl.h>
132 #include <sys/_unrhdr.h>
136 #include <vm/vm_param.h>
137 #include <vm/vm_kern.h>
138 #include <vm/vm_page.h>
139 #include <vm/vm_map.h>
140 #include <vm/vm_object.h>
141 #include <vm/vm_extern.h>
142 #include <vm/vm_pageout.h>
143 #include <vm/vm_pager.h>
144 #include <vm/vm_phys.h>
145 #include <vm/vm_radix.h>
146 #include <vm/vm_reserv.h>
147 #include <vm/vm_dumpset.h>
150 #include <machine/asan.h>
151 #include <machine/machdep.h>
152 #include <machine/md_var.h>
153 #include <machine/pcb.h>
156 #define PMAP_MEMDOM MAXMEMDOM
158 #define PMAP_MEMDOM 1
161 #define PMAP_ASSERT_STAGE1(pmap) MPASS((pmap)->pm_stage == PM_STAGE1)
162 #define PMAP_ASSERT_STAGE2(pmap) MPASS((pmap)->pm_stage == PM_STAGE2)
164 #define NL0PG (PAGE_SIZE/(sizeof (pd_entry_t)))
165 #define NL1PG (PAGE_SIZE/(sizeof (pd_entry_t)))
166 #define NL2PG (PAGE_SIZE/(sizeof (pd_entry_t)))
167 #define NL3PG (PAGE_SIZE/(sizeof (pt_entry_t)))
169 #define NUL0E L0_ENTRIES
170 #define NUL1E (NUL0E * NL1PG)
171 #define NUL2E (NUL1E * NL2PG)
173 #if !defined(DIAGNOSTIC)
174 #ifdef __GNUC_GNU_INLINE__
175 #define PMAP_INLINE __attribute__((__gnu_inline__)) inline
177 #define PMAP_INLINE extern inline
184 #define PV_STAT(x) do { x ; } while (0)
187 #define PV_STAT(x) do { } while (0)
188 #define __pvused __unused
191 #define pmap_l0_pindex(v) (NUL2E + NUL1E + ((v) >> L0_SHIFT))
192 #define pmap_l1_pindex(v) (NUL2E + ((v) >> L1_SHIFT))
193 #define pmap_l2_pindex(v) ((v) >> L2_SHIFT)
195 #define PMAP_SAN_PTE_BITS (ATTR_DEFAULT | ATTR_S1_XN | \
196 ATTR_S1_IDX(VM_MEMATTR_WRITE_BACK) | ATTR_S1_AP(ATTR_S1_AP_RW))
198 struct pmap_large_md_page {
199 struct rwlock pv_lock;
200 struct md_page pv_page;
201 /* Pad to a power of 2, see pmap_init_pv_table(). */
205 static struct pmap_large_md_page *
206 _pa_to_pmdp(vm_paddr_t pa)
208 struct vm_phys_seg *seg;
211 for (segind = 0; segind < vm_phys_nsegs; segind++) {
212 seg = &vm_phys_segs[segind];
213 if (pa >= seg->start && pa < seg->end)
214 return ((struct pmap_large_md_page *)seg->md_first +
215 pmap_l2_pindex(pa) - pmap_l2_pindex(seg->start));
220 static struct pmap_large_md_page *
221 pa_to_pmdp(vm_paddr_t pa)
223 struct pmap_large_md_page *pvd;
225 pvd = _pa_to_pmdp(pa);
227 panic("pa 0x%jx not within vm_phys_segs", (uintmax_t)pa);
231 static struct pmap_large_md_page *
232 page_to_pmdp(vm_page_t m)
234 struct vm_phys_seg *seg;
236 seg = &vm_phys_segs[m->segind];
237 return ((struct pmap_large_md_page *)seg->md_first +
238 pmap_l2_pindex(VM_PAGE_TO_PHYS(m)) - pmap_l2_pindex(seg->start));
241 #define pa_to_pvh(pa) (&(pa_to_pmdp(pa)->pv_page))
242 #define page_to_pvh(m) (&(page_to_pmdp(m)->pv_page))
244 #define PHYS_TO_PV_LIST_LOCK(pa) ({ \
245 struct pmap_large_md_page *_pvd; \
246 struct rwlock *_lock; \
247 _pvd = _pa_to_pmdp(pa); \
248 if (__predict_false(_pvd == NULL)) \
249 _lock = &pv_dummy_large.pv_lock; \
251 _lock = &(_pvd->pv_lock); \
255 #define CHANGE_PV_LIST_LOCK_TO_PHYS(lockp, pa) do { \
256 struct rwlock **_lockp = (lockp); \
257 struct rwlock *_new_lock; \
259 _new_lock = PHYS_TO_PV_LIST_LOCK(pa); \
260 if (_new_lock != *_lockp) { \
261 if (*_lockp != NULL) \
262 rw_wunlock(*_lockp); \
263 *_lockp = _new_lock; \
268 #define CHANGE_PV_LIST_LOCK_TO_VM_PAGE(lockp, m) \
269 CHANGE_PV_LIST_LOCK_TO_PHYS(lockp, VM_PAGE_TO_PHYS(m))
271 #define RELEASE_PV_LIST_LOCK(lockp) do { \
272 struct rwlock **_lockp = (lockp); \
274 if (*_lockp != NULL) { \
275 rw_wunlock(*_lockp); \
280 #define VM_PAGE_TO_PV_LIST_LOCK(m) \
281 PHYS_TO_PV_LIST_LOCK(VM_PAGE_TO_PHYS(m))
284 * The presence of this flag indicates that the mapping is writeable.
285 * If the ATTR_S1_AP_RO bit is also set, then the mapping is clean, otherwise
286 * it is dirty. This flag may only be set on managed mappings.
288 * The DBM bit is reserved on ARMv8.0 but it seems we can safely treat it
289 * as a software managed bit.
291 #define ATTR_SW_DBM ATTR_DBM
293 struct pmap kernel_pmap_store;
295 /* Used for mapping ACPI memory before VM is initialized */
296 #define PMAP_PREINIT_MAPPING_COUNT 32
297 #define PMAP_PREINIT_MAPPING_SIZE (PMAP_PREINIT_MAPPING_COUNT * L2_SIZE)
298 static vm_offset_t preinit_map_va; /* Start VA of pre-init mapping space */
299 static int vm_initialized = 0; /* No need to use pre-init maps when set */
302 * Reserve a few L2 blocks starting from 'preinit_map_va' pointer.
303 * Always map entire L2 block for simplicity.
304 * VA of L2 block = preinit_map_va + i * L2_SIZE
306 static struct pmap_preinit_mapping {
310 } pmap_preinit_mapping[PMAP_PREINIT_MAPPING_COUNT];
312 vm_offset_t virtual_avail; /* VA of first avail page (after kernel bss) */
313 vm_offset_t virtual_end; /* VA of last avail page (end of kernel AS) */
314 vm_offset_t kernel_vm_end = 0;
317 * Data for the pv entry allocation mechanism.
321 pc_to_domain(struct pv_chunk *pc)
323 return (vm_phys_domain(DMAP_TO_PHYS((vm_offset_t)pc)));
327 pc_to_domain(struct pv_chunk *pc __unused)
333 struct pv_chunks_list {
335 TAILQ_HEAD(pch, pv_chunk) pvc_list;
337 } __aligned(CACHE_LINE_SIZE);
339 struct pv_chunks_list __exclusive_cache_line pv_chunks[PMAP_MEMDOM];
341 __exclusive_cache_line static struct pmap_large_md_page pv_dummy_large;
342 #define pv_dummy pv_dummy_large.pv_page
343 __read_mostly static struct pmap_large_md_page *pv_table;
344 __read_mostly vm_paddr_t pmap_last_pa;
346 vm_paddr_t dmap_phys_base; /* The start of the dmap region */
347 vm_paddr_t dmap_phys_max; /* The limit of the dmap region */
348 vm_offset_t dmap_max_addr; /* The virtual address limit of the dmap */
350 extern pt_entry_t pagetable_l0_ttbr1[];
352 #define PHYSMAP_SIZE (2 * (VM_PHYSSEG_MAX - 1))
353 static vm_paddr_t physmap[PHYSMAP_SIZE];
354 static u_int physmap_idx;
356 static SYSCTL_NODE(_vm, OID_AUTO, pmap, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
357 "VM/pmap parameters");
359 #if PAGE_SIZE == PAGE_SIZE_4K
360 #define L1_BLOCKS_SUPPORTED 1
362 /* TODO: Make this dynamic when we support FEAT_LPA2 (TCR_EL1.DS == 1) */
363 #define L1_BLOCKS_SUPPORTED 0
366 #define PMAP_ASSERT_L1_BLOCKS_SUPPORTED MPASS(L1_BLOCKS_SUPPORTED)
369 * This ASID allocator uses a bit vector ("asid_set") to remember which ASIDs
370 * that it has currently allocated to a pmap, a cursor ("asid_next") to
371 * optimize its search for a free ASID in the bit vector, and an epoch number
372 * ("asid_epoch") to indicate when it has reclaimed all previously allocated
373 * ASIDs that are not currently active on a processor.
375 * The current epoch number is always in the range [0, INT_MAX). Negative
376 * numbers and INT_MAX are reserved for special cases that are described
385 struct mtx asid_set_mutex;
388 static struct asid_set asids;
389 static struct asid_set vmids;
391 static SYSCTL_NODE(_vm_pmap, OID_AUTO, asid, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
393 SYSCTL_INT(_vm_pmap_asid, OID_AUTO, bits, CTLFLAG_RD, &asids.asid_bits, 0,
394 "The number of bits in an ASID");
395 SYSCTL_INT(_vm_pmap_asid, OID_AUTO, next, CTLFLAG_RD, &asids.asid_next, 0,
396 "The last allocated ASID plus one");
397 SYSCTL_INT(_vm_pmap_asid, OID_AUTO, epoch, CTLFLAG_RD, &asids.asid_epoch, 0,
398 "The current epoch number");
400 static SYSCTL_NODE(_vm_pmap, OID_AUTO, vmid, CTLFLAG_RD, 0, "VMID allocator");
401 SYSCTL_INT(_vm_pmap_vmid, OID_AUTO, bits, CTLFLAG_RD, &vmids.asid_bits, 0,
402 "The number of bits in an VMID");
403 SYSCTL_INT(_vm_pmap_vmid, OID_AUTO, next, CTLFLAG_RD, &vmids.asid_next, 0,
404 "The last allocated VMID plus one");
405 SYSCTL_INT(_vm_pmap_vmid, OID_AUTO, epoch, CTLFLAG_RD, &vmids.asid_epoch, 0,
406 "The current epoch number");
408 void (*pmap_clean_stage2_tlbi)(void);
409 void (*pmap_invalidate_vpipt_icache)(void);
410 void (*pmap_stage2_invalidate_range)(uint64_t, vm_offset_t, vm_offset_t, bool);
411 void (*pmap_stage2_invalidate_all)(uint64_t);
414 * A pmap's cookie encodes an ASID and epoch number. Cookies for reserved
415 * ASIDs have a negative epoch number, specifically, INT_MIN. Cookies for
416 * dynamically allocated ASIDs have a non-negative epoch number.
418 * An invalid ASID is represented by -1.
420 * There are two special-case cookie values: (1) COOKIE_FROM(-1, INT_MIN),
421 * which indicates that an ASID should never be allocated to the pmap, and
422 * (2) COOKIE_FROM(-1, INT_MAX), which indicates that an ASID should be
423 * allocated when the pmap is next activated.
425 #define COOKIE_FROM(asid, epoch) ((long)((u_int)(asid) | \
426 ((u_long)(epoch) << 32)))
427 #define COOKIE_TO_ASID(cookie) ((int)(cookie))
428 #define COOKIE_TO_EPOCH(cookie) ((int)((u_long)(cookie) >> 32))
430 #define TLBI_VA_SHIFT 12
431 #define TLBI_VA_MASK ((1ul << 44) - 1)
432 #define TLBI_VA(addr) (((addr) >> TLBI_VA_SHIFT) & TLBI_VA_MASK)
433 #define TLBI_VA_L3_INCR (L3_SIZE >> TLBI_VA_SHIFT)
435 static int superpages_enabled = 1;
436 SYSCTL_INT(_vm_pmap, OID_AUTO, superpages_enabled,
437 CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &superpages_enabled, 0,
438 "Are large page mappings enabled?");
441 * Internal flags for pmap_enter()'s helper functions.
443 #define PMAP_ENTER_NORECLAIM 0x1000000 /* Don't reclaim PV entries. */
444 #define PMAP_ENTER_NOREPLACE 0x2000000 /* Don't replace mappings. */
446 TAILQ_HEAD(pv_chunklist, pv_chunk);
448 static void free_pv_chunk(struct pv_chunk *pc);
449 static void free_pv_chunk_batch(struct pv_chunklist *batch);
450 static void free_pv_entry(pmap_t pmap, pv_entry_t pv);
451 static pv_entry_t get_pv_entry(pmap_t pmap, struct rwlock **lockp);
452 static vm_page_t reclaim_pv_chunk(pmap_t locked_pmap, struct rwlock **lockp);
453 static void pmap_pvh_free(struct md_page *pvh, pmap_t pmap, vm_offset_t va);
454 static pv_entry_t pmap_pvh_remove(struct md_page *pvh, pmap_t pmap,
457 static void pmap_abort_ptp(pmap_t pmap, vm_offset_t va, vm_page_t mpte);
458 static bool pmap_activate_int(pmap_t pmap);
459 static void pmap_alloc_asid(pmap_t pmap);
460 static int pmap_change_props_locked(vm_offset_t va, vm_size_t size,
461 vm_prot_t prot, int mode, bool skip_unmapped);
462 static pt_entry_t *pmap_demote_l1(pmap_t pmap, pt_entry_t *l1, vm_offset_t va);
463 static pt_entry_t *pmap_demote_l2_locked(pmap_t pmap, pt_entry_t *l2,
464 vm_offset_t va, struct rwlock **lockp);
465 static pt_entry_t *pmap_demote_l2(pmap_t pmap, pt_entry_t *l2, vm_offset_t va);
466 static vm_page_t pmap_enter_quick_locked(pmap_t pmap, vm_offset_t va,
467 vm_page_t m, vm_prot_t prot, vm_page_t mpte, struct rwlock **lockp);
468 static int pmap_enter_l2(pmap_t pmap, vm_offset_t va, pd_entry_t new_l2,
469 u_int flags, vm_page_t m, struct rwlock **lockp);
470 static int pmap_remove_l2(pmap_t pmap, pt_entry_t *l2, vm_offset_t sva,
471 pd_entry_t l1e, struct spglist *free, struct rwlock **lockp);
472 static int pmap_remove_l3(pmap_t pmap, pt_entry_t *l3, vm_offset_t sva,
473 pd_entry_t l2e, struct spglist *free, struct rwlock **lockp);
474 static void pmap_reset_asid_set(pmap_t pmap);
475 static boolean_t pmap_try_insert_pv_entry(pmap_t pmap, vm_offset_t va,
476 vm_page_t m, struct rwlock **lockp);
478 static vm_page_t _pmap_alloc_l3(pmap_t pmap, vm_pindex_t ptepindex,
479 struct rwlock **lockp);
481 static void _pmap_unwire_l3(pmap_t pmap, vm_offset_t va, vm_page_t m,
482 struct spglist *free);
483 static int pmap_unuse_pt(pmap_t, vm_offset_t, pd_entry_t, struct spglist *);
484 static __inline vm_page_t pmap_remove_pt_page(pmap_t pmap, vm_offset_t va);
487 * These load the old table data and store the new value.
488 * They need to be atomic as the System MMU may write to the table at
489 * the same time as the CPU.
491 #define pmap_clear(table) atomic_store_64(table, 0)
492 #define pmap_clear_bits(table, bits) atomic_clear_64(table, bits)
493 #define pmap_load(table) (*table)
494 #define pmap_load_clear(table) atomic_swap_64(table, 0)
495 #define pmap_load_store(table, entry) atomic_swap_64(table, entry)
496 #define pmap_set_bits(table, bits) atomic_set_64(table, bits)
497 #define pmap_store(table, entry) atomic_store_64(table, entry)
499 /********************/
500 /* Inline functions */
501 /********************/
504 pagecopy(void *s, void *d)
507 memcpy(d, s, PAGE_SIZE);
510 static __inline pd_entry_t *
511 pmap_l0(pmap_t pmap, vm_offset_t va)
514 return (&pmap->pm_l0[pmap_l0_index(va)]);
517 static __inline pd_entry_t *
518 pmap_l0_to_l1(pd_entry_t *l0, vm_offset_t va)
522 l1 = (pd_entry_t *)PHYS_TO_DMAP(pmap_load(l0) & ~ATTR_MASK);
523 return (&l1[pmap_l1_index(va)]);
526 static __inline pd_entry_t *
527 pmap_l1(pmap_t pmap, vm_offset_t va)
531 l0 = pmap_l0(pmap, va);
532 if ((pmap_load(l0) & ATTR_DESCR_MASK) != L0_TABLE)
535 return (pmap_l0_to_l1(l0, va));
538 static __inline pd_entry_t *
539 pmap_l1_to_l2(pd_entry_t *l1p, vm_offset_t va)
545 KASSERT(ADDR_IS_CANONICAL(va),
546 ("%s: Address not in canonical form: %lx", __func__, va));
548 * The valid bit may be clear if pmap_update_entry() is concurrently
549 * modifying the entry, so for KVA only the entry type may be checked.
551 KASSERT(ADDR_IS_KERNEL(va) || (l1 & ATTR_DESCR_VALID) != 0,
552 ("%s: L1 entry %#lx for %#lx is invalid", __func__, l1, va));
553 KASSERT((l1 & ATTR_DESCR_TYPE_MASK) == ATTR_DESCR_TYPE_TABLE,
554 ("%s: L1 entry %#lx for %#lx is a leaf", __func__, l1, va));
555 l2p = (pd_entry_t *)PHYS_TO_DMAP(l1 & ~ATTR_MASK);
556 return (&l2p[pmap_l2_index(va)]);
559 static __inline pd_entry_t *
560 pmap_l2(pmap_t pmap, vm_offset_t va)
564 l1 = pmap_l1(pmap, va);
565 if ((pmap_load(l1) & ATTR_DESCR_MASK) != L1_TABLE)
568 return (pmap_l1_to_l2(l1, va));
571 static __inline pt_entry_t *
572 pmap_l2_to_l3(pd_entry_t *l2p, vm_offset_t va)
579 KASSERT(ADDR_IS_CANONICAL(va),
580 ("%s: Address not in canonical form: %lx", __func__, va));
582 * The valid bit may be clear if pmap_update_entry() is concurrently
583 * modifying the entry, so for KVA only the entry type may be checked.
585 KASSERT(ADDR_IS_KERNEL(va) || (l2 & ATTR_DESCR_VALID) != 0,
586 ("%s: L2 entry %#lx for %#lx is invalid", __func__, l2, va));
587 KASSERT((l2 & ATTR_DESCR_TYPE_MASK) == ATTR_DESCR_TYPE_TABLE,
588 ("%s: L2 entry %#lx for %#lx is a leaf", __func__, l2, va));
589 l3p = (pt_entry_t *)PHYS_TO_DMAP(l2 & ~ATTR_MASK);
590 return (&l3p[pmap_l3_index(va)]);
594 * Returns the lowest valid pde for a given virtual address.
595 * The next level may or may not point to a valid page or block.
597 static __inline pd_entry_t *
598 pmap_pde(pmap_t pmap, vm_offset_t va, int *level)
600 pd_entry_t *l0, *l1, *l2, desc;
602 l0 = pmap_l0(pmap, va);
603 desc = pmap_load(l0) & ATTR_DESCR_MASK;
604 if (desc != L0_TABLE) {
609 l1 = pmap_l0_to_l1(l0, va);
610 desc = pmap_load(l1) & ATTR_DESCR_MASK;
611 if (desc != L1_TABLE) {
616 l2 = pmap_l1_to_l2(l1, va);
617 desc = pmap_load(l2) & ATTR_DESCR_MASK;
618 if (desc != L2_TABLE) {
628 * Returns the lowest valid pte block or table entry for a given virtual
629 * address. If there are no valid entries return NULL and set the level to
630 * the first invalid level.
632 static __inline pt_entry_t *
633 pmap_pte(pmap_t pmap, vm_offset_t va, int *level)
635 pd_entry_t *l1, *l2, desc;
638 l1 = pmap_l1(pmap, va);
643 desc = pmap_load(l1) & ATTR_DESCR_MASK;
644 if (desc == L1_BLOCK) {
645 PMAP_ASSERT_L1_BLOCKS_SUPPORTED;
650 if (desc != L1_TABLE) {
655 l2 = pmap_l1_to_l2(l1, va);
656 desc = pmap_load(l2) & ATTR_DESCR_MASK;
657 if (desc == L2_BLOCK) {
662 if (desc != L2_TABLE) {
668 l3 = pmap_l2_to_l3(l2, va);
669 if ((pmap_load(l3) & ATTR_DESCR_MASK) != L3_PAGE)
676 * If the given pmap has an L{1,2}_BLOCK or L3_PAGE entry at the specified
677 * level that maps the specified virtual address, then a pointer to that entry
678 * is returned. Otherwise, NULL is returned, unless INVARIANTS are enabled
679 * and a diagnostic message is provided, in which case this function panics.
681 static __always_inline pt_entry_t *
682 pmap_pte_exists(pmap_t pmap, vm_offset_t va, int level, const char *diag)
684 pd_entry_t *l0p, *l1p, *l2p;
685 pt_entry_t desc, *l3p;
686 int walk_level __diagused;
688 KASSERT(level >= 0 && level < 4,
689 ("%s: %s passed an out-of-range level (%d)", __func__, diag,
691 l0p = pmap_l0(pmap, va);
692 desc = pmap_load(l0p) & ATTR_DESCR_MASK;
693 if (desc == L0_TABLE && level > 0) {
694 l1p = pmap_l0_to_l1(l0p, va);
695 desc = pmap_load(l1p) & ATTR_DESCR_MASK;
696 if (desc == L1_BLOCK && level == 1) {
697 PMAP_ASSERT_L1_BLOCKS_SUPPORTED;
700 if (desc == L1_TABLE && level > 1) {
701 l2p = pmap_l1_to_l2(l1p, va);
702 desc = pmap_load(l2p) & ATTR_DESCR_MASK;
703 if (desc == L2_BLOCK && level == 2)
705 else if (desc == L2_TABLE && level > 2) {
706 l3p = pmap_l2_to_l3(l2p, va);
707 desc = pmap_load(l3p) & ATTR_DESCR_MASK;
708 if (desc == L3_PAGE && level == 3)
718 KASSERT(diag == NULL,
719 ("%s: va %#lx not mapped at level %d, desc %ld at level %d",
720 diag, va, level, desc, walk_level));
725 pmap_ps_enabled(pmap_t pmap)
728 * Promotion requires a hypervisor call when the kernel is running
729 * in EL1. To stop this disable superpage support on non-stage 1
732 if (pmap->pm_stage != PM_STAGE1)
735 return (superpages_enabled != 0);
739 pmap_get_tables(pmap_t pmap, vm_offset_t va, pd_entry_t **l0, pd_entry_t **l1,
740 pd_entry_t **l2, pt_entry_t **l3)
742 pd_entry_t *l0p, *l1p, *l2p;
744 if (pmap->pm_l0 == NULL)
747 l0p = pmap_l0(pmap, va);
750 if ((pmap_load(l0p) & ATTR_DESCR_MASK) != L0_TABLE)
753 l1p = pmap_l0_to_l1(l0p, va);
756 if ((pmap_load(l1p) & ATTR_DESCR_MASK) == L1_BLOCK) {
757 PMAP_ASSERT_L1_BLOCKS_SUPPORTED;
763 if ((pmap_load(l1p) & ATTR_DESCR_MASK) != L1_TABLE)
766 l2p = pmap_l1_to_l2(l1p, va);
769 if ((pmap_load(l2p) & ATTR_DESCR_MASK) == L2_BLOCK) {
774 if ((pmap_load(l2p) & ATTR_DESCR_MASK) != L2_TABLE)
777 *l3 = pmap_l2_to_l3(l2p, va);
783 pmap_l3_valid(pt_entry_t l3)
786 return ((l3 & ATTR_DESCR_MASK) == L3_PAGE);
789 CTASSERT(L1_BLOCK == L2_BLOCK);
792 pmap_pte_memattr(pmap_t pmap, vm_memattr_t memattr)
796 if (pmap->pm_stage == PM_STAGE1) {
797 val = ATTR_S1_IDX(memattr);
798 if (memattr == VM_MEMATTR_DEVICE)
806 case VM_MEMATTR_DEVICE:
807 return (ATTR_S2_MEMATTR(ATTR_S2_MEMATTR_DEVICE_nGnRnE) |
808 ATTR_S2_XN(ATTR_S2_XN_ALL));
809 case VM_MEMATTR_UNCACHEABLE:
810 return (ATTR_S2_MEMATTR(ATTR_S2_MEMATTR_NC));
811 case VM_MEMATTR_WRITE_BACK:
812 return (ATTR_S2_MEMATTR(ATTR_S2_MEMATTR_WB));
813 case VM_MEMATTR_WRITE_THROUGH:
814 return (ATTR_S2_MEMATTR(ATTR_S2_MEMATTR_WT));
816 panic("%s: invalid memory attribute %x", __func__, memattr);
821 pmap_pte_prot(pmap_t pmap, vm_prot_t prot)
826 if (pmap->pm_stage == PM_STAGE1) {
827 if ((prot & VM_PROT_EXECUTE) == 0)
829 if ((prot & VM_PROT_WRITE) == 0)
830 val |= ATTR_S1_AP(ATTR_S1_AP_RO);
832 if ((prot & VM_PROT_WRITE) != 0)
833 val |= ATTR_S2_S2AP(ATTR_S2_S2AP_WRITE);
834 if ((prot & VM_PROT_READ) != 0)
835 val |= ATTR_S2_S2AP(ATTR_S2_S2AP_READ);
836 if ((prot & VM_PROT_EXECUTE) == 0)
837 val |= ATTR_S2_XN(ATTR_S2_XN_ALL);
844 * Checks if the PTE is dirty.
847 pmap_pte_dirty(pmap_t pmap, pt_entry_t pte)
850 KASSERT((pte & ATTR_SW_MANAGED) != 0, ("pte %#lx is unmanaged", pte));
852 if (pmap->pm_stage == PM_STAGE1) {
853 KASSERT((pte & (ATTR_S1_AP_RW_BIT | ATTR_SW_DBM)) != 0,
854 ("pte %#lx is writeable and missing ATTR_SW_DBM", pte));
856 return ((pte & (ATTR_S1_AP_RW_BIT | ATTR_SW_DBM)) ==
857 (ATTR_S1_AP(ATTR_S1_AP_RW) | ATTR_SW_DBM));
860 return ((pte & ATTR_S2_S2AP(ATTR_S2_S2AP_WRITE)) ==
861 ATTR_S2_S2AP(ATTR_S2_S2AP_WRITE));
865 pmap_resident_count_inc(pmap_t pmap, int count)
868 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
869 pmap->pm_stats.resident_count += count;
873 pmap_resident_count_dec(pmap_t pmap, int count)
876 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
877 KASSERT(pmap->pm_stats.resident_count >= count,
878 ("pmap %p resident count underflow %ld %d", pmap,
879 pmap->pm_stats.resident_count, count));
880 pmap->pm_stats.resident_count -= count;
884 pmap_early_vtophys(vm_offset_t va)
888 pa_page = arm64_address_translate_s1e1r(va) & PAR_PA_MASK;
889 return (pa_page | (va & PAR_LOW_MASK));
892 /* State of the bootstrapped DMAP page tables */
893 struct pmap_bootstrap_state {
897 vm_offset_t freemempos;
900 pt_entry_t table_attrs;
907 /* The bootstrap state */
908 static struct pmap_bootstrap_state bs_state = {
912 .table_attrs = TATTR_PXN_TABLE,
913 .l0_slot = L0_ENTRIES,
914 .l1_slot = Ln_ENTRIES,
915 .l2_slot = Ln_ENTRIES,
920 pmap_bootstrap_l0_table(struct pmap_bootstrap_state *state)
926 /* Link the level 0 table to a level 1 table */
927 l0_slot = pmap_l0_index(state->va);
928 if (l0_slot != state->l0_slot) {
930 * Make sure we move from a low address to high address
931 * before the DMAP region is ready. This ensures we never
932 * modify an existing mapping until we can map from a
933 * physical address to a virtual address.
935 MPASS(state->l0_slot < l0_slot ||
936 state->l0_slot == L0_ENTRIES ||
939 /* Reset lower levels */
942 state->l1_slot = Ln_ENTRIES;
943 state->l2_slot = Ln_ENTRIES;
945 /* Check the existing L0 entry */
946 state->l0_slot = l0_slot;
947 if (state->dmap_valid) {
948 l0e = pagetable_l0_ttbr1[l0_slot];
949 if ((l0e & ATTR_DESCR_VALID) != 0) {
950 MPASS((l0e & ATTR_DESCR_MASK) == L0_TABLE);
951 l1_pa = l0e & ~ATTR_MASK;
952 state->l1 = (pt_entry_t *)PHYS_TO_DMAP(l1_pa);
957 /* Create a new L0 table entry */
958 state->l1 = (pt_entry_t *)state->freemempos;
959 memset(state->l1, 0, PAGE_SIZE);
960 state->freemempos += PAGE_SIZE;
962 l1_pa = pmap_early_vtophys((vm_offset_t)state->l1);
963 MPASS((l1_pa & Ln_TABLE_MASK) == 0);
964 MPASS(pagetable_l0_ttbr1[l0_slot] == 0);
965 pmap_store(&pagetable_l0_ttbr1[l0_slot], l1_pa |
966 TATTR_UXN_TABLE | TATTR_AP_TABLE_NO_EL0 | L0_TABLE);
968 KASSERT(state->l1 != NULL, ("%s: NULL l1", __func__));
972 pmap_bootstrap_l1_table(struct pmap_bootstrap_state *state)
978 /* Make sure there is a valid L0 -> L1 table */
979 pmap_bootstrap_l0_table(state);
981 /* Link the level 1 table to a level 2 table */
982 l1_slot = pmap_l1_index(state->va);
983 if (l1_slot != state->l1_slot) {
984 /* See pmap_bootstrap_l0_table for a description */
985 MPASS(state->l1_slot < l1_slot ||
986 state->l1_slot == Ln_ENTRIES ||
989 /* Reset lower levels */
991 state->l2_slot = Ln_ENTRIES;
993 /* Check the existing L1 entry */
994 state->l1_slot = l1_slot;
995 if (state->dmap_valid) {
996 l1e = state->l1[l1_slot];
997 if ((l1e & ATTR_DESCR_VALID) != 0) {
998 MPASS((l1e & ATTR_DESCR_MASK) == L1_TABLE);
999 l2_pa = l1e & ~ATTR_MASK;
1000 state->l2 = (pt_entry_t *)PHYS_TO_DMAP(l2_pa);
1005 /* Create a new L1 table entry */
1006 state->l2 = (pt_entry_t *)state->freemempos;
1007 memset(state->l2, 0, PAGE_SIZE);
1008 state->freemempos += PAGE_SIZE;
1010 l2_pa = pmap_early_vtophys((vm_offset_t)state->l2);
1011 MPASS((l2_pa & Ln_TABLE_MASK) == 0);
1012 MPASS(state->l1[l1_slot] == 0);
1013 pmap_store(&state->l1[l1_slot], l2_pa | state->table_attrs |
1016 KASSERT(state->l2 != NULL, ("%s: NULL l2", __func__));
1020 pmap_bootstrap_l2_table(struct pmap_bootstrap_state *state)
1026 /* Make sure there is a valid L1 -> L2 table */
1027 pmap_bootstrap_l1_table(state);
1029 /* Link the level 2 table to a level 3 table */
1030 l2_slot = pmap_l2_index(state->va);
1031 if (l2_slot != state->l2_slot) {
1032 /* See pmap_bootstrap_l0_table for a description */
1033 MPASS(state->l2_slot < l2_slot ||
1034 state->l2_slot == Ln_ENTRIES ||
1037 /* Check the existing L2 entry */
1038 state->l2_slot = l2_slot;
1039 if (state->dmap_valid) {
1040 l2e = state->l2[l2_slot];
1041 if ((l2e & ATTR_DESCR_VALID) != 0) {
1042 MPASS((l2e & ATTR_DESCR_MASK) == L2_TABLE);
1043 l3_pa = l2e & ~ATTR_MASK;
1044 state->l3 = (pt_entry_t *)PHYS_TO_DMAP(l3_pa);
1049 /* Create a new L2 table entry */
1050 state->l3 = (pt_entry_t *)state->freemempos;
1051 memset(state->l3, 0, PAGE_SIZE);
1052 state->freemempos += PAGE_SIZE;
1054 l3_pa = pmap_early_vtophys((vm_offset_t)state->l3);
1055 MPASS((l3_pa & Ln_TABLE_MASK) == 0);
1056 MPASS(state->l2[l2_slot] == 0);
1057 pmap_store(&state->l2[l2_slot], l3_pa | state->table_attrs |
1060 KASSERT(state->l3 != NULL, ("%s: NULL l3", __func__));
1064 pmap_bootstrap_l2_block(struct pmap_bootstrap_state *state, int i)
1069 if ((physmap[i + 1] - state->pa) < L2_SIZE)
1072 /* Make sure there is a valid L1 table */
1073 pmap_bootstrap_l1_table(state);
1075 MPASS((state->va & L2_OFFSET) == 0);
1077 state->va < DMAP_MAX_ADDRESS &&
1078 (physmap[i + 1] - state->pa) >= L2_SIZE;
1079 state->va += L2_SIZE, state->pa += L2_SIZE) {
1081 * Stop if we are about to walk off the end of what the
1082 * current L1 slot can address.
1084 if (!first && (state->pa & L1_OFFSET) == 0)
1088 l2_slot = pmap_l2_index(state->va);
1089 MPASS((state->pa & L2_OFFSET) == 0);
1090 MPASS(state->l2[l2_slot] == 0);
1091 pmap_store(&state->l2[l2_slot], state->pa | ATTR_DEFAULT |
1092 ATTR_S1_XN | ATTR_S1_IDX(VM_MEMATTR_WRITE_BACK) |
1095 MPASS(state->va == (state->pa - dmap_phys_base + DMAP_MIN_ADDRESS));
1099 pmap_bootstrap_l3_page(struct pmap_bootstrap_state *state, int i)
1104 if ((physmap[i + 1] - state->pa) < L3_SIZE)
1107 /* Make sure there is a valid L2 table */
1108 pmap_bootstrap_l2_table(state);
1110 MPASS((state->va & L3_OFFSET) == 0);
1112 state->va < DMAP_MAX_ADDRESS &&
1113 (physmap[i + 1] - state->pa) >= L3_SIZE;
1114 state->va += L3_SIZE, state->pa += L3_SIZE) {
1116 * Stop if we are about to walk off the end of what the
1117 * current L2 slot can address.
1119 if (!first && (state->pa & L2_OFFSET) == 0)
1123 l3_slot = pmap_l3_index(state->va);
1124 MPASS((state->pa & L3_OFFSET) == 0);
1125 MPASS(state->l3[l3_slot] == 0);
1126 pmap_store(&state->l3[l3_slot], state->pa | ATTR_DEFAULT |
1127 ATTR_S1_XN | ATTR_S1_IDX(VM_MEMATTR_WRITE_BACK) |
1130 MPASS(state->va == (state->pa - dmap_phys_base + DMAP_MIN_ADDRESS));
1134 pmap_bootstrap_dmap(vm_paddr_t min_pa)
1138 dmap_phys_base = min_pa & ~L1_OFFSET;
1142 for (i = 0; i < (physmap_idx * 2); i += 2) {
1143 bs_state.pa = physmap[i] & ~L3_OFFSET;
1144 bs_state.va = bs_state.pa - dmap_phys_base + DMAP_MIN_ADDRESS;
1146 /* Create L3 mappings at the start of the region */
1147 if ((bs_state.pa & L2_OFFSET) != 0)
1148 pmap_bootstrap_l3_page(&bs_state, i);
1149 MPASS(bs_state.pa <= physmap[i + 1]);
1151 if (L1_BLOCKS_SUPPORTED) {
1152 /* Create L2 mappings at the start of the region */
1153 if ((bs_state.pa & L1_OFFSET) != 0)
1154 pmap_bootstrap_l2_block(&bs_state, i);
1155 MPASS(bs_state.pa <= physmap[i + 1]);
1157 /* Create the main L1 block mappings */
1158 for (; bs_state.va < DMAP_MAX_ADDRESS &&
1159 (physmap[i + 1] - bs_state.pa) >= L1_SIZE;
1160 bs_state.va += L1_SIZE, bs_state.pa += L1_SIZE) {
1161 /* Make sure there is a valid L1 table */
1162 pmap_bootstrap_l0_table(&bs_state);
1163 MPASS((bs_state.pa & L1_OFFSET) == 0);
1165 &bs_state.l1[pmap_l1_index(bs_state.va)],
1166 bs_state.pa | ATTR_DEFAULT | ATTR_S1_XN |
1167 ATTR_S1_IDX(VM_MEMATTR_WRITE_BACK) |
1170 MPASS(bs_state.pa <= physmap[i + 1]);
1172 /* Create L2 mappings at the end of the region */
1173 pmap_bootstrap_l2_block(&bs_state, i);
1175 while (bs_state.va < DMAP_MAX_ADDRESS &&
1176 (physmap[i + 1] - bs_state.pa) >= L2_SIZE) {
1177 pmap_bootstrap_l2_block(&bs_state, i);
1180 MPASS(bs_state.pa <= physmap[i + 1]);
1182 /* Create L3 mappings at the end of the region */
1183 pmap_bootstrap_l3_page(&bs_state, i);
1184 MPASS(bs_state.pa == physmap[i + 1]);
1186 if (bs_state.pa > dmap_phys_max) {
1187 dmap_phys_max = bs_state.pa;
1188 dmap_max_addr = bs_state.va;
1196 pmap_bootstrap_l2(vm_offset_t va)
1198 KASSERT((va & L1_OFFSET) == 0, ("Invalid virtual address"));
1200 /* Leave bs_state.pa as it's only needed to bootstrap blocks and pages*/
1203 for (; bs_state.va < VM_MAX_KERNEL_ADDRESS; bs_state.va += L1_SIZE)
1204 pmap_bootstrap_l1_table(&bs_state);
1208 pmap_bootstrap_l3(vm_offset_t va)
1210 KASSERT((va & L2_OFFSET) == 0, ("Invalid virtual address"));
1212 /* Leave bs_state.pa as it's only needed to bootstrap blocks and pages*/
1215 for (; bs_state.va < VM_MAX_KERNEL_ADDRESS; bs_state.va += L2_SIZE)
1216 pmap_bootstrap_l2_table(&bs_state);
1221 pmap_bootstrap_allocate_kasan_l2(vm_paddr_t start_pa, vm_paddr_t end_pa,
1222 vm_offset_t *start_va, int *nkasan_l2)
1230 pa = rounddown2(end_pa - L2_SIZE, L2_SIZE);
1231 l2 = pmap_l2(kernel_pmap, va);
1233 for (i = 0; pa >= start_pa && i < *nkasan_l2;
1234 i++, va += L2_SIZE, pa -= L2_SIZE, l2++) {
1236 * KASAN stack checking results in us having already allocated
1237 * part of our shadow map, so we can just skip those segments.
1239 if ((pmap_load(l2) & ATTR_DESCR_VALID) != 0) {
1244 pmap_store(l2, (pa & ~Ln_TABLE_MASK) | PMAP_SAN_PTE_BITS |
1249 * Ended the allocation due to start_pa constraint, rather than because
1250 * we allocated everything. Adjust back up to the start_pa and remove
1251 * the invalid L2 block from our accounting.
1253 if (pa < start_pa) {
1259 bzero((void *)PHYS_TO_DMAP(pa), i * L2_SIZE);
1260 physmem_exclude_region(pa, i * L2_SIZE, EXFLAG_NOALLOC);
1268 * Bootstrap the system enough to run with virtual memory.
1271 pmap_bootstrap(vm_paddr_t kernstart, vm_size_t kernlen)
1273 vm_offset_t dpcpu, msgbufpv;
1274 vm_paddr_t start_pa, pa, min_pa;
1275 uint64_t kern_delta;
1278 /* Verify that the ASID is set through TTBR0. */
1279 KASSERT((READ_SPECIALREG(tcr_el1) & TCR_A1) == 0,
1280 ("pmap_bootstrap: TCR_EL1.A1 != 0"));
1282 kern_delta = KERNBASE - kernstart;
1284 printf("pmap_bootstrap %lx %lx\n", kernstart, kernlen);
1285 printf("%lx\n", (KERNBASE >> L1_SHIFT) & Ln_ADDR_MASK);
1287 /* Set this early so we can use the pagetable walking functions */
1288 kernel_pmap_store.pm_l0 = pagetable_l0_ttbr1;
1289 PMAP_LOCK_INIT(kernel_pmap);
1290 kernel_pmap->pm_l0_paddr =
1291 pmap_early_vtophys((vm_offset_t)kernel_pmap_store.pm_l0);
1292 kernel_pmap->pm_cookie = COOKIE_FROM(-1, INT_MIN);
1293 kernel_pmap->pm_stage = PM_STAGE1;
1294 kernel_pmap->pm_levels = 4;
1295 kernel_pmap->pm_ttbr = kernel_pmap->pm_l0_paddr;
1296 kernel_pmap->pm_asid_set = &asids;
1298 /* Assume the address we were loaded to is a valid physical address */
1299 min_pa = KERNBASE - kern_delta;
1301 physmap_idx = physmem_avail(physmap, nitems(physmap));
1305 * Find the minimum physical address. physmap is sorted,
1306 * but may contain empty ranges.
1308 for (i = 0; i < physmap_idx * 2; i += 2) {
1309 if (physmap[i] == physmap[i + 1])
1311 if (physmap[i] <= min_pa)
1312 min_pa = physmap[i];
1315 bs_state.freemempos = KERNBASE + kernlen;
1316 bs_state.freemempos = roundup2(bs_state.freemempos, PAGE_SIZE);
1318 /* Create a direct map region early so we can use it for pa -> va */
1319 pmap_bootstrap_dmap(min_pa);
1320 bs_state.dmap_valid = true;
1322 * We only use PXN when we know nothing will be executed from it, e.g.
1325 bs_state.table_attrs &= ~TATTR_PXN_TABLE;
1327 start_pa = pa = KERNBASE - kern_delta;
1330 * Create the l2 tables up to VM_MAX_KERNEL_ADDRESS. We assume that the
1331 * loader allocated the first and only l2 page table page used to map
1332 * the kernel, preloaded files and module metadata.
1334 pmap_bootstrap_l2(KERNBASE + L1_SIZE);
1335 /* And the l3 tables for the early devmap */
1336 pmap_bootstrap_l3(VM_MAX_KERNEL_ADDRESS - (PMAP_MAPDEV_EARLY_SIZE));
1340 #define alloc_pages(var, np) \
1341 (var) = bs_state.freemempos; \
1342 bs_state.freemempos += (np * PAGE_SIZE); \
1343 memset((char *)(var), 0, ((np) * PAGE_SIZE));
1345 /* Allocate dynamic per-cpu area. */
1346 alloc_pages(dpcpu, DPCPU_SIZE / PAGE_SIZE);
1347 dpcpu_init((void *)dpcpu, 0);
1349 /* Allocate memory for the msgbuf, e.g. for /sbin/dmesg */
1350 alloc_pages(msgbufpv, round_page(msgbufsize) / PAGE_SIZE);
1351 msgbufp = (void *)msgbufpv;
1353 /* Reserve some VA space for early BIOS/ACPI mapping */
1354 preinit_map_va = roundup2(bs_state.freemempos, L2_SIZE);
1356 virtual_avail = preinit_map_va + PMAP_PREINIT_MAPPING_SIZE;
1357 virtual_avail = roundup2(virtual_avail, L1_SIZE);
1358 virtual_end = VM_MAX_KERNEL_ADDRESS - (PMAP_MAPDEV_EARLY_SIZE);
1359 kernel_vm_end = virtual_avail;
1361 pa = pmap_early_vtophys(bs_state.freemempos);
1363 physmem_exclude_region(start_pa, pa - start_pa, EXFLAG_NOALLOC);
1370 * Finish constructing the initial shadow map:
1371 * - Count how many pages from KERNBASE to virtual_avail (scaled for
1373 * - Map that entire range using L2 superpages.
1376 pmap_bootstrap_san(vm_paddr_t kernstart)
1379 int i, shadow_npages, nkasan_l2;
1382 * Rebuild physmap one more time, we may have excluded more regions from
1383 * allocation since pmap_bootstrap().
1385 bzero(physmap, sizeof(physmap));
1386 physmap_idx = physmem_avail(physmap, nitems(physmap));
1389 shadow_npages = (virtual_avail - VM_MIN_KERNEL_ADDRESS) / PAGE_SIZE;
1390 shadow_npages = howmany(shadow_npages, KASAN_SHADOW_SCALE);
1391 nkasan_l2 = howmany(shadow_npages, Ln_ENTRIES);
1393 /* Map the valid KVA up to this point. */
1394 va = KASAN_MIN_ADDRESS;
1397 * Find a slot in the physmap large enough for what we needed. We try to put
1398 * the shadow map as high up as we can to avoid depleting the lower 4GB in case
1399 * it's needed for, e.g., an xhci controller that can only do 32-bit DMA.
1401 for (i = (physmap_idx * 2) - 2; i >= 0 && nkasan_l2 > 0; i -= 2) {
1402 vm_paddr_t plow, phigh;
1404 /* L2 mappings must be backed by memory that is L2-aligned */
1405 plow = roundup2(physmap[i], L2_SIZE);
1406 phigh = physmap[i + 1];
1409 if (kernstart >= plow && kernstart < phigh)
1411 if (phigh - plow >= L2_SIZE)
1412 pmap_bootstrap_allocate_kasan_l2(plow, phigh, &va,
1417 panic("Could not find phys region for shadow map");
1420 * Done. We should now have a valid shadow address mapped for all KVA
1421 * that has been mapped so far, i.e., KERNBASE to virtual_avail. Thus,
1422 * shadow accesses by the kasan(9) runtime will succeed for this range.
1423 * When the kernel virtual address range is later expanded, as will
1424 * happen in vm_mem_init(), the shadow map will be grown as well. This
1425 * is handled by pmap_san_enter().
1431 * Initialize a vm_page's machine-dependent fields.
1434 pmap_page_init(vm_page_t m)
1437 TAILQ_INIT(&m->md.pv_list);
1438 m->md.pv_memattr = VM_MEMATTR_WRITE_BACK;
1442 pmap_init_asids(struct asid_set *set, int bits)
1446 set->asid_bits = bits;
1449 * We may be too early in the overall initialization process to use
1452 set->asid_set_size = 1 << set->asid_bits;
1453 set->asid_set = kmem_malloc(bitstr_size(set->asid_set_size),
1455 for (i = 0; i < ASID_FIRST_AVAILABLE; i++)
1456 bit_set(set->asid_set, i);
1457 set->asid_next = ASID_FIRST_AVAILABLE;
1458 mtx_init(&set->asid_set_mutex, "asid set", NULL, MTX_SPIN);
1462 pmap_init_pv_table(void)
1464 struct vm_phys_seg *seg, *next_seg;
1465 struct pmap_large_md_page *pvd;
1467 int domain, i, j, pages;
1470 * We strongly depend on the size being a power of two, so the assert
1471 * is overzealous. However, should the struct be resized to a
1472 * different power of two, the code below needs to be revisited.
1474 CTASSERT((sizeof(*pvd) == 64));
1477 * Calculate the size of the array.
1480 for (i = 0; i < vm_phys_nsegs; i++) {
1481 seg = &vm_phys_segs[i];
1482 pages = pmap_l2_pindex(roundup2(seg->end, L2_SIZE)) -
1483 pmap_l2_pindex(seg->start);
1484 s += round_page(pages * sizeof(*pvd));
1486 pv_table = (struct pmap_large_md_page *)kva_alloc(s);
1487 if (pv_table == NULL)
1488 panic("%s: kva_alloc failed\n", __func__);
1491 * Iterate physical segments to allocate domain-local memory for PV
1495 for (i = 0; i < vm_phys_nsegs; i++) {
1496 seg = &vm_phys_segs[i];
1497 pages = pmap_l2_pindex(roundup2(seg->end, L2_SIZE)) -
1498 pmap_l2_pindex(seg->start);
1499 domain = seg->domain;
1501 s = round_page(pages * sizeof(*pvd));
1503 for (j = 0; j < s; j += PAGE_SIZE) {
1504 vm_page_t m = vm_page_alloc_noobj_domain(domain,
1507 panic("failed to allocate PV table page");
1508 pmap_qenter((vm_offset_t)pvd + j, &m, 1);
1511 for (j = 0; j < s / sizeof(*pvd); j++) {
1512 rw_init_flags(&pvd->pv_lock, "pmap pv list", RW_NEW);
1513 TAILQ_INIT(&pvd->pv_page.pv_list);
1517 pvd = &pv_dummy_large;
1518 memset(pvd, 0, sizeof(*pvd));
1519 rw_init_flags(&pvd->pv_lock, "pmap pv list dummy", RW_NEW);
1520 TAILQ_INIT(&pvd->pv_page.pv_list);
1523 * Set pointers from vm_phys_segs to pv_table.
1525 for (i = 0, pvd = pv_table; i < vm_phys_nsegs; i++) {
1526 seg = &vm_phys_segs[i];
1527 seg->md_first = pvd;
1528 pvd += pmap_l2_pindex(roundup2(seg->end, L2_SIZE)) -
1529 pmap_l2_pindex(seg->start);
1532 * If there is a following segment, and the final
1533 * superpage of this segment and the initial superpage
1534 * of the next segment are the same then adjust the
1535 * pv_table entry for that next segment down by one so
1536 * that the pv_table entries will be shared.
1538 if (i + 1 < vm_phys_nsegs) {
1539 next_seg = &vm_phys_segs[i + 1];
1540 if (pmap_l2_pindex(roundup2(seg->end, L2_SIZE)) - 1 ==
1541 pmap_l2_pindex(next_seg->start)) {
1549 * Initialize the pmap module.
1550 * Called by vm_init, to initialize any structures that the pmap
1551 * system needs to map virtual memory.
1560 * Are large page mappings enabled?
1562 TUNABLE_INT_FETCH("vm.pmap.superpages_enabled", &superpages_enabled);
1563 if (superpages_enabled) {
1564 KASSERT(MAXPAGESIZES > 1 && pagesizes[1] == 0,
1565 ("pmap_init: can't assign to pagesizes[1]"));
1566 pagesizes[1] = L2_SIZE;
1567 if (L1_BLOCKS_SUPPORTED) {
1568 KASSERT(MAXPAGESIZES > 2 && pagesizes[2] == 0,
1569 ("pmap_init: can't assign to pagesizes[2]"));
1570 pagesizes[2] = L1_SIZE;
1575 * Initialize the ASID allocator.
1577 pmap_init_asids(&asids,
1578 (READ_SPECIALREG(tcr_el1) & TCR_ASID_16) != 0 ? 16 : 8);
1581 mmfr1 = READ_SPECIALREG(id_aa64mmfr1_el1);
1584 if (ID_AA64MMFR1_VMIDBits_VAL(mmfr1) ==
1585 ID_AA64MMFR1_VMIDBits_16)
1587 pmap_init_asids(&vmids, vmid_bits);
1591 * Initialize pv chunk lists.
1593 for (i = 0; i < PMAP_MEMDOM; i++) {
1594 mtx_init(&pv_chunks[i].pvc_lock, "pmap pv chunk list", NULL,
1596 TAILQ_INIT(&pv_chunks[i].pvc_list);
1598 pmap_init_pv_table();
1603 static SYSCTL_NODE(_vm_pmap, OID_AUTO, l2, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
1604 "2MB page mapping counters");
1606 static u_long pmap_l2_demotions;
1607 SYSCTL_ULONG(_vm_pmap_l2, OID_AUTO, demotions, CTLFLAG_RD,
1608 &pmap_l2_demotions, 0, "2MB page demotions");
1610 static u_long pmap_l2_mappings;
1611 SYSCTL_ULONG(_vm_pmap_l2, OID_AUTO, mappings, CTLFLAG_RD,
1612 &pmap_l2_mappings, 0, "2MB page mappings");
1614 static u_long pmap_l2_p_failures;
1615 SYSCTL_ULONG(_vm_pmap_l2, OID_AUTO, p_failures, CTLFLAG_RD,
1616 &pmap_l2_p_failures, 0, "2MB page promotion failures");
1618 static u_long pmap_l2_promotions;
1619 SYSCTL_ULONG(_vm_pmap_l2, OID_AUTO, promotions, CTLFLAG_RD,
1620 &pmap_l2_promotions, 0, "2MB page promotions");
1623 * If the given value for "final_only" is false, then any cached intermediate-
1624 * level entries, i.e., L{0,1,2}_TABLE entries, are invalidated in addition to
1625 * any cached final-level entry, i.e., either an L{1,2}_BLOCK or L3_PAGE entry.
1626 * Otherwise, just the cached final-level entry is invalidated.
1628 static __inline void
1629 pmap_s1_invalidate_kernel(uint64_t r, bool final_only)
1632 __asm __volatile("tlbi vaale1is, %0" : : "r" (r));
1634 __asm __volatile("tlbi vaae1is, %0" : : "r" (r));
1637 static __inline void
1638 pmap_s1_invalidate_user(uint64_t r, bool final_only)
1641 __asm __volatile("tlbi vale1is, %0" : : "r" (r));
1643 __asm __volatile("tlbi vae1is, %0" : : "r" (r));
1647 * Invalidates any cached final- and optionally intermediate-level TLB entries
1648 * for the specified virtual address in the given virtual address space.
1650 static __inline void
1651 pmap_s1_invalidate_page(pmap_t pmap, vm_offset_t va, bool final_only)
1655 PMAP_ASSERT_STAGE1(pmap);
1659 if (pmap == kernel_pmap) {
1660 pmap_s1_invalidate_kernel(r, final_only);
1662 r |= ASID_TO_OPERAND(COOKIE_TO_ASID(pmap->pm_cookie));
1663 pmap_s1_invalidate_user(r, final_only);
1669 static __inline void
1670 pmap_s2_invalidate_page(pmap_t pmap, vm_offset_t va, bool final_only)
1672 PMAP_ASSERT_STAGE2(pmap);
1673 MPASS(pmap_stage2_invalidate_range != NULL);
1674 pmap_stage2_invalidate_range(pmap_to_ttbr0(pmap), va, va + PAGE_SIZE,
1678 static __inline void
1679 pmap_invalidate_page(pmap_t pmap, vm_offset_t va, bool final_only)
1681 if (pmap->pm_stage == PM_STAGE1)
1682 pmap_s1_invalidate_page(pmap, va, final_only);
1684 pmap_s2_invalidate_page(pmap, va, final_only);
1688 * Invalidates any cached final- and optionally intermediate-level TLB entries
1689 * for the specified virtual address range in the given virtual address space.
1691 static __inline void
1692 pmap_s1_invalidate_range(pmap_t pmap, vm_offset_t sva, vm_offset_t eva,
1695 uint64_t end, r, start;
1697 PMAP_ASSERT_STAGE1(pmap);
1700 if (pmap == kernel_pmap) {
1701 start = TLBI_VA(sva);
1703 for (r = start; r < end; r += TLBI_VA_L3_INCR)
1704 pmap_s1_invalidate_kernel(r, final_only);
1706 start = end = ASID_TO_OPERAND(COOKIE_TO_ASID(pmap->pm_cookie));
1707 start |= TLBI_VA(sva);
1708 end |= TLBI_VA(eva);
1709 for (r = start; r < end; r += TLBI_VA_L3_INCR)
1710 pmap_s1_invalidate_user(r, final_only);
1716 static __inline void
1717 pmap_s2_invalidate_range(pmap_t pmap, vm_offset_t sva, vm_offset_t eva,
1720 PMAP_ASSERT_STAGE2(pmap);
1721 MPASS(pmap_stage2_invalidate_range != NULL);
1722 pmap_stage2_invalidate_range(pmap_to_ttbr0(pmap), sva, eva, final_only);
1725 static __inline void
1726 pmap_invalidate_range(pmap_t pmap, vm_offset_t sva, vm_offset_t eva,
1729 if (pmap->pm_stage == PM_STAGE1)
1730 pmap_s1_invalidate_range(pmap, sva, eva, final_only);
1732 pmap_s2_invalidate_range(pmap, sva, eva, final_only);
1736 * Invalidates all cached intermediate- and final-level TLB entries for the
1737 * given virtual address space.
1739 static __inline void
1740 pmap_s1_invalidate_all(pmap_t pmap)
1744 PMAP_ASSERT_STAGE1(pmap);
1747 if (pmap == kernel_pmap) {
1748 __asm __volatile("tlbi vmalle1is");
1750 r = ASID_TO_OPERAND(COOKIE_TO_ASID(pmap->pm_cookie));
1751 __asm __volatile("tlbi aside1is, %0" : : "r" (r));
1757 static __inline void
1758 pmap_s2_invalidate_all(pmap_t pmap)
1760 PMAP_ASSERT_STAGE2(pmap);
1761 MPASS(pmap_stage2_invalidate_all != NULL);
1762 pmap_stage2_invalidate_all(pmap_to_ttbr0(pmap));
1765 static __inline void
1766 pmap_invalidate_all(pmap_t pmap)
1768 if (pmap->pm_stage == PM_STAGE1)
1769 pmap_s1_invalidate_all(pmap);
1771 pmap_s2_invalidate_all(pmap);
1775 * Routine: pmap_extract
1777 * Extract the physical page address associated
1778 * with the given map/virtual_address pair.
1781 pmap_extract(pmap_t pmap, vm_offset_t va)
1783 pt_entry_t *pte, tpte;
1790 * Find the block or page map for this virtual address. pmap_pte
1791 * will return either a valid block/page entry, or NULL.
1793 pte = pmap_pte(pmap, va, &lvl);
1795 tpte = pmap_load(pte);
1796 pa = tpte & ~ATTR_MASK;
1799 PMAP_ASSERT_L1_BLOCKS_SUPPORTED;
1800 KASSERT((tpte & ATTR_DESCR_MASK) == L1_BLOCK,
1801 ("pmap_extract: Invalid L1 pte found: %lx",
1802 tpte & ATTR_DESCR_MASK));
1803 pa |= (va & L1_OFFSET);
1806 KASSERT((tpte & ATTR_DESCR_MASK) == L2_BLOCK,
1807 ("pmap_extract: Invalid L2 pte found: %lx",
1808 tpte & ATTR_DESCR_MASK));
1809 pa |= (va & L2_OFFSET);
1812 KASSERT((tpte & ATTR_DESCR_MASK) == L3_PAGE,
1813 ("pmap_extract: Invalid L3 pte found: %lx",
1814 tpte & ATTR_DESCR_MASK));
1815 pa |= (va & L3_OFFSET);
1824 * Routine: pmap_extract_and_hold
1826 * Atomically extract and hold the physical page
1827 * with the given pmap and virtual address pair
1828 * if that mapping permits the given protection.
1831 pmap_extract_and_hold(pmap_t pmap, vm_offset_t va, vm_prot_t prot)
1833 pt_entry_t *pte, tpte;
1841 pte = pmap_pte(pmap, va, &lvl);
1843 tpte = pmap_load(pte);
1845 KASSERT(lvl > 0 && lvl <= 3,
1846 ("pmap_extract_and_hold: Invalid level %d", lvl));
1848 * Check that the pte is either a L3 page, or a L1 or L2 block
1849 * entry. We can assume L1_BLOCK == L2_BLOCK.
1851 KASSERT((lvl == 3 && (tpte & ATTR_DESCR_MASK) == L3_PAGE) ||
1852 (lvl < 3 && (tpte & ATTR_DESCR_MASK) == L1_BLOCK),
1853 ("pmap_extract_and_hold: Invalid pte at L%d: %lx", lvl,
1854 tpte & ATTR_DESCR_MASK));
1857 if ((prot & VM_PROT_WRITE) == 0)
1859 else if (pmap->pm_stage == PM_STAGE1 &&
1860 (tpte & ATTR_S1_AP_RW_BIT) == ATTR_S1_AP(ATTR_S1_AP_RW))
1862 else if (pmap->pm_stage == PM_STAGE2 &&
1863 ((tpte & ATTR_S2_S2AP(ATTR_S2_S2AP_WRITE)) ==
1864 ATTR_S2_S2AP(ATTR_S2_S2AP_WRITE)))
1870 off = va & L1_OFFSET;
1873 off = va & L2_OFFSET;
1879 m = PHYS_TO_VM_PAGE((tpte & ~ATTR_MASK) | off);
1880 if (m != NULL && !vm_page_wire_mapped(m))
1889 * Walks the page tables to translate a kernel virtual address to a
1890 * physical address. Returns true if the kva is valid and stores the
1891 * physical address in pa if it is not NULL.
1893 * See the comment above data_abort() for the rationale for specifying
1894 * NO_PERTHREAD_SSP here.
1896 bool NO_PERTHREAD_SSP
1897 pmap_klookup(vm_offset_t va, vm_paddr_t *pa)
1899 pt_entry_t *pte, tpte;
1904 * Disable interrupts so we don't get interrupted between asking
1905 * for address translation, and getting the result back.
1907 intr = intr_disable();
1908 par = arm64_address_translate_s1e1r(va);
1911 if (PAR_SUCCESS(par)) {
1913 *pa = (par & PAR_PA_MASK) | (va & PAR_LOW_MASK);
1918 * Fall back to walking the page table. The address translation
1919 * instruction may fail when the page is in a break-before-make
1920 * sequence. As we only clear the valid bit in said sequence we
1921 * can walk the page table to find the physical address.
1924 pte = pmap_l1(kernel_pmap, va);
1929 * A concurrent pmap_update_entry() will clear the entry's valid bit
1930 * but leave the rest of the entry unchanged. Therefore, we treat a
1931 * non-zero entry as being valid, and we ignore the valid bit when
1932 * determining whether the entry maps a block, page, or table.
1934 tpte = pmap_load(pte);
1937 if ((tpte & ATTR_DESCR_TYPE_MASK) == ATTR_DESCR_TYPE_BLOCK) {
1939 *pa = (tpte & ~ATTR_MASK) | (va & L1_OFFSET);
1942 pte = pmap_l1_to_l2(&tpte, va);
1943 tpte = pmap_load(pte);
1946 if ((tpte & ATTR_DESCR_TYPE_MASK) == ATTR_DESCR_TYPE_BLOCK) {
1948 *pa = (tpte & ~ATTR_MASK) | (va & L2_OFFSET);
1951 pte = pmap_l2_to_l3(&tpte, va);
1952 tpte = pmap_load(pte);
1956 *pa = (tpte & ~ATTR_MASK) | (va & L3_OFFSET);
1961 pmap_kextract(vm_offset_t va)
1965 if (va >= DMAP_MIN_ADDRESS && va < DMAP_MAX_ADDRESS)
1966 return (DMAP_TO_PHYS(va));
1968 if (pmap_klookup(va, &pa) == false)
1973 /***************************************************
1974 * Low level mapping routines.....
1975 ***************************************************/
1978 pmap_kenter(vm_offset_t sva, vm_size_t size, vm_paddr_t pa, int mode)
1981 pt_entry_t *pte, attr;
1985 KASSERT((pa & L3_OFFSET) == 0,
1986 ("pmap_kenter: Invalid physical address"));
1987 KASSERT((sva & L3_OFFSET) == 0,
1988 ("pmap_kenter: Invalid virtual address"));
1989 KASSERT((size & PAGE_MASK) == 0,
1990 ("pmap_kenter: Mapping is not page-sized"));
1992 attr = ATTR_DEFAULT | ATTR_S1_AP(ATTR_S1_AP_RW) | ATTR_S1_XN |
1993 ATTR_S1_IDX(mode) | L3_PAGE;
1996 pde = pmap_pde(kernel_pmap, va, &lvl);
1997 KASSERT(pde != NULL,
1998 ("pmap_kenter: Invalid page entry, va: 0x%lx", va));
1999 KASSERT(lvl == 2, ("pmap_kenter: Invalid level %d", lvl));
2001 pte = pmap_l2_to_l3(pde, va);
2002 pmap_load_store(pte, (pa & ~L3_OFFSET) | attr);
2008 pmap_s1_invalidate_range(kernel_pmap, sva, va, true);
2012 pmap_kenter_device(vm_offset_t sva, vm_size_t size, vm_paddr_t pa)
2015 pmap_kenter(sva, size, pa, VM_MEMATTR_DEVICE);
2019 * Remove a page from the kernel pagetables.
2022 pmap_kremove(vm_offset_t va)
2026 pte = pmap_pte_exists(kernel_pmap, va, 3, __func__);
2028 pmap_s1_invalidate_page(kernel_pmap, va, true);
2032 pmap_kremove_device(vm_offset_t sva, vm_size_t size)
2037 KASSERT((sva & L3_OFFSET) == 0,
2038 ("pmap_kremove_device: Invalid virtual address"));
2039 KASSERT((size & PAGE_MASK) == 0,
2040 ("pmap_kremove_device: Mapping is not page-sized"));
2044 pte = pmap_pte_exists(kernel_pmap, va, 3, __func__);
2050 pmap_s1_invalidate_range(kernel_pmap, sva, va, true);
2054 * Used to map a range of physical addresses into kernel
2055 * virtual address space.
2057 * The value passed in '*virt' is a suggested virtual address for
2058 * the mapping. Architectures which can support a direct-mapped
2059 * physical to virtual region can return the appropriate address
2060 * within that region, leaving '*virt' unchanged. Other
2061 * architectures should map the pages starting at '*virt' and
2062 * update '*virt' with the first usable address after the mapped
2066 pmap_map(vm_offset_t *virt, vm_paddr_t start, vm_paddr_t end, int prot)
2068 return PHYS_TO_DMAP(start);
2072 * Add a list of wired pages to the kva
2073 * this routine is only used for temporary
2074 * kernel mappings that do not need to have
2075 * page modification or references recorded.
2076 * Note that old mappings are simply written
2077 * over. The page *must* be wired.
2078 * Note: SMP coherent. Uses a ranged shootdown IPI.
2081 pmap_qenter(vm_offset_t sva, vm_page_t *ma, int count)
2084 pt_entry_t *pte, pa;
2090 for (i = 0; i < count; i++) {
2091 pde = pmap_pde(kernel_pmap, va, &lvl);
2092 KASSERT(pde != NULL,
2093 ("pmap_qenter: Invalid page entry, va: 0x%lx", va));
2095 ("pmap_qenter: Invalid level %d", lvl));
2098 pa = VM_PAGE_TO_PHYS(m) | ATTR_DEFAULT |
2099 ATTR_S1_AP(ATTR_S1_AP_RW) | ATTR_S1_XN |
2100 ATTR_S1_IDX(m->md.pv_memattr) | L3_PAGE;
2101 pte = pmap_l2_to_l3(pde, va);
2102 pmap_load_store(pte, pa);
2106 pmap_s1_invalidate_range(kernel_pmap, sva, va, true);
2110 * This routine tears out page mappings from the
2111 * kernel -- it is meant only for temporary mappings.
2114 pmap_qremove(vm_offset_t sva, int count)
2119 KASSERT(ADDR_IS_CANONICAL(sva),
2120 ("%s: Address not in canonical form: %lx", __func__, sva));
2121 KASSERT(ADDR_IS_KERNEL(sva), ("usermode va %lx", sva));
2124 while (count-- > 0) {
2125 pte = pmap_pte_exists(kernel_pmap, va, 3, NULL);
2132 pmap_s1_invalidate_range(kernel_pmap, sva, va, true);
2135 /***************************************************
2136 * Page table page management routines.....
2137 ***************************************************/
2139 * Schedule the specified unused page table page to be freed. Specifically,
2140 * add the page to the specified list of pages that will be released to the
2141 * physical memory manager after the TLB has been updated.
2143 static __inline void
2144 pmap_add_delayed_free_list(vm_page_t m, struct spglist *free,
2145 boolean_t set_PG_ZERO)
2149 m->flags |= PG_ZERO;
2151 m->flags &= ~PG_ZERO;
2152 SLIST_INSERT_HEAD(free, m, plinks.s.ss);
2156 * Decrements a page table page's reference count, which is used to record the
2157 * number of valid page table entries within the page. If the reference count
2158 * drops to zero, then the page table page is unmapped. Returns TRUE if the
2159 * page table page was unmapped and FALSE otherwise.
2161 static inline boolean_t
2162 pmap_unwire_l3(pmap_t pmap, vm_offset_t va, vm_page_t m, struct spglist *free)
2166 if (m->ref_count == 0) {
2167 _pmap_unwire_l3(pmap, va, m, free);
2174 _pmap_unwire_l3(pmap_t pmap, vm_offset_t va, vm_page_t m, struct spglist *free)
2177 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2179 * unmap the page table page
2181 if (m->pindex >= (NUL2E + NUL1E)) {
2185 l0 = pmap_l0(pmap, va);
2187 } else if (m->pindex >= NUL2E) {
2191 l1 = pmap_l1(pmap, va);
2197 l2 = pmap_l2(pmap, va);
2200 pmap_resident_count_dec(pmap, 1);
2201 if (m->pindex < NUL2E) {
2202 /* We just released an l3, unhold the matching l2 */
2203 pd_entry_t *l1, tl1;
2206 l1 = pmap_l1(pmap, va);
2207 tl1 = pmap_load(l1);
2208 l2pg = PHYS_TO_VM_PAGE(tl1 & ~ATTR_MASK);
2209 pmap_unwire_l3(pmap, va, l2pg, free);
2210 } else if (m->pindex < (NUL2E + NUL1E)) {
2211 /* We just released an l2, unhold the matching l1 */
2212 pd_entry_t *l0, tl0;
2215 l0 = pmap_l0(pmap, va);
2216 tl0 = pmap_load(l0);
2217 l1pg = PHYS_TO_VM_PAGE(tl0 & ~ATTR_MASK);
2218 pmap_unwire_l3(pmap, va, l1pg, free);
2220 pmap_invalidate_page(pmap, va, false);
2223 * Put page on a list so that it is released after
2224 * *ALL* TLB shootdown is done
2226 pmap_add_delayed_free_list(m, free, TRUE);
2230 * After removing a page table entry, this routine is used to
2231 * conditionally free the page, and manage the reference count.
2234 pmap_unuse_pt(pmap_t pmap, vm_offset_t va, pd_entry_t ptepde,
2235 struct spglist *free)
2239 KASSERT(ADDR_IS_CANONICAL(va),
2240 ("%s: Address not in canonical form: %lx", __func__, va));
2241 if (ADDR_IS_KERNEL(va))
2243 KASSERT(ptepde != 0, ("pmap_unuse_pt: ptepde != 0"));
2244 mpte = PHYS_TO_VM_PAGE(ptepde & ~ATTR_MASK);
2245 return (pmap_unwire_l3(pmap, va, mpte, free));
2249 * Release a page table page reference after a failed attempt to create a
2253 pmap_abort_ptp(pmap_t pmap, vm_offset_t va, vm_page_t mpte)
2255 struct spglist free;
2258 if (pmap_unwire_l3(pmap, va, mpte, &free))
2259 vm_page_free_pages_toq(&free, true);
2263 pmap_pinit0(pmap_t pmap)
2266 PMAP_LOCK_INIT(pmap);
2267 bzero(&pmap->pm_stats, sizeof(pmap->pm_stats));
2268 pmap->pm_l0_paddr = READ_SPECIALREG(ttbr0_el1);
2269 pmap->pm_l0 = (pd_entry_t *)PHYS_TO_DMAP(pmap->pm_l0_paddr);
2270 vm_radix_init(&pmap->pm_root);
2271 pmap->pm_cookie = COOKIE_FROM(ASID_RESERVED_FOR_PID_0, INT_MIN);
2272 pmap->pm_stage = PM_STAGE1;
2273 pmap->pm_levels = 4;
2274 pmap->pm_ttbr = pmap->pm_l0_paddr;
2275 pmap->pm_asid_set = &asids;
2277 PCPU_SET(curpmap, pmap);
2281 pmap_pinit_stage(pmap_t pmap, enum pmap_stage stage, int levels)
2286 * allocate the l0 page
2288 m = vm_page_alloc_noobj(VM_ALLOC_WAITOK | VM_ALLOC_WIRED |
2290 pmap->pm_l0_paddr = VM_PAGE_TO_PHYS(m);
2291 pmap->pm_l0 = (pd_entry_t *)PHYS_TO_DMAP(pmap->pm_l0_paddr);
2293 vm_radix_init(&pmap->pm_root);
2294 bzero(&pmap->pm_stats, sizeof(pmap->pm_stats));
2295 pmap->pm_cookie = COOKIE_FROM(-1, INT_MAX);
2297 MPASS(levels == 3 || levels == 4);
2298 pmap->pm_levels = levels;
2299 pmap->pm_stage = stage;
2302 pmap->pm_asid_set = &asids;
2305 pmap->pm_asid_set = &vmids;
2308 panic("%s: Invalid pmap type %d", __func__, stage);
2312 /* XXX Temporarily disable deferred ASID allocation. */
2313 pmap_alloc_asid(pmap);
2316 * Allocate the level 1 entry to use as the root. This will increase
2317 * the refcount on the level 1 page so it won't be removed until
2318 * pmap_release() is called.
2320 if (pmap->pm_levels == 3) {
2322 m = _pmap_alloc_l3(pmap, NUL2E + NUL1E, NULL);
2325 pmap->pm_ttbr = VM_PAGE_TO_PHYS(m);
2331 pmap_pinit(pmap_t pmap)
2334 return (pmap_pinit_stage(pmap, PM_STAGE1, 4));
2338 * This routine is called if the desired page table page does not exist.
2340 * If page table page allocation fails, this routine may sleep before
2341 * returning NULL. It sleeps only if a lock pointer was given.
2343 * Note: If a page allocation fails at page table level two or three,
2344 * one or two pages may be held during the wait, only to be released
2345 * afterwards. This conservative approach is easily argued to avoid
2349 _pmap_alloc_l3(pmap_t pmap, vm_pindex_t ptepindex, struct rwlock **lockp)
2351 vm_page_t m, l1pg, l2pg;
2353 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2356 * Allocate a page table page.
2358 if ((m = vm_page_alloc_noobj(VM_ALLOC_WIRED | VM_ALLOC_ZERO)) == NULL) {
2359 if (lockp != NULL) {
2360 RELEASE_PV_LIST_LOCK(lockp);
2367 * Indicate the need to retry. While waiting, the page table
2368 * page may have been allocated.
2372 m->pindex = ptepindex;
2375 * Because of AArch64's weak memory consistency model, we must have a
2376 * barrier here to ensure that the stores for zeroing "m", whether by
2377 * pmap_zero_page() or an earlier function, are visible before adding
2378 * "m" to the page table. Otherwise, a page table walk by another
2379 * processor's MMU could see the mapping to "m" and a stale, non-zero
2385 * Map the pagetable page into the process address space, if
2386 * it isn't already there.
2389 if (ptepindex >= (NUL2E + NUL1E)) {
2390 pd_entry_t *l0p, l0e;
2391 vm_pindex_t l0index;
2393 l0index = ptepindex - (NUL2E + NUL1E);
2394 l0p = &pmap->pm_l0[l0index];
2395 KASSERT((pmap_load(l0p) & ATTR_DESCR_VALID) == 0,
2396 ("%s: L0 entry %#lx is valid", __func__, pmap_load(l0p)));
2397 l0e = VM_PAGE_TO_PHYS(m) | L0_TABLE;
2400 * Mark all kernel memory as not accessible from userspace
2401 * and userspace memory as not executable from the kernel.
2402 * This has been done for the bootstrap L0 entries in
2405 if (pmap == kernel_pmap)
2406 l0e |= TATTR_UXN_TABLE | TATTR_AP_TABLE_NO_EL0;
2408 l0e |= TATTR_PXN_TABLE;
2409 pmap_store(l0p, l0e);
2410 } else if (ptepindex >= NUL2E) {
2411 vm_pindex_t l0index, l1index;
2412 pd_entry_t *l0, *l1;
2415 l1index = ptepindex - NUL2E;
2416 l0index = l1index >> Ln_ENTRIES_SHIFT;
2418 l0 = &pmap->pm_l0[l0index];
2419 tl0 = pmap_load(l0);
2421 /* recurse for allocating page dir */
2422 if (_pmap_alloc_l3(pmap, NUL2E + NUL1E + l0index,
2424 vm_page_unwire_noq(m);
2425 vm_page_free_zero(m);
2429 l1pg = PHYS_TO_VM_PAGE(tl0 & ~ATTR_MASK);
2433 l1 = (pd_entry_t *)PHYS_TO_DMAP(pmap_load(l0) & ~ATTR_MASK);
2434 l1 = &l1[ptepindex & Ln_ADDR_MASK];
2435 KASSERT((pmap_load(l1) & ATTR_DESCR_VALID) == 0,
2436 ("%s: L1 entry %#lx is valid", __func__, pmap_load(l1)));
2437 pmap_store(l1, VM_PAGE_TO_PHYS(m) | L1_TABLE);
2439 vm_pindex_t l0index, l1index;
2440 pd_entry_t *l0, *l1, *l2;
2441 pd_entry_t tl0, tl1;
2443 l1index = ptepindex >> Ln_ENTRIES_SHIFT;
2444 l0index = l1index >> Ln_ENTRIES_SHIFT;
2446 l0 = &pmap->pm_l0[l0index];
2447 tl0 = pmap_load(l0);
2449 /* recurse for allocating page dir */
2450 if (_pmap_alloc_l3(pmap, NUL2E + l1index,
2452 vm_page_unwire_noq(m);
2453 vm_page_free_zero(m);
2456 tl0 = pmap_load(l0);
2457 l1 = (pd_entry_t *)PHYS_TO_DMAP(tl0 & ~ATTR_MASK);
2458 l1 = &l1[l1index & Ln_ADDR_MASK];
2460 l1 = (pd_entry_t *)PHYS_TO_DMAP(tl0 & ~ATTR_MASK);
2461 l1 = &l1[l1index & Ln_ADDR_MASK];
2462 tl1 = pmap_load(l1);
2464 /* recurse for allocating page dir */
2465 if (_pmap_alloc_l3(pmap, NUL2E + l1index,
2467 vm_page_unwire_noq(m);
2468 vm_page_free_zero(m);
2472 l2pg = PHYS_TO_VM_PAGE(tl1 & ~ATTR_MASK);
2477 l2 = (pd_entry_t *)PHYS_TO_DMAP(pmap_load(l1) & ~ATTR_MASK);
2478 l2 = &l2[ptepindex & Ln_ADDR_MASK];
2479 KASSERT((pmap_load(l2) & ATTR_DESCR_VALID) == 0,
2480 ("%s: L2 entry %#lx is valid", __func__, pmap_load(l2)));
2481 pmap_store(l2, VM_PAGE_TO_PHYS(m) | L2_TABLE);
2484 pmap_resident_count_inc(pmap, 1);
2490 pmap_alloc_l2(pmap_t pmap, vm_offset_t va, vm_page_t *l2pgp,
2491 struct rwlock **lockp)
2493 pd_entry_t *l1, *l2;
2495 vm_pindex_t l2pindex;
2497 KASSERT(ADDR_IS_CANONICAL(va),
2498 ("%s: Address not in canonical form: %lx", __func__, va));
2501 l1 = pmap_l1(pmap, va);
2502 if (l1 != NULL && (pmap_load(l1) & ATTR_DESCR_MASK) == L1_TABLE) {
2503 l2 = pmap_l1_to_l2(l1, va);
2504 if (!ADDR_IS_KERNEL(va)) {
2505 /* Add a reference to the L2 page. */
2506 l2pg = PHYS_TO_VM_PAGE(pmap_load(l1) & ~ATTR_MASK);
2510 } else if (!ADDR_IS_KERNEL(va)) {
2511 /* Allocate a L2 page. */
2512 l2pindex = pmap_l2_pindex(va) >> Ln_ENTRIES_SHIFT;
2513 l2pg = _pmap_alloc_l3(pmap, NUL2E + l2pindex, lockp);
2520 l2 = (pd_entry_t *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(l2pg));
2521 l2 = &l2[pmap_l2_index(va)];
2523 panic("pmap_alloc_l2: missing page table page for va %#lx",
2530 pmap_alloc_l3(pmap_t pmap, vm_offset_t va, struct rwlock **lockp)
2532 vm_pindex_t ptepindex;
2533 pd_entry_t *pde, tpde;
2541 * Calculate pagetable page index
2543 ptepindex = pmap_l2_pindex(va);
2546 * Get the page directory entry
2548 pde = pmap_pde(pmap, va, &lvl);
2551 * If the page table page is mapped, we just increment the hold count,
2552 * and activate it. If we get a level 2 pde it will point to a level 3
2560 pte = pmap_l0_to_l1(pde, va);
2561 KASSERT(pmap_load(pte) == 0,
2562 ("pmap_alloc_l3: TODO: l0 superpages"));
2567 pte = pmap_l1_to_l2(pde, va);
2568 KASSERT(pmap_load(pte) == 0,
2569 ("pmap_alloc_l3: TODO: l1 superpages"));
2573 tpde = pmap_load(pde);
2575 m = PHYS_TO_VM_PAGE(tpde & ~ATTR_MASK);
2581 panic("pmap_alloc_l3: Invalid level %d", lvl);
2585 * Here if the pte page isn't mapped, or if it has been deallocated.
2587 m = _pmap_alloc_l3(pmap, ptepindex, lockp);
2588 if (m == NULL && lockp != NULL)
2594 /***************************************************
2595 * Pmap allocation/deallocation routines.
2596 ***************************************************/
2599 * Release any resources held by the given physical map.
2600 * Called when a pmap initialized by pmap_pinit is being released.
2601 * Should only be called if the map contains no valid mappings.
2604 pmap_release(pmap_t pmap)
2606 boolean_t rv __diagused;
2607 struct spglist free;
2608 struct asid_set *set;
2612 if (pmap->pm_levels != 4) {
2613 PMAP_ASSERT_STAGE2(pmap);
2614 KASSERT(pmap->pm_stats.resident_count == 1,
2615 ("pmap_release: pmap resident count %ld != 0",
2616 pmap->pm_stats.resident_count));
2617 KASSERT((pmap->pm_l0[0] & ATTR_DESCR_VALID) == ATTR_DESCR_VALID,
2618 ("pmap_release: Invalid l0 entry: %lx", pmap->pm_l0[0]));
2621 m = PHYS_TO_VM_PAGE(pmap->pm_ttbr);
2623 rv = pmap_unwire_l3(pmap, 0, m, &free);
2626 vm_page_free_pages_toq(&free, true);
2629 KASSERT(pmap->pm_stats.resident_count == 0,
2630 ("pmap_release: pmap resident count %ld != 0",
2631 pmap->pm_stats.resident_count));
2632 KASSERT(vm_radix_is_empty(&pmap->pm_root),
2633 ("pmap_release: pmap has reserved page table page(s)"));
2635 set = pmap->pm_asid_set;
2636 KASSERT(set != NULL, ("%s: NULL asid set", __func__));
2639 * Allow the ASID to be reused. In stage 2 VMIDs we don't invalidate
2640 * the entries when removing them so rely on a later tlb invalidation.
2641 * this will happen when updating the VMID generation. Because of this
2642 * we don't reuse VMIDs within a generation.
2644 if (pmap->pm_stage == PM_STAGE1) {
2645 mtx_lock_spin(&set->asid_set_mutex);
2646 if (COOKIE_TO_EPOCH(pmap->pm_cookie) == set->asid_epoch) {
2647 asid = COOKIE_TO_ASID(pmap->pm_cookie);
2648 KASSERT(asid >= ASID_FIRST_AVAILABLE &&
2649 asid < set->asid_set_size,
2650 ("pmap_release: pmap cookie has out-of-range asid"));
2651 bit_clear(set->asid_set, asid);
2653 mtx_unlock_spin(&set->asid_set_mutex);
2656 m = PHYS_TO_VM_PAGE(pmap->pm_l0_paddr);
2657 vm_page_unwire_noq(m);
2658 vm_page_free_zero(m);
2662 kvm_size(SYSCTL_HANDLER_ARGS)
2664 unsigned long ksize = VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS;
2666 return sysctl_handle_long(oidp, &ksize, 0, req);
2668 SYSCTL_PROC(_vm, OID_AUTO, kvm_size, CTLTYPE_LONG | CTLFLAG_RD | CTLFLAG_MPSAFE,
2669 0, 0, kvm_size, "LU",
2673 kvm_free(SYSCTL_HANDLER_ARGS)
2675 unsigned long kfree = VM_MAX_KERNEL_ADDRESS - kernel_vm_end;
2677 return sysctl_handle_long(oidp, &kfree, 0, req);
2679 SYSCTL_PROC(_vm, OID_AUTO, kvm_free, CTLTYPE_LONG | CTLFLAG_RD | CTLFLAG_MPSAFE,
2680 0, 0, kvm_free, "LU",
2681 "Amount of KVM free");
2684 * grow the number of kernel page table entries, if needed
2687 pmap_growkernel(vm_offset_t addr)
2691 pd_entry_t *l0, *l1, *l2;
2693 mtx_assert(&kernel_map->system_mtx, MA_OWNED);
2695 addr = roundup2(addr, L2_SIZE);
2696 if (addr - 1 >= vm_map_max(kernel_map))
2697 addr = vm_map_max(kernel_map);
2698 if (kernel_vm_end < addr)
2699 kasan_shadow_map(kernel_vm_end, addr - kernel_vm_end);
2700 while (kernel_vm_end < addr) {
2701 l0 = pmap_l0(kernel_pmap, kernel_vm_end);
2702 KASSERT(pmap_load(l0) != 0,
2703 ("pmap_growkernel: No level 0 kernel entry"));
2705 l1 = pmap_l0_to_l1(l0, kernel_vm_end);
2706 if (pmap_load(l1) == 0) {
2707 /* We need a new PDP entry */
2708 nkpg = vm_page_alloc_noobj(VM_ALLOC_INTERRUPT |
2709 VM_ALLOC_WIRED | VM_ALLOC_ZERO);
2711 panic("pmap_growkernel: no memory to grow kernel");
2712 nkpg->pindex = kernel_vm_end >> L1_SHIFT;
2713 /* See the dmb() in _pmap_alloc_l3(). */
2715 paddr = VM_PAGE_TO_PHYS(nkpg);
2716 pmap_store(l1, paddr | L1_TABLE);
2717 continue; /* try again */
2719 l2 = pmap_l1_to_l2(l1, kernel_vm_end);
2720 if (pmap_load(l2) != 0) {
2721 kernel_vm_end = (kernel_vm_end + L2_SIZE) & ~L2_OFFSET;
2722 if (kernel_vm_end - 1 >= vm_map_max(kernel_map)) {
2723 kernel_vm_end = vm_map_max(kernel_map);
2729 nkpg = vm_page_alloc_noobj(VM_ALLOC_INTERRUPT | VM_ALLOC_WIRED |
2732 panic("pmap_growkernel: no memory to grow kernel");
2733 nkpg->pindex = kernel_vm_end >> L2_SHIFT;
2734 /* See the dmb() in _pmap_alloc_l3(). */
2736 paddr = VM_PAGE_TO_PHYS(nkpg);
2737 pmap_store(l2, paddr | L2_TABLE);
2739 kernel_vm_end = (kernel_vm_end + L2_SIZE) & ~L2_OFFSET;
2740 if (kernel_vm_end - 1 >= vm_map_max(kernel_map)) {
2741 kernel_vm_end = vm_map_max(kernel_map);
2747 /***************************************************
2748 * page management routines.
2749 ***************************************************/
2751 static const uint64_t pc_freemask[_NPCM] = {
2752 [0 ... _NPCM - 2] = PC_FREEN,
2753 [_NPCM - 1] = PC_FREEL
2757 static int pc_chunk_count, pc_chunk_allocs, pc_chunk_frees, pc_chunk_tryfail;
2759 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_count, CTLFLAG_RD, &pc_chunk_count, 0,
2760 "Current number of pv entry chunks");
2761 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_allocs, CTLFLAG_RD, &pc_chunk_allocs, 0,
2762 "Current number of pv entry chunks allocated");
2763 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_frees, CTLFLAG_RD, &pc_chunk_frees, 0,
2764 "Current number of pv entry chunks frees");
2765 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_tryfail, CTLFLAG_RD, &pc_chunk_tryfail, 0,
2766 "Number of times tried to get a chunk page but failed.");
2768 static long pv_entry_frees, pv_entry_allocs, pv_entry_count;
2769 static int pv_entry_spare;
2771 SYSCTL_LONG(_vm_pmap, OID_AUTO, pv_entry_frees, CTLFLAG_RD, &pv_entry_frees, 0,
2772 "Current number of pv entry frees");
2773 SYSCTL_LONG(_vm_pmap, OID_AUTO, pv_entry_allocs, CTLFLAG_RD, &pv_entry_allocs, 0,
2774 "Current number of pv entry allocs");
2775 SYSCTL_LONG(_vm_pmap, OID_AUTO, pv_entry_count, CTLFLAG_RD, &pv_entry_count, 0,
2776 "Current number of pv entries");
2777 SYSCTL_INT(_vm_pmap, OID_AUTO, pv_entry_spare, CTLFLAG_RD, &pv_entry_spare, 0,
2778 "Current number of spare pv entries");
2782 * We are in a serious low memory condition. Resort to
2783 * drastic measures to free some pages so we can allocate
2784 * another pv entry chunk.
2786 * Returns NULL if PV entries were reclaimed from the specified pmap.
2788 * We do not, however, unmap 2mpages because subsequent accesses will
2789 * allocate per-page pv entries until repromotion occurs, thereby
2790 * exacerbating the shortage of free pv entries.
2793 reclaim_pv_chunk_domain(pmap_t locked_pmap, struct rwlock **lockp, int domain)
2795 struct pv_chunks_list *pvc;
2796 struct pv_chunk *pc, *pc_marker, *pc_marker_end;
2797 struct pv_chunk_header pc_marker_b, pc_marker_end_b;
2798 struct md_page *pvh;
2800 pmap_t next_pmap, pmap;
2801 pt_entry_t *pte, tpte;
2805 struct spglist free;
2807 int bit, field, freed, lvl;
2809 PMAP_LOCK_ASSERT(locked_pmap, MA_OWNED);
2810 KASSERT(lockp != NULL, ("reclaim_pv_chunk: lockp is NULL"));
2815 bzero(&pc_marker_b, sizeof(pc_marker_b));
2816 bzero(&pc_marker_end_b, sizeof(pc_marker_end_b));
2817 pc_marker = (struct pv_chunk *)&pc_marker_b;
2818 pc_marker_end = (struct pv_chunk *)&pc_marker_end_b;
2820 pvc = &pv_chunks[domain];
2821 mtx_lock(&pvc->pvc_lock);
2822 pvc->active_reclaims++;
2823 TAILQ_INSERT_HEAD(&pvc->pvc_list, pc_marker, pc_lru);
2824 TAILQ_INSERT_TAIL(&pvc->pvc_list, pc_marker_end, pc_lru);
2825 while ((pc = TAILQ_NEXT(pc_marker, pc_lru)) != pc_marker_end &&
2826 SLIST_EMPTY(&free)) {
2827 next_pmap = pc->pc_pmap;
2828 if (next_pmap == NULL) {
2830 * The next chunk is a marker. However, it is
2831 * not our marker, so active_reclaims must be
2832 * > 1. Consequently, the next_chunk code
2833 * will not rotate the pv_chunks list.
2837 mtx_unlock(&pvc->pvc_lock);
2840 * A pv_chunk can only be removed from the pc_lru list
2841 * when both pvc->pvc_lock is owned and the
2842 * corresponding pmap is locked.
2844 if (pmap != next_pmap) {
2845 if (pmap != NULL && pmap != locked_pmap)
2848 /* Avoid deadlock and lock recursion. */
2849 if (pmap > locked_pmap) {
2850 RELEASE_PV_LIST_LOCK(lockp);
2852 mtx_lock(&pvc->pvc_lock);
2854 } else if (pmap != locked_pmap) {
2855 if (PMAP_TRYLOCK(pmap)) {
2856 mtx_lock(&pvc->pvc_lock);
2859 pmap = NULL; /* pmap is not locked */
2860 mtx_lock(&pvc->pvc_lock);
2861 pc = TAILQ_NEXT(pc_marker, pc_lru);
2863 pc->pc_pmap != next_pmap)
2871 * Destroy every non-wired, 4 KB page mapping in the chunk.
2874 for (field = 0; field < _NPCM; field++) {
2875 for (inuse = ~pc->pc_map[field] & pc_freemask[field];
2876 inuse != 0; inuse &= ~(1UL << bit)) {
2877 bit = ffsl(inuse) - 1;
2878 pv = &pc->pc_pventry[field * 64 + bit];
2880 pde = pmap_pde(pmap, va, &lvl);
2883 pte = pmap_l2_to_l3(pde, va);
2884 tpte = pmap_load(pte);
2885 if ((tpte & ATTR_SW_WIRED) != 0)
2887 tpte = pmap_load_clear(pte);
2888 m = PHYS_TO_VM_PAGE(tpte & ~ATTR_MASK);
2889 if (pmap_pte_dirty(pmap, tpte))
2891 if ((tpte & ATTR_AF) != 0) {
2892 pmap_s1_invalidate_page(pmap, va, true);
2893 vm_page_aflag_set(m, PGA_REFERENCED);
2895 CHANGE_PV_LIST_LOCK_TO_VM_PAGE(lockp, m);
2896 TAILQ_REMOVE(&m->md.pv_list, pv, pv_next);
2898 if (TAILQ_EMPTY(&m->md.pv_list) &&
2899 (m->flags & PG_FICTITIOUS) == 0) {
2900 pvh = page_to_pvh(m);
2901 if (TAILQ_EMPTY(&pvh->pv_list)) {
2902 vm_page_aflag_clear(m,
2906 pc->pc_map[field] |= 1UL << bit;
2907 pmap_unuse_pt(pmap, va, pmap_load(pde), &free);
2912 mtx_lock(&pvc->pvc_lock);
2915 /* Every freed mapping is for a 4 KB page. */
2916 pmap_resident_count_dec(pmap, freed);
2917 PV_STAT(atomic_add_long(&pv_entry_frees, freed));
2918 PV_STAT(atomic_add_int(&pv_entry_spare, freed));
2919 PV_STAT(atomic_subtract_long(&pv_entry_count, freed));
2920 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
2921 if (pc_is_free(pc)) {
2922 PV_STAT(atomic_subtract_int(&pv_entry_spare, _NPCPV));
2923 PV_STAT(atomic_subtract_int(&pc_chunk_count, 1));
2924 PV_STAT(atomic_add_int(&pc_chunk_frees, 1));
2925 /* Entire chunk is free; return it. */
2926 m_pc = PHYS_TO_VM_PAGE(DMAP_TO_PHYS((vm_offset_t)pc));
2927 dump_drop_page(m_pc->phys_addr);
2928 mtx_lock(&pvc->pvc_lock);
2929 TAILQ_REMOVE(&pvc->pvc_list, pc, pc_lru);
2932 TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc, pc_list);
2933 mtx_lock(&pvc->pvc_lock);
2934 /* One freed pv entry in locked_pmap is sufficient. */
2935 if (pmap == locked_pmap)
2939 TAILQ_REMOVE(&pvc->pvc_list, pc_marker, pc_lru);
2940 TAILQ_INSERT_AFTER(&pvc->pvc_list, pc, pc_marker, pc_lru);
2941 if (pvc->active_reclaims == 1 && pmap != NULL) {
2943 * Rotate the pv chunks list so that we do not
2944 * scan the same pv chunks that could not be
2945 * freed (because they contained a wired
2946 * and/or superpage mapping) on every
2947 * invocation of reclaim_pv_chunk().
2949 while ((pc = TAILQ_FIRST(&pvc->pvc_list)) != pc_marker){
2950 MPASS(pc->pc_pmap != NULL);
2951 TAILQ_REMOVE(&pvc->pvc_list, pc, pc_lru);
2952 TAILQ_INSERT_TAIL(&pvc->pvc_list, pc, pc_lru);
2956 TAILQ_REMOVE(&pvc->pvc_list, pc_marker, pc_lru);
2957 TAILQ_REMOVE(&pvc->pvc_list, pc_marker_end, pc_lru);
2958 pvc->active_reclaims--;
2959 mtx_unlock(&pvc->pvc_lock);
2960 if (pmap != NULL && pmap != locked_pmap)
2962 if (m_pc == NULL && !SLIST_EMPTY(&free)) {
2963 m_pc = SLIST_FIRST(&free);
2964 SLIST_REMOVE_HEAD(&free, plinks.s.ss);
2965 /* Recycle a freed page table page. */
2966 m_pc->ref_count = 1;
2968 vm_page_free_pages_toq(&free, true);
2973 reclaim_pv_chunk(pmap_t locked_pmap, struct rwlock **lockp)
2978 domain = PCPU_GET(domain);
2979 for (i = 0; i < vm_ndomains; i++) {
2980 m = reclaim_pv_chunk_domain(locked_pmap, lockp, domain);
2983 domain = (domain + 1) % vm_ndomains;
2990 * free the pv_entry back to the free list
2993 free_pv_entry(pmap_t pmap, pv_entry_t pv)
2995 struct pv_chunk *pc;
2996 int idx, field, bit;
2998 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2999 PV_STAT(atomic_add_long(&pv_entry_frees, 1));
3000 PV_STAT(atomic_add_int(&pv_entry_spare, 1));
3001 PV_STAT(atomic_subtract_long(&pv_entry_count, 1));
3002 pc = pv_to_chunk(pv);
3003 idx = pv - &pc->pc_pventry[0];
3006 pc->pc_map[field] |= 1ul << bit;
3007 if (!pc_is_free(pc)) {
3008 /* 98% of the time, pc is already at the head of the list. */
3009 if (__predict_false(pc != TAILQ_FIRST(&pmap->pm_pvchunk))) {
3010 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
3011 TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc, pc_list);
3015 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
3020 free_pv_chunk_dequeued(struct pv_chunk *pc)
3024 PV_STAT(atomic_subtract_int(&pv_entry_spare, _NPCPV));
3025 PV_STAT(atomic_subtract_int(&pc_chunk_count, 1));
3026 PV_STAT(atomic_add_int(&pc_chunk_frees, 1));
3027 /* entire chunk is free, return it */
3028 m = PHYS_TO_VM_PAGE(DMAP_TO_PHYS((vm_offset_t)pc));
3029 dump_drop_page(m->phys_addr);
3030 vm_page_unwire_noq(m);
3035 free_pv_chunk(struct pv_chunk *pc)
3037 struct pv_chunks_list *pvc;
3039 pvc = &pv_chunks[pc_to_domain(pc)];
3040 mtx_lock(&pvc->pvc_lock);
3041 TAILQ_REMOVE(&pvc->pvc_list, pc, pc_lru);
3042 mtx_unlock(&pvc->pvc_lock);
3043 free_pv_chunk_dequeued(pc);
3047 free_pv_chunk_batch(struct pv_chunklist *batch)
3049 struct pv_chunks_list *pvc;
3050 struct pv_chunk *pc, *npc;
3053 for (i = 0; i < vm_ndomains; i++) {
3054 if (TAILQ_EMPTY(&batch[i]))
3056 pvc = &pv_chunks[i];
3057 mtx_lock(&pvc->pvc_lock);
3058 TAILQ_FOREACH(pc, &batch[i], pc_list) {
3059 TAILQ_REMOVE(&pvc->pvc_list, pc, pc_lru);
3061 mtx_unlock(&pvc->pvc_lock);
3064 for (i = 0; i < vm_ndomains; i++) {
3065 TAILQ_FOREACH_SAFE(pc, &batch[i], pc_list, npc) {
3066 free_pv_chunk_dequeued(pc);
3072 * Returns a new PV entry, allocating a new PV chunk from the system when
3073 * needed. If this PV chunk allocation fails and a PV list lock pointer was
3074 * given, a PV chunk is reclaimed from an arbitrary pmap. Otherwise, NULL is
3077 * The given PV list lock may be released.
3080 get_pv_entry(pmap_t pmap, struct rwlock **lockp)
3082 struct pv_chunks_list *pvc;
3085 struct pv_chunk *pc;
3088 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3089 PV_STAT(atomic_add_long(&pv_entry_allocs, 1));
3091 pc = TAILQ_FIRST(&pmap->pm_pvchunk);
3093 for (field = 0; field < _NPCM; field++) {
3094 if (pc->pc_map[field]) {
3095 bit = ffsl(pc->pc_map[field]) - 1;
3099 if (field < _NPCM) {
3100 pv = &pc->pc_pventry[field * 64 + bit];
3101 pc->pc_map[field] &= ~(1ul << bit);
3102 /* If this was the last item, move it to tail */
3103 if (pc_is_full(pc)) {
3104 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
3105 TAILQ_INSERT_TAIL(&pmap->pm_pvchunk, pc,
3108 PV_STAT(atomic_add_long(&pv_entry_count, 1));
3109 PV_STAT(atomic_subtract_int(&pv_entry_spare, 1));
3113 /* No free items, allocate another chunk */
3114 m = vm_page_alloc_noobj(VM_ALLOC_WIRED);
3116 if (lockp == NULL) {
3117 PV_STAT(pc_chunk_tryfail++);
3120 m = reclaim_pv_chunk(pmap, lockp);
3124 PV_STAT(atomic_add_int(&pc_chunk_count, 1));
3125 PV_STAT(atomic_add_int(&pc_chunk_allocs, 1));
3126 dump_add_page(m->phys_addr);
3127 pc = (void *)PHYS_TO_DMAP(m->phys_addr);
3129 memcpy(pc->pc_map, pc_freemask, sizeof(pc_freemask));
3130 pc->pc_map[0] &= ~1ul; /* preallocated bit 0 */
3131 pvc = &pv_chunks[vm_page_domain(m)];
3132 mtx_lock(&pvc->pvc_lock);
3133 TAILQ_INSERT_TAIL(&pvc->pvc_list, pc, pc_lru);
3134 mtx_unlock(&pvc->pvc_lock);
3135 pv = &pc->pc_pventry[0];
3136 TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc, pc_list);
3137 PV_STAT(atomic_add_long(&pv_entry_count, 1));
3138 PV_STAT(atomic_add_int(&pv_entry_spare, _NPCPV - 1));
3143 * Ensure that the number of spare PV entries in the specified pmap meets or
3144 * exceeds the given count, "needed".
3146 * The given PV list lock may be released.
3149 reserve_pv_entries(pmap_t pmap, int needed, struct rwlock **lockp)
3151 struct pv_chunks_list *pvc;
3152 struct pch new_tail[PMAP_MEMDOM];
3153 struct pv_chunk *pc;
3158 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3159 KASSERT(lockp != NULL, ("reserve_pv_entries: lockp is NULL"));
3162 * Newly allocated PV chunks must be stored in a private list until
3163 * the required number of PV chunks have been allocated. Otherwise,
3164 * reclaim_pv_chunk() could recycle one of these chunks. In
3165 * contrast, these chunks must be added to the pmap upon allocation.
3167 for (i = 0; i < PMAP_MEMDOM; i++)
3168 TAILQ_INIT(&new_tail[i]);
3171 TAILQ_FOREACH(pc, &pmap->pm_pvchunk, pc_list) {
3172 bit_count((bitstr_t *)pc->pc_map, 0,
3173 sizeof(pc->pc_map) * NBBY, &free);
3177 if (avail >= needed)
3180 for (reclaimed = false; avail < needed; avail += _NPCPV) {
3181 m = vm_page_alloc_noobj(VM_ALLOC_WIRED);
3183 m = reclaim_pv_chunk(pmap, lockp);
3188 PV_STAT(atomic_add_int(&pc_chunk_count, 1));
3189 PV_STAT(atomic_add_int(&pc_chunk_allocs, 1));
3190 dump_add_page(m->phys_addr);
3191 pc = (void *)PHYS_TO_DMAP(m->phys_addr);
3193 memcpy(pc->pc_map, pc_freemask, sizeof(pc_freemask));
3194 TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc, pc_list);
3195 TAILQ_INSERT_TAIL(&new_tail[vm_page_domain(m)], pc, pc_lru);
3196 PV_STAT(atomic_add_int(&pv_entry_spare, _NPCPV));
3199 * The reclaim might have freed a chunk from the current pmap.
3200 * If that chunk contained available entries, we need to
3201 * re-count the number of available entries.
3206 for (i = 0; i < vm_ndomains; i++) {
3207 if (TAILQ_EMPTY(&new_tail[i]))
3209 pvc = &pv_chunks[i];
3210 mtx_lock(&pvc->pvc_lock);
3211 TAILQ_CONCAT(&pvc->pvc_list, &new_tail[i], pc_lru);
3212 mtx_unlock(&pvc->pvc_lock);
3217 * First find and then remove the pv entry for the specified pmap and virtual
3218 * address from the specified pv list. Returns the pv entry if found and NULL
3219 * otherwise. This operation can be performed on pv lists for either 4KB or
3220 * 2MB page mappings.
3222 static __inline pv_entry_t
3223 pmap_pvh_remove(struct md_page *pvh, pmap_t pmap, vm_offset_t va)
3227 TAILQ_FOREACH(pv, &pvh->pv_list, pv_next) {
3228 if (pmap == PV_PMAP(pv) && va == pv->pv_va) {
3229 TAILQ_REMOVE(&pvh->pv_list, pv, pv_next);
3238 * After demotion from a 2MB page mapping to 512 4KB page mappings,
3239 * destroy the pv entry for the 2MB page mapping and reinstantiate the pv
3240 * entries for each of the 4KB page mappings.
3243 pmap_pv_demote_l2(pmap_t pmap, vm_offset_t va, vm_paddr_t pa,
3244 struct rwlock **lockp)
3246 struct md_page *pvh;
3247 struct pv_chunk *pc;
3249 vm_offset_t va_last;
3253 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3254 KASSERT((va & L2_OFFSET) == 0,
3255 ("pmap_pv_demote_l2: va is not 2mpage aligned"));
3256 KASSERT((pa & L2_OFFSET) == 0,
3257 ("pmap_pv_demote_l2: pa is not 2mpage aligned"));
3258 CHANGE_PV_LIST_LOCK_TO_PHYS(lockp, pa);
3261 * Transfer the 2mpage's pv entry for this mapping to the first
3262 * page's pv list. Once this transfer begins, the pv list lock
3263 * must not be released until the last pv entry is reinstantiated.
3265 pvh = pa_to_pvh(pa);
3266 pv = pmap_pvh_remove(pvh, pmap, va);
3267 KASSERT(pv != NULL, ("pmap_pv_demote_l2: pv not found"));
3268 m = PHYS_TO_VM_PAGE(pa);
3269 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_next);
3271 /* Instantiate the remaining Ln_ENTRIES - 1 pv entries. */
3272 PV_STAT(atomic_add_long(&pv_entry_allocs, Ln_ENTRIES - 1));
3273 va_last = va + L2_SIZE - PAGE_SIZE;
3275 pc = TAILQ_FIRST(&pmap->pm_pvchunk);
3276 KASSERT(!pc_is_full(pc), ("pmap_pv_demote_l2: missing spare"));
3277 for (field = 0; field < _NPCM; field++) {
3278 while (pc->pc_map[field]) {
3279 bit = ffsl(pc->pc_map[field]) - 1;
3280 pc->pc_map[field] &= ~(1ul << bit);
3281 pv = &pc->pc_pventry[field * 64 + bit];
3285 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
3286 ("pmap_pv_demote_l2: page %p is not managed", m));
3287 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_next);
3293 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
3294 TAILQ_INSERT_TAIL(&pmap->pm_pvchunk, pc, pc_list);
3297 if (pc_is_full(pc)) {
3298 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
3299 TAILQ_INSERT_TAIL(&pmap->pm_pvchunk, pc, pc_list);
3301 PV_STAT(atomic_add_long(&pv_entry_count, Ln_ENTRIES - 1));
3302 PV_STAT(atomic_subtract_int(&pv_entry_spare, Ln_ENTRIES - 1));
3306 * First find and then destroy the pv entry for the specified pmap and virtual
3307 * address. This operation can be performed on pv lists for either 4KB or 2MB
3311 pmap_pvh_free(struct md_page *pvh, pmap_t pmap, vm_offset_t va)
3315 pv = pmap_pvh_remove(pvh, pmap, va);
3316 KASSERT(pv != NULL, ("pmap_pvh_free: pv not found"));
3317 free_pv_entry(pmap, pv);
3321 * Conditionally create the PV entry for a 4KB page mapping if the required
3322 * memory can be allocated without resorting to reclamation.
3325 pmap_try_insert_pv_entry(pmap_t pmap, vm_offset_t va, vm_page_t m,
3326 struct rwlock **lockp)
3330 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3331 /* Pass NULL instead of the lock pointer to disable reclamation. */
3332 if ((pv = get_pv_entry(pmap, NULL)) != NULL) {
3334 CHANGE_PV_LIST_LOCK_TO_VM_PAGE(lockp, m);
3335 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_next);
3343 * Create the PV entry for a 2MB page mapping. Always returns true unless the
3344 * flag PMAP_ENTER_NORECLAIM is specified. If that flag is specified, returns
3345 * false if the PV entry cannot be allocated without resorting to reclamation.
3348 pmap_pv_insert_l2(pmap_t pmap, vm_offset_t va, pd_entry_t l2e, u_int flags,
3349 struct rwlock **lockp)
3351 struct md_page *pvh;
3355 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3356 /* Pass NULL instead of the lock pointer to disable reclamation. */
3357 if ((pv = get_pv_entry(pmap, (flags & PMAP_ENTER_NORECLAIM) != 0 ?
3358 NULL : lockp)) == NULL)
3361 pa = l2e & ~ATTR_MASK;
3362 CHANGE_PV_LIST_LOCK_TO_PHYS(lockp, pa);
3363 pvh = pa_to_pvh(pa);
3364 TAILQ_INSERT_TAIL(&pvh->pv_list, pv, pv_next);
3370 pmap_remove_kernel_l2(pmap_t pmap, pt_entry_t *l2, vm_offset_t va)
3372 pt_entry_t newl2, oldl2 __diagused;
3376 KASSERT(!VIRT_IN_DMAP(va), ("removing direct mapping of %#lx", va));
3377 KASSERT(pmap == kernel_pmap, ("pmap %p is not kernel_pmap", pmap));
3378 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3380 ml3 = pmap_remove_pt_page(pmap, va);
3382 panic("pmap_remove_kernel_l2: Missing pt page");
3384 ml3pa = VM_PAGE_TO_PHYS(ml3);
3385 newl2 = ml3pa | L2_TABLE;
3388 * If this page table page was unmapped by a promotion, then it
3389 * contains valid mappings. Zero it to invalidate those mappings.
3391 if (ml3->valid != 0)
3392 pagezero((void *)PHYS_TO_DMAP(ml3pa));
3395 * Demote the mapping. The caller must have already invalidated the
3396 * mapping (i.e., the "break" in break-before-make).
3398 oldl2 = pmap_load_store(l2, newl2);
3399 KASSERT(oldl2 == 0, ("%s: found existing mapping at %p: %#lx",
3400 __func__, l2, oldl2));
3404 * pmap_remove_l2: Do the things to unmap a level 2 superpage.
3407 pmap_remove_l2(pmap_t pmap, pt_entry_t *l2, vm_offset_t sva,
3408 pd_entry_t l1e, struct spglist *free, struct rwlock **lockp)
3410 struct md_page *pvh;
3412 vm_page_t m, ml3, mt;
3414 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3415 KASSERT((sva & L2_OFFSET) == 0, ("pmap_remove_l2: sva is not aligned"));
3416 old_l2 = pmap_load_clear(l2);
3417 KASSERT((old_l2 & ATTR_DESCR_MASK) == L2_BLOCK,
3418 ("pmap_remove_l2: L2e %lx is not a block mapping", old_l2));
3421 * Since a promotion must break the 4KB page mappings before making
3422 * the 2MB page mapping, a pmap_s1_invalidate_page() suffices.
3424 pmap_s1_invalidate_page(pmap, sva, true);
3426 if (old_l2 & ATTR_SW_WIRED)
3427 pmap->pm_stats.wired_count -= L2_SIZE / PAGE_SIZE;
3428 pmap_resident_count_dec(pmap, L2_SIZE / PAGE_SIZE);
3429 if (old_l2 & ATTR_SW_MANAGED) {
3430 m = PHYS_TO_VM_PAGE(old_l2 & ~ATTR_MASK);
3431 pvh = page_to_pvh(m);
3432 CHANGE_PV_LIST_LOCK_TO_PHYS(lockp, old_l2 & ~ATTR_MASK);
3433 pmap_pvh_free(pvh, pmap, sva);
3434 for (mt = m; mt < &m[L2_SIZE / PAGE_SIZE]; mt++) {
3435 if (pmap_pte_dirty(pmap, old_l2))
3437 if (old_l2 & ATTR_AF)
3438 vm_page_aflag_set(mt, PGA_REFERENCED);
3439 if (TAILQ_EMPTY(&mt->md.pv_list) &&
3440 TAILQ_EMPTY(&pvh->pv_list))
3441 vm_page_aflag_clear(mt, PGA_WRITEABLE);
3444 if (pmap == kernel_pmap) {
3445 pmap_remove_kernel_l2(pmap, l2, sva);
3447 ml3 = pmap_remove_pt_page(pmap, sva);
3449 KASSERT(ml3->valid == VM_PAGE_BITS_ALL,
3450 ("pmap_remove_l2: l3 page not promoted"));
3451 pmap_resident_count_dec(pmap, 1);
3452 KASSERT(ml3->ref_count == NL3PG,
3453 ("pmap_remove_l2: l3 page ref count error"));
3455 pmap_add_delayed_free_list(ml3, free, FALSE);
3458 return (pmap_unuse_pt(pmap, sva, l1e, free));
3462 * pmap_remove_l3: do the things to unmap a page in a process
3465 pmap_remove_l3(pmap_t pmap, pt_entry_t *l3, vm_offset_t va,
3466 pd_entry_t l2e, struct spglist *free, struct rwlock **lockp)
3468 struct md_page *pvh;
3472 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3473 old_l3 = pmap_load_clear(l3);
3474 pmap_s1_invalidate_page(pmap, va, true);
3475 if (old_l3 & ATTR_SW_WIRED)
3476 pmap->pm_stats.wired_count -= 1;
3477 pmap_resident_count_dec(pmap, 1);
3478 if (old_l3 & ATTR_SW_MANAGED) {
3479 m = PHYS_TO_VM_PAGE(old_l3 & ~ATTR_MASK);
3480 if (pmap_pte_dirty(pmap, old_l3))
3482 if (old_l3 & ATTR_AF)
3483 vm_page_aflag_set(m, PGA_REFERENCED);
3484 CHANGE_PV_LIST_LOCK_TO_VM_PAGE(lockp, m);
3485 pmap_pvh_free(&m->md, pmap, va);
3486 if (TAILQ_EMPTY(&m->md.pv_list) &&
3487 (m->flags & PG_FICTITIOUS) == 0) {
3488 pvh = page_to_pvh(m);
3489 if (TAILQ_EMPTY(&pvh->pv_list))
3490 vm_page_aflag_clear(m, PGA_WRITEABLE);
3493 return (pmap_unuse_pt(pmap, va, l2e, free));
3497 * Remove the specified range of addresses from the L3 page table that is
3498 * identified by the given L2 entry.
3501 pmap_remove_l3_range(pmap_t pmap, pd_entry_t l2e, vm_offset_t sva,
3502 vm_offset_t eva, struct spglist *free, struct rwlock **lockp)
3504 struct md_page *pvh;
3505 struct rwlock *new_lock;
3506 pt_entry_t *l3, old_l3;
3510 KASSERT(ADDR_IS_CANONICAL(sva),
3511 ("%s: Start address not in canonical form: %lx", __func__, sva));
3512 KASSERT(ADDR_IS_CANONICAL(eva) || eva == VM_MAX_USER_ADDRESS,
3513 ("%s: End address not in canonical form: %lx", __func__, eva));
3515 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3516 KASSERT(rounddown2(sva, L2_SIZE) + L2_SIZE == roundup2(eva, L2_SIZE),
3517 ("pmap_remove_l3_range: range crosses an L3 page table boundary"));
3518 l3pg = !ADDR_IS_KERNEL(sva) ? PHYS_TO_VM_PAGE(l2e & ~ATTR_MASK) : NULL;
3520 for (l3 = pmap_l2_to_l3(&l2e, sva); sva != eva; l3++, sva += L3_SIZE) {
3521 if (!pmap_l3_valid(pmap_load(l3))) {
3523 pmap_invalidate_range(pmap, va, sva, true);
3528 old_l3 = pmap_load_clear(l3);
3529 if ((old_l3 & ATTR_SW_WIRED) != 0)
3530 pmap->pm_stats.wired_count--;
3531 pmap_resident_count_dec(pmap, 1);
3532 if ((old_l3 & ATTR_SW_MANAGED) != 0) {
3533 m = PHYS_TO_VM_PAGE(old_l3 & ~ATTR_MASK);
3534 if (pmap_pte_dirty(pmap, old_l3))
3536 if ((old_l3 & ATTR_AF) != 0)
3537 vm_page_aflag_set(m, PGA_REFERENCED);
3538 new_lock = PHYS_TO_PV_LIST_LOCK(VM_PAGE_TO_PHYS(m));
3539 if (new_lock != *lockp) {
3540 if (*lockp != NULL) {
3542 * Pending TLB invalidations must be
3543 * performed before the PV list lock is
3544 * released. Otherwise, a concurrent
3545 * pmap_remove_all() on a physical page
3546 * could return while a stale TLB entry
3547 * still provides access to that page.
3550 pmap_invalidate_range(pmap, va,
3559 pmap_pvh_free(&m->md, pmap, sva);
3560 if (TAILQ_EMPTY(&m->md.pv_list) &&
3561 (m->flags & PG_FICTITIOUS) == 0) {
3562 pvh = page_to_pvh(m);
3563 if (TAILQ_EMPTY(&pvh->pv_list))
3564 vm_page_aflag_clear(m, PGA_WRITEABLE);
3567 if (l3pg != NULL && pmap_unwire_l3(pmap, sva, l3pg, free)) {
3569 * _pmap_unwire_l3() has already invalidated the TLB
3570 * entries at all levels for "sva". So, we need not
3571 * perform "sva += L3_SIZE;" here. Moreover, we need
3572 * not perform "va = sva;" if "sva" is at the start
3573 * of a new valid range consisting of a single page.
3581 pmap_invalidate_range(pmap, va, sva, true);
3585 * Remove the given range of addresses from the specified map.
3587 * It is assumed that the start and end are properly
3588 * rounded to the page size.
3591 pmap_remove(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
3593 struct rwlock *lock;
3594 vm_offset_t va_next;
3595 pd_entry_t *l0, *l1, *l2;
3596 pt_entry_t l3_paddr;
3597 struct spglist free;
3600 * Perform an unsynchronized read. This is, however, safe.
3602 if (pmap->pm_stats.resident_count == 0)
3610 for (; sva < eva; sva = va_next) {
3611 if (pmap->pm_stats.resident_count == 0)
3614 l0 = pmap_l0(pmap, sva);
3615 if (pmap_load(l0) == 0) {
3616 va_next = (sva + L0_SIZE) & ~L0_OFFSET;
3622 va_next = (sva + L1_SIZE) & ~L1_OFFSET;
3625 l1 = pmap_l0_to_l1(l0, sva);
3626 if (pmap_load(l1) == 0)
3628 if ((pmap_load(l1) & ATTR_DESCR_MASK) == L1_BLOCK) {
3629 PMAP_ASSERT_L1_BLOCKS_SUPPORTED;
3630 KASSERT(va_next <= eva,
3631 ("partial update of non-transparent 1G page "
3632 "l1 %#lx sva %#lx eva %#lx va_next %#lx",
3633 pmap_load(l1), sva, eva, va_next));
3634 MPASS(pmap != kernel_pmap);
3635 MPASS((pmap_load(l1) & ATTR_SW_MANAGED) == 0);
3637 pmap_s1_invalidate_page(pmap, sva, true);
3638 pmap_resident_count_dec(pmap, L1_SIZE / PAGE_SIZE);
3639 pmap_unuse_pt(pmap, sva, pmap_load(l0), &free);
3644 * Calculate index for next page table.
3646 va_next = (sva + L2_SIZE) & ~L2_OFFSET;
3650 l2 = pmap_l1_to_l2(l1, sva);
3654 l3_paddr = pmap_load(l2);
3656 if ((l3_paddr & ATTR_DESCR_MASK) == L2_BLOCK) {
3657 if (sva + L2_SIZE == va_next && eva >= va_next) {
3658 pmap_remove_l2(pmap, l2, sva, pmap_load(l1),
3661 } else if (pmap_demote_l2_locked(pmap, l2, sva,
3664 l3_paddr = pmap_load(l2);
3668 * Weed out invalid mappings.
3670 if ((l3_paddr & ATTR_DESCR_MASK) != L2_TABLE)
3674 * Limit our scan to either the end of the va represented
3675 * by the current page table page, or to the end of the
3676 * range being removed.
3681 pmap_remove_l3_range(pmap, l3_paddr, sva, va_next, &free,
3687 vm_page_free_pages_toq(&free, true);
3691 * Routine: pmap_remove_all
3693 * Removes this physical page from
3694 * all physical maps in which it resides.
3695 * Reflects back modify bits to the pager.
3698 * Original versions of this routine were very
3699 * inefficient because they iteratively called
3700 * pmap_remove (slow...)
3704 pmap_remove_all(vm_page_t m)
3706 struct md_page *pvh;
3709 struct rwlock *lock;
3710 pd_entry_t *pde, tpde;
3711 pt_entry_t *pte, tpte;
3713 struct spglist free;
3714 int lvl, pvh_gen, md_gen;
3716 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
3717 ("pmap_remove_all: page %p is not managed", m));
3719 lock = VM_PAGE_TO_PV_LIST_LOCK(m);
3720 pvh = (m->flags & PG_FICTITIOUS) != 0 ? &pv_dummy : page_to_pvh(m);
3723 while ((pv = TAILQ_FIRST(&pvh->pv_list)) != NULL) {
3725 if (!PMAP_TRYLOCK(pmap)) {
3726 pvh_gen = pvh->pv_gen;
3730 if (pvh_gen != pvh->pv_gen) {
3736 pte = pmap_pte_exists(pmap, va, 2, __func__);
3737 pmap_demote_l2_locked(pmap, pte, va, &lock);
3740 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
3742 if (!PMAP_TRYLOCK(pmap)) {
3743 pvh_gen = pvh->pv_gen;
3744 md_gen = m->md.pv_gen;
3748 if (pvh_gen != pvh->pv_gen || md_gen != m->md.pv_gen) {
3753 pmap_resident_count_dec(pmap, 1);
3755 pde = pmap_pde(pmap, pv->pv_va, &lvl);
3756 KASSERT(pde != NULL,
3757 ("pmap_remove_all: no page directory entry found"));
3759 ("pmap_remove_all: invalid pde level %d", lvl));
3760 tpde = pmap_load(pde);
3762 pte = pmap_l2_to_l3(pde, pv->pv_va);
3763 tpte = pmap_load_clear(pte);
3764 if (tpte & ATTR_SW_WIRED)
3765 pmap->pm_stats.wired_count--;
3766 if ((tpte & ATTR_AF) != 0) {
3767 pmap_invalidate_page(pmap, pv->pv_va, true);
3768 vm_page_aflag_set(m, PGA_REFERENCED);
3772 * Update the vm_page_t clean and reference bits.
3774 if (pmap_pte_dirty(pmap, tpte))
3776 pmap_unuse_pt(pmap, pv->pv_va, tpde, &free);
3777 TAILQ_REMOVE(&m->md.pv_list, pv, pv_next);
3779 free_pv_entry(pmap, pv);
3782 vm_page_aflag_clear(m, PGA_WRITEABLE);
3784 vm_page_free_pages_toq(&free, true);
3788 * Masks and sets bits in a level 2 page table entries in the specified pmap
3791 pmap_protect_l2(pmap_t pmap, pt_entry_t *l2, vm_offset_t sva, pt_entry_t mask,
3797 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3798 PMAP_ASSERT_STAGE1(pmap);
3799 KASSERT((sva & L2_OFFSET) == 0,
3800 ("pmap_protect_l2: sva is not 2mpage aligned"));
3801 old_l2 = pmap_load(l2);
3802 KASSERT((old_l2 & ATTR_DESCR_MASK) == L2_BLOCK,
3803 ("pmap_protect_l2: L2e %lx is not a block mapping", old_l2));
3806 * Return if the L2 entry already has the desired access restrictions
3809 if ((old_l2 & mask) == nbits)
3812 while (!atomic_fcmpset_64(l2, &old_l2, (old_l2 & ~mask) | nbits))
3816 * When a dirty read/write superpage mapping is write protected,
3817 * update the dirty field of each of the superpage's constituent 4KB
3820 if ((old_l2 & ATTR_SW_MANAGED) != 0 &&
3821 (nbits & ATTR_S1_AP(ATTR_S1_AP_RO)) != 0 &&
3822 pmap_pte_dirty(pmap, old_l2)) {
3823 m = PHYS_TO_VM_PAGE(old_l2 & ~ATTR_MASK);
3824 for (mt = m; mt < &m[L2_SIZE / PAGE_SIZE]; mt++)
3829 * Since a promotion must break the 4KB page mappings before making
3830 * the 2MB page mapping, a pmap_s1_invalidate_page() suffices.
3832 pmap_s1_invalidate_page(pmap, sva, true);
3836 * Masks and sets bits in last level page table entries in the specified
3840 pmap_mask_set_locked(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, pt_entry_t mask,
3841 pt_entry_t nbits, bool invalidate)
3843 vm_offset_t va, va_next;
3844 pd_entry_t *l0, *l1, *l2;
3845 pt_entry_t *l3p, l3;
3847 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3848 for (; sva < eva; sva = va_next) {
3849 l0 = pmap_l0(pmap, sva);
3850 if (pmap_load(l0) == 0) {
3851 va_next = (sva + L0_SIZE) & ~L0_OFFSET;
3857 va_next = (sva + L1_SIZE) & ~L1_OFFSET;
3860 l1 = pmap_l0_to_l1(l0, sva);
3861 if (pmap_load(l1) == 0)
3863 if ((pmap_load(l1) & ATTR_DESCR_MASK) == L1_BLOCK) {
3864 PMAP_ASSERT_L1_BLOCKS_SUPPORTED;
3865 KASSERT(va_next <= eva,
3866 ("partial update of non-transparent 1G page "
3867 "l1 %#lx sva %#lx eva %#lx va_next %#lx",
3868 pmap_load(l1), sva, eva, va_next));
3869 MPASS((pmap_load(l1) & ATTR_SW_MANAGED) == 0);
3870 if ((pmap_load(l1) & mask) != nbits) {
3871 pmap_store(l1, (pmap_load(l1) & ~mask) | nbits);
3873 pmap_s1_invalidate_page(pmap, sva, true);
3878 va_next = (sva + L2_SIZE) & ~L2_OFFSET;
3882 l2 = pmap_l1_to_l2(l1, sva);
3883 if (pmap_load(l2) == 0)
3886 if ((pmap_load(l2) & ATTR_DESCR_MASK) == L2_BLOCK) {
3887 if (sva + L2_SIZE == va_next && eva >= va_next) {
3888 pmap_protect_l2(pmap, l2, sva, mask, nbits);
3890 } else if (pmap_demote_l2(pmap, l2, sva) == NULL)
3893 KASSERT((pmap_load(l2) & ATTR_DESCR_MASK) == L2_TABLE,
3894 ("pmap_protect: Invalid L2 entry after demotion"));
3900 for (l3p = pmap_l2_to_l3(l2, sva); sva != va_next; l3p++,
3902 l3 = pmap_load(l3p);
3905 * Go to the next L3 entry if the current one is
3906 * invalid or already has the desired access
3907 * restrictions in place. (The latter case occurs
3908 * frequently. For example, in a "buildworld"
3909 * workload, almost 1 out of 4 L3 entries already
3910 * have the desired restrictions.)
3912 if (!pmap_l3_valid(l3) || (l3 & mask) == nbits) {
3913 if (va != va_next) {
3915 pmap_s1_invalidate_range(pmap,
3922 while (!atomic_fcmpset_64(l3p, &l3, (l3 & ~mask) |
3927 * When a dirty read/write mapping is write protected,
3928 * update the page's dirty field.
3930 if ((l3 & ATTR_SW_MANAGED) != 0 &&
3931 (nbits & ATTR_S1_AP(ATTR_S1_AP_RO)) != 0 &&
3932 pmap_pte_dirty(pmap, l3))
3933 vm_page_dirty(PHYS_TO_VM_PAGE(l3 & ~ATTR_MASK));
3938 if (va != va_next && invalidate)
3939 pmap_s1_invalidate_range(pmap, va, sva, true);
3944 pmap_mask_set(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, pt_entry_t mask,
3945 pt_entry_t nbits, bool invalidate)
3948 pmap_mask_set_locked(pmap, sva, eva, mask, nbits, invalidate);
3953 * Set the physical protection on the
3954 * specified range of this map as requested.
3957 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
3959 pt_entry_t mask, nbits;
3961 PMAP_ASSERT_STAGE1(pmap);
3962 KASSERT((prot & ~VM_PROT_ALL) == 0, ("invalid prot %x", prot));
3963 if (prot == VM_PROT_NONE) {
3964 pmap_remove(pmap, sva, eva);
3969 if ((prot & VM_PROT_WRITE) == 0) {
3970 mask |= ATTR_S1_AP_RW_BIT | ATTR_SW_DBM;
3971 nbits |= ATTR_S1_AP(ATTR_S1_AP_RO);
3973 if ((prot & VM_PROT_EXECUTE) == 0) {
3975 nbits |= ATTR_S1_XN;
3980 pmap_mask_set(pmap, sva, eva, mask, nbits, true);
3984 pmap_disable_promotion(vm_offset_t sva, vm_size_t size)
3987 MPASS((sva & L3_OFFSET) == 0);
3988 MPASS(((sva + size) & L3_OFFSET) == 0);
3990 pmap_mask_set(kernel_pmap, sva, sva + size, ATTR_SW_NO_PROMOTE,
3991 ATTR_SW_NO_PROMOTE, false);
3995 * Inserts the specified page table page into the specified pmap's collection
3996 * of idle page table pages. Each of a pmap's page table pages is responsible
3997 * for mapping a distinct range of virtual addresses. The pmap's collection is
3998 * ordered by this virtual address range.
4000 * If "promoted" is false, then the page table page "mpte" must be zero filled.
4003 pmap_insert_pt_page(pmap_t pmap, vm_page_t mpte, bool promoted)
4006 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
4007 mpte->valid = promoted ? VM_PAGE_BITS_ALL : 0;
4008 return (vm_radix_insert(&pmap->pm_root, mpte));
4012 * Removes the page table page mapping the specified virtual address from the
4013 * specified pmap's collection of idle page table pages, and returns it.
4014 * Otherwise, returns NULL if there is no page table page corresponding to the
4015 * specified virtual address.
4017 static __inline vm_page_t
4018 pmap_remove_pt_page(pmap_t pmap, vm_offset_t va)
4021 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
4022 return (vm_radix_remove(&pmap->pm_root, pmap_l2_pindex(va)));
4026 * Performs a break-before-make update of a pmap entry. This is needed when
4027 * either promoting or demoting pages to ensure the TLB doesn't get into an
4028 * inconsistent state.
4031 pmap_update_entry(pmap_t pmap, pd_entry_t *pte, pd_entry_t newpte,
4032 vm_offset_t va, vm_size_t size)
4036 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
4038 if ((newpte & ATTR_SW_NO_PROMOTE) != 0)
4039 panic("%s: Updating non-promote pte", __func__);
4042 * Ensure we don't get switched out with the page table in an
4043 * inconsistent state. We also need to ensure no interrupts fire
4044 * as they may make use of an address we are about to invalidate.
4046 intr = intr_disable();
4049 * Clear the old mapping's valid bit, but leave the rest of the entry
4050 * unchanged, so that a lockless, concurrent pmap_kextract() can still
4051 * lookup the physical address.
4053 pmap_clear_bits(pte, ATTR_DESCR_VALID);
4056 * When promoting, the L{1,2}_TABLE entry that is being replaced might
4057 * be cached, so we invalidate intermediate entries as well as final
4060 pmap_s1_invalidate_range(pmap, va, va + size, false);
4062 /* Create the new mapping */
4063 pmap_store(pte, newpte);
4069 #if VM_NRESERVLEVEL > 0
4071 * After promotion from 512 4KB page mappings to a single 2MB page mapping,
4072 * replace the many pv entries for the 4KB page mappings by a single pv entry
4073 * for the 2MB page mapping.
4076 pmap_pv_promote_l2(pmap_t pmap, vm_offset_t va, vm_paddr_t pa,
4077 struct rwlock **lockp)
4079 struct md_page *pvh;
4081 vm_offset_t va_last;
4084 KASSERT((pa & L2_OFFSET) == 0,
4085 ("pmap_pv_promote_l2: pa is not 2mpage aligned"));
4086 CHANGE_PV_LIST_LOCK_TO_PHYS(lockp, pa);
4089 * Transfer the first page's pv entry for this mapping to the 2mpage's
4090 * pv list. Aside from avoiding the cost of a call to get_pv_entry(),
4091 * a transfer avoids the possibility that get_pv_entry() calls
4092 * reclaim_pv_chunk() and that reclaim_pv_chunk() removes one of the
4093 * mappings that is being promoted.
4095 m = PHYS_TO_VM_PAGE(pa);
4096 va = va & ~L2_OFFSET;
4097 pv = pmap_pvh_remove(&m->md, pmap, va);
4098 KASSERT(pv != NULL, ("pmap_pv_promote_l2: pv not found"));
4099 pvh = page_to_pvh(m);
4100 TAILQ_INSERT_TAIL(&pvh->pv_list, pv, pv_next);
4102 /* Free the remaining NPTEPG - 1 pv entries. */
4103 va_last = va + L2_SIZE - PAGE_SIZE;
4107 pmap_pvh_free(&m->md, pmap, va);
4108 } while (va < va_last);
4112 * Tries to promote the 512, contiguous 4KB page mappings that are within a
4113 * single level 2 table entry to a single 2MB page mapping. For promotion
4114 * to occur, two conditions must be met: (1) the 4KB page mappings must map
4115 * aligned, contiguous physical memory and (2) the 4KB page mappings must have
4116 * identical characteristics.
4119 pmap_promote_l2(pmap_t pmap, pd_entry_t *l2, vm_offset_t va, vm_page_t mpte,
4120 struct rwlock **lockp)
4122 pt_entry_t *firstl3, *l3, newl2, oldl3, pa;
4124 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
4125 PMAP_ASSERT_STAGE1(pmap);
4128 * Examine the first L3E in the specified PTP. Abort if this L3E is
4129 * ineligible for promotion, invalid, or does not map the first 4KB
4130 * physical page within a 2MB page.
4132 firstl3 = (pt_entry_t *)PHYS_TO_DMAP(pmap_load(l2) & ~ATTR_MASK);
4133 newl2 = pmap_load(firstl3);
4134 if ((newl2 & ATTR_SW_NO_PROMOTE) != 0)
4136 if ((newl2 & ((~ATTR_MASK & L2_OFFSET) | ATTR_DESCR_MASK)) != L3_PAGE) {
4137 atomic_add_long(&pmap_l2_p_failures, 1);
4138 CTR2(KTR_PMAP, "pmap_promote_l2: failure for va %#lx"
4139 " in pmap %p", va, pmap);
4144 * Both here and in the below "for" loop, to allow for repromotion
4145 * after MADV_FREE, conditionally write protect a clean L3E before
4146 * possibly aborting the promotion due to other L3E attributes. Why?
4147 * Suppose that MADV_FREE is applied to a part of a superpage, the
4148 * address range [S, E). pmap_advise() will demote the superpage
4149 * mapping, destroy the 4KB page mapping at the end of [S, E), and
4150 * set AP_RO and clear AF in the L3Es for the rest of [S, E). Later,
4151 * imagine that the memory in [S, E) is recycled, but the last 4KB
4152 * page in [S, E) is not the last to be rewritten, or simply accessed.
4153 * In other words, there is still a 4KB page in [S, E), call it P,
4154 * that is writeable but AP_RO is set and AF is clear in P's L3E.
4155 * Unless we write protect P before aborting the promotion, if and
4156 * when P is finally rewritten, there won't be a page fault to trigger
4160 if ((newl2 & (ATTR_S1_AP_RW_BIT | ATTR_SW_DBM)) ==
4161 (ATTR_S1_AP(ATTR_S1_AP_RO) | ATTR_SW_DBM)) {
4163 * When the mapping is clean, i.e., ATTR_S1_AP_RO is set,
4164 * ATTR_SW_DBM can be cleared without a TLB invalidation.
4166 if (!atomic_fcmpset_64(firstl3, &newl2, newl2 & ~ATTR_SW_DBM))
4168 newl2 &= ~ATTR_SW_DBM;
4170 if ((newl2 & ATTR_AF) == 0) {
4171 atomic_add_long(&pmap_l2_p_failures, 1);
4172 CTR2(KTR_PMAP, "pmap_promote_l2: failure for va %#lx"
4173 " in pmap %p", va, pmap);
4178 * Examine each of the other L3Es in the specified PTP. Abort if this
4179 * L3E maps an unexpected 4KB physical page or does not have identical
4180 * characteristics to the first L3E.
4182 pa = (newl2 & (~ATTR_MASK | ATTR_DESCR_MASK)) + L2_SIZE - PAGE_SIZE;
4183 for (l3 = firstl3 + NL3PG - 1; l3 > firstl3; l3--) {
4184 oldl3 = pmap_load(l3);
4185 if ((oldl3 & (~ATTR_MASK | ATTR_DESCR_MASK)) != pa) {
4186 atomic_add_long(&pmap_l2_p_failures, 1);
4187 CTR2(KTR_PMAP, "pmap_promote_l2: failure for va %#lx"
4188 " in pmap %p", va, pmap);
4192 if ((oldl3 & (ATTR_S1_AP_RW_BIT | ATTR_SW_DBM)) ==
4193 (ATTR_S1_AP(ATTR_S1_AP_RO) | ATTR_SW_DBM)) {
4195 * When the mapping is clean, i.e., ATTR_S1_AP_RO is
4196 * set, ATTR_SW_DBM can be cleared without a TLB
4199 if (!atomic_fcmpset_64(l3, &oldl3, oldl3 &
4202 oldl3 &= ~ATTR_SW_DBM;
4204 if ((oldl3 & ATTR_MASK) != (newl2 & ATTR_MASK)) {
4205 atomic_add_long(&pmap_l2_p_failures, 1);
4206 CTR2(KTR_PMAP, "pmap_promote_l2: failure for va %#lx"
4207 " in pmap %p", va, pmap);
4214 * Save the page table page in its current state until the L2
4215 * mapping the superpage is demoted by pmap_demote_l2() or
4216 * destroyed by pmap_remove_l3().
4219 mpte = PHYS_TO_VM_PAGE(pmap_load(l2) & ~ATTR_MASK);
4220 KASSERT(mpte >= vm_page_array &&
4221 mpte < &vm_page_array[vm_page_array_size],
4222 ("pmap_promote_l2: page table page is out of range"));
4223 KASSERT(mpte->pindex == pmap_l2_pindex(va),
4224 ("pmap_promote_l2: page table page's pindex is wrong"));
4225 if (pmap_insert_pt_page(pmap, mpte, true)) {
4226 atomic_add_long(&pmap_l2_p_failures, 1);
4228 "pmap_promote_l2: failure for va %#lx in pmap %p", va,
4233 if ((newl2 & ATTR_SW_MANAGED) != 0)
4234 pmap_pv_promote_l2(pmap, va, newl2 & ~ATTR_MASK, lockp);
4236 newl2 &= ~ATTR_DESCR_MASK;
4239 pmap_update_entry(pmap, l2, newl2, va & ~L2_OFFSET, L2_SIZE);
4241 atomic_add_long(&pmap_l2_promotions, 1);
4242 CTR2(KTR_PMAP, "pmap_promote_l2: success for va %#lx in pmap %p", va,
4245 #endif /* VM_NRESERVLEVEL > 0 */
4248 pmap_enter_largepage(pmap_t pmap, vm_offset_t va, pt_entry_t newpte, int flags,
4251 pd_entry_t *l0p, *l1p, *l2p, origpte;
4254 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
4255 KASSERT(psind > 0 && psind < MAXPAGESIZES,
4256 ("psind %d unexpected", psind));
4257 KASSERT(((newpte & ~ATTR_MASK) & (pagesizes[psind] - 1)) == 0,
4258 ("unaligned phys address %#lx newpte %#lx psind %d",
4259 (newpte & ~ATTR_MASK), newpte, psind));
4263 PMAP_ASSERT_L1_BLOCKS_SUPPORTED;
4265 l0p = pmap_l0(pmap, va);
4266 if ((pmap_load(l0p) & ATTR_DESCR_VALID) == 0) {
4267 mp = _pmap_alloc_l3(pmap, pmap_l0_pindex(va), NULL);
4269 if ((flags & PMAP_ENTER_NOSLEEP) != 0)
4270 return (KERN_RESOURCE_SHORTAGE);
4276 l1p = pmap_l0_to_l1(l0p, va);
4277 KASSERT(l1p != NULL, ("va %#lx lost l1 entry", va));
4278 origpte = pmap_load(l1p);
4280 l1p = pmap_l0_to_l1(l0p, va);
4281 KASSERT(l1p != NULL, ("va %#lx lost l1 entry", va));
4282 origpte = pmap_load(l1p);
4283 if ((origpte & ATTR_DESCR_VALID) == 0) {
4284 mp = PHYS_TO_VM_PAGE(pmap_load(l0p) &
4289 KASSERT(((origpte & ~ATTR_MASK) == (newpte & ~ATTR_MASK) &&
4290 (origpte & ATTR_DESCR_MASK) == L1_BLOCK) ||
4291 (origpte & ATTR_DESCR_VALID) == 0,
4292 ("va %#lx changing 1G phys page l1 %#lx newpte %#lx",
4293 va, origpte, newpte));
4294 pmap_store(l1p, newpte);
4295 } else /* (psind == 1) */ {
4296 l2p = pmap_l2(pmap, va);
4298 mp = _pmap_alloc_l3(pmap, pmap_l1_pindex(va), NULL);
4300 if ((flags & PMAP_ENTER_NOSLEEP) != 0)
4301 return (KERN_RESOURCE_SHORTAGE);
4307 l2p = (pd_entry_t *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(mp));
4308 l2p = &l2p[pmap_l2_index(va)];
4309 origpte = pmap_load(l2p);
4311 l1p = pmap_l1(pmap, va);
4312 origpte = pmap_load(l2p);
4313 if ((origpte & ATTR_DESCR_VALID) == 0) {
4314 mp = PHYS_TO_VM_PAGE(pmap_load(l1p) &
4319 KASSERT((origpte & ATTR_DESCR_VALID) == 0 ||
4320 ((origpte & ATTR_DESCR_MASK) == L2_BLOCK &&
4321 (origpte & ~ATTR_MASK) == (newpte & ~ATTR_MASK)),
4322 ("va %#lx changing 2M phys page l2 %#lx newpte %#lx",
4323 va, origpte, newpte));
4324 pmap_store(l2p, newpte);
4328 if ((origpte & ATTR_DESCR_VALID) == 0)
4329 pmap_resident_count_inc(pmap, pagesizes[psind] / PAGE_SIZE);
4330 if ((newpte & ATTR_SW_WIRED) != 0 && (origpte & ATTR_SW_WIRED) == 0)
4331 pmap->pm_stats.wired_count += pagesizes[psind] / PAGE_SIZE;
4332 else if ((newpte & ATTR_SW_WIRED) == 0 &&
4333 (origpte & ATTR_SW_WIRED) != 0)
4334 pmap->pm_stats.wired_count -= pagesizes[psind] / PAGE_SIZE;
4336 return (KERN_SUCCESS);
4340 * Insert the given physical page (p) at
4341 * the specified virtual address (v) in the
4342 * target physical map with the protection requested.
4344 * If specified, the page will be wired down, meaning
4345 * that the related pte can not be reclaimed.
4347 * NB: This is the only routine which MAY NOT lazy-evaluate
4348 * or lose information. That is, this routine must actually
4349 * insert this page into the given map NOW.
4352 pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
4353 u_int flags, int8_t psind)
4355 struct rwlock *lock;
4357 pt_entry_t new_l3, orig_l3;
4358 pt_entry_t *l2, *l3;
4365 KASSERT(ADDR_IS_CANONICAL(va),
4366 ("%s: Address not in canonical form: %lx", __func__, va));
4368 va = trunc_page(va);
4369 if ((m->oflags & VPO_UNMANAGED) == 0)
4370 VM_PAGE_OBJECT_BUSY_ASSERT(m);
4371 pa = VM_PAGE_TO_PHYS(m);
4372 new_l3 = (pt_entry_t)(pa | ATTR_DEFAULT | L3_PAGE);
4373 new_l3 |= pmap_pte_memattr(pmap, m->md.pv_memattr);
4374 new_l3 |= pmap_pte_prot(pmap, prot);
4376 if ((flags & PMAP_ENTER_WIRED) != 0)
4377 new_l3 |= ATTR_SW_WIRED;
4378 if (pmap->pm_stage == PM_STAGE1) {
4379 if (!ADDR_IS_KERNEL(va))
4380 new_l3 |= ATTR_S1_AP(ATTR_S1_AP_USER) | ATTR_S1_PXN;
4382 new_l3 |= ATTR_S1_UXN;
4383 if (pmap != kernel_pmap)
4384 new_l3 |= ATTR_S1_nG;
4387 * Clear the access flag on executable mappings, this will be
4388 * set later when the page is accessed. The fault handler is
4389 * required to invalidate the I-cache.
4391 * TODO: Switch to the valid flag to allow hardware management
4392 * of the access flag. Much of the pmap code assumes the
4393 * valid flag is set and fails to destroy the old page tables
4394 * correctly if it is clear.
4396 if (prot & VM_PROT_EXECUTE)
4399 if ((m->oflags & VPO_UNMANAGED) == 0) {
4400 new_l3 |= ATTR_SW_MANAGED;
4401 if ((prot & VM_PROT_WRITE) != 0) {
4402 new_l3 |= ATTR_SW_DBM;
4403 if ((flags & VM_PROT_WRITE) == 0) {
4404 if (pmap->pm_stage == PM_STAGE1)
4405 new_l3 |= ATTR_S1_AP(ATTR_S1_AP_RO);
4408 ~ATTR_S2_S2AP(ATTR_S2_S2AP_WRITE);
4413 CTR2(KTR_PMAP, "pmap_enter: %.16lx -> %.16lx", va, pa);
4417 if ((flags & PMAP_ENTER_LARGEPAGE) != 0) {
4418 KASSERT((m->oflags & VPO_UNMANAGED) != 0,
4419 ("managed largepage va %#lx flags %#x", va, flags));
4422 PMAP_ASSERT_L1_BLOCKS_SUPPORTED;
4424 } else /* (psind == 1) */
4426 rv = pmap_enter_largepage(pmap, va, new_l3, flags, psind);
4430 /* Assert the required virtual and physical alignment. */
4431 KASSERT((va & L2_OFFSET) == 0, ("pmap_enter: va unaligned"));
4432 KASSERT(m->psind > 0, ("pmap_enter: m->psind < psind"));
4433 rv = pmap_enter_l2(pmap, va, (new_l3 & ~L3_PAGE) | L2_BLOCK,
4440 * In the case that a page table page is not
4441 * resident, we are creating it here.
4444 pde = pmap_pde(pmap, va, &lvl);
4445 if (pde != NULL && lvl == 2) {
4446 l3 = pmap_l2_to_l3(pde, va);
4447 if (!ADDR_IS_KERNEL(va) && mpte == NULL) {
4448 mpte = PHYS_TO_VM_PAGE(pmap_load(pde) & ~ATTR_MASK);
4452 } else if (pde != NULL && lvl == 1) {
4453 l2 = pmap_l1_to_l2(pde, va);
4454 if ((pmap_load(l2) & ATTR_DESCR_MASK) == L2_BLOCK &&
4455 (l3 = pmap_demote_l2_locked(pmap, l2, va, &lock)) != NULL) {
4456 l3 = &l3[pmap_l3_index(va)];
4457 if (!ADDR_IS_KERNEL(va)) {
4458 mpte = PHYS_TO_VM_PAGE(
4459 pmap_load(l2) & ~ATTR_MASK);
4464 /* We need to allocate an L3 table. */
4466 if (!ADDR_IS_KERNEL(va)) {
4467 nosleep = (flags & PMAP_ENTER_NOSLEEP) != 0;
4470 * We use _pmap_alloc_l3() instead of pmap_alloc_l3() in order
4471 * to handle the possibility that a superpage mapping for "va"
4472 * was created while we slept.
4474 mpte = _pmap_alloc_l3(pmap, pmap_l2_pindex(va),
4475 nosleep ? NULL : &lock);
4476 if (mpte == NULL && nosleep) {
4477 CTR0(KTR_PMAP, "pmap_enter: mpte == NULL");
4478 rv = KERN_RESOURCE_SHORTAGE;
4483 panic("pmap_enter: missing L3 table for kernel va %#lx", va);
4486 orig_l3 = pmap_load(l3);
4487 opa = orig_l3 & ~ATTR_MASK;
4491 * Is the specified virtual address already mapped?
4493 if (pmap_l3_valid(orig_l3)) {
4495 * Wiring change, just update stats. We don't worry about
4496 * wiring PT pages as they remain resident as long as there
4497 * are valid mappings in them. Hence, if a user page is wired,
4498 * the PT page will be also.
4500 if ((flags & PMAP_ENTER_WIRED) != 0 &&
4501 (orig_l3 & ATTR_SW_WIRED) == 0)
4502 pmap->pm_stats.wired_count++;
4503 else if ((flags & PMAP_ENTER_WIRED) == 0 &&
4504 (orig_l3 & ATTR_SW_WIRED) != 0)
4505 pmap->pm_stats.wired_count--;
4508 * Remove the extra PT page reference.
4512 KASSERT(mpte->ref_count > 0,
4513 ("pmap_enter: missing reference to page table page,"
4518 * Has the physical page changed?
4522 * No, might be a protection or wiring change.
4524 if ((orig_l3 & ATTR_SW_MANAGED) != 0 &&
4525 (new_l3 & ATTR_SW_DBM) != 0)
4526 vm_page_aflag_set(m, PGA_WRITEABLE);
4531 * The physical page has changed. Temporarily invalidate
4534 orig_l3 = pmap_load_clear(l3);
4535 KASSERT((orig_l3 & ~ATTR_MASK) == opa,
4536 ("pmap_enter: unexpected pa update for %#lx", va));
4537 if ((orig_l3 & ATTR_SW_MANAGED) != 0) {
4538 om = PHYS_TO_VM_PAGE(opa);
4541 * The pmap lock is sufficient to synchronize with
4542 * concurrent calls to pmap_page_test_mappings() and
4543 * pmap_ts_referenced().
4545 if (pmap_pte_dirty(pmap, orig_l3))
4547 if ((orig_l3 & ATTR_AF) != 0) {
4548 pmap_invalidate_page(pmap, va, true);
4549 vm_page_aflag_set(om, PGA_REFERENCED);
4551 CHANGE_PV_LIST_LOCK_TO_PHYS(&lock, opa);
4552 pv = pmap_pvh_remove(&om->md, pmap, va);
4553 if ((m->oflags & VPO_UNMANAGED) != 0)
4554 free_pv_entry(pmap, pv);
4555 if ((om->a.flags & PGA_WRITEABLE) != 0 &&
4556 TAILQ_EMPTY(&om->md.pv_list) &&
4557 ((om->flags & PG_FICTITIOUS) != 0 ||
4558 TAILQ_EMPTY(&page_to_pvh(om)->pv_list)))
4559 vm_page_aflag_clear(om, PGA_WRITEABLE);
4561 KASSERT((orig_l3 & ATTR_AF) != 0,
4562 ("pmap_enter: unmanaged mapping lacks ATTR_AF"));
4563 pmap_invalidate_page(pmap, va, true);
4568 * Increment the counters.
4570 if ((new_l3 & ATTR_SW_WIRED) != 0)
4571 pmap->pm_stats.wired_count++;
4572 pmap_resident_count_inc(pmap, 1);
4575 * Enter on the PV list if part of our managed memory.
4577 if ((m->oflags & VPO_UNMANAGED) == 0) {
4579 pv = get_pv_entry(pmap, &lock);
4582 CHANGE_PV_LIST_LOCK_TO_PHYS(&lock, pa);
4583 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_next);
4585 if ((new_l3 & ATTR_SW_DBM) != 0)
4586 vm_page_aflag_set(m, PGA_WRITEABLE);
4590 if (pmap->pm_stage == PM_STAGE1) {
4592 * Sync icache if exec permission and attribute
4593 * VM_MEMATTR_WRITE_BACK is set. Do it now, before the mapping
4594 * is stored and made valid for hardware table walk. If done
4595 * later, then other can access this page before caches are
4596 * properly synced. Don't do it for kernel memory which is
4597 * mapped with exec permission even if the memory isn't going
4598 * to hold executable code. The only time when icache sync is
4599 * needed is after kernel module is loaded and the relocation
4600 * info is processed. And it's done in elf_cpu_load_file().
4602 if ((prot & VM_PROT_EXECUTE) && pmap != kernel_pmap &&
4603 m->md.pv_memattr == VM_MEMATTR_WRITE_BACK &&
4604 (opa != pa || (orig_l3 & ATTR_S1_XN))) {
4605 PMAP_ASSERT_STAGE1(pmap);
4606 cpu_icache_sync_range(PHYS_TO_DMAP(pa), PAGE_SIZE);
4609 cpu_dcache_wb_range(PHYS_TO_DMAP(pa), PAGE_SIZE);
4613 * Update the L3 entry
4615 if (pmap_l3_valid(orig_l3)) {
4616 KASSERT(opa == pa, ("pmap_enter: invalid update"));
4617 if ((orig_l3 & ~ATTR_AF) != (new_l3 & ~ATTR_AF)) {
4618 /* same PA, different attributes */
4619 orig_l3 = pmap_load_store(l3, new_l3);
4620 pmap_invalidate_page(pmap, va, true);
4621 if ((orig_l3 & ATTR_SW_MANAGED) != 0 &&
4622 pmap_pte_dirty(pmap, orig_l3))
4627 * This can happens if multiple threads simultaneously
4628 * access not yet mapped page. This bad for performance
4629 * since this can cause full demotion-NOP-promotion
4631 * Another possible reasons are:
4632 * - VM and pmap memory layout are diverged
4633 * - tlb flush is missing somewhere and CPU doesn't see
4636 CTR4(KTR_PMAP, "%s: already mapped page - "
4637 "pmap %p va 0x%#lx pte 0x%lx",
4638 __func__, pmap, va, new_l3);
4642 pmap_store(l3, new_l3);
4646 #if VM_NRESERVLEVEL > 0
4648 * Try to promote from level 3 pages to a level 2 superpage. This
4649 * currently only works on stage 1 pmaps as pmap_promote_l2 looks at
4650 * stage 1 specific fields and performs a break-before-make sequence
4651 * that is incorrect a stage 2 pmap.
4653 if ((mpte == NULL || mpte->ref_count == NL3PG) &&
4654 pmap_ps_enabled(pmap) && pmap->pm_stage == PM_STAGE1 &&
4655 (m->flags & PG_FICTITIOUS) == 0 &&
4656 vm_reserv_level_iffullpop(m) == 0) {
4657 pmap_promote_l2(pmap, pde, va, mpte, &lock);
4670 * Tries to create a read- and/or execute-only 2MB page mapping. Returns
4671 * KERN_SUCCESS if the mapping was created. Otherwise, returns an error
4672 * value. See pmap_enter_l2() for the possible error values when "no sleep",
4673 * "no replace", and "no reclaim" are specified.
4676 pmap_enter_2mpage(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
4677 struct rwlock **lockp)
4681 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
4682 PMAP_ASSERT_STAGE1(pmap);
4683 KASSERT(ADDR_IS_CANONICAL(va),
4684 ("%s: Address not in canonical form: %lx", __func__, va));
4686 new_l2 = (pd_entry_t)(VM_PAGE_TO_PHYS(m) | ATTR_DEFAULT |
4687 ATTR_S1_IDX(m->md.pv_memattr) | ATTR_S1_AP(ATTR_S1_AP_RO) |
4689 if ((m->oflags & VPO_UNMANAGED) == 0) {
4690 new_l2 |= ATTR_SW_MANAGED;
4693 if ((prot & VM_PROT_EXECUTE) == 0 ||
4694 m->md.pv_memattr == VM_MEMATTR_DEVICE)
4695 new_l2 |= ATTR_S1_XN;
4696 if (!ADDR_IS_KERNEL(va))
4697 new_l2 |= ATTR_S1_AP(ATTR_S1_AP_USER) | ATTR_S1_PXN;
4699 new_l2 |= ATTR_S1_UXN;
4700 if (pmap != kernel_pmap)
4701 new_l2 |= ATTR_S1_nG;
4702 return (pmap_enter_l2(pmap, va, new_l2, PMAP_ENTER_NOSLEEP |
4703 PMAP_ENTER_NOREPLACE | PMAP_ENTER_NORECLAIM, m, lockp));
4707 * Returns true if every page table entry in the specified page table is
4711 pmap_every_pte_zero(vm_paddr_t pa)
4713 pt_entry_t *pt_end, *pte;
4715 KASSERT((pa & PAGE_MASK) == 0, ("pa is misaligned"));
4716 pte = (pt_entry_t *)PHYS_TO_DMAP(pa);
4717 for (pt_end = pte + Ln_ENTRIES; pte < pt_end; pte++) {
4725 * Tries to create the specified 2MB page mapping. Returns KERN_SUCCESS if
4726 * the mapping was created, and one of KERN_FAILURE, KERN_NO_SPACE, or
4727 * KERN_RESOURCE_SHORTAGE otherwise. Returns KERN_FAILURE if
4728 * PMAP_ENTER_NOREPLACE was specified and a 4KB page mapping already exists
4729 * within the 2MB virtual address range starting at the specified virtual
4730 * address. Returns KERN_NO_SPACE if PMAP_ENTER_NOREPLACE was specified and a
4731 * 2MB page mapping already exists at the specified virtual address. Returns
4732 * KERN_RESOURCE_SHORTAGE if either (1) PMAP_ENTER_NOSLEEP was specified and a
4733 * page table page allocation failed or (2) PMAP_ENTER_NORECLAIM was specified
4734 * and a PV entry allocation failed.
4737 pmap_enter_l2(pmap_t pmap, vm_offset_t va, pd_entry_t new_l2, u_int flags,
4738 vm_page_t m, struct rwlock **lockp)
4740 struct spglist free;
4741 pd_entry_t *l2, old_l2;
4744 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
4745 KASSERT(ADDR_IS_CANONICAL(va),
4746 ("%s: Address not in canonical form: %lx", __func__, va));
4748 if ((l2 = pmap_alloc_l2(pmap, va, &l2pg, (flags &
4749 PMAP_ENTER_NOSLEEP) != 0 ? NULL : lockp)) == NULL) {
4750 CTR2(KTR_PMAP, "pmap_enter_l2: failure for va %#lx in pmap %p",
4752 return (KERN_RESOURCE_SHORTAGE);
4756 * If there are existing mappings, either abort or remove them.
4758 if ((old_l2 = pmap_load(l2)) != 0) {
4759 KASSERT(l2pg == NULL || l2pg->ref_count > 1,
4760 ("pmap_enter_l2: l2pg's ref count is too low"));
4761 if ((flags & PMAP_ENTER_NOREPLACE) != 0) {
4762 if ((old_l2 & ATTR_DESCR_MASK) == L2_BLOCK) {
4766 "pmap_enter_l2: no space for va %#lx"
4767 " in pmap %p", va, pmap);
4768 return (KERN_NO_SPACE);
4769 } else if (!ADDR_IS_KERNEL(va) ||
4770 !pmap_every_pte_zero(old_l2 & ~ATTR_MASK)) {
4774 "pmap_enter_l2: failure for va %#lx"
4775 " in pmap %p", va, pmap);
4776 return (KERN_FAILURE);
4780 if ((old_l2 & ATTR_DESCR_MASK) == L2_BLOCK)
4781 (void)pmap_remove_l2(pmap, l2, va,
4782 pmap_load(pmap_l1(pmap, va)), &free, lockp);
4784 pmap_remove_l3_range(pmap, old_l2, va, va + L2_SIZE,
4786 if (!ADDR_IS_KERNEL(va)) {
4787 vm_page_free_pages_toq(&free, true);
4788 KASSERT(pmap_load(l2) == 0,
4789 ("pmap_enter_l2: non-zero L2 entry %p", l2));
4791 KASSERT(SLIST_EMPTY(&free),
4792 ("pmap_enter_l2: freed kernel page table page"));
4795 * Both pmap_remove_l2() and pmap_remove_l3_range()
4796 * will leave the kernel page table page zero filled.
4797 * Nonetheless, the TLB could have an intermediate
4798 * entry for the kernel page table page, so request
4799 * an invalidation at all levels after clearing
4800 * the L2_TABLE entry.
4802 mt = PHYS_TO_VM_PAGE(pmap_load(l2) & ~ATTR_MASK);
4803 if (pmap_insert_pt_page(pmap, mt, false))
4804 panic("pmap_enter_l2: trie insert failed");
4806 pmap_s1_invalidate_page(pmap, va, false);
4810 if ((new_l2 & ATTR_SW_MANAGED) != 0) {
4812 * Abort this mapping if its PV entry could not be created.
4814 if (!pmap_pv_insert_l2(pmap, va, new_l2, flags, lockp)) {
4816 pmap_abort_ptp(pmap, va, l2pg);
4818 "pmap_enter_l2: failure for va %#lx in pmap %p",
4820 return (KERN_RESOURCE_SHORTAGE);
4822 if ((new_l2 & ATTR_SW_DBM) != 0)
4823 for (mt = m; mt < &m[L2_SIZE / PAGE_SIZE]; mt++)
4824 vm_page_aflag_set(mt, PGA_WRITEABLE);
4828 * Increment counters.
4830 if ((new_l2 & ATTR_SW_WIRED) != 0)
4831 pmap->pm_stats.wired_count += L2_SIZE / PAGE_SIZE;
4832 pmap->pm_stats.resident_count += L2_SIZE / PAGE_SIZE;
4835 * Conditionally sync the icache. See pmap_enter() for details.
4837 if ((new_l2 & ATTR_S1_XN) == 0 && ((new_l2 & ~ATTR_MASK) !=
4838 (old_l2 & ~ATTR_MASK) || (old_l2 & ATTR_S1_XN) != 0) &&
4839 pmap != kernel_pmap && m->md.pv_memattr == VM_MEMATTR_WRITE_BACK) {
4840 cpu_icache_sync_range(PHYS_TO_DMAP(new_l2 & ~ATTR_MASK),
4845 * Map the superpage.
4847 pmap_store(l2, new_l2);
4850 atomic_add_long(&pmap_l2_mappings, 1);
4851 CTR2(KTR_PMAP, "pmap_enter_l2: success for va %#lx in pmap %p",
4854 return (KERN_SUCCESS);
4858 * Maps a sequence of resident pages belonging to the same object.
4859 * The sequence begins with the given page m_start. This page is
4860 * mapped at the given virtual address start. Each subsequent page is
4861 * mapped at a virtual address that is offset from start by the same
4862 * amount as the page is offset from m_start within the object. The
4863 * last page in the sequence is the page with the largest offset from
4864 * m_start that can be mapped at a virtual address less than the given
4865 * virtual address end. Not every virtual page between start and end
4866 * is mapped; only those for which a resident page exists with the
4867 * corresponding offset from m_start are mapped.
4870 pmap_enter_object(pmap_t pmap, vm_offset_t start, vm_offset_t end,
4871 vm_page_t m_start, vm_prot_t prot)
4873 struct rwlock *lock;
4876 vm_pindex_t diff, psize;
4879 VM_OBJECT_ASSERT_LOCKED(m_start->object);
4881 psize = atop(end - start);
4886 while (m != NULL && (diff = m->pindex - m_start->pindex) < psize) {
4887 va = start + ptoa(diff);
4888 if ((va & L2_OFFSET) == 0 && va + L2_SIZE <= end &&
4889 m->psind == 1 && pmap_ps_enabled(pmap) &&
4890 ((rv = pmap_enter_2mpage(pmap, va, m, prot, &lock)) ==
4891 KERN_SUCCESS || rv == KERN_NO_SPACE))
4892 m = &m[L2_SIZE / PAGE_SIZE - 1];
4894 mpte = pmap_enter_quick_locked(pmap, va, m, prot, mpte,
4896 m = TAILQ_NEXT(m, listq);
4904 * this code makes some *MAJOR* assumptions:
4905 * 1. Current pmap & pmap exists.
4908 * 4. No page table pages.
4909 * but is *MUCH* faster than pmap_enter...
4913 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot)
4915 struct rwlock *lock;
4919 (void)pmap_enter_quick_locked(pmap, va, m, prot, NULL, &lock);
4926 pmap_enter_quick_locked(pmap_t pmap, vm_offset_t va, vm_page_t m,
4927 vm_prot_t prot, vm_page_t mpte, struct rwlock **lockp)
4930 pt_entry_t *l1, *l2, *l3, l3_val;
4934 KASSERT(!VA_IS_CLEANMAP(va) ||
4935 (m->oflags & VPO_UNMANAGED) != 0,
4936 ("pmap_enter_quick_locked: managed mapping within the clean submap"));
4937 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
4938 PMAP_ASSERT_STAGE1(pmap);
4939 KASSERT(ADDR_IS_CANONICAL(va),
4940 ("%s: Address not in canonical form: %lx", __func__, va));
4942 CTR2(KTR_PMAP, "pmap_enter_quick_locked: %p %lx", pmap, va);
4944 * In the case that a page table page is not
4945 * resident, we are creating it here.
4947 if (!ADDR_IS_KERNEL(va)) {
4948 vm_pindex_t l2pindex;
4951 * Calculate pagetable page index
4953 l2pindex = pmap_l2_pindex(va);
4954 if (mpte && (mpte->pindex == l2pindex)) {
4958 * If the page table page is mapped, we just increment
4959 * the hold count, and activate it. Otherwise, we
4960 * attempt to allocate a page table page, passing NULL
4961 * instead of the PV list lock pointer because we don't
4962 * intend to sleep. If this attempt fails, we don't
4963 * retry. Instead, we give up.
4965 l1 = pmap_l1(pmap, va);
4966 if (l1 != NULL && pmap_load(l1) != 0) {
4967 if ((pmap_load(l1) & ATTR_DESCR_MASK) ==
4970 l2 = pmap_l1_to_l2(l1, va);
4971 if (pmap_load(l2) != 0) {
4972 if ((pmap_load(l2) & ATTR_DESCR_MASK) ==
4975 mpte = PHYS_TO_VM_PAGE(pmap_load(l2) &
4979 mpte = _pmap_alloc_l3(pmap, l2pindex,
4985 mpte = _pmap_alloc_l3(pmap, l2pindex, NULL);
4990 l3 = (pt_entry_t *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(mpte));
4991 l3 = &l3[pmap_l3_index(va)];
4994 pde = pmap_pde(kernel_pmap, va, &lvl);
4995 KASSERT(pde != NULL,
4996 ("pmap_enter_quick_locked: Invalid page entry, va: 0x%lx",
4999 ("pmap_enter_quick_locked: Invalid level %d", lvl));
5000 l3 = pmap_l2_to_l3(pde, va);
5004 * Abort if a mapping already exists.
5006 if (pmap_load(l3) != 0) {
5013 * Enter on the PV list if part of our managed memory.
5015 if ((m->oflags & VPO_UNMANAGED) == 0 &&
5016 !pmap_try_insert_pv_entry(pmap, va, m, lockp)) {
5018 pmap_abort_ptp(pmap, va, mpte);
5023 * Increment counters
5025 pmap_resident_count_inc(pmap, 1);
5027 pa = VM_PAGE_TO_PHYS(m);
5028 l3_val = pa | ATTR_DEFAULT | ATTR_S1_IDX(m->md.pv_memattr) |
5029 ATTR_S1_AP(ATTR_S1_AP_RO) | L3_PAGE;
5030 if ((prot & VM_PROT_EXECUTE) == 0 ||
5031 m->md.pv_memattr == VM_MEMATTR_DEVICE)
5032 l3_val |= ATTR_S1_XN;
5033 if (!ADDR_IS_KERNEL(va))
5034 l3_val |= ATTR_S1_AP(ATTR_S1_AP_USER) | ATTR_S1_PXN;
5036 l3_val |= ATTR_S1_UXN;
5037 if (pmap != kernel_pmap)
5038 l3_val |= ATTR_S1_nG;
5041 * Now validate mapping with RO protection
5043 if ((m->oflags & VPO_UNMANAGED) == 0) {
5044 l3_val |= ATTR_SW_MANAGED;
5048 /* Sync icache before the mapping is stored to PTE */
5049 if ((prot & VM_PROT_EXECUTE) && pmap != kernel_pmap &&
5050 m->md.pv_memattr == VM_MEMATTR_WRITE_BACK)
5051 cpu_icache_sync_range(PHYS_TO_DMAP(pa), PAGE_SIZE);
5053 pmap_store(l3, l3_val);
5060 * This code maps large physical mmap regions into the
5061 * processor address space. Note that some shortcuts
5062 * are taken, but the code works.
5065 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr, vm_object_t object,
5066 vm_pindex_t pindex, vm_size_t size)
5069 VM_OBJECT_ASSERT_WLOCKED(object);
5070 KASSERT(object->type == OBJT_DEVICE || object->type == OBJT_SG,
5071 ("pmap_object_init_pt: non-device object"));
5075 * Clear the wired attribute from the mappings for the specified range of
5076 * addresses in the given pmap. Every valid mapping within that range
5077 * must have the wired attribute set. In contrast, invalid mappings
5078 * cannot have the wired attribute set, so they are ignored.
5080 * The wired attribute of the page table entry is not a hardware feature,
5081 * so there is no need to invalidate any TLB entries.
5084 pmap_unwire(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
5086 vm_offset_t va_next;
5087 pd_entry_t *l0, *l1, *l2;
5091 for (; sva < eva; sva = va_next) {
5092 l0 = pmap_l0(pmap, sva);
5093 if (pmap_load(l0) == 0) {
5094 va_next = (sva + L0_SIZE) & ~L0_OFFSET;
5100 l1 = pmap_l0_to_l1(l0, sva);
5101 va_next = (sva + L1_SIZE) & ~L1_OFFSET;
5104 if (pmap_load(l1) == 0)
5107 if ((pmap_load(l1) & ATTR_DESCR_MASK) == L1_BLOCK) {
5108 PMAP_ASSERT_L1_BLOCKS_SUPPORTED;
5109 KASSERT(va_next <= eva,
5110 ("partial update of non-transparent 1G page "
5111 "l1 %#lx sva %#lx eva %#lx va_next %#lx",
5112 pmap_load(l1), sva, eva, va_next));
5113 MPASS(pmap != kernel_pmap);
5114 MPASS((pmap_load(l1) & (ATTR_SW_MANAGED |
5115 ATTR_SW_WIRED)) == ATTR_SW_WIRED);
5116 pmap_clear_bits(l1, ATTR_SW_WIRED);
5117 pmap->pm_stats.wired_count -= L1_SIZE / PAGE_SIZE;
5121 va_next = (sva + L2_SIZE) & ~L2_OFFSET;
5125 l2 = pmap_l1_to_l2(l1, sva);
5126 if (pmap_load(l2) == 0)
5129 if ((pmap_load(l2) & ATTR_DESCR_MASK) == L2_BLOCK) {
5130 if ((pmap_load(l2) & ATTR_SW_WIRED) == 0)
5131 panic("pmap_unwire: l2 %#jx is missing "
5132 "ATTR_SW_WIRED", (uintmax_t)pmap_load(l2));
5135 * Are we unwiring the entire large page? If not,
5136 * demote the mapping and fall through.
5138 if (sva + L2_SIZE == va_next && eva >= va_next) {
5139 pmap_clear_bits(l2, ATTR_SW_WIRED);
5140 pmap->pm_stats.wired_count -= L2_SIZE /
5143 } else if (pmap_demote_l2(pmap, l2, sva) == NULL)
5144 panic("pmap_unwire: demotion failed");
5146 KASSERT((pmap_load(l2) & ATTR_DESCR_MASK) == L2_TABLE,
5147 ("pmap_unwire: Invalid l2 entry after demotion"));
5151 for (l3 = pmap_l2_to_l3(l2, sva); sva != va_next; l3++,
5153 if (pmap_load(l3) == 0)
5155 if ((pmap_load(l3) & ATTR_SW_WIRED) == 0)
5156 panic("pmap_unwire: l3 %#jx is missing "
5157 "ATTR_SW_WIRED", (uintmax_t)pmap_load(l3));
5160 * ATTR_SW_WIRED must be cleared atomically. Although
5161 * the pmap lock synchronizes access to ATTR_SW_WIRED,
5162 * the System MMU may write to the entry concurrently.
5164 pmap_clear_bits(l3, ATTR_SW_WIRED);
5165 pmap->pm_stats.wired_count--;
5172 * Copy the range specified by src_addr/len
5173 * from the source map to the range dst_addr/len
5174 * in the destination map.
5176 * This routine is only advisory and need not do anything.
5178 * Because the executable mappings created by this routine are copied,
5179 * it should not have to flush the instruction cache.
5182 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr, vm_size_t len,
5183 vm_offset_t src_addr)
5185 struct rwlock *lock;
5186 pd_entry_t *l0, *l1, *l2, srcptepaddr;
5187 pt_entry_t *dst_pte, mask, nbits, ptetemp, *src_pte;
5188 vm_offset_t addr, end_addr, va_next;
5189 vm_page_t dst_m, dstmpte, srcmpte;
5191 PMAP_ASSERT_STAGE1(dst_pmap);
5192 PMAP_ASSERT_STAGE1(src_pmap);
5194 if (dst_addr != src_addr)
5196 end_addr = src_addr + len;
5198 if (dst_pmap < src_pmap) {
5199 PMAP_LOCK(dst_pmap);
5200 PMAP_LOCK(src_pmap);
5202 PMAP_LOCK(src_pmap);
5203 PMAP_LOCK(dst_pmap);
5205 for (addr = src_addr; addr < end_addr; addr = va_next) {
5206 l0 = pmap_l0(src_pmap, addr);
5207 if (pmap_load(l0) == 0) {
5208 va_next = (addr + L0_SIZE) & ~L0_OFFSET;
5214 va_next = (addr + L1_SIZE) & ~L1_OFFSET;
5217 l1 = pmap_l0_to_l1(l0, addr);
5218 if (pmap_load(l1) == 0)
5220 if ((pmap_load(l1) & ATTR_DESCR_MASK) == L1_BLOCK) {
5221 PMAP_ASSERT_L1_BLOCKS_SUPPORTED;
5222 KASSERT(va_next <= end_addr,
5223 ("partial update of non-transparent 1G page "
5224 "l1 %#lx addr %#lx end_addr %#lx va_next %#lx",
5225 pmap_load(l1), addr, end_addr, va_next));
5226 srcptepaddr = pmap_load(l1);
5227 l1 = pmap_l1(dst_pmap, addr);
5229 if (_pmap_alloc_l3(dst_pmap,
5230 pmap_l0_pindex(addr), NULL) == NULL)
5232 l1 = pmap_l1(dst_pmap, addr);
5234 l0 = pmap_l0(dst_pmap, addr);
5235 dst_m = PHYS_TO_VM_PAGE(pmap_load(l0) &
5239 KASSERT(pmap_load(l1) == 0,
5240 ("1G mapping present in dst pmap "
5241 "l1 %#lx addr %#lx end_addr %#lx va_next %#lx",
5242 pmap_load(l1), addr, end_addr, va_next));
5243 pmap_store(l1, srcptepaddr & ~ATTR_SW_WIRED);
5244 pmap_resident_count_inc(dst_pmap, L1_SIZE / PAGE_SIZE);
5248 va_next = (addr + L2_SIZE) & ~L2_OFFSET;
5251 l2 = pmap_l1_to_l2(l1, addr);
5252 srcptepaddr = pmap_load(l2);
5253 if (srcptepaddr == 0)
5255 if ((srcptepaddr & ATTR_DESCR_MASK) == L2_BLOCK) {
5257 * We can only virtual copy whole superpages.
5259 if ((addr & L2_OFFSET) != 0 ||
5260 addr + L2_SIZE > end_addr)
5262 l2 = pmap_alloc_l2(dst_pmap, addr, &dst_m, NULL);
5265 if (pmap_load(l2) == 0 &&
5266 ((srcptepaddr & ATTR_SW_MANAGED) == 0 ||
5267 pmap_pv_insert_l2(dst_pmap, addr, srcptepaddr,
5268 PMAP_ENTER_NORECLAIM, &lock))) {
5270 * We leave the dirty bit unchanged because
5271 * managed read/write superpage mappings are
5272 * required to be dirty. However, managed
5273 * superpage mappings are not required to
5274 * have their accessed bit set, so we clear
5275 * it because we don't know if this mapping
5278 srcptepaddr &= ~ATTR_SW_WIRED;
5279 if ((srcptepaddr & ATTR_SW_MANAGED) != 0)
5280 srcptepaddr &= ~ATTR_AF;
5281 pmap_store(l2, srcptepaddr);
5282 pmap_resident_count_inc(dst_pmap, L2_SIZE /
5284 atomic_add_long(&pmap_l2_mappings, 1);
5286 pmap_abort_ptp(dst_pmap, addr, dst_m);
5289 KASSERT((srcptepaddr & ATTR_DESCR_MASK) == L2_TABLE,
5290 ("pmap_copy: invalid L2 entry"));
5291 srcptepaddr &= ~ATTR_MASK;
5292 srcmpte = PHYS_TO_VM_PAGE(srcptepaddr);
5293 KASSERT(srcmpte->ref_count > 0,
5294 ("pmap_copy: source page table page is unused"));
5295 if (va_next > end_addr)
5297 src_pte = (pt_entry_t *)PHYS_TO_DMAP(srcptepaddr);
5298 src_pte = &src_pte[pmap_l3_index(addr)];
5300 for (; addr < va_next; addr += PAGE_SIZE, src_pte++) {
5301 ptetemp = pmap_load(src_pte);
5304 * We only virtual copy managed pages.
5306 if ((ptetemp & ATTR_SW_MANAGED) == 0)
5309 if (dstmpte != NULL) {
5310 KASSERT(dstmpte->pindex == pmap_l2_pindex(addr),
5311 ("dstmpte pindex/addr mismatch"));
5312 dstmpte->ref_count++;
5313 } else if ((dstmpte = pmap_alloc_l3(dst_pmap, addr,
5316 dst_pte = (pt_entry_t *)
5317 PHYS_TO_DMAP(VM_PAGE_TO_PHYS(dstmpte));
5318 dst_pte = &dst_pte[pmap_l3_index(addr)];
5319 if (pmap_load(dst_pte) == 0 &&
5320 pmap_try_insert_pv_entry(dst_pmap, addr,
5321 PHYS_TO_VM_PAGE(ptetemp & ~ATTR_MASK), &lock)) {
5323 * Clear the wired, modified, and accessed
5324 * (referenced) bits during the copy.
5326 mask = ATTR_AF | ATTR_SW_WIRED;
5328 if ((ptetemp & ATTR_SW_DBM) != 0)
5329 nbits |= ATTR_S1_AP_RW_BIT;
5330 pmap_store(dst_pte, (ptetemp & ~mask) | nbits);
5331 pmap_resident_count_inc(dst_pmap, 1);
5333 pmap_abort_ptp(dst_pmap, addr, dstmpte);
5336 /* Have we copied all of the valid mappings? */
5337 if (dstmpte->ref_count >= srcmpte->ref_count)
5343 * XXX This barrier may not be needed because the destination pmap is
5350 PMAP_UNLOCK(src_pmap);
5351 PMAP_UNLOCK(dst_pmap);
5355 * pmap_zero_page zeros the specified hardware page by mapping
5356 * the page into KVM and using bzero to clear its contents.
5359 pmap_zero_page(vm_page_t m)
5361 vm_offset_t va = PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m));
5363 pagezero((void *)va);
5367 * pmap_zero_page_area zeros the specified hardware page by mapping
5368 * the page into KVM and using bzero to clear its contents.
5370 * off and size may not cover an area beyond a single hardware page.
5373 pmap_zero_page_area(vm_page_t m, int off, int size)
5375 vm_offset_t va = PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m));
5377 if (off == 0 && size == PAGE_SIZE)
5378 pagezero((void *)va);
5380 bzero((char *)va + off, size);
5384 * pmap_copy_page copies the specified (machine independent)
5385 * page by mapping the page into virtual memory and using
5386 * bcopy to copy the page, one machine dependent page at a
5390 pmap_copy_page(vm_page_t msrc, vm_page_t mdst)
5392 vm_offset_t src = PHYS_TO_DMAP(VM_PAGE_TO_PHYS(msrc));
5393 vm_offset_t dst = PHYS_TO_DMAP(VM_PAGE_TO_PHYS(mdst));
5395 pagecopy((void *)src, (void *)dst);
5398 int unmapped_buf_allowed = 1;
5401 pmap_copy_pages(vm_page_t ma[], vm_offset_t a_offset, vm_page_t mb[],
5402 vm_offset_t b_offset, int xfersize)
5406 vm_paddr_t p_a, p_b;
5407 vm_offset_t a_pg_offset, b_pg_offset;
5410 while (xfersize > 0) {
5411 a_pg_offset = a_offset & PAGE_MASK;
5412 m_a = ma[a_offset >> PAGE_SHIFT];
5413 p_a = m_a->phys_addr;
5414 b_pg_offset = b_offset & PAGE_MASK;
5415 m_b = mb[b_offset >> PAGE_SHIFT];
5416 p_b = m_b->phys_addr;
5417 cnt = min(xfersize, PAGE_SIZE - a_pg_offset);
5418 cnt = min(cnt, PAGE_SIZE - b_pg_offset);
5419 if (__predict_false(!PHYS_IN_DMAP(p_a))) {
5420 panic("!DMAP a %lx", p_a);
5422 a_cp = (char *)PHYS_TO_DMAP(p_a) + a_pg_offset;
5424 if (__predict_false(!PHYS_IN_DMAP(p_b))) {
5425 panic("!DMAP b %lx", p_b);
5427 b_cp = (char *)PHYS_TO_DMAP(p_b) + b_pg_offset;
5429 bcopy(a_cp, b_cp, cnt);
5437 pmap_quick_enter_page(vm_page_t m)
5440 return (PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m)));
5444 pmap_quick_remove_page(vm_offset_t addr)
5449 * Returns true if the pmap's pv is one of the first
5450 * 16 pvs linked to from this page. This count may
5451 * be changed upwards or downwards in the future; it
5452 * is only necessary that true be returned for a small
5453 * subset of pmaps for proper page aging.
5456 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
5458 struct md_page *pvh;
5459 struct rwlock *lock;
5464 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
5465 ("pmap_page_exists_quick: page %p is not managed", m));
5467 lock = VM_PAGE_TO_PV_LIST_LOCK(m);
5469 TAILQ_FOREACH(pv, &m->md.pv_list, pv_next) {
5470 if (PV_PMAP(pv) == pmap) {
5478 if (!rv && loops < 16 && (m->flags & PG_FICTITIOUS) == 0) {
5479 pvh = page_to_pvh(m);
5480 TAILQ_FOREACH(pv, &pvh->pv_list, pv_next) {
5481 if (PV_PMAP(pv) == pmap) {
5495 * pmap_page_wired_mappings:
5497 * Return the number of managed mappings to the given physical page
5501 pmap_page_wired_mappings(vm_page_t m)
5503 struct rwlock *lock;
5504 struct md_page *pvh;
5508 int count, md_gen, pvh_gen;
5510 if ((m->oflags & VPO_UNMANAGED) != 0)
5512 lock = VM_PAGE_TO_PV_LIST_LOCK(m);
5516 TAILQ_FOREACH(pv, &m->md.pv_list, pv_next) {
5518 if (!PMAP_TRYLOCK(pmap)) {
5519 md_gen = m->md.pv_gen;
5523 if (md_gen != m->md.pv_gen) {
5528 pte = pmap_pte_exists(pmap, pv->pv_va, 3, __func__);
5529 if ((pmap_load(pte) & ATTR_SW_WIRED) != 0)
5533 if ((m->flags & PG_FICTITIOUS) == 0) {
5534 pvh = page_to_pvh(m);
5535 TAILQ_FOREACH(pv, &pvh->pv_list, pv_next) {
5537 if (!PMAP_TRYLOCK(pmap)) {
5538 md_gen = m->md.pv_gen;
5539 pvh_gen = pvh->pv_gen;
5543 if (md_gen != m->md.pv_gen ||
5544 pvh_gen != pvh->pv_gen) {
5549 pte = pmap_pte_exists(pmap, pv->pv_va, 2, __func__);
5550 if ((pmap_load(pte) & ATTR_SW_WIRED) != 0)
5560 * Returns true if the given page is mapped individually or as part of
5561 * a 2mpage. Otherwise, returns false.
5564 pmap_page_is_mapped(vm_page_t m)
5566 struct rwlock *lock;
5569 if ((m->oflags & VPO_UNMANAGED) != 0)
5571 lock = VM_PAGE_TO_PV_LIST_LOCK(m);
5573 rv = !TAILQ_EMPTY(&m->md.pv_list) ||
5574 ((m->flags & PG_FICTITIOUS) == 0 &&
5575 !TAILQ_EMPTY(&page_to_pvh(m)->pv_list));
5581 * Destroy all managed, non-wired mappings in the given user-space
5582 * pmap. This pmap cannot be active on any processor besides the
5585 * This function cannot be applied to the kernel pmap. Moreover, it
5586 * is not intended for general use. It is only to be used during
5587 * process termination. Consequently, it can be implemented in ways
5588 * that make it faster than pmap_remove(). First, it can more quickly
5589 * destroy mappings by iterating over the pmap's collection of PV
5590 * entries, rather than searching the page table. Second, it doesn't
5591 * have to test and clear the page table entries atomically, because
5592 * no processor is currently accessing the user address space. In
5593 * particular, a page table entry's dirty bit won't change state once
5594 * this function starts.
5597 pmap_remove_pages(pmap_t pmap)
5600 pt_entry_t *pte, tpte;
5601 struct spglist free;
5602 struct pv_chunklist free_chunks[PMAP_MEMDOM];
5603 vm_page_t m, ml3, mt;
5605 struct md_page *pvh;
5606 struct pv_chunk *pc, *npc;
5607 struct rwlock *lock;
5609 uint64_t inuse, bitmask;
5610 int allfree, field, i, idx, lvl;
5616 for (i = 0; i < PMAP_MEMDOM; i++)
5617 TAILQ_INIT(&free_chunks[i]);
5620 TAILQ_FOREACH_SAFE(pc, &pmap->pm_pvchunk, pc_list, npc) {
5623 for (field = 0; field < _NPCM; field++) {
5624 inuse = ~pc->pc_map[field] & pc_freemask[field];
5625 while (inuse != 0) {
5626 bit = ffsl(inuse) - 1;
5627 bitmask = 1UL << bit;
5628 idx = field * 64 + bit;
5629 pv = &pc->pc_pventry[idx];
5632 pde = pmap_pde(pmap, pv->pv_va, &lvl);
5633 KASSERT(pde != NULL,
5634 ("Attempting to remove an unmapped page"));
5638 pte = pmap_l1_to_l2(pde, pv->pv_va);
5639 tpte = pmap_load(pte);
5640 KASSERT((tpte & ATTR_DESCR_MASK) ==
5642 ("Attempting to remove an invalid "
5643 "block: %lx", tpte));
5646 pte = pmap_l2_to_l3(pde, pv->pv_va);
5647 tpte = pmap_load(pte);
5648 KASSERT((tpte & ATTR_DESCR_MASK) ==
5650 ("Attempting to remove an invalid "
5651 "page: %lx", tpte));
5655 "Invalid page directory level: %d",
5660 * We cannot remove wired pages from a process' mapping at this time
5662 if (tpte & ATTR_SW_WIRED) {
5668 pc->pc_map[field] |= bitmask;
5671 * Because this pmap is not active on other
5672 * processors, the dirty bit cannot have
5673 * changed state since we last loaded pte.
5677 pa = tpte & ~ATTR_MASK;
5679 m = PHYS_TO_VM_PAGE(pa);
5680 KASSERT(m->phys_addr == pa,
5681 ("vm_page_t %p phys_addr mismatch %016jx %016jx",
5682 m, (uintmax_t)m->phys_addr,
5685 KASSERT((m->flags & PG_FICTITIOUS) != 0 ||
5686 m < &vm_page_array[vm_page_array_size],
5687 ("pmap_remove_pages: bad pte %#jx",
5691 * Update the vm_page_t clean/reference bits.
5693 if (pmap_pte_dirty(pmap, tpte)) {
5696 for (mt = m; mt < &m[L2_SIZE / PAGE_SIZE]; mt++)
5705 CHANGE_PV_LIST_LOCK_TO_VM_PAGE(&lock, m);
5709 pmap_resident_count_dec(pmap,
5710 L2_SIZE / PAGE_SIZE);
5711 pvh = page_to_pvh(m);
5712 TAILQ_REMOVE(&pvh->pv_list, pv,pv_next);
5714 if (TAILQ_EMPTY(&pvh->pv_list)) {
5715 for (mt = m; mt < &m[L2_SIZE / PAGE_SIZE]; mt++)
5716 if ((mt->a.flags & PGA_WRITEABLE) != 0 &&
5717 TAILQ_EMPTY(&mt->md.pv_list))
5718 vm_page_aflag_clear(mt, PGA_WRITEABLE);
5720 ml3 = pmap_remove_pt_page(pmap,
5723 KASSERT(ml3->valid == VM_PAGE_BITS_ALL,
5724 ("pmap_remove_pages: l3 page not promoted"));
5725 pmap_resident_count_dec(pmap,1);
5726 KASSERT(ml3->ref_count == NL3PG,
5727 ("pmap_remove_pages: l3 page ref count error"));
5729 pmap_add_delayed_free_list(ml3,
5734 pmap_resident_count_dec(pmap, 1);
5735 TAILQ_REMOVE(&m->md.pv_list, pv,
5738 if ((m->a.flags & PGA_WRITEABLE) != 0 &&
5739 TAILQ_EMPTY(&m->md.pv_list) &&
5740 (m->flags & PG_FICTITIOUS) == 0) {
5741 pvh = page_to_pvh(m);
5742 if (TAILQ_EMPTY(&pvh->pv_list))
5743 vm_page_aflag_clear(m,
5748 pmap_unuse_pt(pmap, pv->pv_va, pmap_load(pde),
5753 PV_STAT(atomic_add_long(&pv_entry_frees, freed));
5754 PV_STAT(atomic_add_int(&pv_entry_spare, freed));
5755 PV_STAT(atomic_subtract_long(&pv_entry_count, freed));
5757 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
5758 TAILQ_INSERT_TAIL(&free_chunks[pc_to_domain(pc)], pc,
5764 pmap_invalidate_all(pmap);
5765 free_pv_chunk_batch(free_chunks);
5767 vm_page_free_pages_toq(&free, true);
5771 * This is used to check if a page has been accessed or modified.
5774 pmap_page_test_mappings(vm_page_t m, boolean_t accessed, boolean_t modified)
5776 struct rwlock *lock;
5778 struct md_page *pvh;
5779 pt_entry_t *pte, mask, value;
5781 int md_gen, pvh_gen;
5785 lock = VM_PAGE_TO_PV_LIST_LOCK(m);
5788 TAILQ_FOREACH(pv, &m->md.pv_list, pv_next) {
5790 PMAP_ASSERT_STAGE1(pmap);
5791 if (!PMAP_TRYLOCK(pmap)) {
5792 md_gen = m->md.pv_gen;
5796 if (md_gen != m->md.pv_gen) {
5801 pte = pmap_pte_exists(pmap, pv->pv_va, 3, __func__);
5805 mask |= ATTR_S1_AP_RW_BIT;
5806 value |= ATTR_S1_AP(ATTR_S1_AP_RW);
5809 mask |= ATTR_AF | ATTR_DESCR_MASK;
5810 value |= ATTR_AF | L3_PAGE;
5812 rv = (pmap_load(pte) & mask) == value;
5817 if ((m->flags & PG_FICTITIOUS) == 0) {
5818 pvh = page_to_pvh(m);
5819 TAILQ_FOREACH(pv, &pvh->pv_list, pv_next) {
5821 PMAP_ASSERT_STAGE1(pmap);
5822 if (!PMAP_TRYLOCK(pmap)) {
5823 md_gen = m->md.pv_gen;
5824 pvh_gen = pvh->pv_gen;
5828 if (md_gen != m->md.pv_gen ||
5829 pvh_gen != pvh->pv_gen) {
5834 pte = pmap_pte_exists(pmap, pv->pv_va, 2, __func__);
5838 mask |= ATTR_S1_AP_RW_BIT;
5839 value |= ATTR_S1_AP(ATTR_S1_AP_RW);
5842 mask |= ATTR_AF | ATTR_DESCR_MASK;
5843 value |= ATTR_AF | L2_BLOCK;
5845 rv = (pmap_load(pte) & mask) == value;
5859 * Return whether or not the specified physical page was modified
5860 * in any physical maps.
5863 pmap_is_modified(vm_page_t m)
5866 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
5867 ("pmap_is_modified: page %p is not managed", m));
5870 * If the page is not busied then this check is racy.
5872 if (!pmap_page_is_write_mapped(m))
5874 return (pmap_page_test_mappings(m, FALSE, TRUE));
5878 * pmap_is_prefaultable:
5880 * Return whether or not the specified virtual address is eligible
5884 pmap_is_prefaultable(pmap_t pmap, vm_offset_t addr)
5892 * Return TRUE if and only if the L3 entry for the specified virtual
5893 * address is allocated but invalid.
5897 pde = pmap_pde(pmap, addr, &lvl);
5898 if (pde != NULL && lvl == 2) {
5899 pte = pmap_l2_to_l3(pde, addr);
5900 rv = pmap_load(pte) == 0;
5907 * pmap_is_referenced:
5909 * Return whether or not the specified physical page was referenced
5910 * in any physical maps.
5913 pmap_is_referenced(vm_page_t m)
5916 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
5917 ("pmap_is_referenced: page %p is not managed", m));
5918 return (pmap_page_test_mappings(m, TRUE, FALSE));
5922 * Clear the write and modified bits in each of the given page's mappings.
5925 pmap_remove_write(vm_page_t m)
5927 struct md_page *pvh;
5929 struct rwlock *lock;
5930 pv_entry_t next_pv, pv;
5931 pt_entry_t oldpte, *pte, set, clear, mask, val;
5933 int md_gen, pvh_gen;
5935 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
5936 ("pmap_remove_write: page %p is not managed", m));
5937 vm_page_assert_busied(m);
5939 if (!pmap_page_is_write_mapped(m))
5941 lock = VM_PAGE_TO_PV_LIST_LOCK(m);
5942 pvh = (m->flags & PG_FICTITIOUS) != 0 ? &pv_dummy : page_to_pvh(m);
5945 TAILQ_FOREACH_SAFE(pv, &pvh->pv_list, pv_next, next_pv) {
5947 PMAP_ASSERT_STAGE1(pmap);
5948 if (!PMAP_TRYLOCK(pmap)) {
5949 pvh_gen = pvh->pv_gen;
5953 if (pvh_gen != pvh->pv_gen) {
5959 pte = pmap_pte_exists(pmap, va, 2, __func__);
5960 if ((pmap_load(pte) & ATTR_SW_DBM) != 0)
5961 (void)pmap_demote_l2_locked(pmap, pte, va, &lock);
5962 KASSERT(lock == VM_PAGE_TO_PV_LIST_LOCK(m),
5963 ("inconsistent pv lock %p %p for page %p",
5964 lock, VM_PAGE_TO_PV_LIST_LOCK(m), m));
5967 TAILQ_FOREACH(pv, &m->md.pv_list, pv_next) {
5969 if (!PMAP_TRYLOCK(pmap)) {
5970 pvh_gen = pvh->pv_gen;
5971 md_gen = m->md.pv_gen;
5975 if (pvh_gen != pvh->pv_gen ||
5976 md_gen != m->md.pv_gen) {
5981 pte = pmap_pte_exists(pmap, pv->pv_va, 3, __func__);
5982 oldpte = pmap_load(pte);
5983 if ((oldpte & ATTR_SW_DBM) != 0) {
5984 if (pmap->pm_stage == PM_STAGE1) {
5985 set = ATTR_S1_AP_RW_BIT;
5987 mask = ATTR_S1_AP_RW_BIT;
5988 val = ATTR_S1_AP(ATTR_S1_AP_RW);
5991 clear = ATTR_S2_S2AP(ATTR_S2_S2AP_WRITE);
5992 mask = ATTR_S2_S2AP(ATTR_S2_S2AP_WRITE);
5993 val = ATTR_S2_S2AP(ATTR_S2_S2AP_WRITE);
5995 clear |= ATTR_SW_DBM;
5996 while (!atomic_fcmpset_64(pte, &oldpte,
5997 (oldpte | set) & ~clear))
6000 if ((oldpte & mask) == val)
6002 pmap_invalidate_page(pmap, pv->pv_va, true);
6007 vm_page_aflag_clear(m, PGA_WRITEABLE);
6011 * pmap_ts_referenced:
6013 * Return a count of reference bits for a page, clearing those bits.
6014 * It is not necessary for every reference bit to be cleared, but it
6015 * is necessary that 0 only be returned when there are truly no
6016 * reference bits set.
6018 * As an optimization, update the page's dirty field if a modified bit is
6019 * found while counting reference bits. This opportunistic update can be
6020 * performed at low cost and can eliminate the need for some future calls
6021 * to pmap_is_modified(). However, since this function stops after
6022 * finding PMAP_TS_REFERENCED_MAX reference bits, it may not detect some
6023 * dirty pages. Those dirty pages will only be detected by a future call
6024 * to pmap_is_modified().
6027 pmap_ts_referenced(vm_page_t m)
6029 struct md_page *pvh;
6032 struct rwlock *lock;
6033 pt_entry_t *pte, tpte;
6036 int cleared, md_gen, not_cleared, pvh_gen;
6037 struct spglist free;
6039 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
6040 ("pmap_ts_referenced: page %p is not managed", m));
6043 pa = VM_PAGE_TO_PHYS(m);
6044 lock = PHYS_TO_PV_LIST_LOCK(pa);
6045 pvh = (m->flags & PG_FICTITIOUS) != 0 ? &pv_dummy : page_to_pvh(m);
6049 if ((pvf = TAILQ_FIRST(&pvh->pv_list)) == NULL)
6050 goto small_mappings;
6056 if (!PMAP_TRYLOCK(pmap)) {
6057 pvh_gen = pvh->pv_gen;
6061 if (pvh_gen != pvh->pv_gen) {
6067 pte = pmap_pte_exists(pmap, va, 2, __func__);
6068 tpte = pmap_load(pte);
6069 if (pmap_pte_dirty(pmap, tpte)) {
6071 * Although "tpte" is mapping a 2MB page, because
6072 * this function is called at a 4KB page granularity,
6073 * we only update the 4KB page under test.
6077 if ((tpte & ATTR_AF) != 0) {
6079 * Since this reference bit is shared by 512 4KB pages,
6080 * it should not be cleared every time it is tested.
6081 * Apply a simple "hash" function on the physical page
6082 * number, the virtual superpage number, and the pmap
6083 * address to select one 4KB page out of the 512 on
6084 * which testing the reference bit will result in
6085 * clearing that reference bit. This function is
6086 * designed to avoid the selection of the same 4KB page
6087 * for every 2MB page mapping.
6089 * On demotion, a mapping that hasn't been referenced
6090 * is simply destroyed. To avoid the possibility of a
6091 * subsequent page fault on a demoted wired mapping,
6092 * always leave its reference bit set. Moreover,
6093 * since the superpage is wired, the current state of
6094 * its reference bit won't affect page replacement.
6096 if ((((pa >> PAGE_SHIFT) ^ (va >> L2_SHIFT) ^
6097 (uintptr_t)pmap) & (Ln_ENTRIES - 1)) == 0 &&
6098 (tpte & ATTR_SW_WIRED) == 0) {
6099 pmap_clear_bits(pte, ATTR_AF);
6100 pmap_invalidate_page(pmap, va, true);
6106 /* Rotate the PV list if it has more than one entry. */
6107 if (TAILQ_NEXT(pv, pv_next) != NULL) {
6108 TAILQ_REMOVE(&pvh->pv_list, pv, pv_next);
6109 TAILQ_INSERT_TAIL(&pvh->pv_list, pv, pv_next);
6112 if (cleared + not_cleared >= PMAP_TS_REFERENCED_MAX)
6114 } while ((pv = TAILQ_FIRST(&pvh->pv_list)) != pvf);
6116 if ((pvf = TAILQ_FIRST(&m->md.pv_list)) == NULL)
6123 if (!PMAP_TRYLOCK(pmap)) {
6124 pvh_gen = pvh->pv_gen;
6125 md_gen = m->md.pv_gen;
6129 if (pvh_gen != pvh->pv_gen || md_gen != m->md.pv_gen) {
6134 pte = pmap_pte_exists(pmap, pv->pv_va, 3, __func__);
6135 tpte = pmap_load(pte);
6136 if (pmap_pte_dirty(pmap, tpte))
6138 if ((tpte & ATTR_AF) != 0) {
6139 if ((tpte & ATTR_SW_WIRED) == 0) {
6140 pmap_clear_bits(pte, ATTR_AF);
6141 pmap_invalidate_page(pmap, pv->pv_va, true);
6147 /* Rotate the PV list if it has more than one entry. */
6148 if (TAILQ_NEXT(pv, pv_next) != NULL) {
6149 TAILQ_REMOVE(&m->md.pv_list, pv, pv_next);
6150 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_next);
6153 } while ((pv = TAILQ_FIRST(&m->md.pv_list)) != pvf && cleared +
6154 not_cleared < PMAP_TS_REFERENCED_MAX);
6157 vm_page_free_pages_toq(&free, true);
6158 return (cleared + not_cleared);
6162 * Apply the given advice to the specified range of addresses within the
6163 * given pmap. Depending on the advice, clear the referenced and/or
6164 * modified flags in each mapping and set the mapped page's dirty field.
6167 pmap_advise(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, int advice)
6169 struct rwlock *lock;
6170 vm_offset_t va, va_next;
6172 pd_entry_t *l0, *l1, *l2, oldl2;
6173 pt_entry_t *l3, oldl3;
6175 PMAP_ASSERT_STAGE1(pmap);
6177 if (advice != MADV_DONTNEED && advice != MADV_FREE)
6181 for (; sva < eva; sva = va_next) {
6182 l0 = pmap_l0(pmap, sva);
6183 if (pmap_load(l0) == 0) {
6184 va_next = (sva + L0_SIZE) & ~L0_OFFSET;
6190 va_next = (sva + L1_SIZE) & ~L1_OFFSET;
6193 l1 = pmap_l0_to_l1(l0, sva);
6194 if (pmap_load(l1) == 0)
6196 if ((pmap_load(l1) & ATTR_DESCR_MASK) == L1_BLOCK) {
6197 PMAP_ASSERT_L1_BLOCKS_SUPPORTED;
6201 va_next = (sva + L2_SIZE) & ~L2_OFFSET;
6204 l2 = pmap_l1_to_l2(l1, sva);
6205 oldl2 = pmap_load(l2);
6208 if ((oldl2 & ATTR_DESCR_MASK) == L2_BLOCK) {
6209 if ((oldl2 & ATTR_SW_MANAGED) == 0)
6212 if (!pmap_demote_l2_locked(pmap, l2, sva, &lock)) {
6217 * The 2MB page mapping was destroyed.
6223 * Unless the page mappings are wired, remove the
6224 * mapping to a single page so that a subsequent
6225 * access may repromote. Choosing the last page
6226 * within the address range [sva, min(va_next, eva))
6227 * generally results in more repromotions. Since the
6228 * underlying page table page is fully populated, this
6229 * removal never frees a page table page.
6231 if ((oldl2 & ATTR_SW_WIRED) == 0) {
6237 ("pmap_advise: no address gap"));
6238 l3 = pmap_l2_to_l3(l2, va);
6239 KASSERT(pmap_load(l3) != 0,
6240 ("pmap_advise: invalid PTE"));
6241 pmap_remove_l3(pmap, l3, va, pmap_load(l2),
6247 KASSERT((pmap_load(l2) & ATTR_DESCR_MASK) == L2_TABLE,
6248 ("pmap_advise: invalid L2 entry after demotion"));
6252 for (l3 = pmap_l2_to_l3(l2, sva); sva != va_next; l3++,
6254 oldl3 = pmap_load(l3);
6255 if ((oldl3 & (ATTR_SW_MANAGED | ATTR_DESCR_MASK)) !=
6256 (ATTR_SW_MANAGED | L3_PAGE))
6258 else if (pmap_pte_dirty(pmap, oldl3)) {
6259 if (advice == MADV_DONTNEED) {
6261 * Future calls to pmap_is_modified()
6262 * can be avoided by making the page
6265 m = PHYS_TO_VM_PAGE(oldl3 & ~ATTR_MASK);
6268 while (!atomic_fcmpset_long(l3, &oldl3,
6269 (oldl3 & ~ATTR_AF) |
6270 ATTR_S1_AP(ATTR_S1_AP_RO)))
6272 } else if ((oldl3 & ATTR_AF) != 0)
6273 pmap_clear_bits(l3, ATTR_AF);
6280 if (va != va_next) {
6281 pmap_s1_invalidate_range(pmap, va, sva, true);
6286 pmap_s1_invalidate_range(pmap, va, sva, true);
6292 * Clear the modify bits on the specified physical page.
6295 pmap_clear_modify(vm_page_t m)
6297 struct md_page *pvh;
6298 struct rwlock *lock;
6300 pv_entry_t next_pv, pv;
6301 pd_entry_t *l2, oldl2;
6302 pt_entry_t *l3, oldl3;
6304 int md_gen, pvh_gen;
6306 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
6307 ("pmap_clear_modify: page %p is not managed", m));
6308 vm_page_assert_busied(m);
6310 if (!pmap_page_is_write_mapped(m))
6312 pvh = (m->flags & PG_FICTITIOUS) != 0 ? &pv_dummy : page_to_pvh(m);
6313 lock = VM_PAGE_TO_PV_LIST_LOCK(m);
6316 TAILQ_FOREACH_SAFE(pv, &pvh->pv_list, pv_next, next_pv) {
6318 PMAP_ASSERT_STAGE1(pmap);
6319 if (!PMAP_TRYLOCK(pmap)) {
6320 pvh_gen = pvh->pv_gen;
6324 if (pvh_gen != pvh->pv_gen) {
6330 l2 = pmap_l2(pmap, va);
6331 oldl2 = pmap_load(l2);
6332 /* If oldl2 has ATTR_SW_DBM set, then it is also dirty. */
6333 if ((oldl2 & ATTR_SW_DBM) != 0 &&
6334 pmap_demote_l2_locked(pmap, l2, va, &lock) &&
6335 (oldl2 & ATTR_SW_WIRED) == 0) {
6337 * Write protect the mapping to a single page so that
6338 * a subsequent write access may repromote.
6340 va += VM_PAGE_TO_PHYS(m) - (oldl2 & ~ATTR_MASK);
6341 l3 = pmap_l2_to_l3(l2, va);
6342 oldl3 = pmap_load(l3);
6343 while (!atomic_fcmpset_long(l3, &oldl3,
6344 (oldl3 & ~ATTR_SW_DBM) | ATTR_S1_AP(ATTR_S1_AP_RO)))
6347 pmap_s1_invalidate_page(pmap, va, true);
6351 TAILQ_FOREACH(pv, &m->md.pv_list, pv_next) {
6353 PMAP_ASSERT_STAGE1(pmap);
6354 if (!PMAP_TRYLOCK(pmap)) {
6355 md_gen = m->md.pv_gen;
6356 pvh_gen = pvh->pv_gen;
6360 if (pvh_gen != pvh->pv_gen || md_gen != m->md.pv_gen) {
6365 l2 = pmap_l2(pmap, pv->pv_va);
6366 l3 = pmap_l2_to_l3(l2, pv->pv_va);
6367 oldl3 = pmap_load(l3);
6368 if ((oldl3 & (ATTR_S1_AP_RW_BIT | ATTR_SW_DBM)) == ATTR_SW_DBM){
6369 pmap_set_bits(l3, ATTR_S1_AP(ATTR_S1_AP_RO));
6370 pmap_s1_invalidate_page(pmap, pv->pv_va, true);
6378 pmap_mapbios(vm_paddr_t pa, vm_size_t size)
6380 struct pmap_preinit_mapping *ppim;
6381 vm_offset_t va, offset;
6384 int i, lvl, l2_blocks, free_l2_count, start_idx;
6386 if (!vm_initialized) {
6388 * No L3 ptables so map entire L2 blocks where start VA is:
6389 * preinit_map_va + start_idx * L2_SIZE
6390 * There may be duplicate mappings (multiple VA -> same PA) but
6391 * ARM64 dcache is always PIPT so that's acceptable.
6396 /* Calculate how many L2 blocks are needed for the mapping */
6397 l2_blocks = (roundup2(pa + size, L2_SIZE) -
6398 rounddown2(pa, L2_SIZE)) >> L2_SHIFT;
6400 offset = pa & L2_OFFSET;
6402 if (preinit_map_va == 0)
6405 /* Map 2MiB L2 blocks from reserved VA space */
6409 /* Find enough free contiguous VA space */
6410 for (i = 0; i < PMAP_PREINIT_MAPPING_COUNT; i++) {
6411 ppim = pmap_preinit_mapping + i;
6412 if (free_l2_count > 0 && ppim->pa != 0) {
6413 /* Not enough space here */
6419 if (ppim->pa == 0) {
6421 if (start_idx == -1)
6424 if (free_l2_count == l2_blocks)
6428 if (free_l2_count != l2_blocks)
6429 panic("%s: too many preinit mappings", __func__);
6431 va = preinit_map_va + (start_idx * L2_SIZE);
6432 for (i = start_idx; i < start_idx + l2_blocks; i++) {
6433 /* Mark entries as allocated */
6434 ppim = pmap_preinit_mapping + i;
6436 ppim->va = va + offset;
6441 pa = rounddown2(pa, L2_SIZE);
6442 for (i = 0; i < l2_blocks; i++) {
6443 pde = pmap_pde(kernel_pmap, va, &lvl);
6444 KASSERT(pde != NULL,
6445 ("pmap_mapbios: Invalid page entry, va: 0x%lx",
6448 ("pmap_mapbios: Invalid level %d", lvl));
6450 /* Insert L2_BLOCK */
6451 l2 = pmap_l1_to_l2(pde, va);
6453 pa | ATTR_DEFAULT | ATTR_S1_XN |
6454 ATTR_S1_IDX(VM_MEMATTR_WRITE_BACK) | L2_BLOCK);
6459 pmap_s1_invalidate_all(kernel_pmap);
6461 va = preinit_map_va + (start_idx * L2_SIZE);
6464 /* kva_alloc may be used to map the pages */
6465 offset = pa & PAGE_MASK;
6466 size = round_page(offset + size);
6468 va = kva_alloc(size);
6470 panic("%s: Couldn't allocate KVA", __func__);
6472 pde = pmap_pde(kernel_pmap, va, &lvl);
6473 KASSERT(lvl == 2, ("pmap_mapbios: Invalid level %d", lvl));
6475 /* L3 table is linked */
6476 va = trunc_page(va);
6477 pa = trunc_page(pa);
6478 pmap_kenter(va, size, pa, memory_mapping_mode(pa));
6481 return ((void *)(va + offset));
6485 pmap_unmapbios(void *p, vm_size_t size)
6487 struct pmap_preinit_mapping *ppim;
6488 vm_offset_t offset, tmpsize, va, va_trunc;
6491 int i, lvl, l2_blocks, block;
6494 va = (vm_offset_t)p;
6496 (roundup2(va + size, L2_SIZE) - rounddown2(va, L2_SIZE)) >> L2_SHIFT;
6497 KASSERT(l2_blocks > 0, ("pmap_unmapbios: invalid size %lx", size));
6499 /* Remove preinit mapping */
6500 preinit_map = false;
6502 for (i = 0; i < PMAP_PREINIT_MAPPING_COUNT; i++) {
6503 ppim = pmap_preinit_mapping + i;
6504 if (ppim->va == va) {
6505 KASSERT(ppim->size == size,
6506 ("pmap_unmapbios: size mismatch"));
6511 offset = block * L2_SIZE;
6512 va_trunc = rounddown2(va, L2_SIZE) + offset;
6514 /* Remove L2_BLOCK */
6515 pde = pmap_pde(kernel_pmap, va_trunc, &lvl);
6516 KASSERT(pde != NULL,
6517 ("pmap_unmapbios: Invalid page entry, va: 0x%lx",
6519 l2 = pmap_l1_to_l2(pde, va_trunc);
6522 if (block == (l2_blocks - 1))
6528 pmap_s1_invalidate_all(kernel_pmap);
6532 /* Unmap the pages reserved with kva_alloc. */
6533 if (vm_initialized) {
6534 offset = va & PAGE_MASK;
6535 size = round_page(offset + size);
6536 va = trunc_page(va);
6538 pde = pmap_pde(kernel_pmap, va, &lvl);
6539 KASSERT(pde != NULL,
6540 ("pmap_unmapbios: Invalid page entry, va: 0x%lx", va));
6541 KASSERT(lvl == 2, ("pmap_unmapbios: Invalid level %d", lvl));
6543 /* Unmap and invalidate the pages */
6544 for (tmpsize = 0; tmpsize < size; tmpsize += PAGE_SIZE)
6545 pmap_kremove(va + tmpsize);
6552 * Sets the memory attribute for the specified page.
6555 pmap_page_set_memattr(vm_page_t m, vm_memattr_t ma)
6558 m->md.pv_memattr = ma;
6561 * If "m" is a normal page, update its direct mapping. This update
6562 * can be relied upon to perform any cache operations that are
6563 * required for data coherence.
6565 if ((m->flags & PG_FICTITIOUS) == 0 &&
6566 pmap_change_attr(PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m)), PAGE_SIZE,
6567 m->md.pv_memattr) != 0)
6568 panic("memory attribute change on the direct map failed");
6572 * Changes the specified virtual address range's memory type to that given by
6573 * the parameter "mode". The specified virtual address range must be
6574 * completely contained within either the direct map or the kernel map. If
6575 * the virtual address range is contained within the kernel map, then the
6576 * memory type for each of the corresponding ranges of the direct map is also
6577 * changed. (The corresponding ranges of the direct map are those ranges that
6578 * map the same physical pages as the specified virtual address range.) These
6579 * changes to the direct map are necessary because Intel describes the
6580 * behavior of their processors as "undefined" if two or more mappings to the
6581 * same physical page have different memory types.
6583 * Returns zero if the change completed successfully, and either EINVAL or
6584 * ENOMEM if the change failed. Specifically, EINVAL is returned if some part
6585 * of the virtual address range was not mapped, and ENOMEM is returned if
6586 * there was insufficient memory available to complete the change. In the
6587 * latter case, the memory type may have been changed on some part of the
6588 * virtual address range or the direct map.
6591 pmap_change_attr(vm_offset_t va, vm_size_t size, int mode)
6595 PMAP_LOCK(kernel_pmap);
6596 error = pmap_change_props_locked(va, size, PROT_NONE, mode, false);
6597 PMAP_UNLOCK(kernel_pmap);
6602 * Changes the specified virtual address range's protections to those
6603 * specified by "prot". Like pmap_change_attr(), protections for aliases
6604 * in the direct map are updated as well. Protections on aliasing mappings may
6605 * be a subset of the requested protections; for example, mappings in the direct
6606 * map are never executable.
6609 pmap_change_prot(vm_offset_t va, vm_size_t size, vm_prot_t prot)
6613 /* Only supported within the kernel map. */
6614 if (va < VM_MIN_KERNEL_ADDRESS)
6617 PMAP_LOCK(kernel_pmap);
6618 error = pmap_change_props_locked(va, size, prot, -1, false);
6619 PMAP_UNLOCK(kernel_pmap);
6624 pmap_change_props_locked(vm_offset_t va, vm_size_t size, vm_prot_t prot,
6625 int mode, bool skip_unmapped)
6627 vm_offset_t base, offset, tmpva;
6630 pt_entry_t pte, *ptep, *newpte;
6631 pt_entry_t bits, mask;
6634 PMAP_LOCK_ASSERT(kernel_pmap, MA_OWNED);
6635 base = trunc_page(va);
6636 offset = va & PAGE_MASK;
6637 size = round_page(offset + size);
6639 if (!VIRT_IN_DMAP(base) &&
6640 !(base >= VM_MIN_KERNEL_ADDRESS && base < VM_MAX_KERNEL_ADDRESS))
6646 bits = ATTR_S1_IDX(mode);
6647 mask = ATTR_S1_IDX_MASK;
6648 if (mode == VM_MEMATTR_DEVICE) {
6653 if (prot != VM_PROT_NONE) {
6654 /* Don't mark the DMAP as executable. It never is on arm64. */
6655 if (VIRT_IN_DMAP(base)) {
6656 prot &= ~VM_PROT_EXECUTE;
6658 * XXX Mark the DMAP as writable for now. We rely
6659 * on this in ddb & dtrace to insert breakpoint
6662 prot |= VM_PROT_WRITE;
6665 if ((prot & VM_PROT_WRITE) == 0) {
6666 bits |= ATTR_S1_AP(ATTR_S1_AP_RO);
6668 if ((prot & VM_PROT_EXECUTE) == 0) {
6669 bits |= ATTR_S1_PXN;
6671 bits |= ATTR_S1_UXN;
6672 mask |= ATTR_S1_AP_MASK | ATTR_S1_XN;
6675 for (tmpva = base; tmpva < base + size; ) {
6676 ptep = pmap_pte(kernel_pmap, tmpva, &lvl);
6677 if (ptep == NULL && !skip_unmapped) {
6679 } else if ((ptep == NULL && skip_unmapped) ||
6680 (pmap_load(ptep) & mask) == bits) {
6682 * We already have the correct attribute or there
6683 * is no memory mapped at this address and we are
6684 * skipping unmapped memory.
6688 panic("Invalid DMAP table level: %d\n", lvl);
6690 tmpva = (tmpva & ~L1_OFFSET) + L1_SIZE;
6693 tmpva = (tmpva & ~L2_OFFSET) + L2_SIZE;
6700 /* We can't demote/promote this entry */
6701 MPASS((pmap_load(ptep) & ATTR_SW_NO_PROMOTE) == 0);
6704 * Split the entry to an level 3 table, then
6705 * set the new attribute.
6709 panic("Invalid DMAP table level: %d\n", lvl);
6711 PMAP_ASSERT_L1_BLOCKS_SUPPORTED;
6712 if ((tmpva & L1_OFFSET) == 0 &&
6713 (base + size - tmpva) >= L1_SIZE) {
6717 newpte = pmap_demote_l1(kernel_pmap, ptep,
6718 tmpva & ~L1_OFFSET);
6721 ptep = pmap_l1_to_l2(ptep, tmpva);
6724 if ((tmpva & L2_OFFSET) == 0 &&
6725 (base + size - tmpva) >= L2_SIZE) {
6729 newpte = pmap_demote_l2(kernel_pmap, ptep,
6733 ptep = pmap_l2_to_l3(ptep, tmpva);
6736 pte_size = PAGE_SIZE;
6740 /* Update the entry */
6741 pte = pmap_load(ptep);
6745 pmap_update_entry(kernel_pmap, ptep, pte, tmpva,
6748 pa = pte & ~ATTR_MASK;
6749 if (!VIRT_IN_DMAP(tmpva) && PHYS_IN_DMAP(pa)) {
6751 * Keep the DMAP memory in sync.
6753 rv = pmap_change_props_locked(
6754 PHYS_TO_DMAP(pa), pte_size,
6761 * If moving to a non-cacheable entry flush
6764 if (mode == VM_MEMATTR_UNCACHEABLE)
6765 cpu_dcache_wbinv_range(tmpva, pte_size);
6774 * Create an L2 table to map all addresses within an L1 mapping.
6777 pmap_demote_l1(pmap_t pmap, pt_entry_t *l1, vm_offset_t va)
6779 pt_entry_t *l2, newl2, oldl1;
6781 vm_paddr_t l2phys, phys;
6785 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
6786 oldl1 = pmap_load(l1);
6787 PMAP_ASSERT_L1_BLOCKS_SUPPORTED;
6788 KASSERT((oldl1 & ATTR_DESCR_MASK) == L1_BLOCK,
6789 ("pmap_demote_l1: Demoting a non-block entry"));
6790 KASSERT((va & L1_OFFSET) == 0,
6791 ("pmap_demote_l1: Invalid virtual address %#lx", va));
6792 KASSERT((oldl1 & ATTR_SW_MANAGED) == 0,
6793 ("pmap_demote_l1: Level 1 table shouldn't be managed"));
6794 KASSERT((oldl1 & ATTR_SW_NO_PROMOTE) == 0,
6795 ("pmap_demote_l1: Demoting entry with no-demote flag set"));
6798 if (va <= (vm_offset_t)l1 && va + L1_SIZE > (vm_offset_t)l1) {
6799 tmpl1 = kva_alloc(PAGE_SIZE);
6804 if ((ml2 = vm_page_alloc_noobj(VM_ALLOC_INTERRUPT | VM_ALLOC_WIRED)) ==
6806 CTR2(KTR_PMAP, "pmap_demote_l1: failure for va %#lx"
6807 " in pmap %p", va, pmap);
6812 l2phys = VM_PAGE_TO_PHYS(ml2);
6813 l2 = (pt_entry_t *)PHYS_TO_DMAP(l2phys);
6815 /* Address the range points at */
6816 phys = oldl1 & ~ATTR_MASK;
6817 /* The attributed from the old l1 table to be copied */
6818 newl2 = oldl1 & ATTR_MASK;
6820 /* Create the new entries */
6821 for (i = 0; i < Ln_ENTRIES; i++) {
6822 l2[i] = newl2 | phys;
6825 KASSERT(l2[0] == ((oldl1 & ~ATTR_DESCR_MASK) | L2_BLOCK),
6826 ("Invalid l2 page (%lx != %lx)", l2[0],
6827 (oldl1 & ~ATTR_DESCR_MASK) | L2_BLOCK));
6830 pmap_kenter(tmpl1, PAGE_SIZE,
6831 DMAP_TO_PHYS((vm_offset_t)l1) & ~L3_OFFSET,
6832 VM_MEMATTR_WRITE_BACK);
6833 l1 = (pt_entry_t *)(tmpl1 + ((vm_offset_t)l1 & PAGE_MASK));
6836 pmap_update_entry(pmap, l1, l2phys | L1_TABLE, va, PAGE_SIZE);
6840 pmap_kremove(tmpl1);
6841 kva_free(tmpl1, PAGE_SIZE);
6848 pmap_fill_l3(pt_entry_t *firstl3, pt_entry_t newl3)
6852 for (l3 = firstl3; l3 - firstl3 < Ln_ENTRIES; l3++) {
6859 pmap_demote_l2_abort(pmap_t pmap, vm_offset_t va, pt_entry_t *l2,
6860 struct rwlock **lockp)
6862 struct spglist free;
6865 (void)pmap_remove_l2(pmap, l2, va, pmap_load(pmap_l1(pmap, va)), &free,
6867 vm_page_free_pages_toq(&free, true);
6871 * Create an L3 table to map all addresses within an L2 mapping.
6874 pmap_demote_l2_locked(pmap_t pmap, pt_entry_t *l2, vm_offset_t va,
6875 struct rwlock **lockp)
6877 pt_entry_t *l3, newl3, oldl2;
6882 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
6883 PMAP_ASSERT_STAGE1(pmap);
6884 KASSERT(ADDR_IS_CANONICAL(va),
6885 ("%s: Address not in canonical form: %lx", __func__, va));
6888 oldl2 = pmap_load(l2);
6889 KASSERT((oldl2 & ATTR_DESCR_MASK) == L2_BLOCK,
6890 ("pmap_demote_l2: Demoting a non-block entry"));
6891 KASSERT((oldl2 & ATTR_SW_NO_PROMOTE) == 0,
6892 ("pmap_demote_l2: Demoting entry with no-demote flag set"));
6896 if (va <= (vm_offset_t)l2 && va + L2_SIZE > (vm_offset_t)l2) {
6897 tmpl2 = kva_alloc(PAGE_SIZE);
6903 * Invalidate the 2MB page mapping and return "failure" if the
6904 * mapping was never accessed.
6906 if ((oldl2 & ATTR_AF) == 0) {
6907 KASSERT((oldl2 & ATTR_SW_WIRED) == 0,
6908 ("pmap_demote_l2: a wired mapping is missing ATTR_AF"));
6909 pmap_demote_l2_abort(pmap, va, l2, lockp);
6910 CTR2(KTR_PMAP, "pmap_demote_l2: failure for va %#lx in pmap %p",
6915 if ((ml3 = pmap_remove_pt_page(pmap, va)) == NULL) {
6916 KASSERT((oldl2 & ATTR_SW_WIRED) == 0,
6917 ("pmap_demote_l2: page table page for a wired mapping"
6921 * If the page table page is missing and the mapping
6922 * is for a kernel address, the mapping must belong to
6923 * either the direct map or the early kernel memory.
6924 * Page table pages are preallocated for every other
6925 * part of the kernel address space, so the direct map
6926 * region and early kernel memory are the only parts of the
6927 * kernel address space that must be handled here.
6929 KASSERT(!ADDR_IS_KERNEL(va) || VIRT_IN_DMAP(va) ||
6930 (va >= VM_MIN_KERNEL_ADDRESS && va < kernel_vm_end),
6931 ("pmap_demote_l2: No saved mpte for va %#lx", va));
6934 * If the 2MB page mapping belongs to the direct map
6935 * region of the kernel's address space, then the page
6936 * allocation request specifies the highest possible
6937 * priority (VM_ALLOC_INTERRUPT). Otherwise, the
6938 * priority is normal.
6940 ml3 = vm_page_alloc_noobj(
6941 (VIRT_IN_DMAP(va) ? VM_ALLOC_INTERRUPT : 0) |
6945 * If the allocation of the new page table page fails,
6946 * invalidate the 2MB page mapping and return "failure".
6949 pmap_demote_l2_abort(pmap, va, l2, lockp);
6950 CTR2(KTR_PMAP, "pmap_demote_l2: failure for va %#lx"
6951 " in pmap %p", va, pmap);
6954 ml3->pindex = pmap_l2_pindex(va);
6956 if (!ADDR_IS_KERNEL(va)) {
6957 ml3->ref_count = NL3PG;
6958 pmap_resident_count_inc(pmap, 1);
6961 l3phys = VM_PAGE_TO_PHYS(ml3);
6962 l3 = (pt_entry_t *)PHYS_TO_DMAP(l3phys);
6963 newl3 = (oldl2 & ~ATTR_DESCR_MASK) | L3_PAGE;
6964 KASSERT((oldl2 & (ATTR_S1_AP_RW_BIT | ATTR_SW_DBM)) !=
6965 (ATTR_S1_AP(ATTR_S1_AP_RO) | ATTR_SW_DBM),
6966 ("pmap_demote_l2: L2 entry is writeable but not dirty"));
6969 * If the page table page is not leftover from an earlier promotion,
6970 * or the mapping attributes have changed, (re)initialize the L3 table.
6972 * When pmap_update_entry() clears the old L2 mapping, it (indirectly)
6973 * performs a dsb(). That dsb() ensures that the stores for filling
6974 * "l3" are visible before "l3" is added to the page table.
6976 if (ml3->valid == 0 || (l3[0] & ATTR_MASK) != (newl3 & ATTR_MASK))
6977 pmap_fill_l3(l3, newl3);
6980 * Map the temporary page so we don't lose access to the l2 table.
6983 pmap_kenter(tmpl2, PAGE_SIZE,
6984 DMAP_TO_PHYS((vm_offset_t)l2) & ~L3_OFFSET,
6985 VM_MEMATTR_WRITE_BACK);
6986 l2 = (pt_entry_t *)(tmpl2 + ((vm_offset_t)l2 & PAGE_MASK));
6990 * The spare PV entries must be reserved prior to demoting the
6991 * mapping, that is, prior to changing the PDE. Otherwise, the state
6992 * of the L2 and the PV lists will be inconsistent, which can result
6993 * in reclaim_pv_chunk() attempting to remove a PV entry from the
6994 * wrong PV list and pmap_pv_demote_l2() failing to find the expected
6995 * PV entry for the 2MB page mapping that is being demoted.
6997 if ((oldl2 & ATTR_SW_MANAGED) != 0)
6998 reserve_pv_entries(pmap, Ln_ENTRIES - 1, lockp);
7001 * Pass PAGE_SIZE so that a single TLB invalidation is performed on
7002 * the 2MB page mapping.
7004 pmap_update_entry(pmap, l2, l3phys | L2_TABLE, va, PAGE_SIZE);
7007 * Demote the PV entry.
7009 if ((oldl2 & ATTR_SW_MANAGED) != 0)
7010 pmap_pv_demote_l2(pmap, va, oldl2 & ~ATTR_MASK, lockp);
7012 atomic_add_long(&pmap_l2_demotions, 1);
7013 CTR3(KTR_PMAP, "pmap_demote_l2: success for va %#lx"
7014 " in pmap %p %lx", va, pmap, l3[0]);
7018 pmap_kremove(tmpl2);
7019 kva_free(tmpl2, PAGE_SIZE);
7027 pmap_demote_l2(pmap_t pmap, pt_entry_t *l2, vm_offset_t va)
7029 struct rwlock *lock;
7033 l3 = pmap_demote_l2_locked(pmap, l2, va, &lock);
7040 * Perform the pmap work for mincore(2). If the page is not both referenced and
7041 * modified by this pmap, returns its physical address so that the caller can
7042 * find other mappings.
7045 pmap_mincore(pmap_t pmap, vm_offset_t addr, vm_paddr_t *pap)
7047 pt_entry_t *pte, tpte;
7048 vm_paddr_t mask, pa;
7052 PMAP_ASSERT_STAGE1(pmap);
7054 pte = pmap_pte(pmap, addr, &lvl);
7056 tpte = pmap_load(pte);
7069 panic("pmap_mincore: invalid level %d", lvl);
7072 managed = (tpte & ATTR_SW_MANAGED) != 0;
7073 val = MINCORE_INCORE;
7075 val |= MINCORE_PSIND(3 - lvl);
7076 if ((managed && pmap_pte_dirty(pmap, tpte)) || (!managed &&
7077 (tpte & ATTR_S1_AP_RW_BIT) == ATTR_S1_AP(ATTR_S1_AP_RW)))
7078 val |= MINCORE_MODIFIED | MINCORE_MODIFIED_OTHER;
7079 if ((tpte & ATTR_AF) == ATTR_AF)
7080 val |= MINCORE_REFERENCED | MINCORE_REFERENCED_OTHER;
7082 pa = (tpte & ~ATTR_MASK) | (addr & mask);
7088 if ((val & (MINCORE_MODIFIED_OTHER | MINCORE_REFERENCED_OTHER)) !=
7089 (MINCORE_MODIFIED_OTHER | MINCORE_REFERENCED_OTHER) && managed) {
7097 * Garbage collect every ASID that is neither active on a processor nor
7101 pmap_reset_asid_set(pmap_t pmap)
7104 int asid, cpuid, epoch;
7105 struct asid_set *set;
7106 enum pmap_stage stage;
7108 set = pmap->pm_asid_set;
7109 stage = pmap->pm_stage;
7111 set = pmap->pm_asid_set;
7112 KASSERT(set != NULL, ("%s: NULL asid set", __func__));
7113 mtx_assert(&set->asid_set_mutex, MA_OWNED);
7116 * Ensure that the store to asid_epoch is globally visible before the
7117 * loads from pc_curpmap are performed.
7119 epoch = set->asid_epoch + 1;
7120 if (epoch == INT_MAX)
7122 set->asid_epoch = epoch;
7124 if (stage == PM_STAGE1) {
7125 __asm __volatile("tlbi vmalle1is");
7127 KASSERT(pmap_clean_stage2_tlbi != NULL,
7128 ("%s: Unset stage 2 tlb invalidation callback\n",
7130 pmap_clean_stage2_tlbi();
7133 bit_nclear(set->asid_set, ASID_FIRST_AVAILABLE,
7134 set->asid_set_size - 1);
7135 CPU_FOREACH(cpuid) {
7136 if (cpuid == curcpu)
7138 if (stage == PM_STAGE1) {
7139 curpmap = pcpu_find(cpuid)->pc_curpmap;
7140 PMAP_ASSERT_STAGE1(pmap);
7142 curpmap = pcpu_find(cpuid)->pc_curvmpmap;
7143 if (curpmap == NULL)
7145 PMAP_ASSERT_STAGE2(pmap);
7147 KASSERT(curpmap->pm_asid_set == set, ("Incorrect set"));
7148 asid = COOKIE_TO_ASID(curpmap->pm_cookie);
7151 bit_set(set->asid_set, asid);
7152 curpmap->pm_cookie = COOKIE_FROM(asid, epoch);
7157 * Allocate a new ASID for the specified pmap.
7160 pmap_alloc_asid(pmap_t pmap)
7162 struct asid_set *set;
7165 set = pmap->pm_asid_set;
7166 KASSERT(set != NULL, ("%s: NULL asid set", __func__));
7168 mtx_lock_spin(&set->asid_set_mutex);
7171 * While this processor was waiting to acquire the asid set mutex,
7172 * pmap_reset_asid_set() running on another processor might have
7173 * updated this pmap's cookie to the current epoch. In which case, we
7174 * don't need to allocate a new ASID.
7176 if (COOKIE_TO_EPOCH(pmap->pm_cookie) == set->asid_epoch)
7179 bit_ffc_at(set->asid_set, set->asid_next, set->asid_set_size,
7181 if (new_asid == -1) {
7182 bit_ffc_at(set->asid_set, ASID_FIRST_AVAILABLE,
7183 set->asid_next, &new_asid);
7184 if (new_asid == -1) {
7185 pmap_reset_asid_set(pmap);
7186 bit_ffc_at(set->asid_set, ASID_FIRST_AVAILABLE,
7187 set->asid_set_size, &new_asid);
7188 KASSERT(new_asid != -1, ("ASID allocation failure"));
7191 bit_set(set->asid_set, new_asid);
7192 set->asid_next = new_asid + 1;
7193 pmap->pm_cookie = COOKIE_FROM(new_asid, set->asid_epoch);
7195 mtx_unlock_spin(&set->asid_set_mutex);
7198 static uint64_t __read_mostly ttbr_flags;
7201 * Compute the value that should be stored in ttbr0 to activate the specified
7202 * pmap. This value may change from time to time.
7205 pmap_to_ttbr0(pmap_t pmap)
7209 ttbr = pmap->pm_ttbr;
7210 ttbr |= ASID_TO_OPERAND(COOKIE_TO_ASID(pmap->pm_cookie));
7217 pmap_set_cnp(void *arg)
7219 uint64_t ttbr0, ttbr1;
7222 cpuid = *(u_int *)arg;
7223 if (cpuid == curcpu) {
7225 * Set the flags while all CPUs are handling the
7226 * smp_rendezvous so will not call pmap_to_ttbr0. Any calls
7227 * to pmap_to_ttbr0 after this will have the CnP flag set.
7228 * The dsb after invalidating the TLB will act as a barrier
7229 * to ensure all CPUs can observe this change.
7231 ttbr_flags |= TTBR_CnP;
7234 ttbr0 = READ_SPECIALREG(ttbr0_el1);
7237 ttbr1 = READ_SPECIALREG(ttbr1_el1);
7240 /* Update ttbr{0,1}_el1 with the CnP flag */
7241 WRITE_SPECIALREG(ttbr0_el1, ttbr0);
7242 WRITE_SPECIALREG(ttbr1_el1, ttbr1);
7244 __asm __volatile("tlbi vmalle1is");
7250 * Defer enabling CnP until we have read the ID registers to know if it's
7251 * supported on all CPUs.
7254 pmap_init_cnp(void *dummy __unused)
7259 if (!get_kernel_reg(ID_AA64MMFR2_EL1, ®))
7262 if (ID_AA64MMFR2_CnP_VAL(reg) != ID_AA64MMFR2_CnP_NONE) {
7264 printf("Enabling CnP\n");
7266 smp_rendezvous(NULL, pmap_set_cnp, NULL, &cpuid);
7270 SYSINIT(pmap_init_cnp, SI_SUB_SMP, SI_ORDER_ANY, pmap_init_cnp, NULL);
7273 pmap_activate_int(pmap_t pmap)
7275 struct asid_set *set;
7278 KASSERT(PCPU_GET(curpmap) != NULL, ("no active pmap"));
7279 KASSERT(pmap != kernel_pmap, ("kernel pmap activation"));
7281 if ((pmap->pm_stage == PM_STAGE1 && pmap == PCPU_GET(curpmap)) ||
7282 (pmap->pm_stage == PM_STAGE2 && pmap == PCPU_GET(curvmpmap))) {
7284 * Handle the possibility that the old thread was preempted
7285 * after an "ic" or "tlbi" instruction but before it performed
7286 * a "dsb" instruction. If the old thread migrates to a new
7287 * processor, its completion of a "dsb" instruction on that
7288 * new processor does not guarantee that the "ic" or "tlbi"
7289 * instructions performed on the old processor have completed.
7295 set = pmap->pm_asid_set;
7296 KASSERT(set != NULL, ("%s: NULL asid set", __func__));
7299 * Ensure that the store to curpmap is globally visible before the
7300 * load from asid_epoch is performed.
7302 if (pmap->pm_stage == PM_STAGE1)
7303 PCPU_SET(curpmap, pmap);
7305 PCPU_SET(curvmpmap, pmap);
7307 epoch = COOKIE_TO_EPOCH(pmap->pm_cookie);
7308 if (epoch >= 0 && epoch != set->asid_epoch)
7309 pmap_alloc_asid(pmap);
7311 if (pmap->pm_stage == PM_STAGE1) {
7312 set_ttbr0(pmap_to_ttbr0(pmap));
7313 if (PCPU_GET(bcast_tlbi_workaround) != 0)
7314 invalidate_local_icache();
7320 pmap_activate_vm(pmap_t pmap)
7323 PMAP_ASSERT_STAGE2(pmap);
7325 (void)pmap_activate_int(pmap);
7329 pmap_activate(struct thread *td)
7333 pmap = vmspace_pmap(td->td_proc->p_vmspace);
7334 PMAP_ASSERT_STAGE1(pmap);
7336 (void)pmap_activate_int(pmap);
7341 * Activate the thread we are switching to.
7342 * To simplify the assembly in cpu_throw return the new threads pcb.
7345 pmap_switch(struct thread *new)
7347 pcpu_bp_harden bp_harden;
7350 /* Store the new curthread */
7351 PCPU_SET(curthread, new);
7353 /* And the new pcb */
7355 PCPU_SET(curpcb, pcb);
7358 * TODO: We may need to flush the cache here if switching
7359 * to a user process.
7362 if (pmap_activate_int(vmspace_pmap(new->td_proc->p_vmspace))) {
7364 * Stop userspace from training the branch predictor against
7365 * other processes. This will call into a CPU specific
7366 * function that clears the branch predictor state.
7368 bp_harden = PCPU_GET(bp_harden);
7369 if (bp_harden != NULL)
7377 pmap_sync_icache(pmap_t pmap, vm_offset_t va, vm_size_t sz)
7380 PMAP_ASSERT_STAGE1(pmap);
7381 KASSERT(ADDR_IS_CANONICAL(va),
7382 ("%s: Address not in canonical form: %lx", __func__, va));
7384 if (ADDR_IS_KERNEL(va)) {
7385 cpu_icache_sync_range(va, sz);
7390 /* Find the length of data in this page to flush */
7391 offset = va & PAGE_MASK;
7392 len = imin(PAGE_SIZE - offset, sz);
7395 /* Extract the physical address & find it in the DMAP */
7396 pa = pmap_extract(pmap, va);
7398 cpu_icache_sync_range(PHYS_TO_DMAP(pa), len);
7400 /* Move to the next page */
7403 /* Set the length for the next iteration */
7404 len = imin(PAGE_SIZE, sz);
7410 pmap_stage2_fault(pmap_t pmap, uint64_t esr, uint64_t far)
7413 pt_entry_t *ptep, pte;
7416 PMAP_ASSERT_STAGE2(pmap);
7419 /* Data and insn aborts use same encoding for FSC field. */
7420 dfsc = esr & ISS_DATA_DFSC_MASK;
7422 case ISS_DATA_DFSC_TF_L0:
7423 case ISS_DATA_DFSC_TF_L1:
7424 case ISS_DATA_DFSC_TF_L2:
7425 case ISS_DATA_DFSC_TF_L3:
7427 pdep = pmap_pde(pmap, far, &lvl);
7428 if (pdep == NULL || lvl != (dfsc - ISS_DATA_DFSC_TF_L1)) {
7435 ptep = pmap_l0_to_l1(pdep, far);
7438 ptep = pmap_l1_to_l2(pdep, far);
7441 ptep = pmap_l2_to_l3(pdep, far);
7444 panic("%s: Invalid pde level %d", __func__,lvl);
7448 case ISS_DATA_DFSC_AFF_L1:
7449 case ISS_DATA_DFSC_AFF_L2:
7450 case ISS_DATA_DFSC_AFF_L3:
7452 ptep = pmap_pte(pmap, far, &lvl);
7454 if (ptep != NULL && (pte = pmap_load(ptep)) != 0) {
7456 pmap_invalidate_vpipt_icache();
7459 * If accessing an executable page invalidate
7460 * the I-cache so it will be valid when we
7461 * continue execution in the guest. The D-cache
7462 * is assumed to already be clean to the Point
7465 if ((pte & ATTR_S2_XN_MASK) !=
7466 ATTR_S2_XN(ATTR_S2_XN_NONE)) {
7467 invalidate_icache();
7470 pmap_set_bits(ptep, ATTR_AF | ATTR_DESCR_VALID);
7481 pmap_fault(pmap_t pmap, uint64_t esr, uint64_t far)
7483 pt_entry_t pte, *ptep;
7490 ec = ESR_ELx_EXCEPTION(esr);
7492 case EXCP_INSN_ABORT_L:
7493 case EXCP_INSN_ABORT:
7494 case EXCP_DATA_ABORT_L:
7495 case EXCP_DATA_ABORT:
7501 if (pmap->pm_stage == PM_STAGE2)
7502 return (pmap_stage2_fault(pmap, esr, far));
7504 /* Data and insn aborts use same encoding for FSC field. */
7505 switch (esr & ISS_DATA_DFSC_MASK) {
7506 case ISS_DATA_DFSC_AFF_L1:
7507 case ISS_DATA_DFSC_AFF_L2:
7508 case ISS_DATA_DFSC_AFF_L3:
7510 ptep = pmap_pte(pmap, far, &lvl);
7512 pmap_set_bits(ptep, ATTR_AF);
7515 * XXXMJ as an optimization we could mark the entry
7516 * dirty if this is a write fault.
7521 case ISS_DATA_DFSC_PF_L1:
7522 case ISS_DATA_DFSC_PF_L2:
7523 case ISS_DATA_DFSC_PF_L3:
7524 if ((ec != EXCP_DATA_ABORT_L && ec != EXCP_DATA_ABORT) ||
7525 (esr & ISS_DATA_WnR) == 0)
7528 ptep = pmap_pte(pmap, far, &lvl);
7530 ((pte = pmap_load(ptep)) & ATTR_SW_DBM) != 0) {
7531 if ((pte & ATTR_S1_AP_RW_BIT) ==
7532 ATTR_S1_AP(ATTR_S1_AP_RO)) {
7533 pmap_clear_bits(ptep, ATTR_S1_AP_RW_BIT);
7534 pmap_s1_invalidate_page(pmap, far, true);
7540 case ISS_DATA_DFSC_TF_L0:
7541 case ISS_DATA_DFSC_TF_L1:
7542 case ISS_DATA_DFSC_TF_L2:
7543 case ISS_DATA_DFSC_TF_L3:
7545 * Retry the translation. A break-before-make sequence can
7546 * produce a transient fault.
7548 if (pmap == kernel_pmap) {
7550 * The translation fault may have occurred within a
7551 * critical section. Therefore, we must check the
7552 * address without acquiring the kernel pmap's lock.
7554 if (pmap_klookup(far, NULL))
7558 /* Ask the MMU to check the address. */
7559 intr = intr_disable();
7560 par = arm64_address_translate_s1e0r(far);
7565 * If the translation was successful, then we can
7566 * return success to the trap handler.
7568 if (PAR_SUCCESS(par))
7578 * Increase the starting virtual address of the given mapping if a
7579 * different alignment might result in more superpage mappings.
7582 pmap_align_superpage(vm_object_t object, vm_ooffset_t offset,
7583 vm_offset_t *addr, vm_size_t size)
7585 vm_offset_t superpage_offset;
7589 if (object != NULL && (object->flags & OBJ_COLORED) != 0)
7590 offset += ptoa(object->pg_color);
7591 superpage_offset = offset & L2_OFFSET;
7592 if (size - ((L2_SIZE - superpage_offset) & L2_OFFSET) < L2_SIZE ||
7593 (*addr & L2_OFFSET) == superpage_offset)
7595 if ((*addr & L2_OFFSET) < superpage_offset)
7596 *addr = (*addr & ~L2_OFFSET) + superpage_offset;
7598 *addr = ((*addr + L2_OFFSET) & ~L2_OFFSET) + superpage_offset;
7602 * Get the kernel virtual address of a set of physical pages. If there are
7603 * physical addresses not covered by the DMAP perform a transient mapping
7604 * that will be removed when calling pmap_unmap_io_transient.
7606 * \param page The pages the caller wishes to obtain the virtual
7607 * address on the kernel memory map.
7608 * \param vaddr On return contains the kernel virtual memory address
7609 * of the pages passed in the page parameter.
7610 * \param count Number of pages passed in.
7611 * \param can_fault TRUE if the thread using the mapped pages can take
7612 * page faults, FALSE otherwise.
7614 * \returns TRUE if the caller must call pmap_unmap_io_transient when
7615 * finished or FALSE otherwise.
7619 pmap_map_io_transient(vm_page_t page[], vm_offset_t vaddr[], int count,
7620 boolean_t can_fault)
7623 boolean_t needs_mapping;
7624 int error __diagused, i;
7627 * Allocate any KVA space that we need, this is done in a separate
7628 * loop to prevent calling vmem_alloc while pinned.
7630 needs_mapping = FALSE;
7631 for (i = 0; i < count; i++) {
7632 paddr = VM_PAGE_TO_PHYS(page[i]);
7633 if (__predict_false(!PHYS_IN_DMAP(paddr))) {
7634 error = vmem_alloc(kernel_arena, PAGE_SIZE,
7635 M_BESTFIT | M_WAITOK, &vaddr[i]);
7636 KASSERT(error == 0, ("vmem_alloc failed: %d", error));
7637 needs_mapping = TRUE;
7639 vaddr[i] = PHYS_TO_DMAP(paddr);
7643 /* Exit early if everything is covered by the DMAP */
7649 for (i = 0; i < count; i++) {
7650 paddr = VM_PAGE_TO_PHYS(page[i]);
7651 if (!PHYS_IN_DMAP(paddr)) {
7653 "pmap_map_io_transient: TODO: Map out of DMAP data");
7657 return (needs_mapping);
7661 pmap_unmap_io_transient(vm_page_t page[], vm_offset_t vaddr[], int count,
7662 boolean_t can_fault)
7669 for (i = 0; i < count; i++) {
7670 paddr = VM_PAGE_TO_PHYS(page[i]);
7671 if (!PHYS_IN_DMAP(paddr)) {
7672 panic("ARM64TODO: pmap_unmap_io_transient: Unmap data");
7678 pmap_is_valid_memattr(pmap_t pmap __unused, vm_memattr_t mode)
7681 return (mode >= VM_MEMATTR_DEVICE && mode <= VM_MEMATTR_WRITE_THROUGH);
7685 static vm_paddr_t pmap_san_early_kernstart;
7686 static pd_entry_t *pmap_san_early_l2;
7688 void __nosanitizeaddress
7689 pmap_san_bootstrap(struct arm64_bootparams *abp)
7692 pmap_san_early_kernstart = KERNBASE - abp->kern_delta;
7693 kasan_init_early(abp->kern_stack, KSTACK_PAGES * PAGE_SIZE);
7696 #define SAN_BOOTSTRAP_L2_SIZE (1 * L2_SIZE)
7697 #define SAN_BOOTSTRAP_SIZE (2 * PAGE_SIZE)
7698 static vm_offset_t __nosanitizeaddress
7699 pmap_san_enter_bootstrap_alloc_l2(void)
7701 static uint8_t bootstrap_data[SAN_BOOTSTRAP_L2_SIZE] __aligned(L2_SIZE);
7702 static size_t offset = 0;
7705 if (offset + L2_SIZE > sizeof(bootstrap_data)) {
7706 panic("%s: out of memory for the bootstrap shadow map L2 entries",
7710 addr = (uintptr_t)&bootstrap_data[offset];
7716 * SAN L1 + L2 pages, maybe L3 entries later?
7718 static vm_offset_t __nosanitizeaddress
7719 pmap_san_enter_bootstrap_alloc_pages(int npages)
7721 static uint8_t bootstrap_data[SAN_BOOTSTRAP_SIZE] __aligned(PAGE_SIZE);
7722 static size_t offset = 0;
7725 if (offset + (npages * PAGE_SIZE) > sizeof(bootstrap_data)) {
7726 panic("%s: out of memory for the bootstrap shadow map",
7730 addr = (uintptr_t)&bootstrap_data[offset];
7731 offset += (npages * PAGE_SIZE);
7735 static void __nosanitizeaddress
7736 pmap_san_enter_bootstrap(void)
7738 vm_offset_t freemempos;
7741 freemempos = pmap_san_enter_bootstrap_alloc_pages(2);
7742 bs_state.freemempos = freemempos;
7743 bs_state.va = KASAN_MIN_ADDRESS;
7744 pmap_bootstrap_l1_table(&bs_state);
7745 pmap_san_early_l2 = bs_state.l2;
7749 pmap_san_enter_alloc_l3(void)
7753 m = vm_page_alloc_noobj(VM_ALLOC_INTERRUPT | VM_ALLOC_WIRED |
7756 panic("%s: no memory to grow shadow map", __func__);
7761 pmap_san_enter_alloc_l2(void)
7763 return (vm_page_alloc_noobj_contig(VM_ALLOC_WIRED | VM_ALLOC_ZERO,
7764 Ln_ENTRIES, 0, ~0ul, L2_SIZE, 0, VM_MEMATTR_DEFAULT));
7767 void __nosanitizeaddress
7768 pmap_san_enter(vm_offset_t va)
7770 pd_entry_t *l1, *l2;
7774 if (virtual_avail == 0) {
7779 /* Temporary shadow map prior to pmap_bootstrap(). */
7780 first = pmap_san_early_l2 == NULL;
7782 pmap_san_enter_bootstrap();
7784 l2 = pmap_san_early_l2;
7785 slot = pmap_l2_index(va);
7787 if ((pmap_load(&l2[slot]) & ATTR_DESCR_VALID) == 0) {
7789 block = pmap_san_enter_bootstrap_alloc_l2();
7790 pmap_store(&l2[slot], pmap_early_vtophys(block) |
7791 PMAP_SAN_PTE_BITS | L2_BLOCK);
7798 mtx_assert(&kernel_map->system_mtx, MA_OWNED);
7799 l1 = pmap_l1(kernel_pmap, va);
7801 if ((pmap_load(l1) & ATTR_DESCR_VALID) == 0) {
7802 m = pmap_san_enter_alloc_l3();
7803 pmap_store(l1, (VM_PAGE_TO_PHYS(m) & ~Ln_TABLE_MASK) |
7806 l2 = pmap_l1_to_l2(l1, va);
7807 if ((pmap_load(l2) & ATTR_DESCR_VALID) == 0) {
7808 m = pmap_san_enter_alloc_l2();
7810 pmap_store(l2, VM_PAGE_TO_PHYS(m) | PMAP_SAN_PTE_BITS |
7813 m = pmap_san_enter_alloc_l3();
7814 pmap_store(l2, VM_PAGE_TO_PHYS(m) | L2_TABLE);
7818 if ((pmap_load(l2) & ATTR_DESCR_MASK) == L2_BLOCK)
7820 l3 = pmap_l2_to_l3(l2, va);
7821 if ((pmap_load(l3) & ATTR_DESCR_VALID) != 0)
7823 m = pmap_san_enter_alloc_l3();
7824 pmap_store(l3, VM_PAGE_TO_PHYS(m) | PMAP_SAN_PTE_BITS | L3_PAGE);
7830 * Track a range of the kernel's virtual address space that is contiguous
7831 * in various mapping attributes.
7833 struct pmap_kernel_map_range {
7843 sysctl_kmaps_dump(struct sbuf *sb, struct pmap_kernel_map_range *range,
7849 if (eva <= range->sva)
7852 index = range->attrs & ATTR_S1_IDX_MASK;
7854 case ATTR_S1_IDX(VM_MEMATTR_DEVICE_NP):
7857 case ATTR_S1_IDX(VM_MEMATTR_DEVICE):
7860 case ATTR_S1_IDX(VM_MEMATTR_UNCACHEABLE):
7863 case ATTR_S1_IDX(VM_MEMATTR_WRITE_BACK):
7866 case ATTR_S1_IDX(VM_MEMATTR_WRITE_THROUGH):
7871 "%s: unknown memory type %x for range 0x%016lx-0x%016lx\n",
7872 __func__, index, range->sva, eva);
7877 sbuf_printf(sb, "0x%016lx-0x%016lx r%c%c%c%c %6s %d %d %d %d\n",
7879 (range->attrs & ATTR_S1_AP_RW_BIT) == ATTR_S1_AP_RW ? 'w' : '-',
7880 (range->attrs & ATTR_S1_PXN) != 0 ? '-' : 'x',
7881 (range->attrs & ATTR_S1_UXN) != 0 ? '-' : 'X',
7882 (range->attrs & ATTR_S1_AP(ATTR_S1_AP_USER)) != 0 ? 'u' : 's',
7883 mode, range->l1blocks, range->l2blocks, range->l3contig,
7886 /* Reset to sentinel value. */
7887 range->sva = 0xfffffffffffffffful;
7891 * Determine whether the attributes specified by a page table entry match those
7892 * being tracked by the current range.
7895 sysctl_kmaps_match(struct pmap_kernel_map_range *range, pt_entry_t attrs)
7898 return (range->attrs == attrs);
7902 sysctl_kmaps_reinit(struct pmap_kernel_map_range *range, vm_offset_t va,
7906 memset(range, 0, sizeof(*range));
7908 range->attrs = attrs;
7911 /* Get the block/page attributes that correspond to the table attributes */
7913 sysctl_kmaps_table_attrs(pd_entry_t table)
7918 if ((table & TATTR_UXN_TABLE) != 0)
7919 attrs |= ATTR_S1_UXN;
7920 if ((table & TATTR_PXN_TABLE) != 0)
7921 attrs |= ATTR_S1_PXN;
7922 if ((table & TATTR_AP_TABLE_RO) != 0)
7923 attrs |= ATTR_S1_AP(ATTR_S1_AP_RO);
7928 /* Read the block/page attributes we care about */
7930 sysctl_kmaps_block_attrs(pt_entry_t block)
7932 return (block & (ATTR_S1_AP_MASK | ATTR_S1_XN | ATTR_S1_IDX_MASK));
7936 * Given a leaf PTE, derive the mapping's attributes. If they do not match
7937 * those of the current run, dump the address range and its attributes, and
7941 sysctl_kmaps_check(struct sbuf *sb, struct pmap_kernel_map_range *range,
7942 vm_offset_t va, pd_entry_t l0e, pd_entry_t l1e, pd_entry_t l2e,
7947 attrs = sysctl_kmaps_table_attrs(l0e);
7949 if ((l1e & ATTR_DESCR_TYPE_MASK) == ATTR_DESCR_TYPE_BLOCK) {
7950 attrs |= sysctl_kmaps_block_attrs(l1e);
7953 attrs |= sysctl_kmaps_table_attrs(l1e);
7955 if ((l2e & ATTR_DESCR_TYPE_MASK) == ATTR_DESCR_TYPE_BLOCK) {
7956 attrs |= sysctl_kmaps_block_attrs(l2e);
7959 attrs |= sysctl_kmaps_table_attrs(l2e);
7960 attrs |= sysctl_kmaps_block_attrs(l3e);
7963 if (range->sva > va || !sysctl_kmaps_match(range, attrs)) {
7964 sysctl_kmaps_dump(sb, range, va);
7965 sysctl_kmaps_reinit(range, va, attrs);
7970 sysctl_kmaps(SYSCTL_HANDLER_ARGS)
7972 struct pmap_kernel_map_range range;
7973 struct sbuf sbuf, *sb;
7974 pd_entry_t l0e, *l1, l1e, *l2, l2e;
7975 pt_entry_t *l3, l3e;
7978 int error, i, j, k, l;
7980 error = sysctl_wire_old_buffer(req, 0);
7984 sbuf_new_for_sysctl(sb, NULL, PAGE_SIZE, req);
7986 /* Sentinel value. */
7987 range.sva = 0xfffffffffffffffful;
7990 * Iterate over the kernel page tables without holding the kernel pmap
7991 * lock. Kernel page table pages are never freed, so at worst we will
7992 * observe inconsistencies in the output.
7994 for (sva = 0xffff000000000000ul, i = pmap_l0_index(sva); i < Ln_ENTRIES;
7996 if (i == pmap_l0_index(DMAP_MIN_ADDRESS))
7997 sbuf_printf(sb, "\nDirect map:\n");
7998 else if (i == pmap_l0_index(VM_MIN_KERNEL_ADDRESS))
7999 sbuf_printf(sb, "\nKernel map:\n");
8001 else if (i == pmap_l0_index(KASAN_MIN_ADDRESS))
8002 sbuf_printf(sb, "\nKASAN shadow map:\n");
8005 l0e = kernel_pmap->pm_l0[i];
8006 if ((l0e & ATTR_DESCR_VALID) == 0) {
8007 sysctl_kmaps_dump(sb, &range, sva);
8011 pa = l0e & ~ATTR_MASK;
8012 l1 = (pd_entry_t *)PHYS_TO_DMAP(pa);
8014 for (j = pmap_l1_index(sva); j < Ln_ENTRIES; j++) {
8016 if ((l1e & ATTR_DESCR_VALID) == 0) {
8017 sysctl_kmaps_dump(sb, &range, sva);
8021 if ((l1e & ATTR_DESCR_MASK) == L1_BLOCK) {
8022 PMAP_ASSERT_L1_BLOCKS_SUPPORTED;
8023 sysctl_kmaps_check(sb, &range, sva, l0e, l1e,
8029 pa = l1e & ~ATTR_MASK;
8030 l2 = (pd_entry_t *)PHYS_TO_DMAP(pa);
8032 for (k = pmap_l2_index(sva); k < Ln_ENTRIES; k++) {
8034 if ((l2e & ATTR_DESCR_VALID) == 0) {
8035 sysctl_kmaps_dump(sb, &range, sva);
8039 if ((l2e & ATTR_DESCR_MASK) == L2_BLOCK) {
8040 sysctl_kmaps_check(sb, &range, sva,
8046 pa = l2e & ~ATTR_MASK;
8047 l3 = (pt_entry_t *)PHYS_TO_DMAP(pa);
8049 for (l = pmap_l3_index(sva); l < Ln_ENTRIES;
8050 l++, sva += L3_SIZE) {
8052 if ((l3e & ATTR_DESCR_VALID) == 0) {
8053 sysctl_kmaps_dump(sb, &range,
8057 sysctl_kmaps_check(sb, &range, sva,
8058 l0e, l1e, l2e, l3e);
8059 if ((l3e & ATTR_CONTIGUOUS) != 0)
8060 range.l3contig += l % 16 == 0 ?
8069 error = sbuf_finish(sb);
8073 SYSCTL_OID(_vm_pmap, OID_AUTO, kernel_maps,
8074 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE | CTLFLAG_SKIP,
8075 NULL, 0, sysctl_kmaps, "A",
8076 "Dump kernel address layout");