2 * Copyright (c) 1991 Regents of the University of California.
4 * Copyright (c) 1994 John S. Dyson
6 * Copyright (c) 1994 David Greenman
8 * Copyright (c) 2003 Peter Wemm
10 * Copyright (c) 2005-2010 Alan L. Cox <alc@cs.rice.edu>
11 * All rights reserved.
12 * Copyright (c) 2014 Andrew Turner
13 * All rights reserved.
14 * Copyright (c) 2014-2016 The FreeBSD Foundation
15 * All rights reserved.
17 * This code is derived from software contributed to Berkeley by
18 * the Systems Programming Group of the University of Utah Computer
19 * Science Department and William Jolitz of UUNET Technologies Inc.
21 * This software was developed by Andrew Turner under sponsorship from
22 * the FreeBSD Foundation.
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
27 * 1. Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * 2. Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in the
31 * documentation and/or other materials provided with the distribution.
32 * 3. All advertising materials mentioning features or use of this software
33 * must display the following acknowledgement:
34 * This product includes software developed by the University of
35 * California, Berkeley and its contributors.
36 * 4. Neither the name of the University nor the names of its contributors
37 * may be used to endorse or promote products derived from this software
38 * without specific prior written permission.
40 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * from: @(#)pmap.c 7.7 (Berkeley) 5/12/91
55 * Copyright (c) 2003 Networks Associates Technology, Inc.
56 * All rights reserved.
58 * This software was developed for the FreeBSD Project by Jake Burkholder,
59 * Safeport Network Services, and Network Associates Laboratories, the
60 * Security Research Division of Network Associates, Inc. under
61 * DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA
62 * CHATS research program.
64 * Redistribution and use in source and binary forms, with or without
65 * modification, are permitted provided that the following conditions
67 * 1. Redistributions of source code must retain the above copyright
68 * notice, this list of conditions and the following disclaimer.
69 * 2. Redistributions in binary form must reproduce the above copyright
70 * notice, this list of conditions and the following disclaimer in the
71 * documentation and/or other materials provided with the distribution.
73 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
74 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
75 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
76 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
77 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
78 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
79 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
80 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
81 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
82 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
86 #include <sys/cdefs.h>
87 __FBSDID("$FreeBSD$");
90 * Manages physical address maps.
92 * Since the information managed by this module is
93 * also stored by the logical address mapping module,
94 * this module may throw away valid virtual-to-physical
95 * mappings at almost any time. However, invalidations
96 * of virtual-to-physical mappings must be done as
99 * In order to cope with hardware architectures which
100 * make virtual-to-physical map invalidates expensive,
101 * this module may delay invalidate or reduced protection
102 * operations until such time as they are actually
103 * necessary. This module is given full information as
104 * to which processors are currently using which maps,
105 * and to when physical maps must be made correct.
110 #include <sys/param.h>
111 #include <sys/bitstring.h>
113 #include <sys/systm.h>
114 #include <sys/kernel.h>
116 #include <sys/lock.h>
117 #include <sys/malloc.h>
118 #include <sys/mman.h>
119 #include <sys/msgbuf.h>
120 #include <sys/mutex.h>
121 #include <sys/proc.h>
122 #include <sys/rwlock.h>
124 #include <sys/vmem.h>
125 #include <sys/vmmeter.h>
126 #include <sys/sched.h>
127 #include <sys/sysctl.h>
128 #include <sys/_unrhdr.h>
132 #include <vm/vm_param.h>
133 #include <vm/vm_kern.h>
134 #include <vm/vm_page.h>
135 #include <vm/vm_map.h>
136 #include <vm/vm_object.h>
137 #include <vm/vm_extern.h>
138 #include <vm/vm_pageout.h>
139 #include <vm/vm_pager.h>
140 #include <vm/vm_phys.h>
141 #include <vm/vm_radix.h>
142 #include <vm/vm_reserv.h>
145 #include <machine/machdep.h>
146 #include <machine/md_var.h>
147 #include <machine/pcb.h>
149 #include <arm/include/physmem.h>
151 #define NL0PG (PAGE_SIZE/(sizeof (pd_entry_t)))
152 #define NL1PG (PAGE_SIZE/(sizeof (pd_entry_t)))
153 #define NL2PG (PAGE_SIZE/(sizeof (pd_entry_t)))
154 #define NL3PG (PAGE_SIZE/(sizeof (pt_entry_t)))
156 #define NUL0E L0_ENTRIES
157 #define NUL1E (NUL0E * NL1PG)
158 #define NUL2E (NUL1E * NL2PG)
160 #if !defined(DIAGNOSTIC)
161 #ifdef __GNUC_GNU_INLINE__
162 #define PMAP_INLINE __attribute__((__gnu_inline__)) inline
164 #define PMAP_INLINE extern inline
171 * These are configured by the mair_el1 register. This is set up in locore.S
173 #define DEVICE_MEMORY 0
174 #define UNCACHED_MEMORY 1
175 #define CACHED_MEMORY 2
179 #define PV_STAT(x) do { x ; } while (0)
181 #define PV_STAT(x) do { } while (0)
184 #define pmap_l2_pindex(v) ((v) >> L2_SHIFT)
185 #define pa_to_pvh(pa) (&pv_table[pmap_l2_pindex(pa)])
187 #define NPV_LIST_LOCKS MAXCPU
189 #define PHYS_TO_PV_LIST_LOCK(pa) \
190 (&pv_list_locks[pa_index(pa) % NPV_LIST_LOCKS])
192 #define CHANGE_PV_LIST_LOCK_TO_PHYS(lockp, pa) do { \
193 struct rwlock **_lockp = (lockp); \
194 struct rwlock *_new_lock; \
196 _new_lock = PHYS_TO_PV_LIST_LOCK(pa); \
197 if (_new_lock != *_lockp) { \
198 if (*_lockp != NULL) \
199 rw_wunlock(*_lockp); \
200 *_lockp = _new_lock; \
205 #define CHANGE_PV_LIST_LOCK_TO_VM_PAGE(lockp, m) \
206 CHANGE_PV_LIST_LOCK_TO_PHYS(lockp, VM_PAGE_TO_PHYS(m))
208 #define RELEASE_PV_LIST_LOCK(lockp) do { \
209 struct rwlock **_lockp = (lockp); \
211 if (*_lockp != NULL) { \
212 rw_wunlock(*_lockp); \
217 #define VM_PAGE_TO_PV_LIST_LOCK(m) \
218 PHYS_TO_PV_LIST_LOCK(VM_PAGE_TO_PHYS(m))
220 struct pmap kernel_pmap_store;
222 /* Used for mapping ACPI memory before VM is initialized */
223 #define PMAP_PREINIT_MAPPING_COUNT 32
224 #define PMAP_PREINIT_MAPPING_SIZE (PMAP_PREINIT_MAPPING_COUNT * L2_SIZE)
225 static vm_offset_t preinit_map_va; /* Start VA of pre-init mapping space */
226 static int vm_initialized = 0; /* No need to use pre-init maps when set */
229 * Reserve a few L2 blocks starting from 'preinit_map_va' pointer.
230 * Always map entire L2 block for simplicity.
231 * VA of L2 block = preinit_map_va + i * L2_SIZE
233 static struct pmap_preinit_mapping {
237 } pmap_preinit_mapping[PMAP_PREINIT_MAPPING_COUNT];
239 vm_offset_t virtual_avail; /* VA of first avail page (after kernel bss) */
240 vm_offset_t virtual_end; /* VA of last avail page (end of kernel AS) */
241 vm_offset_t kernel_vm_end = 0;
244 * Data for the pv entry allocation mechanism.
246 static TAILQ_HEAD(pch, pv_chunk) pv_chunks = TAILQ_HEAD_INITIALIZER(pv_chunks);
247 static struct mtx pv_chunks_mutex;
248 static struct rwlock pv_list_locks[NPV_LIST_LOCKS];
249 static struct md_page *pv_table;
250 static struct md_page pv_dummy;
252 vm_paddr_t dmap_phys_base; /* The start of the dmap region */
253 vm_paddr_t dmap_phys_max; /* The limit of the dmap region */
254 vm_offset_t dmap_max_addr; /* The virtual address limit of the dmap */
256 /* This code assumes all L1 DMAP entries will be used */
257 CTASSERT((DMAP_MIN_ADDRESS & ~L0_OFFSET) == DMAP_MIN_ADDRESS);
258 CTASSERT((DMAP_MAX_ADDRESS & ~L0_OFFSET) == DMAP_MAX_ADDRESS);
260 #define DMAP_TABLES ((DMAP_MAX_ADDRESS - DMAP_MIN_ADDRESS) >> L0_SHIFT)
261 extern pt_entry_t pagetable_dmap[];
263 #define PHYSMAP_SIZE (2 * (VM_PHYSSEG_MAX - 1))
264 static vm_paddr_t physmap[PHYSMAP_SIZE];
265 static u_int physmap_idx;
267 static SYSCTL_NODE(_vm, OID_AUTO, pmap, CTLFLAG_RD, 0, "VM/pmap parameters");
269 static int superpages_enabled = 1;
270 SYSCTL_INT(_vm_pmap, OID_AUTO, superpages_enabled,
271 CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &superpages_enabled, 0,
272 "Are large page mappings enabled?");
275 * Internal flags for pmap_enter()'s helper functions.
277 #define PMAP_ENTER_NORECLAIM 0x1000000 /* Don't reclaim PV entries. */
278 #define PMAP_ENTER_NOREPLACE 0x2000000 /* Don't replace mappings. */
280 static void free_pv_chunk(struct pv_chunk *pc);
281 static void free_pv_entry(pmap_t pmap, pv_entry_t pv);
282 static pv_entry_t get_pv_entry(pmap_t pmap, struct rwlock **lockp);
283 static vm_page_t reclaim_pv_chunk(pmap_t locked_pmap, struct rwlock **lockp);
284 static void pmap_pvh_free(struct md_page *pvh, pmap_t pmap, vm_offset_t va);
285 static pv_entry_t pmap_pvh_remove(struct md_page *pvh, pmap_t pmap,
288 static int pmap_change_attr(vm_offset_t va, vm_size_t size, int mode);
289 static int pmap_change_attr_locked(vm_offset_t va, vm_size_t size, int mode);
290 static pt_entry_t *pmap_demote_l1(pmap_t pmap, pt_entry_t *l1, vm_offset_t va);
291 static pt_entry_t *pmap_demote_l2_locked(pmap_t pmap, pt_entry_t *l2,
292 vm_offset_t va, struct rwlock **lockp);
293 static pt_entry_t *pmap_demote_l2(pmap_t pmap, pt_entry_t *l2, vm_offset_t va);
294 static vm_page_t pmap_enter_quick_locked(pmap_t pmap, vm_offset_t va,
295 vm_page_t m, vm_prot_t prot, vm_page_t mpte, struct rwlock **lockp);
296 static int pmap_enter_l2(pmap_t pmap, vm_offset_t va, pd_entry_t new_l2,
297 u_int flags, vm_page_t m, struct rwlock **lockp);
298 static int pmap_remove_l2(pmap_t pmap, pt_entry_t *l2, vm_offset_t sva,
299 pd_entry_t l1e, struct spglist *free, struct rwlock **lockp);
300 static int pmap_remove_l3(pmap_t pmap, pt_entry_t *l3, vm_offset_t sva,
301 pd_entry_t l2e, struct spglist *free, struct rwlock **lockp);
302 static boolean_t pmap_try_insert_pv_entry(pmap_t pmap, vm_offset_t va,
303 vm_page_t m, struct rwlock **lockp);
305 static vm_page_t _pmap_alloc_l3(pmap_t pmap, vm_pindex_t ptepindex,
306 struct rwlock **lockp);
308 static void _pmap_unwire_l3(pmap_t pmap, vm_offset_t va, vm_page_t m,
309 struct spglist *free);
310 static int pmap_unuse_pt(pmap_t, vm_offset_t, pd_entry_t, struct spglist *);
311 static __inline vm_page_t pmap_remove_pt_page(pmap_t pmap, vm_offset_t va);
314 * These load the old table data and store the new value.
315 * They need to be atomic as the System MMU may write to the table at
316 * the same time as the CPU.
318 #define pmap_load_store(table, entry) atomic_swap_64(table, entry)
319 #define pmap_set(table, mask) atomic_set_64(table, mask)
320 #define pmap_load_clear(table) atomic_swap_64(table, 0)
321 #define pmap_load(table) (*table)
323 /********************/
324 /* Inline functions */
325 /********************/
328 pagecopy(void *s, void *d)
331 memcpy(d, s, PAGE_SIZE);
334 static __inline pd_entry_t *
335 pmap_l0(pmap_t pmap, vm_offset_t va)
338 return (&pmap->pm_l0[pmap_l0_index(va)]);
341 static __inline pd_entry_t *
342 pmap_l0_to_l1(pd_entry_t *l0, vm_offset_t va)
346 l1 = (pd_entry_t *)PHYS_TO_DMAP(pmap_load(l0) & ~ATTR_MASK);
347 return (&l1[pmap_l1_index(va)]);
350 static __inline pd_entry_t *
351 pmap_l1(pmap_t pmap, vm_offset_t va)
355 l0 = pmap_l0(pmap, va);
356 if ((pmap_load(l0) & ATTR_DESCR_MASK) != L0_TABLE)
359 return (pmap_l0_to_l1(l0, va));
362 static __inline pd_entry_t *
363 pmap_l1_to_l2(pd_entry_t *l1, vm_offset_t va)
367 l2 = (pd_entry_t *)PHYS_TO_DMAP(pmap_load(l1) & ~ATTR_MASK);
368 return (&l2[pmap_l2_index(va)]);
371 static __inline pd_entry_t *
372 pmap_l2(pmap_t pmap, vm_offset_t va)
376 l1 = pmap_l1(pmap, va);
377 if ((pmap_load(l1) & ATTR_DESCR_MASK) != L1_TABLE)
380 return (pmap_l1_to_l2(l1, va));
383 static __inline pt_entry_t *
384 pmap_l2_to_l3(pd_entry_t *l2, vm_offset_t va)
388 l3 = (pd_entry_t *)PHYS_TO_DMAP(pmap_load(l2) & ~ATTR_MASK);
389 return (&l3[pmap_l3_index(va)]);
393 * Returns the lowest valid pde for a given virtual address.
394 * The next level may or may not point to a valid page or block.
396 static __inline pd_entry_t *
397 pmap_pde(pmap_t pmap, vm_offset_t va, int *level)
399 pd_entry_t *l0, *l1, *l2, desc;
401 l0 = pmap_l0(pmap, va);
402 desc = pmap_load(l0) & ATTR_DESCR_MASK;
403 if (desc != L0_TABLE) {
408 l1 = pmap_l0_to_l1(l0, va);
409 desc = pmap_load(l1) & ATTR_DESCR_MASK;
410 if (desc != L1_TABLE) {
415 l2 = pmap_l1_to_l2(l1, va);
416 desc = pmap_load(l2) & ATTR_DESCR_MASK;
417 if (desc != L2_TABLE) {
427 * Returns the lowest valid pte block or table entry for a given virtual
428 * address. If there are no valid entries return NULL and set the level to
429 * the first invalid level.
431 static __inline pt_entry_t *
432 pmap_pte(pmap_t pmap, vm_offset_t va, int *level)
434 pd_entry_t *l1, *l2, desc;
437 l1 = pmap_l1(pmap, va);
442 desc = pmap_load(l1) & ATTR_DESCR_MASK;
443 if (desc == L1_BLOCK) {
448 if (desc != L1_TABLE) {
453 l2 = pmap_l1_to_l2(l1, va);
454 desc = pmap_load(l2) & ATTR_DESCR_MASK;
455 if (desc == L2_BLOCK) {
460 if (desc != L2_TABLE) {
466 l3 = pmap_l2_to_l3(l2, va);
467 if ((pmap_load(l3) & ATTR_DESCR_MASK) != L3_PAGE)
474 pmap_ps_enabled(pmap_t pmap __unused)
477 return (superpages_enabled != 0);
481 pmap_get_tables(pmap_t pmap, vm_offset_t va, pd_entry_t **l0, pd_entry_t **l1,
482 pd_entry_t **l2, pt_entry_t **l3)
484 pd_entry_t *l0p, *l1p, *l2p;
486 if (pmap->pm_l0 == NULL)
489 l0p = pmap_l0(pmap, va);
492 if ((pmap_load(l0p) & ATTR_DESCR_MASK) != L0_TABLE)
495 l1p = pmap_l0_to_l1(l0p, va);
498 if ((pmap_load(l1p) & ATTR_DESCR_MASK) == L1_BLOCK) {
504 if ((pmap_load(l1p) & ATTR_DESCR_MASK) != L1_TABLE)
507 l2p = pmap_l1_to_l2(l1p, va);
510 if ((pmap_load(l2p) & ATTR_DESCR_MASK) == L2_BLOCK) {
515 if ((pmap_load(l2p) & ATTR_DESCR_MASK) != L2_TABLE)
518 *l3 = pmap_l2_to_l3(l2p, va);
524 pmap_l3_valid(pt_entry_t l3)
527 return ((l3 & ATTR_DESCR_MASK) == L3_PAGE);
531 CTASSERT(L1_BLOCK == L2_BLOCK);
534 * Checks if the page is dirty. We currently lack proper tracking of this on
535 * arm64 so for now assume is a page mapped as rw was accessed it is.
538 pmap_page_dirty(pt_entry_t pte)
541 return ((pte & (ATTR_AF | ATTR_AP_RW_BIT)) ==
542 (ATTR_AF | ATTR_AP(ATTR_AP_RW)));
546 pmap_resident_count_inc(pmap_t pmap, int count)
549 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
550 pmap->pm_stats.resident_count += count;
554 pmap_resident_count_dec(pmap_t pmap, int count)
557 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
558 KASSERT(pmap->pm_stats.resident_count >= count,
559 ("pmap %p resident count underflow %ld %d", pmap,
560 pmap->pm_stats.resident_count, count));
561 pmap->pm_stats.resident_count -= count;
565 pmap_early_page_idx(vm_offset_t l1pt, vm_offset_t va, u_int *l1_slot,
571 l1 = (pd_entry_t *)l1pt;
572 *l1_slot = (va >> L1_SHIFT) & Ln_ADDR_MASK;
574 /* Check locore has used a table L1 map */
575 KASSERT((l1[*l1_slot] & ATTR_DESCR_MASK) == L1_TABLE,
576 ("Invalid bootstrap L1 table"));
577 /* Find the address of the L2 table */
578 l2 = (pt_entry_t *)init_pt_va;
579 *l2_slot = pmap_l2_index(va);
585 pmap_early_vtophys(vm_offset_t l1pt, vm_offset_t va)
587 u_int l1_slot, l2_slot;
590 l2 = pmap_early_page_idx(l1pt, va, &l1_slot, &l2_slot);
592 return ((l2[l2_slot] & ~ATTR_MASK) + (va & L2_OFFSET));
596 pmap_bootstrap_dmap(vm_offset_t kern_l1, vm_paddr_t min_pa,
597 vm_offset_t freemempos)
601 vm_paddr_t l2_pa, pa;
602 u_int l1_slot, l2_slot, prev_l1_slot;
605 dmap_phys_base = min_pa & ~L1_OFFSET;
611 #define DMAP_TABLES ((DMAP_MAX_ADDRESS - DMAP_MIN_ADDRESS) >> L0_SHIFT)
612 memset(pagetable_dmap, 0, PAGE_SIZE * DMAP_TABLES);
614 for (i = 0; i < (physmap_idx * 2); i += 2) {
615 pa = physmap[i] & ~L2_OFFSET;
616 va = pa - dmap_phys_base + DMAP_MIN_ADDRESS;
618 /* Create L2 mappings at the start of the region */
619 if ((pa & L1_OFFSET) != 0) {
620 l1_slot = ((va - DMAP_MIN_ADDRESS) >> L1_SHIFT);
621 if (l1_slot != prev_l1_slot) {
622 prev_l1_slot = l1_slot;
623 l2 = (pt_entry_t *)freemempos;
624 l2_pa = pmap_early_vtophys(kern_l1,
626 freemempos += PAGE_SIZE;
628 pmap_load_store(&pagetable_dmap[l1_slot],
629 (l2_pa & ~Ln_TABLE_MASK) | L1_TABLE);
631 memset(l2, 0, PAGE_SIZE);
634 ("pmap_bootstrap_dmap: NULL l2 map"));
635 for (; va < DMAP_MAX_ADDRESS && pa < physmap[i + 1];
636 pa += L2_SIZE, va += L2_SIZE) {
638 * We are on a boundary, stop to
639 * create a level 1 block
641 if ((pa & L1_OFFSET) == 0)
644 l2_slot = pmap_l2_index(va);
645 KASSERT(l2_slot != 0, ("..."));
646 pmap_load_store(&l2[l2_slot],
647 (pa & ~L2_OFFSET) | ATTR_DEFAULT | ATTR_XN |
648 ATTR_IDX(CACHED_MEMORY) | L2_BLOCK);
650 KASSERT(va == (pa - dmap_phys_base + DMAP_MIN_ADDRESS),
654 for (; va < DMAP_MAX_ADDRESS && pa < physmap[i + 1] &&
655 (physmap[i + 1] - pa) >= L1_SIZE;
656 pa += L1_SIZE, va += L1_SIZE) {
657 l1_slot = ((va - DMAP_MIN_ADDRESS) >> L1_SHIFT);
658 pmap_load_store(&pagetable_dmap[l1_slot],
659 (pa & ~L1_OFFSET) | ATTR_DEFAULT | ATTR_XN |
660 ATTR_IDX(CACHED_MEMORY) | L1_BLOCK);
663 /* Create L2 mappings at the end of the region */
664 if (pa < physmap[i + 1]) {
665 l1_slot = ((va - DMAP_MIN_ADDRESS) >> L1_SHIFT);
666 if (l1_slot != prev_l1_slot) {
667 prev_l1_slot = l1_slot;
668 l2 = (pt_entry_t *)freemempos;
669 l2_pa = pmap_early_vtophys(kern_l1,
671 freemempos += PAGE_SIZE;
673 pmap_load_store(&pagetable_dmap[l1_slot],
674 (l2_pa & ~Ln_TABLE_MASK) | L1_TABLE);
676 memset(l2, 0, PAGE_SIZE);
679 ("pmap_bootstrap_dmap: NULL l2 map"));
680 for (; va < DMAP_MAX_ADDRESS && pa < physmap[i + 1];
681 pa += L2_SIZE, va += L2_SIZE) {
682 l2_slot = pmap_l2_index(va);
683 pmap_load_store(&l2[l2_slot],
684 (pa & ~L2_OFFSET) | ATTR_DEFAULT | ATTR_XN |
685 ATTR_IDX(CACHED_MEMORY) | L2_BLOCK);
689 if (pa > dmap_phys_max) {
701 pmap_bootstrap_l2(vm_offset_t l1pt, vm_offset_t va, vm_offset_t l2_start)
708 KASSERT((va & L1_OFFSET) == 0, ("Invalid virtual address"));
710 l1 = (pd_entry_t *)l1pt;
711 l1_slot = pmap_l1_index(va);
714 for (; va < VM_MAX_KERNEL_ADDRESS; l1_slot++, va += L1_SIZE) {
715 KASSERT(l1_slot < Ln_ENTRIES, ("Invalid L1 index"));
717 pa = pmap_early_vtophys(l1pt, l2pt);
718 pmap_load_store(&l1[l1_slot],
719 (pa & ~Ln_TABLE_MASK) | L1_TABLE);
723 /* Clean the L2 page table */
724 memset((void *)l2_start, 0, l2pt - l2_start);
730 pmap_bootstrap_l3(vm_offset_t l1pt, vm_offset_t va, vm_offset_t l3_start)
737 KASSERT((va & L2_OFFSET) == 0, ("Invalid virtual address"));
739 l2 = pmap_l2(kernel_pmap, va);
740 l2 = (pd_entry_t *)rounddown2((uintptr_t)l2, PAGE_SIZE);
741 l2_slot = pmap_l2_index(va);
744 for (; va < VM_MAX_KERNEL_ADDRESS; l2_slot++, va += L2_SIZE) {
745 KASSERT(l2_slot < Ln_ENTRIES, ("Invalid L2 index"));
747 pa = pmap_early_vtophys(l1pt, l3pt);
748 pmap_load_store(&l2[l2_slot],
749 (pa & ~Ln_TABLE_MASK) | L2_TABLE);
753 /* Clean the L2 page table */
754 memset((void *)l3_start, 0, l3pt - l3_start);
760 * Bootstrap the system enough to run with virtual memory.
763 pmap_bootstrap(vm_offset_t l0pt, vm_offset_t l1pt, vm_paddr_t kernstart,
766 u_int l1_slot, l2_slot;
769 vm_offset_t va, freemempos;
770 vm_offset_t dpcpu, msgbufpv;
771 vm_paddr_t start_pa, pa, min_pa;
774 kern_delta = KERNBASE - kernstart;
776 printf("pmap_bootstrap %lx %lx %lx\n", l1pt, kernstart, kernlen);
777 printf("%lx\n", l1pt);
778 printf("%lx\n", (KERNBASE >> L1_SHIFT) & Ln_ADDR_MASK);
780 /* Set this early so we can use the pagetable walking functions */
781 kernel_pmap_store.pm_l0 = (pd_entry_t *)l0pt;
782 PMAP_LOCK_INIT(kernel_pmap);
784 /* Assume the address we were loaded to is a valid physical address */
785 min_pa = KERNBASE - kern_delta;
787 physmap_idx = arm_physmem_avail(physmap, nitems(physmap));
791 * Find the minimum physical address. physmap is sorted,
792 * but may contain empty ranges.
794 for (i = 0; i < (physmap_idx * 2); i += 2) {
795 if (physmap[i] == physmap[i + 1])
797 if (physmap[i] <= min_pa)
801 freemempos = KERNBASE + kernlen;
802 freemempos = roundup2(freemempos, PAGE_SIZE);
804 /* Create a direct map region early so we can use it for pa -> va */
805 freemempos = pmap_bootstrap_dmap(l1pt, min_pa, freemempos);
808 start_pa = pa = KERNBASE - kern_delta;
811 * Read the page table to find out what is already mapped.
812 * This assumes we have mapped a block of memory from KERNBASE
813 * using a single L1 entry.
815 l2 = pmap_early_page_idx(l1pt, KERNBASE, &l1_slot, &l2_slot);
817 /* Sanity check the index, KERNBASE should be the first VA */
818 KASSERT(l2_slot == 0, ("The L2 index is non-zero"));
820 /* Find how many pages we have mapped */
821 for (; l2_slot < Ln_ENTRIES; l2_slot++) {
822 if ((l2[l2_slot] & ATTR_DESCR_MASK) == 0)
825 /* Check locore used L2 blocks */
826 KASSERT((l2[l2_slot] & ATTR_DESCR_MASK) == L2_BLOCK,
827 ("Invalid bootstrap L2 table"));
828 KASSERT((l2[l2_slot] & ~ATTR_MASK) == pa,
829 ("Incorrect PA in L2 table"));
835 va = roundup2(va, L1_SIZE);
837 /* Create the l2 tables up to VM_MAX_KERNEL_ADDRESS */
838 freemempos = pmap_bootstrap_l2(l1pt, va, freemempos);
839 /* And the l3 tables for the early devmap */
840 freemempos = pmap_bootstrap_l3(l1pt,
841 VM_MAX_KERNEL_ADDRESS - (PMAP_MAPDEV_EARLY_SIZE), freemempos);
845 #define alloc_pages(var, np) \
846 (var) = freemempos; \
847 freemempos += (np * PAGE_SIZE); \
848 memset((char *)(var), 0, ((np) * PAGE_SIZE));
850 /* Allocate dynamic per-cpu area. */
851 alloc_pages(dpcpu, DPCPU_SIZE / PAGE_SIZE);
852 dpcpu_init((void *)dpcpu, 0);
854 /* Allocate memory for the msgbuf, e.g. for /sbin/dmesg */
855 alloc_pages(msgbufpv, round_page(msgbufsize) / PAGE_SIZE);
856 msgbufp = (void *)msgbufpv;
858 /* Reserve some VA space for early BIOS/ACPI mapping */
859 preinit_map_va = roundup2(freemempos, L2_SIZE);
861 virtual_avail = preinit_map_va + PMAP_PREINIT_MAPPING_SIZE;
862 virtual_avail = roundup2(virtual_avail, L1_SIZE);
863 virtual_end = VM_MAX_KERNEL_ADDRESS - (PMAP_MAPDEV_EARLY_SIZE);
864 kernel_vm_end = virtual_avail;
866 pa = pmap_early_vtophys(l1pt, freemempos);
868 arm_physmem_exclude_region(start_pa, pa - start_pa, EXFLAG_NOALLOC);
874 * Initialize a vm_page's machine-dependent fields.
877 pmap_page_init(vm_page_t m)
880 TAILQ_INIT(&m->md.pv_list);
881 m->md.pv_memattr = VM_MEMATTR_WRITE_BACK;
885 * Initialize the pmap module.
886 * Called by vm_init, to initialize any structures that the pmap
887 * system needs to map virtual memory.
896 * Are large page mappings enabled?
898 TUNABLE_INT_FETCH("vm.pmap.superpages_enabled", &superpages_enabled);
899 if (superpages_enabled) {
900 KASSERT(MAXPAGESIZES > 1 && pagesizes[1] == 0,
901 ("pmap_init: can't assign to pagesizes[1]"));
902 pagesizes[1] = L2_SIZE;
906 * Initialize the pv chunk list mutex.
908 mtx_init(&pv_chunks_mutex, "pmap pv chunk list", NULL, MTX_DEF);
911 * Initialize the pool of pv list locks.
913 for (i = 0; i < NPV_LIST_LOCKS; i++)
914 rw_init(&pv_list_locks[i], "pmap pv list");
917 * Calculate the size of the pv head table for superpages.
919 pv_npg = howmany(vm_phys_segs[vm_phys_nsegs - 1].end, L2_SIZE);
922 * Allocate memory for the pv head table for superpages.
924 s = (vm_size_t)(pv_npg * sizeof(struct md_page));
926 pv_table = (struct md_page *)kmem_malloc(s, M_WAITOK | M_ZERO);
927 for (i = 0; i < pv_npg; i++)
928 TAILQ_INIT(&pv_table[i].pv_list);
929 TAILQ_INIT(&pv_dummy.pv_list);
934 static SYSCTL_NODE(_vm_pmap, OID_AUTO, l2, CTLFLAG_RD, 0,
935 "2MB page mapping counters");
937 static u_long pmap_l2_demotions;
938 SYSCTL_ULONG(_vm_pmap_l2, OID_AUTO, demotions, CTLFLAG_RD,
939 &pmap_l2_demotions, 0, "2MB page demotions");
941 static u_long pmap_l2_mappings;
942 SYSCTL_ULONG(_vm_pmap_l2, OID_AUTO, mappings, CTLFLAG_RD,
943 &pmap_l2_mappings, 0, "2MB page mappings");
945 static u_long pmap_l2_p_failures;
946 SYSCTL_ULONG(_vm_pmap_l2, OID_AUTO, p_failures, CTLFLAG_RD,
947 &pmap_l2_p_failures, 0, "2MB page promotion failures");
949 static u_long pmap_l2_promotions;
950 SYSCTL_ULONG(_vm_pmap_l2, OID_AUTO, promotions, CTLFLAG_RD,
951 &pmap_l2_promotions, 0, "2MB page promotions");
954 * Invalidate a single TLB entry.
957 pmap_invalidate_page(pmap_t pmap, vm_offset_t va)
963 "tlbi vaae1is, %0 \n"
966 : : "r"(va >> PAGE_SHIFT));
971 pmap_invalidate_range_nopin(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
976 for (addr = sva; addr < eva; addr += PAGE_SIZE) {
978 "tlbi vaae1is, %0" : : "r"(addr >> PAGE_SHIFT));
986 pmap_invalidate_range(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
990 pmap_invalidate_range_nopin(pmap, sva, eva);
995 pmap_invalidate_all(pmap_t pmap)
1008 * Routine: pmap_extract
1010 * Extract the physical page address associated
1011 * with the given map/virtual_address pair.
1014 pmap_extract(pmap_t pmap, vm_offset_t va)
1016 pt_entry_t *pte, tpte;
1023 * Find the block or page map for this virtual address. pmap_pte
1024 * will return either a valid block/page entry, or NULL.
1026 pte = pmap_pte(pmap, va, &lvl);
1028 tpte = pmap_load(pte);
1029 pa = tpte & ~ATTR_MASK;
1032 KASSERT((tpte & ATTR_DESCR_MASK) == L1_BLOCK,
1033 ("pmap_extract: Invalid L1 pte found: %lx",
1034 tpte & ATTR_DESCR_MASK));
1035 pa |= (va & L1_OFFSET);
1038 KASSERT((tpte & ATTR_DESCR_MASK) == L2_BLOCK,
1039 ("pmap_extract: Invalid L2 pte found: %lx",
1040 tpte & ATTR_DESCR_MASK));
1041 pa |= (va & L2_OFFSET);
1044 KASSERT((tpte & ATTR_DESCR_MASK) == L3_PAGE,
1045 ("pmap_extract: Invalid L3 pte found: %lx",
1046 tpte & ATTR_DESCR_MASK));
1047 pa |= (va & L3_OFFSET);
1056 * Routine: pmap_extract_and_hold
1058 * Atomically extract and hold the physical page
1059 * with the given pmap and virtual address pair
1060 * if that mapping permits the given protection.
1063 pmap_extract_and_hold(pmap_t pmap, vm_offset_t va, vm_prot_t prot)
1065 pt_entry_t *pte, tpte;
1075 pte = pmap_pte(pmap, va, &lvl);
1077 tpte = pmap_load(pte);
1079 KASSERT(lvl > 0 && lvl <= 3,
1080 ("pmap_extract_and_hold: Invalid level %d", lvl));
1081 CTASSERT(L1_BLOCK == L2_BLOCK);
1082 KASSERT((lvl == 3 && (tpte & ATTR_DESCR_MASK) == L3_PAGE) ||
1083 (lvl < 3 && (tpte & ATTR_DESCR_MASK) == L1_BLOCK),
1084 ("pmap_extract_and_hold: Invalid pte at L%d: %lx", lvl,
1085 tpte & ATTR_DESCR_MASK));
1086 if (((tpte & ATTR_AP_RW_BIT) == ATTR_AP(ATTR_AP_RW)) ||
1087 ((prot & VM_PROT_WRITE) == 0)) {
1090 off = va & L1_OFFSET;
1093 off = va & L2_OFFSET;
1099 if (vm_page_pa_tryrelock(pmap,
1100 (tpte & ~ATTR_MASK) | off, &pa))
1102 m = PHYS_TO_VM_PAGE((tpte & ~ATTR_MASK) | off);
1112 pmap_kextract(vm_offset_t va)
1114 pt_entry_t *pte, tpte;
1118 if (va >= DMAP_MIN_ADDRESS && va < DMAP_MAX_ADDRESS) {
1119 pa = DMAP_TO_PHYS(va);
1122 pte = pmap_pte(kernel_pmap, va, &lvl);
1124 tpte = pmap_load(pte);
1125 pa = tpte & ~ATTR_MASK;
1128 KASSERT((tpte & ATTR_DESCR_MASK) == L1_BLOCK,
1129 ("pmap_kextract: Invalid L1 pte found: %lx",
1130 tpte & ATTR_DESCR_MASK));
1131 pa |= (va & L1_OFFSET);
1134 KASSERT((tpte & ATTR_DESCR_MASK) == L2_BLOCK,
1135 ("pmap_kextract: Invalid L2 pte found: %lx",
1136 tpte & ATTR_DESCR_MASK));
1137 pa |= (va & L2_OFFSET);
1140 KASSERT((tpte & ATTR_DESCR_MASK) == L3_PAGE,
1141 ("pmap_kextract: Invalid L3 pte found: %lx",
1142 tpte & ATTR_DESCR_MASK));
1143 pa |= (va & L3_OFFSET);
1151 /***************************************************
1152 * Low level mapping routines.....
1153 ***************************************************/
1156 pmap_kenter(vm_offset_t sva, vm_size_t size, vm_paddr_t pa, int mode)
1159 pt_entry_t *pte, attr;
1163 KASSERT((pa & L3_OFFSET) == 0,
1164 ("pmap_kenter: Invalid physical address"));
1165 KASSERT((sva & L3_OFFSET) == 0,
1166 ("pmap_kenter: Invalid virtual address"));
1167 KASSERT((size & PAGE_MASK) == 0,
1168 ("pmap_kenter: Mapping is not page-sized"));
1170 attr = ATTR_DEFAULT | ATTR_IDX(mode) | L3_PAGE;
1171 if (mode == DEVICE_MEMORY)
1176 pde = pmap_pde(kernel_pmap, va, &lvl);
1177 KASSERT(pde != NULL,
1178 ("pmap_kenter: Invalid page entry, va: 0x%lx", va));
1179 KASSERT(lvl == 2, ("pmap_kenter: Invalid level %d", lvl));
1181 pte = pmap_l2_to_l3(pde, va);
1182 pmap_load_store(pte, (pa & ~L3_OFFSET) | attr);
1188 pmap_invalidate_range(kernel_pmap, sva, va);
1192 pmap_kenter_device(vm_offset_t sva, vm_size_t size, vm_paddr_t pa)
1195 pmap_kenter(sva, size, pa, DEVICE_MEMORY);
1199 * Remove a page from the kernel pagetables.
1202 pmap_kremove(vm_offset_t va)
1207 pte = pmap_pte(kernel_pmap, va, &lvl);
1208 KASSERT(pte != NULL, ("pmap_kremove: Invalid address"));
1209 KASSERT(lvl == 3, ("pmap_kremove: Invalid pte level %d", lvl));
1211 pmap_load_clear(pte);
1212 pmap_invalidate_page(kernel_pmap, va);
1216 pmap_kremove_device(vm_offset_t sva, vm_size_t size)
1222 KASSERT((sva & L3_OFFSET) == 0,
1223 ("pmap_kremove_device: Invalid virtual address"));
1224 KASSERT((size & PAGE_MASK) == 0,
1225 ("pmap_kremove_device: Mapping is not page-sized"));
1229 pte = pmap_pte(kernel_pmap, va, &lvl);
1230 KASSERT(pte != NULL, ("Invalid page table, va: 0x%lx", va));
1232 ("Invalid device pagetable level: %d != 3", lvl));
1233 pmap_load_clear(pte);
1238 pmap_invalidate_range(kernel_pmap, sva, va);
1242 * Used to map a range of physical addresses into kernel
1243 * virtual address space.
1245 * The value passed in '*virt' is a suggested virtual address for
1246 * the mapping. Architectures which can support a direct-mapped
1247 * physical to virtual region can return the appropriate address
1248 * within that region, leaving '*virt' unchanged. Other
1249 * architectures should map the pages starting at '*virt' and
1250 * update '*virt' with the first usable address after the mapped
1254 pmap_map(vm_offset_t *virt, vm_paddr_t start, vm_paddr_t end, int prot)
1256 return PHYS_TO_DMAP(start);
1261 * Add a list of wired pages to the kva
1262 * this routine is only used for temporary
1263 * kernel mappings that do not need to have
1264 * page modification or references recorded.
1265 * Note that old mappings are simply written
1266 * over. The page *must* be wired.
1267 * Note: SMP coherent. Uses a ranged shootdown IPI.
1270 pmap_qenter(vm_offset_t sva, vm_page_t *ma, int count)
1273 pt_entry_t *pte, pa;
1279 for (i = 0; i < count; i++) {
1280 pde = pmap_pde(kernel_pmap, va, &lvl);
1281 KASSERT(pde != NULL,
1282 ("pmap_qenter: Invalid page entry, va: 0x%lx", va));
1284 ("pmap_qenter: Invalid level %d", lvl));
1287 pa = VM_PAGE_TO_PHYS(m) | ATTR_DEFAULT | ATTR_AP(ATTR_AP_RW) |
1288 ATTR_IDX(m->md.pv_memattr) | L3_PAGE;
1289 if (m->md.pv_memattr == DEVICE_MEMORY)
1291 pte = pmap_l2_to_l3(pde, va);
1292 pmap_load_store(pte, pa);
1296 pmap_invalidate_range(kernel_pmap, sva, va);
1300 * This routine tears out page mappings from the
1301 * kernel -- it is meant only for temporary mappings.
1304 pmap_qremove(vm_offset_t sva, int count)
1310 KASSERT(sva >= VM_MIN_KERNEL_ADDRESS, ("usermode va %lx", sva));
1313 while (count-- > 0) {
1314 pte = pmap_pte(kernel_pmap, va, &lvl);
1316 ("Invalid device pagetable level: %d != 3", lvl));
1318 pmap_load_clear(pte);
1323 pmap_invalidate_range(kernel_pmap, sva, va);
1326 /***************************************************
1327 * Page table page management routines.....
1328 ***************************************************/
1330 * Schedule the specified unused page table page to be freed. Specifically,
1331 * add the page to the specified list of pages that will be released to the
1332 * physical memory manager after the TLB has been updated.
1334 static __inline void
1335 pmap_add_delayed_free_list(vm_page_t m, struct spglist *free,
1336 boolean_t set_PG_ZERO)
1340 m->flags |= PG_ZERO;
1342 m->flags &= ~PG_ZERO;
1343 SLIST_INSERT_HEAD(free, m, plinks.s.ss);
1347 * Decrements a page table page's wire count, which is used to record the
1348 * number of valid page table entries within the page. If the wire count
1349 * drops to zero, then the page table page is unmapped. Returns TRUE if the
1350 * page table page was unmapped and FALSE otherwise.
1352 static inline boolean_t
1353 pmap_unwire_l3(pmap_t pmap, vm_offset_t va, vm_page_t m, struct spglist *free)
1357 if (m->wire_count == 0) {
1358 _pmap_unwire_l3(pmap, va, m, free);
1365 _pmap_unwire_l3(pmap_t pmap, vm_offset_t va, vm_page_t m, struct spglist *free)
1368 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1370 * unmap the page table page
1372 if (m->pindex >= (NUL2E + NUL1E)) {
1376 l0 = pmap_l0(pmap, va);
1377 pmap_load_clear(l0);
1378 } else if (m->pindex >= NUL2E) {
1382 l1 = pmap_l1(pmap, va);
1383 pmap_load_clear(l1);
1388 l2 = pmap_l2(pmap, va);
1389 pmap_load_clear(l2);
1391 pmap_resident_count_dec(pmap, 1);
1392 if (m->pindex < NUL2E) {
1393 /* We just released an l3, unhold the matching l2 */
1394 pd_entry_t *l1, tl1;
1397 l1 = pmap_l1(pmap, va);
1398 tl1 = pmap_load(l1);
1399 l2pg = PHYS_TO_VM_PAGE(tl1 & ~ATTR_MASK);
1400 pmap_unwire_l3(pmap, va, l2pg, free);
1401 } else if (m->pindex < (NUL2E + NUL1E)) {
1402 /* We just released an l2, unhold the matching l1 */
1403 pd_entry_t *l0, tl0;
1406 l0 = pmap_l0(pmap, va);
1407 tl0 = pmap_load(l0);
1408 l1pg = PHYS_TO_VM_PAGE(tl0 & ~ATTR_MASK);
1409 pmap_unwire_l3(pmap, va, l1pg, free);
1411 pmap_invalidate_page(pmap, va);
1414 * Put page on a list so that it is released after
1415 * *ALL* TLB shootdown is done
1417 pmap_add_delayed_free_list(m, free, TRUE);
1421 * After removing a page table entry, this routine is used to
1422 * conditionally free the page, and manage the hold/wire counts.
1425 pmap_unuse_pt(pmap_t pmap, vm_offset_t va, pd_entry_t ptepde,
1426 struct spglist *free)
1430 if (va >= VM_MAXUSER_ADDRESS)
1432 KASSERT(ptepde != 0, ("pmap_unuse_pt: ptepde != 0"));
1433 mpte = PHYS_TO_VM_PAGE(ptepde & ~ATTR_MASK);
1434 return (pmap_unwire_l3(pmap, va, mpte, free));
1438 pmap_pinit0(pmap_t pmap)
1441 PMAP_LOCK_INIT(pmap);
1442 bzero(&pmap->pm_stats, sizeof(pmap->pm_stats));
1443 pmap->pm_l0 = kernel_pmap->pm_l0;
1444 pmap->pm_root.rt_root = 0;
1448 pmap_pinit(pmap_t pmap)
1454 * allocate the l0 page
1456 while ((l0pt = vm_page_alloc(NULL, 0, VM_ALLOC_NORMAL |
1457 VM_ALLOC_NOOBJ | VM_ALLOC_WIRED | VM_ALLOC_ZERO)) == NULL)
1460 l0phys = VM_PAGE_TO_PHYS(l0pt);
1461 pmap->pm_l0 = (pd_entry_t *)PHYS_TO_DMAP(l0phys);
1463 if ((l0pt->flags & PG_ZERO) == 0)
1464 pagezero(pmap->pm_l0);
1466 pmap->pm_root.rt_root = 0;
1467 bzero(&pmap->pm_stats, sizeof(pmap->pm_stats));
1473 * This routine is called if the desired page table page does not exist.
1475 * If page table page allocation fails, this routine may sleep before
1476 * returning NULL. It sleeps only if a lock pointer was given.
1478 * Note: If a page allocation fails at page table level two or three,
1479 * one or two pages may be held during the wait, only to be released
1480 * afterwards. This conservative approach is easily argued to avoid
1484 _pmap_alloc_l3(pmap_t pmap, vm_pindex_t ptepindex, struct rwlock **lockp)
1486 vm_page_t m, l1pg, l2pg;
1488 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1491 * Allocate a page table page.
1493 if ((m = vm_page_alloc(NULL, ptepindex, VM_ALLOC_NOOBJ |
1494 VM_ALLOC_WIRED | VM_ALLOC_ZERO)) == NULL) {
1495 if (lockp != NULL) {
1496 RELEASE_PV_LIST_LOCK(lockp);
1503 * Indicate the need to retry. While waiting, the page table
1504 * page may have been allocated.
1508 if ((m->flags & PG_ZERO) == 0)
1512 * Map the pagetable page into the process address space, if
1513 * it isn't already there.
1516 if (ptepindex >= (NUL2E + NUL1E)) {
1518 vm_pindex_t l0index;
1520 l0index = ptepindex - (NUL2E + NUL1E);
1521 l0 = &pmap->pm_l0[l0index];
1522 pmap_load_store(l0, VM_PAGE_TO_PHYS(m) | L0_TABLE);
1523 } else if (ptepindex >= NUL2E) {
1524 vm_pindex_t l0index, l1index;
1525 pd_entry_t *l0, *l1;
1528 l1index = ptepindex - NUL2E;
1529 l0index = l1index >> L0_ENTRIES_SHIFT;
1531 l0 = &pmap->pm_l0[l0index];
1532 tl0 = pmap_load(l0);
1534 /* recurse for allocating page dir */
1535 if (_pmap_alloc_l3(pmap, NUL2E + NUL1E + l0index,
1537 vm_page_unwire_noq(m);
1538 vm_page_free_zero(m);
1542 l1pg = PHYS_TO_VM_PAGE(tl0 & ~ATTR_MASK);
1546 l1 = (pd_entry_t *)PHYS_TO_DMAP(pmap_load(l0) & ~ATTR_MASK);
1547 l1 = &l1[ptepindex & Ln_ADDR_MASK];
1548 pmap_load_store(l1, VM_PAGE_TO_PHYS(m) | L1_TABLE);
1550 vm_pindex_t l0index, l1index;
1551 pd_entry_t *l0, *l1, *l2;
1552 pd_entry_t tl0, tl1;
1554 l1index = ptepindex >> Ln_ENTRIES_SHIFT;
1555 l0index = l1index >> L0_ENTRIES_SHIFT;
1557 l0 = &pmap->pm_l0[l0index];
1558 tl0 = pmap_load(l0);
1560 /* recurse for allocating page dir */
1561 if (_pmap_alloc_l3(pmap, NUL2E + l1index,
1563 vm_page_unwire_noq(m);
1564 vm_page_free_zero(m);
1567 tl0 = pmap_load(l0);
1568 l1 = (pd_entry_t *)PHYS_TO_DMAP(tl0 & ~ATTR_MASK);
1569 l1 = &l1[l1index & Ln_ADDR_MASK];
1571 l1 = (pd_entry_t *)PHYS_TO_DMAP(tl0 & ~ATTR_MASK);
1572 l1 = &l1[l1index & Ln_ADDR_MASK];
1573 tl1 = pmap_load(l1);
1575 /* recurse for allocating page dir */
1576 if (_pmap_alloc_l3(pmap, NUL2E + l1index,
1578 vm_page_unwire_noq(m);
1579 vm_page_free_zero(m);
1583 l2pg = PHYS_TO_VM_PAGE(tl1 & ~ATTR_MASK);
1588 l2 = (pd_entry_t *)PHYS_TO_DMAP(pmap_load(l1) & ~ATTR_MASK);
1589 l2 = &l2[ptepindex & Ln_ADDR_MASK];
1590 pmap_load_store(l2, VM_PAGE_TO_PHYS(m) | L2_TABLE);
1593 pmap_resident_count_inc(pmap, 1);
1599 pmap_alloc_l2(pmap_t pmap, vm_offset_t va, struct rwlock **lockp)
1603 vm_pindex_t l2pindex;
1606 l1 = pmap_l1(pmap, va);
1607 if (l1 != NULL && (pmap_load(l1) & ATTR_DESCR_MASK) == L1_TABLE) {
1608 /* Add a reference to the L2 page. */
1609 l2pg = PHYS_TO_VM_PAGE(pmap_load(l1) & ~ATTR_MASK);
1612 /* Allocate a L2 page. */
1613 l2pindex = pmap_l2_pindex(va) >> Ln_ENTRIES_SHIFT;
1614 l2pg = _pmap_alloc_l3(pmap, NUL2E + l2pindex, lockp);
1615 if (l2pg == NULL && lockp != NULL)
1622 pmap_alloc_l3(pmap_t pmap, vm_offset_t va, struct rwlock **lockp)
1624 vm_pindex_t ptepindex;
1625 pd_entry_t *pde, tpde;
1633 * Calculate pagetable page index
1635 ptepindex = pmap_l2_pindex(va);
1638 * Get the page directory entry
1640 pde = pmap_pde(pmap, va, &lvl);
1643 * If the page table page is mapped, we just increment the hold count,
1644 * and activate it. If we get a level 2 pde it will point to a level 3
1652 pte = pmap_l0_to_l1(pde, va);
1653 KASSERT(pmap_load(pte) == 0,
1654 ("pmap_alloc_l3: TODO: l0 superpages"));
1659 pte = pmap_l1_to_l2(pde, va);
1660 KASSERT(pmap_load(pte) == 0,
1661 ("pmap_alloc_l3: TODO: l1 superpages"));
1665 tpde = pmap_load(pde);
1667 m = PHYS_TO_VM_PAGE(tpde & ~ATTR_MASK);
1673 panic("pmap_alloc_l3: Invalid level %d", lvl);
1677 * Here if the pte page isn't mapped, or if it has been deallocated.
1679 m = _pmap_alloc_l3(pmap, ptepindex, lockp);
1680 if (m == NULL && lockp != NULL)
1686 /***************************************************
1687 * Pmap allocation/deallocation routines.
1688 ***************************************************/
1691 * Release any resources held by the given physical map.
1692 * Called when a pmap initialized by pmap_pinit is being released.
1693 * Should only be called if the map contains no valid mappings.
1696 pmap_release(pmap_t pmap)
1700 KASSERT(pmap->pm_stats.resident_count == 0,
1701 ("pmap_release: pmap resident count %ld != 0",
1702 pmap->pm_stats.resident_count));
1703 KASSERT(vm_radix_is_empty(&pmap->pm_root),
1704 ("pmap_release: pmap has reserved page table page(s)"));
1706 m = PHYS_TO_VM_PAGE(DMAP_TO_PHYS((vm_offset_t)pmap->pm_l0));
1708 vm_page_unwire_noq(m);
1709 vm_page_free_zero(m);
1713 kvm_size(SYSCTL_HANDLER_ARGS)
1715 unsigned long ksize = VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS;
1717 return sysctl_handle_long(oidp, &ksize, 0, req);
1719 SYSCTL_PROC(_vm, OID_AUTO, kvm_size, CTLTYPE_LONG|CTLFLAG_RD,
1720 0, 0, kvm_size, "LU", "Size of KVM");
1723 kvm_free(SYSCTL_HANDLER_ARGS)
1725 unsigned long kfree = VM_MAX_KERNEL_ADDRESS - kernel_vm_end;
1727 return sysctl_handle_long(oidp, &kfree, 0, req);
1729 SYSCTL_PROC(_vm, OID_AUTO, kvm_free, CTLTYPE_LONG|CTLFLAG_RD,
1730 0, 0, kvm_free, "LU", "Amount of KVM free");
1733 * grow the number of kernel page table entries, if needed
1736 pmap_growkernel(vm_offset_t addr)
1740 pd_entry_t *l0, *l1, *l2;
1742 mtx_assert(&kernel_map->system_mtx, MA_OWNED);
1744 addr = roundup2(addr, L2_SIZE);
1745 if (addr - 1 >= vm_map_max(kernel_map))
1746 addr = vm_map_max(kernel_map);
1747 while (kernel_vm_end < addr) {
1748 l0 = pmap_l0(kernel_pmap, kernel_vm_end);
1749 KASSERT(pmap_load(l0) != 0,
1750 ("pmap_growkernel: No level 0 kernel entry"));
1752 l1 = pmap_l0_to_l1(l0, kernel_vm_end);
1753 if (pmap_load(l1) == 0) {
1754 /* We need a new PDP entry */
1755 nkpg = vm_page_alloc(NULL, kernel_vm_end >> L1_SHIFT,
1756 VM_ALLOC_INTERRUPT | VM_ALLOC_NOOBJ |
1757 VM_ALLOC_WIRED | VM_ALLOC_ZERO);
1759 panic("pmap_growkernel: no memory to grow kernel");
1760 if ((nkpg->flags & PG_ZERO) == 0)
1761 pmap_zero_page(nkpg);
1762 paddr = VM_PAGE_TO_PHYS(nkpg);
1763 pmap_load_store(l1, paddr | L1_TABLE);
1764 continue; /* try again */
1766 l2 = pmap_l1_to_l2(l1, kernel_vm_end);
1767 if ((pmap_load(l2) & ATTR_AF) != 0) {
1768 kernel_vm_end = (kernel_vm_end + L2_SIZE) & ~L2_OFFSET;
1769 if (kernel_vm_end - 1 >= vm_map_max(kernel_map)) {
1770 kernel_vm_end = vm_map_max(kernel_map);
1776 nkpg = vm_page_alloc(NULL, kernel_vm_end >> L2_SHIFT,
1777 VM_ALLOC_INTERRUPT | VM_ALLOC_NOOBJ | VM_ALLOC_WIRED |
1780 panic("pmap_growkernel: no memory to grow kernel");
1781 if ((nkpg->flags & PG_ZERO) == 0)
1782 pmap_zero_page(nkpg);
1783 paddr = VM_PAGE_TO_PHYS(nkpg);
1784 pmap_load_store(l2, paddr | L2_TABLE);
1785 pmap_invalidate_page(kernel_pmap, kernel_vm_end);
1787 kernel_vm_end = (kernel_vm_end + L2_SIZE) & ~L2_OFFSET;
1788 if (kernel_vm_end - 1 >= vm_map_max(kernel_map)) {
1789 kernel_vm_end = vm_map_max(kernel_map);
1796 /***************************************************
1797 * page management routines.
1798 ***************************************************/
1800 CTASSERT(sizeof(struct pv_chunk) == PAGE_SIZE);
1801 CTASSERT(_NPCM == 3);
1802 CTASSERT(_NPCPV == 168);
1804 static __inline struct pv_chunk *
1805 pv_to_chunk(pv_entry_t pv)
1808 return ((struct pv_chunk *)((uintptr_t)pv & ~(uintptr_t)PAGE_MASK));
1811 #define PV_PMAP(pv) (pv_to_chunk(pv)->pc_pmap)
1813 #define PC_FREE0 0xfffffffffffffffful
1814 #define PC_FREE1 0xfffffffffffffffful
1815 #define PC_FREE2 0x000000fffffffffful
1817 static const uint64_t pc_freemask[_NPCM] = { PC_FREE0, PC_FREE1, PC_FREE2 };
1821 static int pc_chunk_count, pc_chunk_allocs, pc_chunk_frees, pc_chunk_tryfail;
1823 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_count, CTLFLAG_RD, &pc_chunk_count, 0,
1824 "Current number of pv entry chunks");
1825 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_allocs, CTLFLAG_RD, &pc_chunk_allocs, 0,
1826 "Current number of pv entry chunks allocated");
1827 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_frees, CTLFLAG_RD, &pc_chunk_frees, 0,
1828 "Current number of pv entry chunks frees");
1829 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_tryfail, CTLFLAG_RD, &pc_chunk_tryfail, 0,
1830 "Number of times tried to get a chunk page but failed.");
1832 static long pv_entry_frees, pv_entry_allocs, pv_entry_count;
1833 static int pv_entry_spare;
1835 SYSCTL_LONG(_vm_pmap, OID_AUTO, pv_entry_frees, CTLFLAG_RD, &pv_entry_frees, 0,
1836 "Current number of pv entry frees");
1837 SYSCTL_LONG(_vm_pmap, OID_AUTO, pv_entry_allocs, CTLFLAG_RD, &pv_entry_allocs, 0,
1838 "Current number of pv entry allocs");
1839 SYSCTL_LONG(_vm_pmap, OID_AUTO, pv_entry_count, CTLFLAG_RD, &pv_entry_count, 0,
1840 "Current number of pv entries");
1841 SYSCTL_INT(_vm_pmap, OID_AUTO, pv_entry_spare, CTLFLAG_RD, &pv_entry_spare, 0,
1842 "Current number of spare pv entries");
1847 * We are in a serious low memory condition. Resort to
1848 * drastic measures to free some pages so we can allocate
1849 * another pv entry chunk.
1851 * Returns NULL if PV entries were reclaimed from the specified pmap.
1853 * We do not, however, unmap 2mpages because subsequent accesses will
1854 * allocate per-page pv entries until repromotion occurs, thereby
1855 * exacerbating the shortage of free pv entries.
1858 reclaim_pv_chunk(pmap_t locked_pmap, struct rwlock **lockp)
1860 struct pv_chunk *pc, *pc_marker, *pc_marker_end;
1861 struct pv_chunk_header pc_marker_b, pc_marker_end_b;
1862 struct md_page *pvh;
1864 pmap_t next_pmap, pmap;
1865 pt_entry_t *pte, tpte;
1869 struct spglist free;
1871 int bit, field, freed, lvl;
1872 static int active_reclaims = 0;
1874 PMAP_LOCK_ASSERT(locked_pmap, MA_OWNED);
1875 KASSERT(lockp != NULL, ("reclaim_pv_chunk: lockp is NULL"));
1880 bzero(&pc_marker_b, sizeof(pc_marker_b));
1881 bzero(&pc_marker_end_b, sizeof(pc_marker_end_b));
1882 pc_marker = (struct pv_chunk *)&pc_marker_b;
1883 pc_marker_end = (struct pv_chunk *)&pc_marker_end_b;
1885 mtx_lock(&pv_chunks_mutex);
1887 TAILQ_INSERT_HEAD(&pv_chunks, pc_marker, pc_lru);
1888 TAILQ_INSERT_TAIL(&pv_chunks, pc_marker_end, pc_lru);
1889 while ((pc = TAILQ_NEXT(pc_marker, pc_lru)) != pc_marker_end &&
1890 SLIST_EMPTY(&free)) {
1891 next_pmap = pc->pc_pmap;
1892 if (next_pmap == NULL) {
1894 * The next chunk is a marker. However, it is
1895 * not our marker, so active_reclaims must be
1896 * > 1. Consequently, the next_chunk code
1897 * will not rotate the pv_chunks list.
1901 mtx_unlock(&pv_chunks_mutex);
1904 * A pv_chunk can only be removed from the pc_lru list
1905 * when both pv_chunks_mutex is owned and the
1906 * corresponding pmap is locked.
1908 if (pmap != next_pmap) {
1909 if (pmap != NULL && pmap != locked_pmap)
1912 /* Avoid deadlock and lock recursion. */
1913 if (pmap > locked_pmap) {
1914 RELEASE_PV_LIST_LOCK(lockp);
1916 mtx_lock(&pv_chunks_mutex);
1918 } else if (pmap != locked_pmap) {
1919 if (PMAP_TRYLOCK(pmap)) {
1920 mtx_lock(&pv_chunks_mutex);
1923 pmap = NULL; /* pmap is not locked */
1924 mtx_lock(&pv_chunks_mutex);
1925 pc = TAILQ_NEXT(pc_marker, pc_lru);
1927 pc->pc_pmap != next_pmap)
1935 * Destroy every non-wired, 4 KB page mapping in the chunk.
1938 for (field = 0; field < _NPCM; field++) {
1939 for (inuse = ~pc->pc_map[field] & pc_freemask[field];
1940 inuse != 0; inuse &= ~(1UL << bit)) {
1941 bit = ffsl(inuse) - 1;
1942 pv = &pc->pc_pventry[field * 64 + bit];
1944 pde = pmap_pde(pmap, va, &lvl);
1947 pte = pmap_l2_to_l3(pde, va);
1948 tpte = pmap_load(pte);
1949 if ((tpte & ATTR_SW_WIRED) != 0)
1951 tpte = pmap_load_clear(pte);
1952 pmap_invalidate_page(pmap, va);
1953 m = PHYS_TO_VM_PAGE(tpte & ~ATTR_MASK);
1954 if (pmap_page_dirty(tpte))
1956 if ((tpte & ATTR_AF) != 0)
1957 vm_page_aflag_set(m, PGA_REFERENCED);
1958 CHANGE_PV_LIST_LOCK_TO_VM_PAGE(lockp, m);
1959 TAILQ_REMOVE(&m->md.pv_list, pv, pv_next);
1961 if (TAILQ_EMPTY(&m->md.pv_list) &&
1962 (m->flags & PG_FICTITIOUS) == 0) {
1963 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
1964 if (TAILQ_EMPTY(&pvh->pv_list)) {
1965 vm_page_aflag_clear(m,
1969 pc->pc_map[field] |= 1UL << bit;
1970 pmap_unuse_pt(pmap, va, pmap_load(pde), &free);
1975 mtx_lock(&pv_chunks_mutex);
1978 /* Every freed mapping is for a 4 KB page. */
1979 pmap_resident_count_dec(pmap, freed);
1980 PV_STAT(atomic_add_long(&pv_entry_frees, freed));
1981 PV_STAT(atomic_add_int(&pv_entry_spare, freed));
1982 PV_STAT(atomic_subtract_long(&pv_entry_count, freed));
1983 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
1984 if (pc->pc_map[0] == PC_FREE0 && pc->pc_map[1] == PC_FREE1 &&
1985 pc->pc_map[2] == PC_FREE2) {
1986 PV_STAT(atomic_subtract_int(&pv_entry_spare, _NPCPV));
1987 PV_STAT(atomic_subtract_int(&pc_chunk_count, 1));
1988 PV_STAT(atomic_add_int(&pc_chunk_frees, 1));
1989 /* Entire chunk is free; return it. */
1990 m_pc = PHYS_TO_VM_PAGE(DMAP_TO_PHYS((vm_offset_t)pc));
1991 dump_drop_page(m_pc->phys_addr);
1992 mtx_lock(&pv_chunks_mutex);
1993 TAILQ_REMOVE(&pv_chunks, pc, pc_lru);
1996 TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc, pc_list);
1997 mtx_lock(&pv_chunks_mutex);
1998 /* One freed pv entry in locked_pmap is sufficient. */
1999 if (pmap == locked_pmap)
2003 TAILQ_REMOVE(&pv_chunks, pc_marker, pc_lru);
2004 TAILQ_INSERT_AFTER(&pv_chunks, pc, pc_marker, pc_lru);
2005 if (active_reclaims == 1 && pmap != NULL) {
2007 * Rotate the pv chunks list so that we do not
2008 * scan the same pv chunks that could not be
2009 * freed (because they contained a wired
2010 * and/or superpage mapping) on every
2011 * invocation of reclaim_pv_chunk().
2013 while ((pc = TAILQ_FIRST(&pv_chunks)) != pc_marker) {
2014 MPASS(pc->pc_pmap != NULL);
2015 TAILQ_REMOVE(&pv_chunks, pc, pc_lru);
2016 TAILQ_INSERT_TAIL(&pv_chunks, pc, pc_lru);
2020 TAILQ_REMOVE(&pv_chunks, pc_marker, pc_lru);
2021 TAILQ_REMOVE(&pv_chunks, pc_marker_end, pc_lru);
2023 mtx_unlock(&pv_chunks_mutex);
2024 if (pmap != NULL && pmap != locked_pmap)
2026 if (m_pc == NULL && !SLIST_EMPTY(&free)) {
2027 m_pc = SLIST_FIRST(&free);
2028 SLIST_REMOVE_HEAD(&free, plinks.s.ss);
2029 /* Recycle a freed page table page. */
2030 m_pc->wire_count = 1;
2032 vm_page_free_pages_toq(&free, true);
2037 * free the pv_entry back to the free list
2040 free_pv_entry(pmap_t pmap, pv_entry_t pv)
2042 struct pv_chunk *pc;
2043 int idx, field, bit;
2045 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2046 PV_STAT(atomic_add_long(&pv_entry_frees, 1));
2047 PV_STAT(atomic_add_int(&pv_entry_spare, 1));
2048 PV_STAT(atomic_subtract_long(&pv_entry_count, 1));
2049 pc = pv_to_chunk(pv);
2050 idx = pv - &pc->pc_pventry[0];
2053 pc->pc_map[field] |= 1ul << bit;
2054 if (pc->pc_map[0] != PC_FREE0 || pc->pc_map[1] != PC_FREE1 ||
2055 pc->pc_map[2] != PC_FREE2) {
2056 /* 98% of the time, pc is already at the head of the list. */
2057 if (__predict_false(pc != TAILQ_FIRST(&pmap->pm_pvchunk))) {
2058 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
2059 TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc, pc_list);
2063 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
2068 free_pv_chunk(struct pv_chunk *pc)
2072 mtx_lock(&pv_chunks_mutex);
2073 TAILQ_REMOVE(&pv_chunks, pc, pc_lru);
2074 mtx_unlock(&pv_chunks_mutex);
2075 PV_STAT(atomic_subtract_int(&pv_entry_spare, _NPCPV));
2076 PV_STAT(atomic_subtract_int(&pc_chunk_count, 1));
2077 PV_STAT(atomic_add_int(&pc_chunk_frees, 1));
2078 /* entire chunk is free, return it */
2079 m = PHYS_TO_VM_PAGE(DMAP_TO_PHYS((vm_offset_t)pc));
2080 dump_drop_page(m->phys_addr);
2081 vm_page_unwire_noq(m);
2086 * Returns a new PV entry, allocating a new PV chunk from the system when
2087 * needed. If this PV chunk allocation fails and a PV list lock pointer was
2088 * given, a PV chunk is reclaimed from an arbitrary pmap. Otherwise, NULL is
2091 * The given PV list lock may be released.
2094 get_pv_entry(pmap_t pmap, struct rwlock **lockp)
2098 struct pv_chunk *pc;
2101 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2102 PV_STAT(atomic_add_long(&pv_entry_allocs, 1));
2104 pc = TAILQ_FIRST(&pmap->pm_pvchunk);
2106 for (field = 0; field < _NPCM; field++) {
2107 if (pc->pc_map[field]) {
2108 bit = ffsl(pc->pc_map[field]) - 1;
2112 if (field < _NPCM) {
2113 pv = &pc->pc_pventry[field * 64 + bit];
2114 pc->pc_map[field] &= ~(1ul << bit);
2115 /* If this was the last item, move it to tail */
2116 if (pc->pc_map[0] == 0 && pc->pc_map[1] == 0 &&
2117 pc->pc_map[2] == 0) {
2118 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
2119 TAILQ_INSERT_TAIL(&pmap->pm_pvchunk, pc,
2122 PV_STAT(atomic_add_long(&pv_entry_count, 1));
2123 PV_STAT(atomic_subtract_int(&pv_entry_spare, 1));
2127 /* No free items, allocate another chunk */
2128 m = vm_page_alloc(NULL, 0, VM_ALLOC_NORMAL | VM_ALLOC_NOOBJ |
2131 if (lockp == NULL) {
2132 PV_STAT(pc_chunk_tryfail++);
2135 m = reclaim_pv_chunk(pmap, lockp);
2139 PV_STAT(atomic_add_int(&pc_chunk_count, 1));
2140 PV_STAT(atomic_add_int(&pc_chunk_allocs, 1));
2141 dump_add_page(m->phys_addr);
2142 pc = (void *)PHYS_TO_DMAP(m->phys_addr);
2144 pc->pc_map[0] = PC_FREE0 & ~1ul; /* preallocated bit 0 */
2145 pc->pc_map[1] = PC_FREE1;
2146 pc->pc_map[2] = PC_FREE2;
2147 mtx_lock(&pv_chunks_mutex);
2148 TAILQ_INSERT_TAIL(&pv_chunks, pc, pc_lru);
2149 mtx_unlock(&pv_chunks_mutex);
2150 pv = &pc->pc_pventry[0];
2151 TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc, pc_list);
2152 PV_STAT(atomic_add_long(&pv_entry_count, 1));
2153 PV_STAT(atomic_add_int(&pv_entry_spare, _NPCPV - 1));
2158 * Ensure that the number of spare PV entries in the specified pmap meets or
2159 * exceeds the given count, "needed".
2161 * The given PV list lock may be released.
2164 reserve_pv_entries(pmap_t pmap, int needed, struct rwlock **lockp)
2166 struct pch new_tail;
2167 struct pv_chunk *pc;
2172 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2173 KASSERT(lockp != NULL, ("reserve_pv_entries: lockp is NULL"));
2176 * Newly allocated PV chunks must be stored in a private list until
2177 * the required number of PV chunks have been allocated. Otherwise,
2178 * reclaim_pv_chunk() could recycle one of these chunks. In
2179 * contrast, these chunks must be added to the pmap upon allocation.
2181 TAILQ_INIT(&new_tail);
2184 TAILQ_FOREACH(pc, &pmap->pm_pvchunk, pc_list) {
2185 bit_count((bitstr_t *)pc->pc_map, 0,
2186 sizeof(pc->pc_map) * NBBY, &free);
2190 if (avail >= needed)
2193 for (reclaimed = false; avail < needed; avail += _NPCPV) {
2194 m = vm_page_alloc(NULL, 0, VM_ALLOC_NORMAL | VM_ALLOC_NOOBJ |
2197 m = reclaim_pv_chunk(pmap, lockp);
2202 PV_STAT(atomic_add_int(&pc_chunk_count, 1));
2203 PV_STAT(atomic_add_int(&pc_chunk_allocs, 1));
2204 dump_add_page(m->phys_addr);
2205 pc = (void *)PHYS_TO_DMAP(m->phys_addr);
2207 pc->pc_map[0] = PC_FREE0;
2208 pc->pc_map[1] = PC_FREE1;
2209 pc->pc_map[2] = PC_FREE2;
2210 TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc, pc_list);
2211 TAILQ_INSERT_TAIL(&new_tail, pc, pc_lru);
2212 PV_STAT(atomic_add_int(&pv_entry_spare, _NPCPV));
2215 * The reclaim might have freed a chunk from the current pmap.
2216 * If that chunk contained available entries, we need to
2217 * re-count the number of available entries.
2222 if (!TAILQ_EMPTY(&new_tail)) {
2223 mtx_lock(&pv_chunks_mutex);
2224 TAILQ_CONCAT(&pv_chunks, &new_tail, pc_lru);
2225 mtx_unlock(&pv_chunks_mutex);
2230 * First find and then remove the pv entry for the specified pmap and virtual
2231 * address from the specified pv list. Returns the pv entry if found and NULL
2232 * otherwise. This operation can be performed on pv lists for either 4KB or
2233 * 2MB page mappings.
2235 static __inline pv_entry_t
2236 pmap_pvh_remove(struct md_page *pvh, pmap_t pmap, vm_offset_t va)
2240 TAILQ_FOREACH(pv, &pvh->pv_list, pv_next) {
2241 if (pmap == PV_PMAP(pv) && va == pv->pv_va) {
2242 TAILQ_REMOVE(&pvh->pv_list, pv, pv_next);
2251 * After demotion from a 2MB page mapping to 512 4KB page mappings,
2252 * destroy the pv entry for the 2MB page mapping and reinstantiate the pv
2253 * entries for each of the 4KB page mappings.
2256 pmap_pv_demote_l2(pmap_t pmap, vm_offset_t va, vm_paddr_t pa,
2257 struct rwlock **lockp)
2259 struct md_page *pvh;
2260 struct pv_chunk *pc;
2262 vm_offset_t va_last;
2266 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2267 KASSERT((va & L2_OFFSET) == 0,
2268 ("pmap_pv_demote_l2: va is not 2mpage aligned"));
2269 KASSERT((pa & L2_OFFSET) == 0,
2270 ("pmap_pv_demote_l2: pa is not 2mpage aligned"));
2271 CHANGE_PV_LIST_LOCK_TO_PHYS(lockp, pa);
2274 * Transfer the 2mpage's pv entry for this mapping to the first
2275 * page's pv list. Once this transfer begins, the pv list lock
2276 * must not be released until the last pv entry is reinstantiated.
2278 pvh = pa_to_pvh(pa);
2279 pv = pmap_pvh_remove(pvh, pmap, va);
2280 KASSERT(pv != NULL, ("pmap_pv_demote_l2: pv not found"));
2281 m = PHYS_TO_VM_PAGE(pa);
2282 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_next);
2284 /* Instantiate the remaining Ln_ENTRIES - 1 pv entries. */
2285 PV_STAT(atomic_add_long(&pv_entry_allocs, Ln_ENTRIES - 1));
2286 va_last = va + L2_SIZE - PAGE_SIZE;
2288 pc = TAILQ_FIRST(&pmap->pm_pvchunk);
2289 KASSERT(pc->pc_map[0] != 0 || pc->pc_map[1] != 0 ||
2290 pc->pc_map[2] != 0, ("pmap_pv_demote_l2: missing spare"));
2291 for (field = 0; field < _NPCM; field++) {
2292 while (pc->pc_map[field]) {
2293 bit = ffsl(pc->pc_map[field]) - 1;
2294 pc->pc_map[field] &= ~(1ul << bit);
2295 pv = &pc->pc_pventry[field * 64 + bit];
2299 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2300 ("pmap_pv_demote_l2: page %p is not managed", m));
2301 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_next);
2307 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
2308 TAILQ_INSERT_TAIL(&pmap->pm_pvchunk, pc, pc_list);
2311 if (pc->pc_map[0] == 0 && pc->pc_map[1] == 0 && pc->pc_map[2] == 0) {
2312 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
2313 TAILQ_INSERT_TAIL(&pmap->pm_pvchunk, pc, pc_list);
2315 PV_STAT(atomic_add_long(&pv_entry_count, Ln_ENTRIES - 1));
2316 PV_STAT(atomic_subtract_int(&pv_entry_spare, Ln_ENTRIES - 1));
2320 * First find and then destroy the pv entry for the specified pmap and virtual
2321 * address. This operation can be performed on pv lists for either 4KB or 2MB
2325 pmap_pvh_free(struct md_page *pvh, pmap_t pmap, vm_offset_t va)
2329 pv = pmap_pvh_remove(pvh, pmap, va);
2330 KASSERT(pv != NULL, ("pmap_pvh_free: pv not found"));
2331 free_pv_entry(pmap, pv);
2335 * Conditionally create the PV entry for a 4KB page mapping if the required
2336 * memory can be allocated without resorting to reclamation.
2339 pmap_try_insert_pv_entry(pmap_t pmap, vm_offset_t va, vm_page_t m,
2340 struct rwlock **lockp)
2344 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2345 /* Pass NULL instead of the lock pointer to disable reclamation. */
2346 if ((pv = get_pv_entry(pmap, NULL)) != NULL) {
2348 CHANGE_PV_LIST_LOCK_TO_VM_PAGE(lockp, m);
2349 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_next);
2357 * Create the PV entry for a 2MB page mapping. Always returns true unless the
2358 * flag PMAP_ENTER_NORECLAIM is specified. If that flag is specified, returns
2359 * false if the PV entry cannot be allocated without resorting to reclamation.
2362 pmap_pv_insert_l2(pmap_t pmap, vm_offset_t va, pd_entry_t l2e, u_int flags,
2363 struct rwlock **lockp)
2365 struct md_page *pvh;
2369 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2370 /* Pass NULL instead of the lock pointer to disable reclamation. */
2371 if ((pv = get_pv_entry(pmap, (flags & PMAP_ENTER_NORECLAIM) != 0 ?
2372 NULL : lockp)) == NULL)
2375 pa = l2e & ~ATTR_MASK;
2376 CHANGE_PV_LIST_LOCK_TO_PHYS(lockp, pa);
2377 pvh = pa_to_pvh(pa);
2378 TAILQ_INSERT_TAIL(&pvh->pv_list, pv, pv_next);
2384 pmap_remove_kernel_l2(pmap_t pmap, pt_entry_t *l2, vm_offset_t va)
2386 pt_entry_t newl2, oldl2;
2390 KASSERT(!VIRT_IN_DMAP(va), ("removing direct mapping of %#lx", va));
2391 KASSERT(pmap == kernel_pmap, ("pmap %p is not kernel_pmap", pmap));
2392 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2394 ml3 = pmap_remove_pt_page(pmap, va);
2396 panic("pmap_remove_kernel_l2: Missing pt page");
2398 ml3pa = VM_PAGE_TO_PHYS(ml3);
2399 newl2 = ml3pa | L2_TABLE;
2402 * If this page table page was unmapped by a promotion, then it
2403 * contains valid mappings. Zero it to invalidate those mappings.
2405 if (ml3->valid != 0)
2406 pagezero((void *)PHYS_TO_DMAP(ml3pa));
2409 * Demote the mapping. The caller must have already invalidated the
2410 * mapping (i.e., the "break" in break-before-make).
2412 oldl2 = pmap_load_store(l2, newl2);
2413 KASSERT(oldl2 == 0, ("%s: found existing mapping at %p: %#lx",
2414 __func__, l2, oldl2));
2418 * pmap_remove_l2: Do the things to unmap a level 2 superpage.
2421 pmap_remove_l2(pmap_t pmap, pt_entry_t *l2, vm_offset_t sva,
2422 pd_entry_t l1e, struct spglist *free, struct rwlock **lockp)
2424 struct md_page *pvh;
2426 vm_offset_t eva, va;
2429 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2430 KASSERT((sva & L2_OFFSET) == 0, ("pmap_remove_l2: sva is not aligned"));
2431 old_l2 = pmap_load_clear(l2);
2432 KASSERT((old_l2 & ATTR_DESCR_MASK) == L2_BLOCK,
2433 ("pmap_remove_l2: L2e %lx is not a block mapping", old_l2));
2436 * Since a promotion must break the 4KB page mappings before making
2437 * the 2MB page mapping, a pmap_invalidate_page() suffices.
2439 pmap_invalidate_page(pmap, sva);
2441 if (old_l2 & ATTR_SW_WIRED)
2442 pmap->pm_stats.wired_count -= L2_SIZE / PAGE_SIZE;
2443 pmap_resident_count_dec(pmap, L2_SIZE / PAGE_SIZE);
2444 if (old_l2 & ATTR_SW_MANAGED) {
2445 CHANGE_PV_LIST_LOCK_TO_PHYS(lockp, old_l2 & ~ATTR_MASK);
2446 pvh = pa_to_pvh(old_l2 & ~ATTR_MASK);
2447 pmap_pvh_free(pvh, pmap, sva);
2448 eva = sva + L2_SIZE;
2449 for (va = sva, m = PHYS_TO_VM_PAGE(old_l2 & ~ATTR_MASK);
2450 va < eva; va += PAGE_SIZE, m++) {
2451 if (pmap_page_dirty(old_l2))
2453 if (old_l2 & ATTR_AF)
2454 vm_page_aflag_set(m, PGA_REFERENCED);
2455 if (TAILQ_EMPTY(&m->md.pv_list) &&
2456 TAILQ_EMPTY(&pvh->pv_list))
2457 vm_page_aflag_clear(m, PGA_WRITEABLE);
2460 if (pmap == kernel_pmap) {
2461 pmap_remove_kernel_l2(pmap, l2, sva);
2463 ml3 = pmap_remove_pt_page(pmap, sva);
2465 KASSERT(ml3->valid == VM_PAGE_BITS_ALL,
2466 ("pmap_remove_l2: l3 page not promoted"));
2467 pmap_resident_count_dec(pmap, 1);
2468 KASSERT(ml3->wire_count == NL3PG,
2469 ("pmap_remove_l2: l3 page wire count error"));
2470 ml3->wire_count = 0;
2471 pmap_add_delayed_free_list(ml3, free, FALSE);
2474 return (pmap_unuse_pt(pmap, sva, l1e, free));
2478 * pmap_remove_l3: do the things to unmap a page in a process
2481 pmap_remove_l3(pmap_t pmap, pt_entry_t *l3, vm_offset_t va,
2482 pd_entry_t l2e, struct spglist *free, struct rwlock **lockp)
2484 struct md_page *pvh;
2488 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2489 old_l3 = pmap_load_clear(l3);
2490 pmap_invalidate_page(pmap, va);
2491 if (old_l3 & ATTR_SW_WIRED)
2492 pmap->pm_stats.wired_count -= 1;
2493 pmap_resident_count_dec(pmap, 1);
2494 if (old_l3 & ATTR_SW_MANAGED) {
2495 m = PHYS_TO_VM_PAGE(old_l3 & ~ATTR_MASK);
2496 if (pmap_page_dirty(old_l3))
2498 if (old_l3 & ATTR_AF)
2499 vm_page_aflag_set(m, PGA_REFERENCED);
2500 CHANGE_PV_LIST_LOCK_TO_VM_PAGE(lockp, m);
2501 pmap_pvh_free(&m->md, pmap, va);
2502 if (TAILQ_EMPTY(&m->md.pv_list) &&
2503 (m->flags & PG_FICTITIOUS) == 0) {
2504 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
2505 if (TAILQ_EMPTY(&pvh->pv_list))
2506 vm_page_aflag_clear(m, PGA_WRITEABLE);
2509 return (pmap_unuse_pt(pmap, va, l2e, free));
2513 * Remove the given range of addresses from the specified map.
2515 * It is assumed that the start and end are properly
2516 * rounded to the page size.
2519 pmap_remove(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
2521 struct rwlock *lock;
2522 vm_offset_t va, va_next;
2523 pd_entry_t *l0, *l1, *l2;
2524 pt_entry_t l3_paddr, *l3;
2525 struct spglist free;
2528 * Perform an unsynchronized read. This is, however, safe.
2530 if (pmap->pm_stats.resident_count == 0)
2538 for (; sva < eva; sva = va_next) {
2540 if (pmap->pm_stats.resident_count == 0)
2543 l0 = pmap_l0(pmap, sva);
2544 if (pmap_load(l0) == 0) {
2545 va_next = (sva + L0_SIZE) & ~L0_OFFSET;
2551 l1 = pmap_l0_to_l1(l0, sva);
2552 if (pmap_load(l1) == 0) {
2553 va_next = (sva + L1_SIZE) & ~L1_OFFSET;
2560 * Calculate index for next page table.
2562 va_next = (sva + L2_SIZE) & ~L2_OFFSET;
2566 l2 = pmap_l1_to_l2(l1, sva);
2570 l3_paddr = pmap_load(l2);
2572 if ((l3_paddr & ATTR_DESCR_MASK) == L2_BLOCK) {
2573 if (sva + L2_SIZE == va_next && eva >= va_next) {
2574 pmap_remove_l2(pmap, l2, sva, pmap_load(l1),
2577 } else if (pmap_demote_l2_locked(pmap, l2, sva,
2580 l3_paddr = pmap_load(l2);
2584 * Weed out invalid mappings.
2586 if ((l3_paddr & ATTR_DESCR_MASK) != L2_TABLE)
2590 * Limit our scan to either the end of the va represented
2591 * by the current page table page, or to the end of the
2592 * range being removed.
2598 for (l3 = pmap_l2_to_l3(l2, sva); sva != va_next; l3++,
2601 panic("l3 == NULL");
2602 if (pmap_load(l3) == 0) {
2603 if (va != va_next) {
2604 pmap_invalidate_range(pmap, va, sva);
2611 if (pmap_remove_l3(pmap, l3, sva, l3_paddr, &free,
2618 pmap_invalidate_range(pmap, va, sva);
2623 vm_page_free_pages_toq(&free, true);
2627 * Routine: pmap_remove_all
2629 * Removes this physical page from
2630 * all physical maps in which it resides.
2631 * Reflects back modify bits to the pager.
2634 * Original versions of this routine were very
2635 * inefficient because they iteratively called
2636 * pmap_remove (slow...)
2640 pmap_remove_all(vm_page_t m)
2642 struct md_page *pvh;
2645 struct rwlock *lock;
2646 pd_entry_t *pde, tpde;
2647 pt_entry_t *pte, tpte;
2649 struct spglist free;
2650 int lvl, pvh_gen, md_gen;
2652 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2653 ("pmap_remove_all: page %p is not managed", m));
2655 lock = VM_PAGE_TO_PV_LIST_LOCK(m);
2656 pvh = (m->flags & PG_FICTITIOUS) != 0 ? &pv_dummy :
2657 pa_to_pvh(VM_PAGE_TO_PHYS(m));
2660 while ((pv = TAILQ_FIRST(&pvh->pv_list)) != NULL) {
2662 if (!PMAP_TRYLOCK(pmap)) {
2663 pvh_gen = pvh->pv_gen;
2667 if (pvh_gen != pvh->pv_gen) {
2674 pte = pmap_pte(pmap, va, &lvl);
2675 KASSERT(pte != NULL,
2676 ("pmap_remove_all: no page table entry found"));
2678 ("pmap_remove_all: invalid pte level %d", lvl));
2680 pmap_demote_l2_locked(pmap, pte, va, &lock);
2683 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
2685 if (!PMAP_TRYLOCK(pmap)) {
2686 pvh_gen = pvh->pv_gen;
2687 md_gen = m->md.pv_gen;
2691 if (pvh_gen != pvh->pv_gen || md_gen != m->md.pv_gen) {
2697 pmap_resident_count_dec(pmap, 1);
2699 pde = pmap_pde(pmap, pv->pv_va, &lvl);
2700 KASSERT(pde != NULL,
2701 ("pmap_remove_all: no page directory entry found"));
2703 ("pmap_remove_all: invalid pde level %d", lvl));
2704 tpde = pmap_load(pde);
2706 pte = pmap_l2_to_l3(pde, pv->pv_va);
2707 tpte = pmap_load(pte);
2708 pmap_load_clear(pte);
2709 pmap_invalidate_page(pmap, pv->pv_va);
2710 if (tpte & ATTR_SW_WIRED)
2711 pmap->pm_stats.wired_count--;
2712 if ((tpte & ATTR_AF) != 0)
2713 vm_page_aflag_set(m, PGA_REFERENCED);
2716 * Update the vm_page_t clean and reference bits.
2718 if (pmap_page_dirty(tpte))
2720 pmap_unuse_pt(pmap, pv->pv_va, tpde, &free);
2721 TAILQ_REMOVE(&m->md.pv_list, pv, pv_next);
2723 free_pv_entry(pmap, pv);
2726 vm_page_aflag_clear(m, PGA_WRITEABLE);
2728 vm_page_free_pages_toq(&free, true);
2732 * Set the physical protection on the
2733 * specified range of this map as requested.
2736 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
2738 vm_offset_t va, va_next;
2739 pd_entry_t *l0, *l1, *l2;
2740 pt_entry_t *l3p, l3, nbits;
2742 KASSERT((prot & ~VM_PROT_ALL) == 0, ("invalid prot %x", prot));
2743 if (prot == VM_PROT_NONE) {
2744 pmap_remove(pmap, sva, eva);
2748 if ((prot & (VM_PROT_WRITE | VM_PROT_EXECUTE)) ==
2749 (VM_PROT_WRITE | VM_PROT_EXECUTE))
2753 for (; sva < eva; sva = va_next) {
2755 l0 = pmap_l0(pmap, sva);
2756 if (pmap_load(l0) == 0) {
2757 va_next = (sva + L0_SIZE) & ~L0_OFFSET;
2763 l1 = pmap_l0_to_l1(l0, sva);
2764 if (pmap_load(l1) == 0) {
2765 va_next = (sva + L1_SIZE) & ~L1_OFFSET;
2771 va_next = (sva + L2_SIZE) & ~L2_OFFSET;
2775 l2 = pmap_l1_to_l2(l1, sva);
2776 if (pmap_load(l2) == 0)
2779 if ((pmap_load(l2) & ATTR_DESCR_MASK) == L2_BLOCK) {
2780 l3p = pmap_demote_l2(pmap, l2, sva);
2784 KASSERT((pmap_load(l2) & ATTR_DESCR_MASK) == L2_TABLE,
2785 ("pmap_protect: Invalid L2 entry after demotion"));
2791 for (l3p = pmap_l2_to_l3(l2, sva); sva != va_next; l3p++,
2793 l3 = pmap_load(l3p);
2794 if (!pmap_l3_valid(l3)) {
2795 if (va != va_next) {
2796 pmap_invalidate_range(pmap, va, sva);
2805 if ((prot & VM_PROT_WRITE) == 0) {
2806 if ((l3 & ATTR_SW_MANAGED) &&
2807 pmap_page_dirty(l3)) {
2808 vm_page_dirty(PHYS_TO_VM_PAGE(l3 &
2811 nbits |= ATTR_AP(ATTR_AP_RO);
2813 if ((prot & VM_PROT_EXECUTE) == 0)
2816 pmap_set(l3p, nbits);
2819 pmap_invalidate_range(pmap, va, sva);
2825 * Inserts the specified page table page into the specified pmap's collection
2826 * of idle page table pages. Each of a pmap's page table pages is responsible
2827 * for mapping a distinct range of virtual addresses. The pmap's collection is
2828 * ordered by this virtual address range.
2830 * If "promoted" is false, then the page table page "mpte" must be zero filled.
2833 pmap_insert_pt_page(pmap_t pmap, vm_page_t mpte, bool promoted)
2836 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2837 mpte->valid = promoted ? VM_PAGE_BITS_ALL : 0;
2838 return (vm_radix_insert(&pmap->pm_root, mpte));
2842 * Removes the page table page mapping the specified virtual address from the
2843 * specified pmap's collection of idle page table pages, and returns it.
2844 * Otherwise, returns NULL if there is no page table page corresponding to the
2845 * specified virtual address.
2847 static __inline vm_page_t
2848 pmap_remove_pt_page(pmap_t pmap, vm_offset_t va)
2851 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2852 return (vm_radix_remove(&pmap->pm_root, pmap_l2_pindex(va)));
2856 * Performs a break-before-make update of a pmap entry. This is needed when
2857 * either promoting or demoting pages to ensure the TLB doesn't get into an
2858 * inconsistent state.
2861 pmap_update_entry(pmap_t pmap, pd_entry_t *pte, pd_entry_t newpte,
2862 vm_offset_t va, vm_size_t size)
2866 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2869 * Ensure we don't get switched out with the page table in an
2870 * inconsistent state. We also need to ensure no interrupts fire
2871 * as they may make use of an address we are about to invalidate.
2873 intr = intr_disable();
2876 /* Clear the old mapping */
2877 pmap_load_clear(pte);
2878 pmap_invalidate_range_nopin(pmap, va, va + size);
2880 /* Create the new mapping */
2881 pmap_load_store(pte, newpte);
2888 #if VM_NRESERVLEVEL > 0
2890 * After promotion from 512 4KB page mappings to a single 2MB page mapping,
2891 * replace the many pv entries for the 4KB page mappings by a single pv entry
2892 * for the 2MB page mapping.
2895 pmap_pv_promote_l2(pmap_t pmap, vm_offset_t va, vm_paddr_t pa,
2896 struct rwlock **lockp)
2898 struct md_page *pvh;
2900 vm_offset_t va_last;
2903 KASSERT((pa & L2_OFFSET) == 0,
2904 ("pmap_pv_promote_l2: pa is not 2mpage aligned"));
2905 CHANGE_PV_LIST_LOCK_TO_PHYS(lockp, pa);
2908 * Transfer the first page's pv entry for this mapping to the 2mpage's
2909 * pv list. Aside from avoiding the cost of a call to get_pv_entry(),
2910 * a transfer avoids the possibility that get_pv_entry() calls
2911 * reclaim_pv_chunk() and that reclaim_pv_chunk() removes one of the
2912 * mappings that is being promoted.
2914 m = PHYS_TO_VM_PAGE(pa);
2915 va = va & ~L2_OFFSET;
2916 pv = pmap_pvh_remove(&m->md, pmap, va);
2917 KASSERT(pv != NULL, ("pmap_pv_promote_l2: pv not found"));
2918 pvh = pa_to_pvh(pa);
2919 TAILQ_INSERT_TAIL(&pvh->pv_list, pv, pv_next);
2921 /* Free the remaining NPTEPG - 1 pv entries. */
2922 va_last = va + L2_SIZE - PAGE_SIZE;
2926 pmap_pvh_free(&m->md, pmap, va);
2927 } while (va < va_last);
2931 * Tries to promote the 512, contiguous 4KB page mappings that are within a
2932 * single level 2 table entry to a single 2MB page mapping. For promotion
2933 * to occur, two conditions must be met: (1) the 4KB page mappings must map
2934 * aligned, contiguous physical memory and (2) the 4KB page mappings must have
2935 * identical characteristics.
2938 pmap_promote_l2(pmap_t pmap, pd_entry_t *l2, vm_offset_t va,
2939 struct rwlock **lockp)
2941 pt_entry_t *firstl3, *l3, newl2, oldl3, pa;
2945 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2947 sva = va & ~L2_OFFSET;
2948 firstl3 = pmap_l2_to_l3(l2, sva);
2949 newl2 = pmap_load(firstl3);
2951 /* Check the alingment is valid */
2952 if (((newl2 & ~ATTR_MASK) & L2_OFFSET) != 0) {
2953 atomic_add_long(&pmap_l2_p_failures, 1);
2954 CTR2(KTR_PMAP, "pmap_promote_l2: failure for va %#lx"
2955 " in pmap %p", va, pmap);
2959 pa = newl2 + L2_SIZE - PAGE_SIZE;
2960 for (l3 = firstl3 + NL3PG - 1; l3 > firstl3; l3--) {
2961 oldl3 = pmap_load(l3);
2963 atomic_add_long(&pmap_l2_p_failures, 1);
2964 CTR2(KTR_PMAP, "pmap_promote_l2: failure for va %#lx"
2965 " in pmap %p", va, pmap);
2972 * Save the page table page in its current state until the L2
2973 * mapping the superpage is demoted by pmap_demote_l2() or
2974 * destroyed by pmap_remove_l3().
2976 mpte = PHYS_TO_VM_PAGE(pmap_load(l2) & ~ATTR_MASK);
2977 KASSERT(mpte >= vm_page_array &&
2978 mpte < &vm_page_array[vm_page_array_size],
2979 ("pmap_promote_l2: page table page is out of range"));
2980 KASSERT(mpte->pindex == pmap_l2_pindex(va),
2981 ("pmap_promote_l2: page table page's pindex is wrong"));
2982 if (pmap_insert_pt_page(pmap, mpte, true)) {
2983 atomic_add_long(&pmap_l2_p_failures, 1);
2985 "pmap_promote_l2: failure for va %#lx in pmap %p", va,
2990 if ((newl2 & ATTR_SW_MANAGED) != 0)
2991 pmap_pv_promote_l2(pmap, va, newl2 & ~ATTR_MASK, lockp);
2993 newl2 &= ~ATTR_DESCR_MASK;
2996 pmap_update_entry(pmap, l2, newl2, sva, L2_SIZE);
2998 atomic_add_long(&pmap_l2_promotions, 1);
2999 CTR2(KTR_PMAP, "pmap_promote_l2: success for va %#lx in pmap %p", va,
3002 #endif /* VM_NRESERVLEVEL > 0 */
3005 * Insert the given physical page (p) at
3006 * the specified virtual address (v) in the
3007 * target physical map with the protection requested.
3009 * If specified, the page will be wired down, meaning
3010 * that the related pte can not be reclaimed.
3012 * NB: This is the only routine which MAY NOT lazy-evaluate
3013 * or lose information. That is, this routine must actually
3014 * insert this page into the given map NOW.
3017 pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
3018 u_int flags, int8_t psind)
3020 struct rwlock *lock;
3022 pt_entry_t new_l3, orig_l3;
3023 pt_entry_t *l2, *l3;
3025 vm_paddr_t opa, pa, l1_pa, l2_pa, l3_pa;
3026 vm_page_t mpte, om, l1_m, l2_m, l3_m;
3030 va = trunc_page(va);
3031 if ((m->oflags & VPO_UNMANAGED) == 0 && !vm_page_xbusied(m))
3032 VM_OBJECT_ASSERT_LOCKED(m->object);
3033 pa = VM_PAGE_TO_PHYS(m);
3034 new_l3 = (pt_entry_t)(pa | ATTR_DEFAULT | ATTR_IDX(m->md.pv_memattr) |
3036 if ((prot & VM_PROT_WRITE) == 0)
3037 new_l3 |= ATTR_AP(ATTR_AP_RO);
3038 if ((prot & VM_PROT_EXECUTE) == 0 || m->md.pv_memattr == DEVICE_MEMORY)
3040 if ((flags & PMAP_ENTER_WIRED) != 0)
3041 new_l3 |= ATTR_SW_WIRED;
3042 if (va < VM_MAXUSER_ADDRESS)
3043 new_l3 |= ATTR_AP(ATTR_AP_USER) | ATTR_PXN;
3044 if ((m->oflags & VPO_UNMANAGED) == 0)
3045 new_l3 |= ATTR_SW_MANAGED;
3047 CTR2(KTR_PMAP, "pmap_enter: %.16lx -> %.16lx", va, pa);
3053 /* Assert the required virtual and physical alignment. */
3054 KASSERT((va & L2_OFFSET) == 0, ("pmap_enter: va unaligned"));
3055 KASSERT(m->psind > 0, ("pmap_enter: m->psind < psind"));
3056 rv = pmap_enter_l2(pmap, va, (new_l3 & ~L3_PAGE) | L2_BLOCK,
3061 pde = pmap_pde(pmap, va, &lvl);
3062 if (pde != NULL && lvl == 1) {
3063 l2 = pmap_l1_to_l2(pde, va);
3064 if ((pmap_load(l2) & ATTR_DESCR_MASK) == L2_BLOCK &&
3065 (l3 = pmap_demote_l2_locked(pmap, l2, va, &lock)) != NULL) {
3066 l3 = &l3[pmap_l3_index(va)];
3067 if (va < VM_MAXUSER_ADDRESS) {
3068 mpte = PHYS_TO_VM_PAGE(
3069 pmap_load(l2) & ~ATTR_MASK);
3076 if (va < VM_MAXUSER_ADDRESS) {
3077 nosleep = (flags & PMAP_ENTER_NOSLEEP) != 0;
3078 mpte = pmap_alloc_l3(pmap, va, nosleep ? NULL : &lock);
3079 if (mpte == NULL && nosleep) {
3080 CTR0(KTR_PMAP, "pmap_enter: mpte == NULL");
3084 return (KERN_RESOURCE_SHORTAGE);
3086 pde = pmap_pde(pmap, va, &lvl);
3087 KASSERT(pde != NULL,
3088 ("pmap_enter: Invalid page entry, va: 0x%lx", va));
3090 ("pmap_enter: Invalid level %d", lvl));
3093 * If we get a level 2 pde it must point to a level 3 entry
3094 * otherwise we will need to create the intermediate tables
3100 /* Get the l0 pde to update */
3101 pde = pmap_l0(pmap, va);
3102 KASSERT(pde != NULL, ("..."));
3104 l1_m = vm_page_alloc(NULL, 0, VM_ALLOC_NORMAL |
3105 VM_ALLOC_NOOBJ | VM_ALLOC_WIRED |
3108 panic("pmap_enter: l1 pte_m == NULL");
3109 if ((l1_m->flags & PG_ZERO) == 0)
3110 pmap_zero_page(l1_m);
3112 l1_pa = VM_PAGE_TO_PHYS(l1_m);
3113 pmap_load_store(pde, l1_pa | L0_TABLE);
3116 /* Get the l1 pde to update */
3117 pde = pmap_l1_to_l2(pde, va);
3118 KASSERT(pde != NULL, ("..."));
3120 l2_m = vm_page_alloc(NULL, 0, VM_ALLOC_NORMAL |
3121 VM_ALLOC_NOOBJ | VM_ALLOC_WIRED |
3124 panic("pmap_enter: l2 pte_m == NULL");
3125 if ((l2_m->flags & PG_ZERO) == 0)
3126 pmap_zero_page(l2_m);
3128 l2_pa = VM_PAGE_TO_PHYS(l2_m);
3129 pmap_load_store(pde, l2_pa | L1_TABLE);
3132 /* Get the l2 pde to update */
3133 pde = pmap_l1_to_l2(pde, va);
3134 KASSERT(pde != NULL, ("..."));
3136 l3_m = vm_page_alloc(NULL, 0, VM_ALLOC_NORMAL |
3137 VM_ALLOC_NOOBJ | VM_ALLOC_WIRED |
3140 panic("pmap_enter: l3 pte_m == NULL");
3141 if ((l3_m->flags & PG_ZERO) == 0)
3142 pmap_zero_page(l3_m);
3144 l3_pa = VM_PAGE_TO_PHYS(l3_m);
3145 pmap_load_store(pde, l3_pa | L2_TABLE);
3150 l3 = pmap_l2_to_l3(pde, va);
3153 orig_l3 = pmap_load(l3);
3154 opa = orig_l3 & ~ATTR_MASK;
3158 * Is the specified virtual address already mapped?
3160 if (pmap_l3_valid(orig_l3)) {
3162 * Wiring change, just update stats. We don't worry about
3163 * wiring PT pages as they remain resident as long as there
3164 * are valid mappings in them. Hence, if a user page is wired,
3165 * the PT page will be also.
3167 if ((flags & PMAP_ENTER_WIRED) != 0 &&
3168 (orig_l3 & ATTR_SW_WIRED) == 0)
3169 pmap->pm_stats.wired_count++;
3170 else if ((flags & PMAP_ENTER_WIRED) == 0 &&
3171 (orig_l3 & ATTR_SW_WIRED) != 0)
3172 pmap->pm_stats.wired_count--;
3175 * Remove the extra PT page reference.
3179 KASSERT(mpte->wire_count > 0,
3180 ("pmap_enter: missing reference to page table page,"
3185 * Has the physical page changed?
3189 * No, might be a protection or wiring change.
3191 if ((orig_l3 & ATTR_SW_MANAGED) != 0) {
3192 if ((new_l3 & ATTR_AP(ATTR_AP_RW)) ==
3193 ATTR_AP(ATTR_AP_RW)) {
3194 vm_page_aflag_set(m, PGA_WRITEABLE);
3201 * The physical page has changed.
3203 (void)pmap_load_clear(l3);
3204 KASSERT((orig_l3 & ~ATTR_MASK) == opa,
3205 ("pmap_enter: unexpected pa update for %#lx", va));
3206 if ((orig_l3 & ATTR_SW_MANAGED) != 0) {
3207 om = PHYS_TO_VM_PAGE(opa);
3210 * The pmap lock is sufficient to synchronize with
3211 * concurrent calls to pmap_page_test_mappings() and
3212 * pmap_ts_referenced().
3214 if (pmap_page_dirty(orig_l3))
3216 if ((orig_l3 & ATTR_AF) != 0)
3217 vm_page_aflag_set(om, PGA_REFERENCED);
3218 CHANGE_PV_LIST_LOCK_TO_PHYS(&lock, opa);
3219 pv = pmap_pvh_remove(&om->md, pmap, va);
3220 if ((m->oflags & VPO_UNMANAGED) != 0)
3221 free_pv_entry(pmap, pv);
3222 if ((om->aflags & PGA_WRITEABLE) != 0 &&
3223 TAILQ_EMPTY(&om->md.pv_list) &&
3224 ((om->flags & PG_FICTITIOUS) != 0 ||
3225 TAILQ_EMPTY(&pa_to_pvh(opa)->pv_list)))
3226 vm_page_aflag_clear(om, PGA_WRITEABLE);
3228 pmap_invalidate_page(pmap, va);
3232 * Increment the counters.
3234 if ((new_l3 & ATTR_SW_WIRED) != 0)
3235 pmap->pm_stats.wired_count++;
3236 pmap_resident_count_inc(pmap, 1);
3239 * Enter on the PV list if part of our managed memory.
3241 if ((m->oflags & VPO_UNMANAGED) == 0) {
3243 pv = get_pv_entry(pmap, &lock);
3246 CHANGE_PV_LIST_LOCK_TO_PHYS(&lock, pa);
3247 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_next);
3249 if ((new_l3 & ATTR_AP_RW_BIT) == ATTR_AP(ATTR_AP_RW))
3250 vm_page_aflag_set(m, PGA_WRITEABLE);
3255 * Sync icache if exec permission and attribute VM_MEMATTR_WRITE_BACK
3256 * is set. Do it now, before the mapping is stored and made
3257 * valid for hardware table walk. If done later, then other can
3258 * access this page before caches are properly synced.
3259 * Don't do it for kernel memory which is mapped with exec
3260 * permission even if the memory isn't going to hold executable
3261 * code. The only time when icache sync is needed is after
3262 * kernel module is loaded and the relocation info is processed.
3263 * And it's done in elf_cpu_load_file().
3265 if ((prot & VM_PROT_EXECUTE) && pmap != kernel_pmap &&
3266 m->md.pv_memattr == VM_MEMATTR_WRITE_BACK &&
3267 (opa != pa || (orig_l3 & ATTR_XN)))
3268 cpu_icache_sync_range(PHYS_TO_DMAP(pa), PAGE_SIZE);
3271 * Update the L3 entry
3273 if (pmap_l3_valid(orig_l3)) {
3274 KASSERT(opa == pa, ("pmap_enter: invalid update"));
3275 if ((orig_l3 & ~ATTR_AF) != (new_l3 & ~ATTR_AF)) {
3276 /* same PA, different attributes */
3277 pmap_load_store(l3, new_l3);
3278 pmap_invalidate_page(pmap, va);
3279 if (pmap_page_dirty(orig_l3) &&
3280 (orig_l3 & ATTR_SW_MANAGED) != 0)
3285 * This can happens if multiple threads simultaneously
3286 * access not yet mapped page. This bad for performance
3287 * since this can cause full demotion-NOP-promotion
3289 * Another possible reasons are:
3290 * - VM and pmap memory layout are diverged
3291 * - tlb flush is missing somewhere and CPU doesn't see
3294 CTR4(KTR_PMAP, "%s: already mapped page - "
3295 "pmap %p va 0x%#lx pte 0x%lx",
3296 __func__, pmap, va, new_l3);
3300 pmap_load_store(l3, new_l3);
3304 #if VM_NRESERVLEVEL > 0
3305 if (pmap != pmap_kernel() &&
3306 (mpte == NULL || mpte->wire_count == NL3PG) &&
3307 pmap_ps_enabled(pmap) &&
3308 (m->flags & PG_FICTITIOUS) == 0 &&
3309 vm_reserv_level_iffullpop(m) == 0) {
3310 pmap_promote_l2(pmap, pde, va, &lock);
3323 * Tries to create a read- and/or execute-only 2MB page mapping. Returns true
3324 * if successful. Returns false if (1) a page table page cannot be allocated
3325 * without sleeping, (2) a mapping already exists at the specified virtual
3326 * address, or (3) a PV entry cannot be allocated without reclaiming another
3330 pmap_enter_2mpage(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
3331 struct rwlock **lockp)
3335 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3337 new_l2 = (pd_entry_t)(VM_PAGE_TO_PHYS(m) | ATTR_DEFAULT |
3338 ATTR_IDX(m->md.pv_memattr) | ATTR_AP(ATTR_AP_RO) | L2_BLOCK);
3339 if ((m->oflags & VPO_UNMANAGED) == 0)
3340 new_l2 |= ATTR_SW_MANAGED;
3341 if ((prot & VM_PROT_EXECUTE) == 0 || m->md.pv_memattr == DEVICE_MEMORY)
3343 if (va < VM_MAXUSER_ADDRESS)
3344 new_l2 |= ATTR_AP(ATTR_AP_USER) | ATTR_PXN;
3345 return (pmap_enter_l2(pmap, va, new_l2, PMAP_ENTER_NOSLEEP |
3346 PMAP_ENTER_NOREPLACE | PMAP_ENTER_NORECLAIM, NULL, lockp) ==
3351 * Tries to create the specified 2MB page mapping. Returns KERN_SUCCESS if
3352 * the mapping was created, and either KERN_FAILURE or KERN_RESOURCE_SHORTAGE
3353 * otherwise. Returns KERN_FAILURE if PMAP_ENTER_NOREPLACE was specified and
3354 * a mapping already exists at the specified virtual address. Returns
3355 * KERN_RESOURCE_SHORTAGE if PMAP_ENTER_NOSLEEP was specified and a page table
3356 * page allocation failed. Returns KERN_RESOURCE_SHORTAGE if
3357 * PMAP_ENTER_NORECLAIM was specified and a PV entry allocation failed.
3359 * The parameter "m" is only used when creating a managed, writeable mapping.
3362 pmap_enter_l2(pmap_t pmap, vm_offset_t va, pd_entry_t new_l2, u_int flags,
3363 vm_page_t m, struct rwlock **lockp)
3365 struct spglist free;
3366 pd_entry_t *l2, *l3, old_l2;
3370 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3372 if ((l2pg = pmap_alloc_l2(pmap, va, (flags & PMAP_ENTER_NOSLEEP) != 0 ?
3373 NULL : lockp)) == NULL) {
3374 CTR2(KTR_PMAP, "pmap_enter_l2: failure for va %#lx in pmap %p",
3376 return (KERN_RESOURCE_SHORTAGE);
3379 l2 = (pd_entry_t *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(l2pg));
3380 l2 = &l2[pmap_l2_index(va)];
3381 if ((old_l2 = pmap_load(l2)) != 0) {
3382 KASSERT(l2pg->wire_count > 1,
3383 ("pmap_enter_l2: l2pg's wire count is too low"));
3384 if ((flags & PMAP_ENTER_NOREPLACE) != 0) {
3387 "pmap_enter_l2: failure for va %#lx in pmap %p",
3389 return (KERN_FAILURE);
3392 if ((old_l2 & ATTR_DESCR_MASK) == L2_BLOCK)
3393 (void)pmap_remove_l2(pmap, l2, va,
3394 pmap_load(pmap_l1(pmap, va)), &free, lockp);
3396 for (sva = va; sva < va + L2_SIZE; sva += PAGE_SIZE) {
3397 l3 = pmap_l2_to_l3(l2, sva);
3398 if (pmap_l3_valid(pmap_load(l3)) &&
3399 pmap_remove_l3(pmap, l3, sva, old_l2, &free,
3403 vm_page_free_pages_toq(&free, true);
3404 if (va >= VM_MAXUSER_ADDRESS) {
3406 * Both pmap_remove_l2() and pmap_remove_l3() will
3407 * leave the kernel page table page zero filled.
3409 mt = PHYS_TO_VM_PAGE(pmap_load(l2) & ~ATTR_MASK);
3410 if (pmap_insert_pt_page(pmap, mt, false))
3411 panic("pmap_enter_l2: trie insert failed");
3413 KASSERT(pmap_load(l2) == 0,
3414 ("pmap_enter_l2: non-zero L2 entry %p", l2));
3417 if ((new_l2 & ATTR_SW_MANAGED) != 0) {
3419 * Abort this mapping if its PV entry could not be created.
3421 if (!pmap_pv_insert_l2(pmap, va, new_l2, flags, lockp)) {
3423 if (pmap_unwire_l3(pmap, va, l2pg, &free)) {
3425 * Although "va" is not mapped, paging-structure
3426 * caches could nonetheless have entries that
3427 * refer to the freed page table pages.
3428 * Invalidate those entries.
3430 pmap_invalidate_page(pmap, va);
3431 vm_page_free_pages_toq(&free, true);
3434 "pmap_enter_l2: failure for va %#lx in pmap %p",
3436 return (KERN_RESOURCE_SHORTAGE);
3438 if ((new_l2 & ATTR_AP_RW_BIT) == ATTR_AP(ATTR_AP_RW))
3439 for (mt = m; mt < &m[L2_SIZE / PAGE_SIZE]; mt++)
3440 vm_page_aflag_set(mt, PGA_WRITEABLE);
3444 * Increment counters.
3446 if ((new_l2 & ATTR_SW_WIRED) != 0)
3447 pmap->pm_stats.wired_count += L2_SIZE / PAGE_SIZE;
3448 pmap->pm_stats.resident_count += L2_SIZE / PAGE_SIZE;
3451 * Map the superpage.
3453 (void)pmap_load_store(l2, new_l2);
3456 atomic_add_long(&pmap_l2_mappings, 1);
3457 CTR2(KTR_PMAP, "pmap_enter_l2: success for va %#lx in pmap %p",
3460 return (KERN_SUCCESS);
3464 * Maps a sequence of resident pages belonging to the same object.
3465 * The sequence begins with the given page m_start. This page is
3466 * mapped at the given virtual address start. Each subsequent page is
3467 * mapped at a virtual address that is offset from start by the same
3468 * amount as the page is offset from m_start within the object. The
3469 * last page in the sequence is the page with the largest offset from
3470 * m_start that can be mapped at a virtual address less than the given
3471 * virtual address end. Not every virtual page between start and end
3472 * is mapped; only those for which a resident page exists with the
3473 * corresponding offset from m_start are mapped.
3476 pmap_enter_object(pmap_t pmap, vm_offset_t start, vm_offset_t end,
3477 vm_page_t m_start, vm_prot_t prot)
3479 struct rwlock *lock;
3482 vm_pindex_t diff, psize;
3484 VM_OBJECT_ASSERT_LOCKED(m_start->object);
3486 psize = atop(end - start);
3491 while (m != NULL && (diff = m->pindex - m_start->pindex) < psize) {
3492 va = start + ptoa(diff);
3493 if ((va & L2_OFFSET) == 0 && va + L2_SIZE <= end &&
3494 m->psind == 1 && pmap_ps_enabled(pmap) &&
3495 pmap_enter_2mpage(pmap, va, m, prot, &lock))
3496 m = &m[L2_SIZE / PAGE_SIZE - 1];
3498 mpte = pmap_enter_quick_locked(pmap, va, m, prot, mpte,
3500 m = TAILQ_NEXT(m, listq);
3508 * this code makes some *MAJOR* assumptions:
3509 * 1. Current pmap & pmap exists.
3512 * 4. No page table pages.
3513 * but is *MUCH* faster than pmap_enter...
3517 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot)
3519 struct rwlock *lock;
3523 (void)pmap_enter_quick_locked(pmap, va, m, prot, NULL, &lock);
3530 pmap_enter_quick_locked(pmap_t pmap, vm_offset_t va, vm_page_t m,
3531 vm_prot_t prot, vm_page_t mpte, struct rwlock **lockp)
3533 struct spglist free;
3535 pt_entry_t *l2, *l3, l3_val;
3539 KASSERT(va < kmi.clean_sva || va >= kmi.clean_eva ||
3540 (m->oflags & VPO_UNMANAGED) != 0,
3541 ("pmap_enter_quick_locked: managed mapping within the clean submap"));
3542 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3544 CTR2(KTR_PMAP, "pmap_enter_quick_locked: %p %lx", pmap, va);
3546 * In the case that a page table page is not
3547 * resident, we are creating it here.
3549 if (va < VM_MAXUSER_ADDRESS) {
3550 vm_pindex_t l2pindex;
3553 * Calculate pagetable page index
3555 l2pindex = pmap_l2_pindex(va);
3556 if (mpte && (mpte->pindex == l2pindex)) {
3562 pde = pmap_pde(pmap, va, &lvl);
3565 * If the page table page is mapped, we just increment
3566 * the hold count, and activate it. Otherwise, we
3567 * attempt to allocate a page table page. If this
3568 * attempt fails, we don't retry. Instead, we give up.
3571 l2 = pmap_l1_to_l2(pde, va);
3572 if ((pmap_load(l2) & ATTR_DESCR_MASK) ==
3576 if (lvl == 2 && pmap_load(pde) != 0) {
3578 PHYS_TO_VM_PAGE(pmap_load(pde) & ~ATTR_MASK);
3582 * Pass NULL instead of the PV list lock
3583 * pointer, because we don't intend to sleep.
3585 mpte = _pmap_alloc_l3(pmap, l2pindex, NULL);
3590 l3 = (pt_entry_t *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(mpte));
3591 l3 = &l3[pmap_l3_index(va)];
3594 pde = pmap_pde(kernel_pmap, va, &lvl);
3595 KASSERT(pde != NULL,
3596 ("pmap_enter_quick_locked: Invalid page entry, va: 0x%lx",
3599 ("pmap_enter_quick_locked: Invalid level %d", lvl));
3600 l3 = pmap_l2_to_l3(pde, va);
3603 if (pmap_load(l3) != 0) {
3612 * Enter on the PV list if part of our managed memory.
3614 if ((m->oflags & VPO_UNMANAGED) == 0 &&
3615 !pmap_try_insert_pv_entry(pmap, va, m, lockp)) {
3618 if (pmap_unwire_l3(pmap, va, mpte, &free)) {
3619 pmap_invalidate_page(pmap, va);
3620 vm_page_free_pages_toq(&free, true);
3628 * Increment counters
3630 pmap_resident_count_inc(pmap, 1);
3632 pa = VM_PAGE_TO_PHYS(m);
3633 l3_val = pa | ATTR_DEFAULT | ATTR_IDX(m->md.pv_memattr) |
3634 ATTR_AP(ATTR_AP_RO) | L3_PAGE;
3635 if ((prot & VM_PROT_EXECUTE) == 0 || m->md.pv_memattr == DEVICE_MEMORY)
3637 else if (va < VM_MAXUSER_ADDRESS)
3641 * Now validate mapping with RO protection
3643 if ((m->oflags & VPO_UNMANAGED) == 0)
3644 l3_val |= ATTR_SW_MANAGED;
3646 /* Sync icache before the mapping is stored to PTE */
3647 if ((prot & VM_PROT_EXECUTE) && pmap != kernel_pmap &&
3648 m->md.pv_memattr == VM_MEMATTR_WRITE_BACK)
3649 cpu_icache_sync_range(PHYS_TO_DMAP(pa), PAGE_SIZE);
3651 pmap_load_store(l3, l3_val);
3652 pmap_invalidate_page(pmap, va);
3657 * This code maps large physical mmap regions into the
3658 * processor address space. Note that some shortcuts
3659 * are taken, but the code works.
3662 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr, vm_object_t object,
3663 vm_pindex_t pindex, vm_size_t size)
3666 VM_OBJECT_ASSERT_WLOCKED(object);
3667 KASSERT(object->type == OBJT_DEVICE || object->type == OBJT_SG,
3668 ("pmap_object_init_pt: non-device object"));
3672 * Clear the wired attribute from the mappings for the specified range of
3673 * addresses in the given pmap. Every valid mapping within that range
3674 * must have the wired attribute set. In contrast, invalid mappings
3675 * cannot have the wired attribute set, so they are ignored.
3677 * The wired attribute of the page table entry is not a hardware feature,
3678 * so there is no need to invalidate any TLB entries.
3681 pmap_unwire(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
3683 vm_offset_t va_next;
3684 pd_entry_t *l0, *l1, *l2;
3688 for (; sva < eva; sva = va_next) {
3689 l0 = pmap_l0(pmap, sva);
3690 if (pmap_load(l0) == 0) {
3691 va_next = (sva + L0_SIZE) & ~L0_OFFSET;
3697 l1 = pmap_l0_to_l1(l0, sva);
3698 if (pmap_load(l1) == 0) {
3699 va_next = (sva + L1_SIZE) & ~L1_OFFSET;
3705 va_next = (sva + L2_SIZE) & ~L2_OFFSET;
3709 l2 = pmap_l1_to_l2(l1, sva);
3710 if (pmap_load(l2) == 0)
3713 if ((pmap_load(l2) & ATTR_DESCR_MASK) == L2_BLOCK) {
3714 l3 = pmap_demote_l2(pmap, l2, sva);
3718 KASSERT((pmap_load(l2) & ATTR_DESCR_MASK) == L2_TABLE,
3719 ("pmap_unwire: Invalid l2 entry after demotion"));
3723 for (l3 = pmap_l2_to_l3(l2, sva); sva != va_next; l3++,
3725 if (pmap_load(l3) == 0)
3727 if ((pmap_load(l3) & ATTR_SW_WIRED) == 0)
3728 panic("pmap_unwire: l3 %#jx is missing "
3729 "ATTR_SW_WIRED", (uintmax_t)pmap_load(l3));
3732 * PG_W must be cleared atomically. Although the pmap
3733 * lock synchronizes access to PG_W, another processor
3734 * could be setting PG_M and/or PG_A concurrently.
3736 atomic_clear_long(l3, ATTR_SW_WIRED);
3737 pmap->pm_stats.wired_count--;
3744 * Copy the range specified by src_addr/len
3745 * from the source map to the range dst_addr/len
3746 * in the destination map.
3748 * This routine is only advisory and need not do anything.
3752 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr, vm_size_t len,
3753 vm_offset_t src_addr)
3758 * pmap_zero_page zeros the specified hardware page by mapping
3759 * the page into KVM and using bzero to clear its contents.
3762 pmap_zero_page(vm_page_t m)
3764 vm_offset_t va = PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m));
3766 pagezero((void *)va);
3770 * pmap_zero_page_area zeros the specified hardware page by mapping
3771 * the page into KVM and using bzero to clear its contents.
3773 * off and size may not cover an area beyond a single hardware page.
3776 pmap_zero_page_area(vm_page_t m, int off, int size)
3778 vm_offset_t va = PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m));
3780 if (off == 0 && size == PAGE_SIZE)
3781 pagezero((void *)va);
3783 bzero((char *)va + off, size);
3787 * pmap_copy_page copies the specified (machine independent)
3788 * page by mapping the page into virtual memory and using
3789 * bcopy to copy the page, one machine dependent page at a
3793 pmap_copy_page(vm_page_t msrc, vm_page_t mdst)
3795 vm_offset_t src = PHYS_TO_DMAP(VM_PAGE_TO_PHYS(msrc));
3796 vm_offset_t dst = PHYS_TO_DMAP(VM_PAGE_TO_PHYS(mdst));
3798 pagecopy((void *)src, (void *)dst);
3801 int unmapped_buf_allowed = 1;
3804 pmap_copy_pages(vm_page_t ma[], vm_offset_t a_offset, vm_page_t mb[],
3805 vm_offset_t b_offset, int xfersize)
3809 vm_paddr_t p_a, p_b;
3810 vm_offset_t a_pg_offset, b_pg_offset;
3813 while (xfersize > 0) {
3814 a_pg_offset = a_offset & PAGE_MASK;
3815 m_a = ma[a_offset >> PAGE_SHIFT];
3816 p_a = m_a->phys_addr;
3817 b_pg_offset = b_offset & PAGE_MASK;
3818 m_b = mb[b_offset >> PAGE_SHIFT];
3819 p_b = m_b->phys_addr;
3820 cnt = min(xfersize, PAGE_SIZE - a_pg_offset);
3821 cnt = min(cnt, PAGE_SIZE - b_pg_offset);
3822 if (__predict_false(!PHYS_IN_DMAP(p_a))) {
3823 panic("!DMAP a %lx", p_a);
3825 a_cp = (char *)PHYS_TO_DMAP(p_a) + a_pg_offset;
3827 if (__predict_false(!PHYS_IN_DMAP(p_b))) {
3828 panic("!DMAP b %lx", p_b);
3830 b_cp = (char *)PHYS_TO_DMAP(p_b) + b_pg_offset;
3832 bcopy(a_cp, b_cp, cnt);
3840 pmap_quick_enter_page(vm_page_t m)
3843 return (PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m)));
3847 pmap_quick_remove_page(vm_offset_t addr)
3852 * Returns true if the pmap's pv is one of the first
3853 * 16 pvs linked to from this page. This count may
3854 * be changed upwards or downwards in the future; it
3855 * is only necessary that true be returned for a small
3856 * subset of pmaps for proper page aging.
3859 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
3861 struct md_page *pvh;
3862 struct rwlock *lock;
3867 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
3868 ("pmap_page_exists_quick: page %p is not managed", m));
3870 lock = VM_PAGE_TO_PV_LIST_LOCK(m);
3872 TAILQ_FOREACH(pv, &m->md.pv_list, pv_next) {
3873 if (PV_PMAP(pv) == pmap) {
3881 if (!rv && loops < 16 && (m->flags & PG_FICTITIOUS) == 0) {
3882 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
3883 TAILQ_FOREACH(pv, &pvh->pv_list, pv_next) {
3884 if (PV_PMAP(pv) == pmap) {
3898 * pmap_page_wired_mappings:
3900 * Return the number of managed mappings to the given physical page
3904 pmap_page_wired_mappings(vm_page_t m)
3906 struct rwlock *lock;
3907 struct md_page *pvh;
3911 int count, lvl, md_gen, pvh_gen;
3913 if ((m->oflags & VPO_UNMANAGED) != 0)
3915 lock = VM_PAGE_TO_PV_LIST_LOCK(m);
3919 TAILQ_FOREACH(pv, &m->md.pv_list, pv_next) {
3921 if (!PMAP_TRYLOCK(pmap)) {
3922 md_gen = m->md.pv_gen;
3926 if (md_gen != m->md.pv_gen) {
3931 pte = pmap_pte(pmap, pv->pv_va, &lvl);
3932 if (pte != NULL && (pmap_load(pte) & ATTR_SW_WIRED) != 0)
3936 if ((m->flags & PG_FICTITIOUS) == 0) {
3937 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
3938 TAILQ_FOREACH(pv, &pvh->pv_list, pv_next) {
3940 if (!PMAP_TRYLOCK(pmap)) {
3941 md_gen = m->md.pv_gen;
3942 pvh_gen = pvh->pv_gen;
3946 if (md_gen != m->md.pv_gen ||
3947 pvh_gen != pvh->pv_gen) {
3952 pte = pmap_pte(pmap, pv->pv_va, &lvl);
3954 (pmap_load(pte) & ATTR_SW_WIRED) != 0)
3964 * Destroy all managed, non-wired mappings in the given user-space
3965 * pmap. This pmap cannot be active on any processor besides the
3968 * This function cannot be applied to the kernel pmap. Moreover, it
3969 * is not intended for general use. It is only to be used during
3970 * process termination. Consequently, it can be implemented in ways
3971 * that make it faster than pmap_remove(). First, it can more quickly
3972 * destroy mappings by iterating over the pmap's collection of PV
3973 * entries, rather than searching the page table. Second, it doesn't
3974 * have to test and clear the page table entries atomically, because
3975 * no processor is currently accessing the user address space. In
3976 * particular, a page table entry's dirty bit won't change state once
3977 * this function starts.
3980 pmap_remove_pages(pmap_t pmap)
3983 pt_entry_t *pte, tpte;
3984 struct spglist free;
3985 vm_page_t m, ml3, mt;
3987 struct md_page *pvh;
3988 struct pv_chunk *pc, *npc;
3989 struct rwlock *lock;
3991 uint64_t inuse, bitmask;
3992 int allfree, field, freed, idx, lvl;
3999 TAILQ_FOREACH_SAFE(pc, &pmap->pm_pvchunk, pc_list, npc) {
4002 for (field = 0; field < _NPCM; field++) {
4003 inuse = ~pc->pc_map[field] & pc_freemask[field];
4004 while (inuse != 0) {
4005 bit = ffsl(inuse) - 1;
4006 bitmask = 1UL << bit;
4007 idx = field * 64 + bit;
4008 pv = &pc->pc_pventry[idx];
4011 pde = pmap_pde(pmap, pv->pv_va, &lvl);
4012 KASSERT(pde != NULL,
4013 ("Attempting to remove an unmapped page"));
4017 pte = pmap_l1_to_l2(pde, pv->pv_va);
4018 tpte = pmap_load(pte);
4019 KASSERT((tpte & ATTR_DESCR_MASK) ==
4021 ("Attempting to remove an invalid "
4022 "block: %lx", tpte));
4023 tpte = pmap_load(pte);
4026 pte = pmap_l2_to_l3(pde, pv->pv_va);
4027 tpte = pmap_load(pte);
4028 KASSERT((tpte & ATTR_DESCR_MASK) ==
4030 ("Attempting to remove an invalid "
4031 "page: %lx", tpte));
4035 "Invalid page directory level: %d",
4040 * We cannot remove wired pages from a process' mapping at this time
4042 if (tpte & ATTR_SW_WIRED) {
4047 pa = tpte & ~ATTR_MASK;
4049 m = PHYS_TO_VM_PAGE(pa);
4050 KASSERT(m->phys_addr == pa,
4051 ("vm_page_t %p phys_addr mismatch %016jx %016jx",
4052 m, (uintmax_t)m->phys_addr,
4055 KASSERT((m->flags & PG_FICTITIOUS) != 0 ||
4056 m < &vm_page_array[vm_page_array_size],
4057 ("pmap_remove_pages: bad pte %#jx",
4060 pmap_load_clear(pte);
4063 * Update the vm_page_t clean/reference bits.
4065 if ((tpte & ATTR_AP_RW_BIT) ==
4066 ATTR_AP(ATTR_AP_RW)) {
4069 for (mt = m; mt < &m[L2_SIZE / PAGE_SIZE]; mt++)
4078 CHANGE_PV_LIST_LOCK_TO_VM_PAGE(&lock, m);
4081 pc->pc_map[field] |= bitmask;
4084 pmap_resident_count_dec(pmap,
4085 L2_SIZE / PAGE_SIZE);
4086 pvh = pa_to_pvh(tpte & ~ATTR_MASK);
4087 TAILQ_REMOVE(&pvh->pv_list, pv,pv_next);
4089 if (TAILQ_EMPTY(&pvh->pv_list)) {
4090 for (mt = m; mt < &m[L2_SIZE / PAGE_SIZE]; mt++)
4091 if ((mt->aflags & PGA_WRITEABLE) != 0 &&
4092 TAILQ_EMPTY(&mt->md.pv_list))
4093 vm_page_aflag_clear(mt, PGA_WRITEABLE);
4095 ml3 = pmap_remove_pt_page(pmap,
4098 KASSERT(ml3->valid == VM_PAGE_BITS_ALL,
4099 ("pmap_remove_pages: l3 page not promoted"));
4100 pmap_resident_count_dec(pmap,1);
4101 KASSERT(ml3->wire_count == NL3PG,
4102 ("pmap_remove_pages: l3 page wire count error"));
4103 ml3->wire_count = 0;
4104 pmap_add_delayed_free_list(ml3,
4109 pmap_resident_count_dec(pmap, 1);
4110 TAILQ_REMOVE(&m->md.pv_list, pv,
4113 if ((m->aflags & PGA_WRITEABLE) != 0 &&
4114 TAILQ_EMPTY(&m->md.pv_list) &&
4115 (m->flags & PG_FICTITIOUS) == 0) {
4117 VM_PAGE_TO_PHYS(m));
4118 if (TAILQ_EMPTY(&pvh->pv_list))
4119 vm_page_aflag_clear(m,
4124 pmap_unuse_pt(pmap, pv->pv_va, pmap_load(pde),
4129 PV_STAT(atomic_add_long(&pv_entry_frees, freed));
4130 PV_STAT(atomic_add_int(&pv_entry_spare, freed));
4131 PV_STAT(atomic_subtract_long(&pv_entry_count, freed));
4133 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
4137 pmap_invalidate_all(pmap);
4141 vm_page_free_pages_toq(&free, true);
4145 * This is used to check if a page has been accessed or modified. As we
4146 * don't have a bit to see if it has been modified we have to assume it
4147 * has been if the page is read/write.
4150 pmap_page_test_mappings(vm_page_t m, boolean_t accessed, boolean_t modified)
4152 struct rwlock *lock;
4154 struct md_page *pvh;
4155 pt_entry_t *pte, mask, value;
4157 int lvl, md_gen, pvh_gen;
4161 lock = VM_PAGE_TO_PV_LIST_LOCK(m);
4164 TAILQ_FOREACH(pv, &m->md.pv_list, pv_next) {
4166 if (!PMAP_TRYLOCK(pmap)) {
4167 md_gen = m->md.pv_gen;
4171 if (md_gen != m->md.pv_gen) {
4176 pte = pmap_pte(pmap, pv->pv_va, &lvl);
4178 ("pmap_page_test_mappings: Invalid level %d", lvl));
4182 mask |= ATTR_AP_RW_BIT;
4183 value |= ATTR_AP(ATTR_AP_RW);
4186 mask |= ATTR_AF | ATTR_DESCR_MASK;
4187 value |= ATTR_AF | L3_PAGE;
4189 rv = (pmap_load(pte) & mask) == value;
4194 if ((m->flags & PG_FICTITIOUS) == 0) {
4195 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
4196 TAILQ_FOREACH(pv, &pvh->pv_list, pv_next) {
4198 if (!PMAP_TRYLOCK(pmap)) {
4199 md_gen = m->md.pv_gen;
4200 pvh_gen = pvh->pv_gen;
4204 if (md_gen != m->md.pv_gen ||
4205 pvh_gen != pvh->pv_gen) {
4210 pte = pmap_pte(pmap, pv->pv_va, &lvl);
4212 ("pmap_page_test_mappings: Invalid level %d", lvl));
4216 mask |= ATTR_AP_RW_BIT;
4217 value |= ATTR_AP(ATTR_AP_RW);
4220 mask |= ATTR_AF | ATTR_DESCR_MASK;
4221 value |= ATTR_AF | L2_BLOCK;
4223 rv = (pmap_load(pte) & mask) == value;
4237 * Return whether or not the specified physical page was modified
4238 * in any physical maps.
4241 pmap_is_modified(vm_page_t m)
4244 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4245 ("pmap_is_modified: page %p is not managed", m));
4248 * If the page is not exclusive busied, then PGA_WRITEABLE cannot be
4249 * concurrently set while the object is locked. Thus, if PGA_WRITEABLE
4250 * is clear, no PTEs can have PG_M set.
4252 VM_OBJECT_ASSERT_WLOCKED(m->object);
4253 if (!vm_page_xbusied(m) && (m->aflags & PGA_WRITEABLE) == 0)
4255 return (pmap_page_test_mappings(m, FALSE, TRUE));
4259 * pmap_is_prefaultable:
4261 * Return whether or not the specified virtual address is eligible
4265 pmap_is_prefaultable(pmap_t pmap, vm_offset_t addr)
4273 pte = pmap_pte(pmap, addr, &lvl);
4274 if (pte != NULL && pmap_load(pte) != 0) {
4282 * pmap_is_referenced:
4284 * Return whether or not the specified physical page was referenced
4285 * in any physical maps.
4288 pmap_is_referenced(vm_page_t m)
4291 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4292 ("pmap_is_referenced: page %p is not managed", m));
4293 return (pmap_page_test_mappings(m, TRUE, FALSE));
4297 * Clear the write and modified bits in each of the given page's mappings.
4300 pmap_remove_write(vm_page_t m)
4302 struct md_page *pvh;
4304 struct rwlock *lock;
4305 pv_entry_t next_pv, pv;
4306 pt_entry_t oldpte, *pte;
4308 int lvl, md_gen, pvh_gen;
4310 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4311 ("pmap_remove_write: page %p is not managed", m));
4314 * If the page is not exclusive busied, then PGA_WRITEABLE cannot be
4315 * set by another thread while the object is locked. Thus,
4316 * if PGA_WRITEABLE is clear, no page table entries need updating.
4318 VM_OBJECT_ASSERT_WLOCKED(m->object);
4319 if (!vm_page_xbusied(m) && (m->aflags & PGA_WRITEABLE) == 0)
4321 lock = VM_PAGE_TO_PV_LIST_LOCK(m);
4322 pvh = (m->flags & PG_FICTITIOUS) != 0 ? &pv_dummy :
4323 pa_to_pvh(VM_PAGE_TO_PHYS(m));
4326 TAILQ_FOREACH_SAFE(pv, &pvh->pv_list, pv_next, next_pv) {
4328 if (!PMAP_TRYLOCK(pmap)) {
4329 pvh_gen = pvh->pv_gen;
4333 if (pvh_gen != pvh->pv_gen) {
4340 pte = pmap_pte(pmap, pv->pv_va, &lvl);
4341 if ((pmap_load(pte) & ATTR_AP_RW_BIT) == ATTR_AP(ATTR_AP_RW))
4342 (void)pmap_demote_l2_locked(pmap, pte, va, &lock);
4343 KASSERT(lock == VM_PAGE_TO_PV_LIST_LOCK(m),
4344 ("inconsistent pv lock %p %p for page %p",
4345 lock, VM_PAGE_TO_PV_LIST_LOCK(m), m));
4348 TAILQ_FOREACH(pv, &m->md.pv_list, pv_next) {
4350 if (!PMAP_TRYLOCK(pmap)) {
4351 pvh_gen = pvh->pv_gen;
4352 md_gen = m->md.pv_gen;
4356 if (pvh_gen != pvh->pv_gen ||
4357 md_gen != m->md.pv_gen) {
4363 pte = pmap_pte(pmap, pv->pv_va, &lvl);
4365 oldpte = pmap_load(pte);
4366 if ((oldpte & ATTR_AP_RW_BIT) == ATTR_AP(ATTR_AP_RW)) {
4367 if (!atomic_cmpset_long(pte, oldpte,
4368 oldpte | ATTR_AP(ATTR_AP_RO)))
4370 if ((oldpte & ATTR_AF) != 0)
4372 pmap_invalidate_page(pmap, pv->pv_va);
4377 vm_page_aflag_clear(m, PGA_WRITEABLE);
4380 static __inline boolean_t
4381 safe_to_clear_referenced(pmap_t pmap, pt_entry_t pte)
4388 * pmap_ts_referenced:
4390 * Return a count of reference bits for a page, clearing those bits.
4391 * It is not necessary for every reference bit to be cleared, but it
4392 * is necessary that 0 only be returned when there are truly no
4393 * reference bits set.
4395 * As an optimization, update the page's dirty field if a modified bit is
4396 * found while counting reference bits. This opportunistic update can be
4397 * performed at low cost and can eliminate the need for some future calls
4398 * to pmap_is_modified(). However, since this function stops after
4399 * finding PMAP_TS_REFERENCED_MAX reference bits, it may not detect some
4400 * dirty pages. Those dirty pages will only be detected by a future call
4401 * to pmap_is_modified().
4404 pmap_ts_referenced(vm_page_t m)
4406 struct md_page *pvh;
4409 struct rwlock *lock;
4410 pd_entry_t *pde, tpde;
4411 pt_entry_t *pte, tpte;
4415 int cleared, md_gen, not_cleared, lvl, pvh_gen;
4416 struct spglist free;
4419 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4420 ("pmap_ts_referenced: page %p is not managed", m));
4423 pa = VM_PAGE_TO_PHYS(m);
4424 lock = PHYS_TO_PV_LIST_LOCK(pa);
4425 pvh = (m->flags & PG_FICTITIOUS) != 0 ? &pv_dummy : pa_to_pvh(pa);
4429 if ((pvf = TAILQ_FIRST(&pvh->pv_list)) == NULL)
4430 goto small_mappings;
4436 if (!PMAP_TRYLOCK(pmap)) {
4437 pvh_gen = pvh->pv_gen;
4441 if (pvh_gen != pvh->pv_gen) {
4447 pde = pmap_pde(pmap, pv->pv_va, &lvl);
4448 KASSERT(pde != NULL, ("pmap_ts_referenced: no l1 table found"));
4450 ("pmap_ts_referenced: invalid pde level %d", lvl));
4451 tpde = pmap_load(pde);
4452 KASSERT((tpde & ATTR_DESCR_MASK) == L1_TABLE,
4453 ("pmap_ts_referenced: found an invalid l1 table"));
4454 pte = pmap_l1_to_l2(pde, pv->pv_va);
4455 tpte = pmap_load(pte);
4456 if (pmap_page_dirty(tpte)) {
4458 * Although "tpte" is mapping a 2MB page, because
4459 * this function is called at a 4KB page granularity,
4460 * we only update the 4KB page under test.
4464 if ((tpte & ATTR_AF) != 0) {
4466 * Since this reference bit is shared by 512 4KB
4467 * pages, it should not be cleared every time it is
4468 * tested. Apply a simple "hash" function on the
4469 * physical page number, the virtual superpage number,
4470 * and the pmap address to select one 4KB page out of
4471 * the 512 on which testing the reference bit will
4472 * result in clearing that reference bit. This
4473 * function is designed to avoid the selection of the
4474 * same 4KB page for every 2MB page mapping.
4476 * On demotion, a mapping that hasn't been referenced
4477 * is simply destroyed. To avoid the possibility of a
4478 * subsequent page fault on a demoted wired mapping,
4479 * always leave its reference bit set. Moreover,
4480 * since the superpage is wired, the current state of
4481 * its reference bit won't affect page replacement.
4483 if ((((pa >> PAGE_SHIFT) ^ (pv->pv_va >> L2_SHIFT) ^
4484 (uintptr_t)pmap) & (Ln_ENTRIES - 1)) == 0 &&
4485 (tpte & ATTR_SW_WIRED) == 0) {
4486 if (safe_to_clear_referenced(pmap, tpte)) {
4488 * TODO: We don't handle the access
4489 * flag at all. We need to be able
4490 * to set it in the exception handler.
4493 "safe_to_clear_referenced\n");
4494 } else if (pmap_demote_l2_locked(pmap, pte,
4495 pv->pv_va, &lock) != NULL) {
4497 va += VM_PAGE_TO_PHYS(m) -
4498 (tpte & ~ATTR_MASK);
4499 l3 = pmap_l2_to_l3(pte, va);
4500 pmap_remove_l3(pmap, l3, va,
4501 pmap_load(pte), NULL, &lock);
4507 * The superpage mapping was removed
4508 * entirely and therefore 'pv' is no
4516 KASSERT(lock == VM_PAGE_TO_PV_LIST_LOCK(m),
4517 ("inconsistent pv lock %p %p for page %p",
4518 lock, VM_PAGE_TO_PV_LIST_LOCK(m), m));
4523 /* Rotate the PV list if it has more than one entry. */
4524 if (pv != NULL && TAILQ_NEXT(pv, pv_next) != NULL) {
4525 TAILQ_REMOVE(&pvh->pv_list, pv, pv_next);
4526 TAILQ_INSERT_TAIL(&pvh->pv_list, pv, pv_next);
4529 if (cleared + not_cleared >= PMAP_TS_REFERENCED_MAX)
4531 } while ((pv = TAILQ_FIRST(&pvh->pv_list)) != pvf);
4533 if ((pvf = TAILQ_FIRST(&m->md.pv_list)) == NULL)
4540 if (!PMAP_TRYLOCK(pmap)) {
4541 pvh_gen = pvh->pv_gen;
4542 md_gen = m->md.pv_gen;
4546 if (pvh_gen != pvh->pv_gen || md_gen != m->md.pv_gen) {
4551 pde = pmap_pde(pmap, pv->pv_va, &lvl);
4552 KASSERT(pde != NULL, ("pmap_ts_referenced: no l2 table found"));
4554 ("pmap_ts_referenced: invalid pde level %d", lvl));
4555 tpde = pmap_load(pde);
4556 KASSERT((tpde & ATTR_DESCR_MASK) == L2_TABLE,
4557 ("pmap_ts_referenced: found an invalid l2 table"));
4558 pte = pmap_l2_to_l3(pde, pv->pv_va);
4559 tpte = pmap_load(pte);
4560 if (pmap_page_dirty(tpte))
4562 if ((tpte & ATTR_AF) != 0) {
4563 if (safe_to_clear_referenced(pmap, tpte)) {
4565 * TODO: We don't handle the access flag
4566 * at all. We need to be able to set it in
4567 * the exception handler.
4569 panic("ARM64TODO: safe_to_clear_referenced\n");
4570 } else if ((tpte & ATTR_SW_WIRED) == 0) {
4572 * Wired pages cannot be paged out so
4573 * doing accessed bit emulation for
4574 * them is wasted effort. We do the
4575 * hard work for unwired pages only.
4577 pmap_remove_l3(pmap, pte, pv->pv_va, tpde,
4579 pmap_invalidate_page(pmap, pv->pv_va);
4584 KASSERT(lock == VM_PAGE_TO_PV_LIST_LOCK(m),
4585 ("inconsistent pv lock %p %p for page %p",
4586 lock, VM_PAGE_TO_PV_LIST_LOCK(m), m));
4591 /* Rotate the PV list if it has more than one entry. */
4592 if (pv != NULL && TAILQ_NEXT(pv, pv_next) != NULL) {
4593 TAILQ_REMOVE(&m->md.pv_list, pv, pv_next);
4594 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_next);
4597 } while ((pv = TAILQ_FIRST(&m->md.pv_list)) != pvf && cleared +
4598 not_cleared < PMAP_TS_REFERENCED_MAX);
4601 vm_page_free_pages_toq(&free, true);
4602 return (cleared + not_cleared);
4606 * Apply the given advice to the specified range of addresses within the
4607 * given pmap. Depending on the advice, clear the referenced and/or
4608 * modified flags in each mapping and set the mapped page's dirty field.
4611 pmap_advise(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, int advice)
4616 * Clear the modify bits on the specified physical page.
4619 pmap_clear_modify(vm_page_t m)
4622 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4623 ("pmap_clear_modify: page %p is not managed", m));
4624 VM_OBJECT_ASSERT_WLOCKED(m->object);
4625 KASSERT(!vm_page_xbusied(m),
4626 ("pmap_clear_modify: page %p is exclusive busied", m));
4629 * If the page is not PGA_WRITEABLE, then no PTEs can have PG_M set.
4630 * If the object containing the page is locked and the page is not
4631 * exclusive busied, then PGA_WRITEABLE cannot be concurrently set.
4633 if ((m->aflags & PGA_WRITEABLE) == 0)
4636 /* ARM64TODO: We lack support for tracking if a page is modified */
4640 pmap_mapbios(vm_paddr_t pa, vm_size_t size)
4642 struct pmap_preinit_mapping *ppim;
4643 vm_offset_t va, offset;
4646 int i, lvl, l2_blocks, free_l2_count, start_idx;
4648 if (!vm_initialized) {
4650 * No L3 ptables so map entire L2 blocks where start VA is:
4651 * preinit_map_va + start_idx * L2_SIZE
4652 * There may be duplicate mappings (multiple VA -> same PA) but
4653 * ARM64 dcache is always PIPT so that's acceptable.
4658 /* Calculate how many L2 blocks are needed for the mapping */
4659 l2_blocks = (roundup2(pa + size, L2_SIZE) -
4660 rounddown2(pa, L2_SIZE)) >> L2_SHIFT;
4662 offset = pa & L2_OFFSET;
4664 if (preinit_map_va == 0)
4667 /* Map 2MiB L2 blocks from reserved VA space */
4671 /* Find enough free contiguous VA space */
4672 for (i = 0; i < PMAP_PREINIT_MAPPING_COUNT; i++) {
4673 ppim = pmap_preinit_mapping + i;
4674 if (free_l2_count > 0 && ppim->pa != 0) {
4675 /* Not enough space here */
4681 if (ppim->pa == 0) {
4683 if (start_idx == -1)
4686 if (free_l2_count == l2_blocks)
4690 if (free_l2_count != l2_blocks)
4691 panic("%s: too many preinit mappings", __func__);
4693 va = preinit_map_va + (start_idx * L2_SIZE);
4694 for (i = start_idx; i < start_idx + l2_blocks; i++) {
4695 /* Mark entries as allocated */
4696 ppim = pmap_preinit_mapping + i;
4698 ppim->va = va + offset;
4703 pa = rounddown2(pa, L2_SIZE);
4704 for (i = 0; i < l2_blocks; i++) {
4705 pde = pmap_pde(kernel_pmap, va, &lvl);
4706 KASSERT(pde != NULL,
4707 ("pmap_mapbios: Invalid page entry, va: 0x%lx",
4710 ("pmap_mapbios: Invalid level %d", lvl));
4712 /* Insert L2_BLOCK */
4713 l2 = pmap_l1_to_l2(pde, va);
4715 pa | ATTR_DEFAULT | ATTR_XN |
4716 ATTR_IDX(CACHED_MEMORY) | L2_BLOCK);
4721 pmap_invalidate_all(kernel_pmap);
4723 va = preinit_map_va + (start_idx * L2_SIZE);
4726 /* kva_alloc may be used to map the pages */
4727 offset = pa & PAGE_MASK;
4728 size = round_page(offset + size);
4730 va = kva_alloc(size);
4732 panic("%s: Couldn't allocate KVA", __func__);
4734 pde = pmap_pde(kernel_pmap, va, &lvl);
4735 KASSERT(lvl == 2, ("pmap_mapbios: Invalid level %d", lvl));
4737 /* L3 table is linked */
4738 va = trunc_page(va);
4739 pa = trunc_page(pa);
4740 pmap_kenter(va, size, pa, CACHED_MEMORY);
4743 return ((void *)(va + offset));
4747 pmap_unmapbios(vm_offset_t va, vm_size_t size)
4749 struct pmap_preinit_mapping *ppim;
4750 vm_offset_t offset, tmpsize, va_trunc;
4753 int i, lvl, l2_blocks, block;
4757 (roundup2(va + size, L2_SIZE) - rounddown2(va, L2_SIZE)) >> L2_SHIFT;
4758 KASSERT(l2_blocks > 0, ("pmap_unmapbios: invalid size %lx", size));
4760 /* Remove preinit mapping */
4761 preinit_map = false;
4763 for (i = 0; i < PMAP_PREINIT_MAPPING_COUNT; i++) {
4764 ppim = pmap_preinit_mapping + i;
4765 if (ppim->va == va) {
4766 KASSERT(ppim->size == size,
4767 ("pmap_unmapbios: size mismatch"));
4772 offset = block * L2_SIZE;
4773 va_trunc = rounddown2(va, L2_SIZE) + offset;
4775 /* Remove L2_BLOCK */
4776 pde = pmap_pde(kernel_pmap, va_trunc, &lvl);
4777 KASSERT(pde != NULL,
4778 ("pmap_unmapbios: Invalid page entry, va: 0x%lx",
4780 l2 = pmap_l1_to_l2(pde, va_trunc);
4781 pmap_load_clear(l2);
4783 if (block == (l2_blocks - 1))
4789 pmap_invalidate_all(kernel_pmap);
4793 /* Unmap the pages reserved with kva_alloc. */
4794 if (vm_initialized) {
4795 offset = va & PAGE_MASK;
4796 size = round_page(offset + size);
4797 va = trunc_page(va);
4799 pde = pmap_pde(kernel_pmap, va, &lvl);
4800 KASSERT(pde != NULL,
4801 ("pmap_unmapbios: Invalid page entry, va: 0x%lx", va));
4802 KASSERT(lvl == 2, ("pmap_unmapbios: Invalid level %d", lvl));
4804 /* Unmap and invalidate the pages */
4805 for (tmpsize = 0; tmpsize < size; tmpsize += PAGE_SIZE)
4806 pmap_kremove(va + tmpsize);
4813 * Sets the memory attribute for the specified page.
4816 pmap_page_set_memattr(vm_page_t m, vm_memattr_t ma)
4819 m->md.pv_memattr = ma;
4822 * If "m" is a normal page, update its direct mapping. This update
4823 * can be relied upon to perform any cache operations that are
4824 * required for data coherence.
4826 if ((m->flags & PG_FICTITIOUS) == 0 &&
4827 pmap_change_attr(PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m)), PAGE_SIZE,
4828 m->md.pv_memattr) != 0)
4829 panic("memory attribute change on the direct map failed");
4833 * Changes the specified virtual address range's memory type to that given by
4834 * the parameter "mode". The specified virtual address range must be
4835 * completely contained within either the direct map or the kernel map. If
4836 * the virtual address range is contained within the kernel map, then the
4837 * memory type for each of the corresponding ranges of the direct map is also
4838 * changed. (The corresponding ranges of the direct map are those ranges that
4839 * map the same physical pages as the specified virtual address range.) These
4840 * changes to the direct map are necessary because Intel describes the
4841 * behavior of their processors as "undefined" if two or more mappings to the
4842 * same physical page have different memory types.
4844 * Returns zero if the change completed successfully, and either EINVAL or
4845 * ENOMEM if the change failed. Specifically, EINVAL is returned if some part
4846 * of the virtual address range was not mapped, and ENOMEM is returned if
4847 * there was insufficient memory available to complete the change. In the
4848 * latter case, the memory type may have been changed on some part of the
4849 * virtual address range or the direct map.
4852 pmap_change_attr(vm_offset_t va, vm_size_t size, int mode)
4856 PMAP_LOCK(kernel_pmap);
4857 error = pmap_change_attr_locked(va, size, mode);
4858 PMAP_UNLOCK(kernel_pmap);
4863 pmap_change_attr_locked(vm_offset_t va, vm_size_t size, int mode)
4865 vm_offset_t base, offset, tmpva;
4866 pt_entry_t l3, *pte, *newpte;
4869 PMAP_LOCK_ASSERT(kernel_pmap, MA_OWNED);
4870 base = trunc_page(va);
4871 offset = va & PAGE_MASK;
4872 size = round_page(offset + size);
4874 if (!VIRT_IN_DMAP(base))
4877 for (tmpva = base; tmpva < base + size; ) {
4878 pte = pmap_pte(kernel_pmap, tmpva, &lvl);
4882 if ((pmap_load(pte) & ATTR_IDX_MASK) == ATTR_IDX(mode)) {
4884 * We already have the correct attribute,
4885 * ignore this entry.
4889 panic("Invalid DMAP table level: %d\n", lvl);
4891 tmpva = (tmpva & ~L1_OFFSET) + L1_SIZE;
4894 tmpva = (tmpva & ~L2_OFFSET) + L2_SIZE;
4902 * Split the entry to an level 3 table, then
4903 * set the new attribute.
4907 panic("Invalid DMAP table level: %d\n", lvl);
4909 newpte = pmap_demote_l1(kernel_pmap, pte,
4910 tmpva & ~L1_OFFSET);
4913 pte = pmap_l1_to_l2(pte, tmpva);
4915 newpte = pmap_demote_l2(kernel_pmap, pte,
4919 pte = pmap_l2_to_l3(pte, tmpva);
4921 /* Update the entry */
4922 l3 = pmap_load(pte);
4923 l3 &= ~ATTR_IDX_MASK;
4924 l3 |= ATTR_IDX(mode);
4925 if (mode == DEVICE_MEMORY)
4928 pmap_update_entry(kernel_pmap, pte, l3, tmpva,
4932 * If moving to a non-cacheable entry flush
4935 if (mode == VM_MEMATTR_UNCACHEABLE)
4936 cpu_dcache_wbinv_range(tmpva, L3_SIZE);
4948 * Create an L2 table to map all addresses within an L1 mapping.
4951 pmap_demote_l1(pmap_t pmap, pt_entry_t *l1, vm_offset_t va)
4953 pt_entry_t *l2, newl2, oldl1;
4955 vm_paddr_t l2phys, phys;
4959 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
4960 oldl1 = pmap_load(l1);
4961 KASSERT((oldl1 & ATTR_DESCR_MASK) == L1_BLOCK,
4962 ("pmap_demote_l1: Demoting a non-block entry"));
4963 KASSERT((va & L1_OFFSET) == 0,
4964 ("pmap_demote_l1: Invalid virtual address %#lx", va));
4965 KASSERT((oldl1 & ATTR_SW_MANAGED) == 0,
4966 ("pmap_demote_l1: Level 1 table shouldn't be managed"));
4969 if (va <= (vm_offset_t)l1 && va + L1_SIZE > (vm_offset_t)l1) {
4970 tmpl1 = kva_alloc(PAGE_SIZE);
4975 if ((ml2 = vm_page_alloc(NULL, 0, VM_ALLOC_INTERRUPT |
4976 VM_ALLOC_NOOBJ | VM_ALLOC_WIRED)) == NULL) {
4977 CTR2(KTR_PMAP, "pmap_demote_l1: failure for va %#lx"
4978 " in pmap %p", va, pmap);
4982 l2phys = VM_PAGE_TO_PHYS(ml2);
4983 l2 = (pt_entry_t *)PHYS_TO_DMAP(l2phys);
4985 /* Address the range points at */
4986 phys = oldl1 & ~ATTR_MASK;
4987 /* The attributed from the old l1 table to be copied */
4988 newl2 = oldl1 & ATTR_MASK;
4990 /* Create the new entries */
4991 for (i = 0; i < Ln_ENTRIES; i++) {
4992 l2[i] = newl2 | phys;
4995 KASSERT(l2[0] == ((oldl1 & ~ATTR_DESCR_MASK) | L2_BLOCK),
4996 ("Invalid l2 page (%lx != %lx)", l2[0],
4997 (oldl1 & ~ATTR_DESCR_MASK) | L2_BLOCK));
5000 pmap_kenter(tmpl1, PAGE_SIZE,
5001 DMAP_TO_PHYS((vm_offset_t)l1) & ~L3_OFFSET, CACHED_MEMORY);
5002 l1 = (pt_entry_t *)(tmpl1 + ((vm_offset_t)l1 & PAGE_MASK));
5005 pmap_update_entry(pmap, l1, l2phys | L1_TABLE, va, PAGE_SIZE);
5008 pmap_kremove(tmpl1);
5009 kva_free(tmpl1, PAGE_SIZE);
5016 pmap_demote_l2_abort(pmap_t pmap, vm_offset_t va, pt_entry_t *l2,
5017 struct rwlock **lockp)
5019 struct spglist free;
5022 (void)pmap_remove_l2(pmap, l2, va, pmap_load(pmap_l1(pmap, va)), &free,
5024 vm_page_free_pages_toq(&free, true);
5028 * Create an L3 table to map all addresses within an L2 mapping.
5031 pmap_demote_l2_locked(pmap_t pmap, pt_entry_t *l2, vm_offset_t va,
5032 struct rwlock **lockp)
5034 pt_entry_t *l3, newl3, oldl2;
5036 vm_paddr_t l3phys, phys;
5040 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
5042 oldl2 = pmap_load(l2);
5043 KASSERT((oldl2 & ATTR_DESCR_MASK) == L2_BLOCK,
5044 ("pmap_demote_l2: Demoting a non-block entry"));
5048 if (va <= (vm_offset_t)l2 && va + L2_SIZE > (vm_offset_t)l2) {
5049 tmpl2 = kva_alloc(PAGE_SIZE);
5055 * Invalidate the 2MB page mapping and return "failure" if the
5056 * mapping was never accessed.
5058 if ((oldl2 & ATTR_AF) == 0) {
5059 KASSERT((oldl2 & ATTR_SW_WIRED) == 0,
5060 ("pmap_demote_l2: a wired mapping is missing ATTR_AF"));
5061 pmap_demote_l2_abort(pmap, va, l2, lockp);
5062 CTR2(KTR_PMAP, "pmap_demote_l2: failure for va %#lx in pmap %p",
5067 if ((ml3 = pmap_remove_pt_page(pmap, va)) == NULL) {
5068 KASSERT((oldl2 & ATTR_SW_WIRED) == 0,
5069 ("pmap_demote_l2: page table page for a wired mapping"
5073 * If the page table page is missing and the mapping
5074 * is for a kernel address, the mapping must belong to
5075 * the direct map. Page table pages are preallocated
5076 * for every other part of the kernel address space,
5077 * so the direct map region is the only part of the
5078 * kernel address space that must be handled here.
5080 KASSERT(va < VM_MAXUSER_ADDRESS || VIRT_IN_DMAP(va),
5081 ("pmap_demote_l2: No saved mpte for va %#lx", va));
5084 * If the 2MB page mapping belongs to the direct map
5085 * region of the kernel's address space, then the page
5086 * allocation request specifies the highest possible
5087 * priority (VM_ALLOC_INTERRUPT). Otherwise, the
5088 * priority is normal.
5090 ml3 = vm_page_alloc(NULL, pmap_l2_pindex(va),
5091 (VIRT_IN_DMAP(va) ? VM_ALLOC_INTERRUPT : VM_ALLOC_NORMAL) |
5092 VM_ALLOC_NOOBJ | VM_ALLOC_WIRED);
5095 * If the allocation of the new page table page fails,
5096 * invalidate the 2MB page mapping and return "failure".
5099 pmap_demote_l2_abort(pmap, va, l2, lockp);
5100 CTR2(KTR_PMAP, "pmap_demote_l2: failure for va %#lx"
5101 " in pmap %p", va, pmap);
5105 if (va < VM_MAXUSER_ADDRESS) {
5106 ml3->wire_count = NL3PG;
5107 pmap_resident_count_inc(pmap, 1);
5111 l3phys = VM_PAGE_TO_PHYS(ml3);
5112 l3 = (pt_entry_t *)PHYS_TO_DMAP(l3phys);
5114 /* Address the range points at */
5115 phys = oldl2 & ~ATTR_MASK;
5116 /* The attributed from the old l2 table to be copied */
5117 newl3 = (oldl2 & (ATTR_MASK & ~ATTR_DESCR_MASK)) | L3_PAGE;
5120 * If the page table page is not leftover from an earlier promotion,
5123 if (ml3->valid == 0) {
5124 for (i = 0; i < Ln_ENTRIES; i++) {
5125 l3[i] = newl3 | phys;
5129 KASSERT(l3[0] == ((oldl2 & ~ATTR_DESCR_MASK) | L3_PAGE),
5130 ("Invalid l3 page (%lx != %lx)", l3[0],
5131 (oldl2 & ~ATTR_DESCR_MASK) | L3_PAGE));
5134 * Map the temporary page so we don't lose access to the l2 table.
5137 pmap_kenter(tmpl2, PAGE_SIZE,
5138 DMAP_TO_PHYS((vm_offset_t)l2) & ~L3_OFFSET, CACHED_MEMORY);
5139 l2 = (pt_entry_t *)(tmpl2 + ((vm_offset_t)l2 & PAGE_MASK));
5143 * The spare PV entries must be reserved prior to demoting the
5144 * mapping, that is, prior to changing the PDE. Otherwise, the state
5145 * of the L2 and the PV lists will be inconsistent, which can result
5146 * in reclaim_pv_chunk() attempting to remove a PV entry from the
5147 * wrong PV list and pmap_pv_demote_l2() failing to find the expected
5148 * PV entry for the 2MB page mapping that is being demoted.
5150 if ((oldl2 & ATTR_SW_MANAGED) != 0)
5151 reserve_pv_entries(pmap, Ln_ENTRIES - 1, lockp);
5154 * Pass PAGE_SIZE so that a single TLB invalidation is performed on
5155 * the 2MB page mapping.
5157 pmap_update_entry(pmap, l2, l3phys | L2_TABLE, va, PAGE_SIZE);
5160 * Demote the PV entry.
5162 if ((oldl2 & ATTR_SW_MANAGED) != 0)
5163 pmap_pv_demote_l2(pmap, va, oldl2 & ~ATTR_MASK, lockp);
5165 atomic_add_long(&pmap_l2_demotions, 1);
5166 CTR3(KTR_PMAP, "pmap_demote_l2: success for va %#lx"
5167 " in pmap %p %lx", va, pmap, l3[0]);
5171 pmap_kremove(tmpl2);
5172 kva_free(tmpl2, PAGE_SIZE);
5180 pmap_demote_l2(pmap_t pmap, pt_entry_t *l2, vm_offset_t va)
5182 struct rwlock *lock;
5186 l3 = pmap_demote_l2_locked(pmap, l2, va, &lock);
5193 * perform the pmap work for mincore
5196 pmap_mincore(pmap_t pmap, vm_offset_t addr, vm_paddr_t *locked_pa)
5198 pt_entry_t *pte, tpte;
5199 vm_paddr_t mask, pa;
5206 pte = pmap_pte(pmap, addr, &lvl);
5208 tpte = pmap_load(pte);
5221 panic("pmap_mincore: invalid level %d", lvl);
5224 val = MINCORE_INCORE;
5226 val |= MINCORE_SUPER;
5227 if (pmap_page_dirty(tpte))
5228 val |= MINCORE_MODIFIED | MINCORE_MODIFIED_OTHER;
5229 if ((tpte & ATTR_AF) == ATTR_AF)
5230 val |= MINCORE_REFERENCED | MINCORE_REFERENCED_OTHER;
5232 managed = (tpte & ATTR_SW_MANAGED) == ATTR_SW_MANAGED;
5233 pa = (tpte & ~ATTR_MASK) | (addr & mask);
5237 if ((val & (MINCORE_MODIFIED_OTHER | MINCORE_REFERENCED_OTHER)) !=
5238 (MINCORE_MODIFIED_OTHER | MINCORE_REFERENCED_OTHER) && managed) {
5239 /* Ensure that "PHYS_TO_VM_PAGE(pa)->object" doesn't change. */
5240 if (vm_page_pa_tryrelock(pmap, pa, locked_pa))
5243 PA_UNLOCK_COND(*locked_pa);
5250 pmap_activate(struct thread *td)
5255 pmap = vmspace_pmap(td->td_proc->p_vmspace);
5256 td->td_proc->p_md.md_l0addr = vtophys(pmap->pm_l0);
5257 __asm __volatile("msr ttbr0_el1, %0" : :
5258 "r"(td->td_proc->p_md.md_l0addr));
5259 pmap_invalidate_all(pmap);
5264 pmap_switch(struct thread *old, struct thread *new)
5266 pcpu_bp_harden bp_harden;
5269 /* Store the new curthread */
5270 PCPU_SET(curthread, new);
5272 /* And the new pcb */
5274 PCPU_SET(curpcb, pcb);
5277 * TODO: We may need to flush the cache here if switching
5278 * to a user process.
5282 old->td_proc->p_md.md_l0addr != new->td_proc->p_md.md_l0addr) {
5284 /* Switch to the new pmap */
5285 "msr ttbr0_el1, %0 \n"
5288 /* Invalidate the TLB */
5293 : : "r"(new->td_proc->p_md.md_l0addr));
5296 * Stop userspace from training the branch predictor against
5297 * other processes. This will call into a CPU specific
5298 * function that clears the branch predictor state.
5300 bp_harden = PCPU_GET(bp_harden);
5301 if (bp_harden != NULL)
5309 pmap_sync_icache(pmap_t pmap, vm_offset_t va, vm_size_t sz)
5312 if (va >= VM_MIN_KERNEL_ADDRESS) {
5313 cpu_icache_sync_range(va, sz);
5318 /* Find the length of data in this page to flush */
5319 offset = va & PAGE_MASK;
5320 len = imin(PAGE_SIZE - offset, sz);
5323 /* Extract the physical address & find it in the DMAP */
5324 pa = pmap_extract(pmap, va);
5326 cpu_icache_sync_range(PHYS_TO_DMAP(pa), len);
5328 /* Move to the next page */
5331 /* Set the length for the next iteration */
5332 len = imin(PAGE_SIZE, sz);
5338 pmap_fault(pmap_t pmap, uint64_t esr, uint64_t far)
5344 switch (ESR_ELx_EXCEPTION(esr)) {
5345 case EXCP_INSN_ABORT_L:
5346 case EXCP_INSN_ABORT:
5347 case EXCP_DATA_ABORT_L:
5348 case EXCP_DATA_ABORT:
5351 return (KERN_FAILURE);
5354 /* Data and insn aborts use same encoding for FCS field. */
5355 switch (esr & ISS_DATA_DFSC_MASK) {
5356 case ISS_DATA_DFSC_TF_L0:
5357 case ISS_DATA_DFSC_TF_L1:
5358 case ISS_DATA_DFSC_TF_L2:
5359 case ISS_DATA_DFSC_TF_L3:
5361 /* Ask the MMU to check the address */
5362 intr = intr_disable();
5363 if (pmap == kernel_pmap)
5364 par = arm64_address_translate_s1e1r(far);
5366 par = arm64_address_translate_s1e0r(far);
5371 * If the translation was successful the address was invalid
5372 * due to a break-before-make sequence. We can unlock and
5373 * return success to the trap handler.
5375 if (PAR_SUCCESS(par))
5376 return (KERN_SUCCESS);
5383 return (KERN_FAILURE);
5387 * Increase the starting virtual address of the given mapping if a
5388 * different alignment might result in more superpage mappings.
5391 pmap_align_superpage(vm_object_t object, vm_ooffset_t offset,
5392 vm_offset_t *addr, vm_size_t size)
5394 vm_offset_t superpage_offset;
5398 if (object != NULL && (object->flags & OBJ_COLORED) != 0)
5399 offset += ptoa(object->pg_color);
5400 superpage_offset = offset & L2_OFFSET;
5401 if (size - ((L2_SIZE - superpage_offset) & L2_OFFSET) < L2_SIZE ||
5402 (*addr & L2_OFFSET) == superpage_offset)
5404 if ((*addr & L2_OFFSET) < superpage_offset)
5405 *addr = (*addr & ~L2_OFFSET) + superpage_offset;
5407 *addr = ((*addr + L2_OFFSET) & ~L2_OFFSET) + superpage_offset;
5411 * Get the kernel virtual address of a set of physical pages. If there are
5412 * physical addresses not covered by the DMAP perform a transient mapping
5413 * that will be removed when calling pmap_unmap_io_transient.
5415 * \param page The pages the caller wishes to obtain the virtual
5416 * address on the kernel memory map.
5417 * \param vaddr On return contains the kernel virtual memory address
5418 * of the pages passed in the page parameter.
5419 * \param count Number of pages passed in.
5420 * \param can_fault TRUE if the thread using the mapped pages can take
5421 * page faults, FALSE otherwise.
5423 * \returns TRUE if the caller must call pmap_unmap_io_transient when
5424 * finished or FALSE otherwise.
5428 pmap_map_io_transient(vm_page_t page[], vm_offset_t vaddr[], int count,
5429 boolean_t can_fault)
5432 boolean_t needs_mapping;
5436 * Allocate any KVA space that we need, this is done in a separate
5437 * loop to prevent calling vmem_alloc while pinned.
5439 needs_mapping = FALSE;
5440 for (i = 0; i < count; i++) {
5441 paddr = VM_PAGE_TO_PHYS(page[i]);
5442 if (__predict_false(!PHYS_IN_DMAP(paddr))) {
5443 error = vmem_alloc(kernel_arena, PAGE_SIZE,
5444 M_BESTFIT | M_WAITOK, &vaddr[i]);
5445 KASSERT(error == 0, ("vmem_alloc failed: %d", error));
5446 needs_mapping = TRUE;
5448 vaddr[i] = PHYS_TO_DMAP(paddr);
5452 /* Exit early if everything is covered by the DMAP */
5458 for (i = 0; i < count; i++) {
5459 paddr = VM_PAGE_TO_PHYS(page[i]);
5460 if (!PHYS_IN_DMAP(paddr)) {
5462 "pmap_map_io_transient: TODO: Map out of DMAP data");
5466 return (needs_mapping);
5470 pmap_unmap_io_transient(vm_page_t page[], vm_offset_t vaddr[], int count,
5471 boolean_t can_fault)
5478 for (i = 0; i < count; i++) {
5479 paddr = VM_PAGE_TO_PHYS(page[i]);
5480 if (!PHYS_IN_DMAP(paddr)) {
5481 panic("ARM64TODO: pmap_unmap_io_transient: Unmap data");
5487 pmap_is_valid_memattr(pmap_t pmap __unused, vm_memattr_t mode)
5490 return (mode >= VM_MEMATTR_DEVICE && mode <= VM_MEMATTR_WRITE_THROUGH);