2 * SPDX-License-Identifier: BSD-3-Clause AND BSD-2-Clause-FreeBSD
4 * Copyright (c) 1991 Regents of the University of California.
5 * Copyright (c) 1994 John S. Dyson
6 * Copyright (c) 1994 David Greenman
7 * Copyright (c) 2005-2010 Alan L. Cox <alc@cs.rice.edu>
8 * Copyright (c) 2014-2016 Svatopluk Kraus <skra@FreeBSD.org>
9 * Copyright (c) 2014-2016 Michal Meloun <mmel@FreeBSD.org>
10 * All rights reserved.
12 * This code is derived from software contributed to Berkeley by
13 * the Systems Programming Group of the University of Utah Computer
14 * Science Department and William Jolitz of UUNET Technologies Inc.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 3. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * from: @(#)pmap.c 7.7 (Berkeley) 5/12/91
43 * Copyright (c) 2003 Networks Associates Technology, Inc.
44 * All rights reserved.
46 * This software was developed for the FreeBSD Project by Jake Burkholder,
47 * Safeport Network Services, and Network Associates Laboratories, the
48 * Security Research Division of Network Associates, Inc. under
49 * DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA
50 * CHATS research program.
52 * Redistribution and use in source and binary forms, with or without
53 * modification, are permitted provided that the following conditions
55 * 1. Redistributions of source code must retain the above copyright
56 * notice, this list of conditions and the following disclaimer.
57 * 2. Redistributions in binary form must reproduce the above copyright
58 * notice, this list of conditions and the following disclaimer in the
59 * documentation and/or other materials provided with the distribution.
61 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
62 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
63 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
64 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
65 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
66 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
67 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
68 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
69 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
70 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
74 #include <sys/cdefs.h>
75 __FBSDID("$FreeBSD$");
78 * Manages physical address maps.
80 * Since the information managed by this module is
81 * also stored by the logical address mapping module,
82 * this module may throw away valid virtual-to-physical
83 * mappings at almost any time. However, invalidations
84 * of virtual-to-physical mappings must be done as
87 * In order to cope with hardware architectures which
88 * make virtual-to-physical map invalidates expensive,
89 * this module may delay invalidate or reduced protection
90 * operations until such time as they are actually
91 * necessary. This module is given full information as
92 * to which processors are currently using which maps,
93 * and to when physical maps must be made correct.
100 #include <sys/param.h>
101 #include <sys/systm.h>
102 #include <sys/kernel.h>
104 #include <sys/lock.h>
105 #include <sys/proc.h>
106 #include <sys/rwlock.h>
107 #include <sys/malloc.h>
108 #include <sys/vmmeter.h>
109 #include <sys/malloc.h>
110 #include <sys/mman.h>
111 #include <sys/sf_buf.h>
113 #include <sys/sched.h>
114 #include <sys/sysctl.h>
120 #include <machine/physmem.h>
125 #include <vm/vm_param.h>
126 #include <vm/vm_kern.h>
127 #include <vm/vm_object.h>
128 #include <vm/vm_map.h>
129 #include <vm/vm_page.h>
130 #include <vm/vm_pageout.h>
131 #include <vm/vm_phys.h>
132 #include <vm/vm_extern.h>
133 #include <vm/vm_reserv.h>
134 #include <sys/lock.h>
135 #include <sys/mutex.h>
137 #include <machine/md_var.h>
138 #include <machine/pmap_var.h>
139 #include <machine/cpu.h>
140 #include <machine/pcb.h>
141 #include <machine/sf_buf.h>
143 #include <machine/smp.h>
145 #ifndef PMAP_SHPGPERPROC
146 #define PMAP_SHPGPERPROC 200
150 #define PMAP_INLINE __inline
156 static void pmap_zero_page_check(vm_page_t m);
157 void pmap_debug(int level);
158 int pmap_pid_dump(int pid);
160 #define PDEBUG(_lev_,_stat_) \
161 if (pmap_debug_level >= (_lev_)) \
163 #define dprintf printf
164 int pmap_debug_level = 1;
165 #else /* PMAP_DEBUG */
166 #define PDEBUG(_lev_,_stat_) /* Nothing */
167 #define dprintf(x, arg...)
168 #endif /* PMAP_DEBUG */
171 * Level 2 page tables map definion ('max' is excluded).
174 #define PT2V_MIN_ADDRESS ((vm_offset_t)PT2MAP)
175 #define PT2V_MAX_ADDRESS ((vm_offset_t)PT2MAP + PT2MAP_SIZE)
177 #define UPT2V_MIN_ADDRESS ((vm_offset_t)PT2MAP)
178 #define UPT2V_MAX_ADDRESS \
179 ((vm_offset_t)(PT2MAP + (KERNBASE >> PT2MAP_SHIFT)))
182 * Promotion to a 1MB (PTE1) page mapping requires that the corresponding
183 * 4KB (PTE2) page mappings have identical settings for the following fields:
185 #define PTE2_PROMOTE (PTE2_V | PTE2_A | PTE2_NM | PTE2_S | PTE2_NG | \
186 PTE2_NX | PTE2_RO | PTE2_U | PTE2_W | \
189 #define PTE1_PROMOTE (PTE1_V | PTE1_A | PTE1_NM | PTE1_S | PTE1_NG | \
190 PTE1_NX | PTE1_RO | PTE1_U | PTE1_W | \
193 #define ATTR_TO_L1(l2_attr) ((((l2_attr) & L2_TEX0) ? L1_S_TEX0 : 0) | \
194 (((l2_attr) & L2_C) ? L1_S_C : 0) | \
195 (((l2_attr) & L2_B) ? L1_S_B : 0) | \
196 (((l2_attr) & PTE2_A) ? PTE1_A : 0) | \
197 (((l2_attr) & PTE2_NM) ? PTE1_NM : 0) | \
198 (((l2_attr) & PTE2_S) ? PTE1_S : 0) | \
199 (((l2_attr) & PTE2_NG) ? PTE1_NG : 0) | \
200 (((l2_attr) & PTE2_NX) ? PTE1_NX : 0) | \
201 (((l2_attr) & PTE2_RO) ? PTE1_RO : 0) | \
202 (((l2_attr) & PTE2_U) ? PTE1_U : 0) | \
203 (((l2_attr) & PTE2_W) ? PTE1_W : 0))
205 #define ATTR_TO_L2(l1_attr) ((((l1_attr) & L1_S_TEX0) ? L2_TEX0 : 0) | \
206 (((l1_attr) & L1_S_C) ? L2_C : 0) | \
207 (((l1_attr) & L1_S_B) ? L2_B : 0) | \
208 (((l1_attr) & PTE1_A) ? PTE2_A : 0) | \
209 (((l1_attr) & PTE1_NM) ? PTE2_NM : 0) | \
210 (((l1_attr) & PTE1_S) ? PTE2_S : 0) | \
211 (((l1_attr) & PTE1_NG) ? PTE2_NG : 0) | \
212 (((l1_attr) & PTE1_NX) ? PTE2_NX : 0) | \
213 (((l1_attr) & PTE1_RO) ? PTE2_RO : 0) | \
214 (((l1_attr) & PTE1_U) ? PTE2_U : 0) | \
215 (((l1_attr) & PTE1_W) ? PTE2_W : 0))
218 * PTE2 descriptors creation macros.
220 #define PTE2_ATTR_DEFAULT vm_memattr_to_pte2(VM_MEMATTR_DEFAULT)
221 #define PTE2_ATTR_PT vm_memattr_to_pte2(pt_memattr)
223 #define PTE2_KPT(pa) PTE2_KERN(pa, PTE2_AP_KRW, PTE2_ATTR_PT)
224 #define PTE2_KPT_NG(pa) PTE2_KERN_NG(pa, PTE2_AP_KRW, PTE2_ATTR_PT)
226 #define PTE2_KRW(pa) PTE2_KERN(pa, PTE2_AP_KRW, PTE2_ATTR_DEFAULT)
227 #define PTE2_KRO(pa) PTE2_KERN(pa, PTE2_AP_KR, PTE2_ATTR_DEFAULT)
231 #define PV_STAT(x) do { x ; } while (0)
233 #define PV_STAT(x) do { } while (0)
237 * The boot_pt1 is used temporary in very early boot stage as L1 page table.
238 * We can init many things with no memory allocation thanks to its static
239 * allocation and this brings two main advantages:
240 * (1) other cores can be started very simply,
241 * (2) various boot loaders can be supported as its arguments can be processed
242 * in virtual address space and can be moved to safe location before
243 * first allocation happened.
244 * Only disadvantage is that boot_pt1 is used only in very early boot stage.
245 * However, the table is uninitialized and so lays in bss. Therefore kernel
246 * image size is not influenced.
248 * QQQ: In the future, maybe, boot_pt1 can be used for soft reset and
249 * CPU suspend/resume game.
251 extern pt1_entry_t boot_pt1[];
254 pt1_entry_t *kern_pt1;
255 pt2_entry_t *kern_pt2tab;
258 static uint32_t ttb_flags;
259 static vm_memattr_t pt_memattr;
260 ttb_entry_t pmap_kern_ttb;
262 struct pmap kernel_pmap_store;
263 LIST_HEAD(pmaplist, pmap);
264 static struct pmaplist allpmaps;
265 static struct mtx allpmaps_lock;
267 vm_offset_t virtual_avail; /* VA of first avail page (after kernel bss) */
268 vm_offset_t virtual_end; /* VA of last avail page (end of kernel AS) */
270 static vm_offset_t kernel_vm_end_new;
271 vm_offset_t kernel_vm_end = KERNBASE + NKPT2PG * NPT2_IN_PG * PTE1_SIZE;
272 vm_offset_t vm_max_kernel_address;
273 vm_paddr_t kernel_l1pa;
275 static struct rwlock __aligned(CACHE_LINE_SIZE) pvh_global_lock;
278 * Data for the pv entry allocation mechanism
280 static TAILQ_HEAD(pch, pv_chunk) pv_chunks = TAILQ_HEAD_INITIALIZER(pv_chunks);
281 static int pv_entry_count = 0, pv_entry_max = 0, pv_entry_high_water = 0;
282 static struct md_page *pv_table; /* XXX: Is it used only the list in md_page? */
283 static int shpgperproc = PMAP_SHPGPERPROC;
285 struct pv_chunk *pv_chunkbase; /* KVA block for pv_chunks */
286 int pv_maxchunks; /* How many chunks we have KVA for */
287 vm_offset_t pv_vafree; /* freelist stored in the PTE */
289 vm_paddr_t first_managed_pa;
290 #define pa_to_pvh(pa) (&pv_table[pte1_index(pa - first_managed_pa)])
293 * All those kernel PT submaps that BSD is so fond of
300 static caddr_t crashdumpmap;
302 static pt2_entry_t *PMAP1 = NULL, *PMAP2;
303 static pt2_entry_t *PADDR1 = NULL, *PADDR2;
305 static pt2_entry_t *PMAP3;
306 static pt2_entry_t *PADDR3;
307 static int PMAP3cpu __unused; /* for SMP only */
311 static int PMAP1changedcpu;
312 SYSCTL_INT(_debug, OID_AUTO, PMAP1changedcpu, CTLFLAG_RD,
314 "Number of times pmap_pte2_quick changed CPU with same PMAP1");
316 static int PMAP1changed;
317 SYSCTL_INT(_debug, OID_AUTO, PMAP1changed, CTLFLAG_RD,
319 "Number of times pmap_pte2_quick changed PMAP1");
320 static int PMAP1unchanged;
321 SYSCTL_INT(_debug, OID_AUTO, PMAP1unchanged, CTLFLAG_RD,
323 "Number of times pmap_pte2_quick didn't change PMAP1");
324 static struct mtx PMAP2mutex;
327 * Internal flags for pmap_enter()'s helper functions.
329 #define PMAP_ENTER_NORECLAIM 0x1000000 /* Don't reclaim PV entries. */
330 #define PMAP_ENTER_NOREPLACE 0x2000000 /* Don't replace mappings. */
332 static __inline void pt2_wirecount_init(vm_page_t m);
333 static boolean_t pmap_demote_pte1(pmap_t pmap, pt1_entry_t *pte1p,
335 static int pmap_enter_pte1(pmap_t pmap, vm_offset_t va, pt1_entry_t pte1,
336 u_int flags, vm_page_t m);
337 void cache_icache_sync_fresh(vm_offset_t va, vm_paddr_t pa, vm_size_t size);
340 * Function to set the debug level of the pmap code.
344 pmap_debug(int level)
347 pmap_debug_level = level;
348 dprintf("pmap_debug: level=%d\n", pmap_debug_level);
350 #endif /* PMAP_DEBUG */
353 * This table must corespond with memory attribute configuration in vm.h.
354 * First entry is used for normal system mapping.
356 * Device memory is always marked as shared.
357 * Normal memory is shared only in SMP .
358 * Not outer shareable bits are not used yet.
359 * Class 6 cannot be used on ARM11.
361 #define TEXDEF_TYPE_SHIFT 0
362 #define TEXDEF_TYPE_MASK 0x3
363 #define TEXDEF_INNER_SHIFT 2
364 #define TEXDEF_INNER_MASK 0x3
365 #define TEXDEF_OUTER_SHIFT 4
366 #define TEXDEF_OUTER_MASK 0x3
367 #define TEXDEF_NOS_SHIFT 6
368 #define TEXDEF_NOS_MASK 0x1
370 #define TEX(t, i, o, s) \
371 ((t) << TEXDEF_TYPE_SHIFT) | \
372 ((i) << TEXDEF_INNER_SHIFT) | \
373 ((o) << TEXDEF_OUTER_SHIFT | \
374 ((s) << TEXDEF_NOS_SHIFT))
376 static uint32_t tex_class[8] = {
377 /* type inner cache outer cache */
378 TEX(PRRR_MEM, NMRR_WB_WA, NMRR_WB_WA, 0), /* 0 - ATTR_WB_WA */
379 TEX(PRRR_MEM, NMRR_NC, NMRR_NC, 0), /* 1 - ATTR_NOCACHE */
380 TEX(PRRR_DEV, NMRR_NC, NMRR_NC, 0), /* 2 - ATTR_DEVICE */
381 TEX(PRRR_SO, NMRR_NC, NMRR_NC, 0), /* 3 - ATTR_SO */
382 TEX(PRRR_MEM, NMRR_WT, NMRR_WT, 0), /* 4 - ATTR_WT */
383 TEX(PRRR_MEM, NMRR_NC, NMRR_NC, 0), /* 5 - NOT USED YET */
384 TEX(PRRR_MEM, NMRR_NC, NMRR_NC, 0), /* 6 - NOT USED YET */
385 TEX(PRRR_MEM, NMRR_NC, NMRR_NC, 0), /* 7 - NOT USED YET */
389 static uint32_t pte2_attr_tab[8] = {
390 PTE2_ATTR_WB_WA, /* 0 - VM_MEMATTR_WB_WA */
391 PTE2_ATTR_NOCACHE, /* 1 - VM_MEMATTR_NOCACHE */
392 PTE2_ATTR_DEVICE, /* 2 - VM_MEMATTR_DEVICE */
393 PTE2_ATTR_SO, /* 3 - VM_MEMATTR_SO */
394 PTE2_ATTR_WT, /* 4 - VM_MEMATTR_WRITE_THROUGH */
395 0, /* 5 - NOT USED YET */
396 0, /* 6 - NOT USED YET */
397 0 /* 7 - NOT USED YET */
399 CTASSERT(VM_MEMATTR_WB_WA == 0);
400 CTASSERT(VM_MEMATTR_NOCACHE == 1);
401 CTASSERT(VM_MEMATTR_DEVICE == 2);
402 CTASSERT(VM_MEMATTR_SO == 3);
403 CTASSERT(VM_MEMATTR_WRITE_THROUGH == 4);
404 #define VM_MEMATTR_END (VM_MEMATTR_WRITE_THROUGH + 1)
407 pmap_is_valid_memattr(pmap_t pmap __unused, vm_memattr_t mode)
410 return (mode >= 0 && mode < VM_MEMATTR_END);
413 static inline uint32_t
414 vm_memattr_to_pte2(vm_memattr_t ma)
417 KASSERT((u_int)ma < VM_MEMATTR_END,
418 ("%s: bad vm_memattr_t %d", __func__, ma));
419 return (pte2_attr_tab[(u_int)ma]);
422 static inline uint32_t
423 vm_page_pte2_attr(vm_page_t m)
426 return (vm_memattr_to_pte2(m->md.pat_mode));
430 * Convert TEX definition entry to TTB flags.
433 encode_ttb_flags(int idx)
435 uint32_t inner, outer, nos, reg;
437 inner = (tex_class[idx] >> TEXDEF_INNER_SHIFT) &
439 outer = (tex_class[idx] >> TEXDEF_OUTER_SHIFT) &
441 nos = (tex_class[idx] >> TEXDEF_NOS_SHIFT) &
446 if (cpuinfo.coherent_walk)
447 reg |= (inner & 0x1) << 6;
448 reg |= (inner & 0x2) >> 1;
458 * Set TEX remapping registers in current CPU.
464 uint32_t type, inner, outer, nos;
467 #ifdef PMAP_PTE_NOCACHE
469 if (cpuinfo.coherent_walk) {
470 pt_memattr = VM_MEMATTR_WB_WA;
471 ttb_flags = encode_ttb_flags(0);
474 pt_memattr = VM_MEMATTR_NOCACHE;
475 ttb_flags = encode_ttb_flags(1);
478 pt_memattr = VM_MEMATTR_WB_WA;
479 ttb_flags = encode_ttb_flags(0);
485 /* Build remapping register from TEX classes. */
486 for (i = 0; i < 8; i++) {
487 type = (tex_class[i] >> TEXDEF_TYPE_SHIFT) &
489 inner = (tex_class[i] >> TEXDEF_INNER_SHIFT) &
491 outer = (tex_class[i] >> TEXDEF_OUTER_SHIFT) &
493 nos = (tex_class[i] >> TEXDEF_NOS_SHIFT) &
496 prrr |= type << (i * 2);
497 prrr |= nos << (i + 24);
498 nmrr |= inner << (i * 2);
499 nmrr |= outer << (i * 2 + 16);
501 /* Add shareable bits for device memory. */
502 prrr |= PRRR_DS0 | PRRR_DS1;
504 /* Add shareable bits for normal memory in SMP case. */
513 /* Caches are disabled, so full TLB flush should be enough. */
514 tlb_flush_all_local();
518 * Remap one vm_meattr class to another one. This can be useful as
519 * workaround for SOC errata, e.g. if devices must be accessed using
522 * !!! Please note that this function is absolutely last resort thing.
523 * It should not be used under normal circumstances. !!!
526 * - it shall be called after pmap_bootstrap_prepare() and before
527 * cpu_mp_start() (thus only on boot CPU). In practice, it's expected
528 * to be called from platform_attach() or platform_late_init().
530 * - if remapping doesn't change caching mode, or until uncached class
531 * is remapped to any kind of cached one, then no other restriction exists.
533 * - if pmap_remap_vm_attr() changes caching mode, but both (original and
534 * remapped) remain cached, then caller is resposible for calling
535 * of dcache_wbinv_poc_all().
537 * - remapping of any kind of cached class to uncached is not permitted.
540 pmap_remap_vm_attr(vm_memattr_t old_attr, vm_memattr_t new_attr)
542 int old_idx, new_idx;
544 /* Map VM memattrs to indexes to tex_class table. */
545 old_idx = PTE2_ATTR2IDX(pte2_attr_tab[(int)old_attr]);
546 new_idx = PTE2_ATTR2IDX(pte2_attr_tab[(int)new_attr]);
548 /* Replace TEX attribute and apply it. */
549 tex_class[old_idx] = tex_class[new_idx];
554 * KERNBASE must be multiple of NPT2_IN_PG * PTE1_SIZE. In other words,
555 * KERNBASE is mapped by first L2 page table in L2 page table page. It
556 * meets same constrain due to PT2MAP being placed just under KERNBASE.
558 CTASSERT((KERNBASE & (NPT2_IN_PG * PTE1_SIZE - 1)) == 0);
559 CTASSERT((KERNBASE - VM_MAXUSER_ADDRESS) >= PT2MAP_SIZE);
562 * In crazy dreams, PAGE_SIZE could be a multiple of PTE2_SIZE in general.
563 * For now, anyhow, the following check must be fulfilled.
565 CTASSERT(PAGE_SIZE == PTE2_SIZE);
567 * We don't want to mess up MI code with all MMU and PMAP definitions,
568 * so some things, which depend on other ones, are defined independently.
569 * Now, it is time to check that we don't screw up something.
571 CTASSERT(PDRSHIFT == PTE1_SHIFT);
573 * Check L1 and L2 page table entries definitions consistency.
575 CTASSERT(NB_IN_PT1 == (sizeof(pt1_entry_t) * NPTE1_IN_PT1));
576 CTASSERT(NB_IN_PT2 == (sizeof(pt2_entry_t) * NPTE2_IN_PT2));
578 * Check L2 page tables page consistency.
580 CTASSERT(PAGE_SIZE == (NPT2_IN_PG * NB_IN_PT2));
581 CTASSERT((1 << PT2PG_SHIFT) == NPT2_IN_PG);
583 * Check PT2TAB consistency.
584 * PT2TAB_ENTRIES is defined as a division of NPTE1_IN_PT1 by NPT2_IN_PG.
585 * This should be done without remainder.
587 CTASSERT(NPTE1_IN_PT1 == (PT2TAB_ENTRIES * NPT2_IN_PG));
592 * All level 2 page tables (PT2s) are mapped continuously and accordingly
593 * into PT2MAP address space. As PT2 size is less than PAGE_SIZE, this can
594 * be done only if PAGE_SIZE is a multiple of PT2 size. All PT2s in one page
595 * must be used together, but not necessary at once. The first PT2 in a page
596 * must map things on correctly aligned address and the others must follow
599 #define NB_IN_PT2TAB (PT2TAB_ENTRIES * sizeof(pt2_entry_t))
600 #define NPT2_IN_PT2TAB (NB_IN_PT2TAB / NB_IN_PT2)
601 #define NPG_IN_PT2TAB (NB_IN_PT2TAB / PAGE_SIZE)
604 * Check PT2TAB consistency.
605 * NPT2_IN_PT2TAB is defined as a division of NB_IN_PT2TAB by NB_IN_PT2.
606 * NPG_IN_PT2TAB is defined as a division of NB_IN_PT2TAB by PAGE_SIZE.
607 * The both should be done without remainder.
609 CTASSERT(NB_IN_PT2TAB == (NPT2_IN_PT2TAB * NB_IN_PT2));
610 CTASSERT(NB_IN_PT2TAB == (NPG_IN_PT2TAB * PAGE_SIZE));
612 * The implementation was made general, however, with the assumption
613 * bellow in mind. In case of another value of NPG_IN_PT2TAB,
614 * the code should be once more rechecked.
616 CTASSERT(NPG_IN_PT2TAB == 1);
619 * Get offset of PT2 in a page
620 * associated with given PT1 index.
622 static __inline u_int
623 page_pt2off(u_int pt1_idx)
626 return ((pt1_idx & PT2PG_MASK) * NB_IN_PT2);
630 * Get physical address of PT2
631 * associated with given PT2s page and PT1 index.
633 static __inline vm_paddr_t
634 page_pt2pa(vm_paddr_t pgpa, u_int pt1_idx)
637 return (pgpa + page_pt2off(pt1_idx));
641 * Get first entry of PT2
642 * associated with given PT2s page and PT1 index.
644 static __inline pt2_entry_t *
645 page_pt2(vm_offset_t pgva, u_int pt1_idx)
648 return ((pt2_entry_t *)(pgva + page_pt2off(pt1_idx)));
652 * Get virtual address of PT2s page (mapped in PT2MAP)
653 * which holds PT2 which holds entry which maps given virtual address.
655 static __inline vm_offset_t
656 pt2map_pt2pg(vm_offset_t va)
659 va &= ~(NPT2_IN_PG * PTE1_SIZE - 1);
660 return ((vm_offset_t)pt2map_entry(va));
663 /*****************************************************************************
665 * THREE pmap initialization milestones exist:
668 * -> fundamental init (including MMU) in ASM
671 * -> fundamental init continues in C
672 * -> first available physical address is known
674 * pmap_bootstrap_prepare() -> FIRST PMAP MILESTONE (first epoch begins)
675 * -> basic (safe) interface for physical address allocation is made
676 * -> basic (safe) interface for virtual mapping is made
677 * -> limited not SMP coherent work is possible
679 * -> more fundamental init continues in C
680 * -> locks and some more things are available
681 * -> all fundamental allocations and mappings are done
683 * pmap_bootstrap() -> SECOND PMAP MILESTONE (second epoch begins)
684 * -> phys_avail[] and virtual_avail is set
685 * -> control is passed to vm subsystem
686 * -> physical and virtual address allocation are off limit
687 * -> low level mapping functions, some SMP coherent,
688 * are available, which cannot be used before vm subsystem
692 * -> vm subsystem is being inited
694 * pmap_init() -> THIRD PMAP MILESTONE (third epoch begins)
695 * -> pmap is fully inited
697 *****************************************************************************/
699 /*****************************************************************************
701 * PMAP first stage initialization and utility functions
702 * for pre-bootstrap epoch.
704 * After pmap_bootstrap_prepare() is called, the following functions
707 * (1) strictly only for this stage functions for physical page allocations,
708 * virtual space allocations, and mappings:
710 * vm_paddr_t pmap_preboot_get_pages(u_int num);
711 * void pmap_preboot_map_pages(vm_paddr_t pa, vm_offset_t va, u_int num);
712 * vm_offset_t pmap_preboot_reserve_pages(u_int num);
713 * vm_offset_t pmap_preboot_get_vpages(u_int num);
714 * void pmap_preboot_map_attr(vm_paddr_t pa, vm_offset_t va, vm_size_t size,
715 * vm_prot_t prot, vm_memattr_t attr);
717 * (2) for all stages:
719 * vm_paddr_t pmap_kextract(vm_offset_t va);
721 * NOTE: This is not SMP coherent stage.
723 *****************************************************************************/
725 #define KERNEL_P2V(pa) \
726 ((vm_offset_t)((pa) - arm_physmem_kernaddr + KERNVIRTADDR))
727 #define KERNEL_V2P(va) \
728 ((vm_paddr_t)((va) - KERNVIRTADDR + arm_physmem_kernaddr))
730 static vm_paddr_t last_paddr;
733 * Pre-bootstrap epoch page allocator.
736 pmap_preboot_get_pages(u_int num)
741 last_paddr += num * PAGE_SIZE;
747 * The fundamental initialization of PMAP stuff.
749 * Some things already happened in locore.S and some things could happen
750 * before pmap_bootstrap_prepare() is called, so let's recall what is done:
751 * 1. Caches are disabled.
752 * 2. We are running on virtual addresses already with 'boot_pt1'
754 * 3. So far, all virtual addresses can be converted to physical ones and
755 * vice versa by the following macros:
756 * KERNEL_P2V(pa) .... physical to virtual ones,
757 * KERNEL_V2P(va) .... virtual to physical ones.
759 * What is done herein:
760 * 1. The 'boot_pt1' is replaced by real kernel L1 page table 'kern_pt1'.
761 * 2. PT2MAP magic is brought to live.
762 * 3. Basic preboot functions for page allocations and mappings can be used.
763 * 4. Everything is prepared for L1 cache enabling.
766 * 1. To use second TTB register, so kernel and users page tables will be
767 * separated. This way process forking - pmap_pinit() - could be faster,
768 * it saves physical pages and KVA per a process, and it's simple change.
769 * However, it will lead, due to hardware matter, to the following:
770 * (a) 2G space for kernel and 2G space for users.
771 * (b) 1G space for kernel in low addresses and 3G for users above it.
772 * A question is: Is the case (b) really an option? Note that case (b)
773 * does save neither physical memory and KVA.
776 pmap_bootstrap_prepare(vm_paddr_t last)
778 vm_paddr_t pt2pg_pa, pt2tab_pa, pa, size;
779 vm_offset_t pt2pg_va;
786 * Now, we are going to make real kernel mapping. Note that we are
787 * already running on some mapping made in locore.S and we expect
788 * that it's large enough to ensure nofault access to physical memory
789 * allocated herein before switch.
791 * As kernel image and everything needed before are and will be mapped
792 * by section mappings, we align last physical address to PTE1_SIZE.
794 last_paddr = pte1_roundup(last);
797 * Allocate and zero page(s) for kernel L1 page table.
799 * Note that it's first allocation on space which was PTE1_SIZE
800 * aligned and as such base_pt1 is aligned to NB_IN_PT1 too.
802 base_pt1 = pmap_preboot_get_pages(NPG_IN_PT1);
803 kern_pt1 = (pt1_entry_t *)KERNEL_P2V(base_pt1);
804 bzero((void*)kern_pt1, NB_IN_PT1);
805 pte1_sync_range(kern_pt1, NB_IN_PT1);
807 /* Allocate and zero page(s) for kernel PT2TAB. */
808 pt2tab_pa = pmap_preboot_get_pages(NPG_IN_PT2TAB);
809 kern_pt2tab = (pt2_entry_t *)KERNEL_P2V(pt2tab_pa);
810 bzero(kern_pt2tab, NB_IN_PT2TAB);
811 pte2_sync_range(kern_pt2tab, NB_IN_PT2TAB);
813 /* Allocate and zero page(s) for kernel L2 page tables. */
814 pt2pg_pa = pmap_preboot_get_pages(NKPT2PG);
815 pt2pg_va = KERNEL_P2V(pt2pg_pa);
816 size = NKPT2PG * PAGE_SIZE;
817 bzero((void*)pt2pg_va, size);
818 pte2_sync_range((pt2_entry_t *)pt2pg_va, size);
821 * Add a physical memory segment (vm_phys_seg) corresponding to the
822 * preallocated pages for kernel L2 page tables so that vm_page
823 * structures representing these pages will be created. The vm_page
824 * structures are required for promotion of the corresponding kernel
825 * virtual addresses to section mappings.
827 vm_phys_add_seg(pt2tab_pa, pmap_preboot_get_pages(0));
830 * Insert allocated L2 page table pages to PT2TAB and make
831 * link to all PT2s in L1 page table. See how kernel_vm_end
834 * We play simple and safe. So every KVA will have underlaying
835 * L2 page table, even kernel image mapped by sections.
837 pte2p = kern_pt2tab_entry(KERNBASE);
838 for (pa = pt2pg_pa; pa < pt2pg_pa + size; pa += PTE2_SIZE)
839 pt2tab_store(pte2p++, PTE2_KPT(pa));
841 pte1p = kern_pte1(KERNBASE);
842 for (pa = pt2pg_pa; pa < pt2pg_pa + size; pa += NB_IN_PT2)
843 pte1_store(pte1p++, PTE1_LINK(pa));
845 /* Make section mappings for kernel. */
846 l1_attr = ATTR_TO_L1(PTE2_ATTR_DEFAULT);
847 pte1p = kern_pte1(KERNBASE);
848 for (pa = KERNEL_V2P(KERNBASE); pa < last; pa += PTE1_SIZE)
849 pte1_store(pte1p++, PTE1_KERN(pa, PTE1_AP_KRW, l1_attr));
852 * Get free and aligned space for PT2MAP and make L1 page table links
853 * to L2 page tables held in PT2TAB.
855 * Note that pages holding PT2s are stored in PT2TAB as pt2_entry_t
856 * descriptors and PT2TAB page(s) itself is(are) used as PT2s. Thus
857 * each entry in PT2TAB maps all PT2s in a page. This implies that
858 * virtual address of PT2MAP must be aligned to NPT2_IN_PG * PTE1_SIZE.
860 PT2MAP = (pt2_entry_t *)(KERNBASE - PT2MAP_SIZE);
861 pte1p = kern_pte1((vm_offset_t)PT2MAP);
862 for (pa = pt2tab_pa, i = 0; i < NPT2_IN_PT2TAB; i++, pa += NB_IN_PT2) {
863 pte1_store(pte1p++, PTE1_LINK(pa));
867 * Store PT2TAB in PT2TAB itself, i.e. self reference mapping.
868 * Each pmap will hold own PT2TAB, so the mapping should be not global.
870 pte2p = kern_pt2tab_entry((vm_offset_t)PT2MAP);
871 for (pa = pt2tab_pa, i = 0; i < NPG_IN_PT2TAB; i++, pa += PTE2_SIZE) {
872 pt2tab_store(pte2p++, PTE2_KPT_NG(pa));
876 * Choose correct L2 page table and make mappings for allocations
877 * made herein which replaces temporary locore.S mappings after a while.
878 * Note that PT2MAP cannot be used until we switch to kern_pt1.
880 * Note, that these allocations started aligned on 1M section and
881 * kernel PT1 was allocated first. Making of mappings must follow
882 * order of physical allocations as we've used KERNEL_P2V() macro
883 * for virtual addresses resolution.
885 pte2p = kern_pt2tab_entry((vm_offset_t)kern_pt1);
886 pt2pg_va = KERNEL_P2V(pte2_pa(pte2_load(pte2p)));
888 pte2p = page_pt2(pt2pg_va, pte1_index((vm_offset_t)kern_pt1));
890 /* Make mapping for kernel L1 page table. */
891 for (pa = base_pt1, i = 0; i < NPG_IN_PT1; i++, pa += PTE2_SIZE)
892 pte2_store(pte2p++, PTE2_KPT(pa));
894 /* Make mapping for kernel PT2TAB. */
895 for (pa = pt2tab_pa, i = 0; i < NPG_IN_PT2TAB; i++, pa += PTE2_SIZE)
896 pte2_store(pte2p++, PTE2_KPT(pa));
898 /* Finally, switch from 'boot_pt1' to 'kern_pt1'. */
899 pmap_kern_ttb = base_pt1 | ttb_flags;
900 cpuinfo_reinit_mmu(pmap_kern_ttb);
902 * Initialize the first available KVA. As kernel image is mapped by
903 * sections, we are leaving some gap behind.
905 virtual_avail = (vm_offset_t)kern_pt2tab + NPG_IN_PT2TAB * PAGE_SIZE;
909 * Setup L2 page table page for given KVA.
910 * Used in pre-bootstrap epoch.
912 * Note that we have allocated NKPT2PG pages for L2 page tables in advance
913 * and used them for mapping KVA starting from KERNBASE. However, this is not
914 * enough. Vectors and devices need L2 page tables too. Note that they are
915 * even above VM_MAX_KERNEL_ADDRESS.
917 static __inline vm_paddr_t
918 pmap_preboot_pt2pg_setup(vm_offset_t va)
920 pt2_entry_t *pte2p, pte2;
923 /* Get associated entry in PT2TAB. */
924 pte2p = kern_pt2tab_entry(va);
926 /* Just return, if PT2s page exists already. */
927 pte2 = pt2tab_load(pte2p);
928 if (pte2_is_valid(pte2))
929 return (pte2_pa(pte2));
931 KASSERT(va >= VM_MAX_KERNEL_ADDRESS,
932 ("%s: NKPT2PG too small", __func__));
935 * Allocate page for PT2s and insert it to PT2TAB.
936 * In other words, map it into PT2MAP space.
938 pt2pg_pa = pmap_preboot_get_pages(1);
939 pt2tab_store(pte2p, PTE2_KPT(pt2pg_pa));
941 /* Zero all PT2s in allocated page. */
942 bzero((void*)pt2map_pt2pg(va), PAGE_SIZE);
943 pte2_sync_range((pt2_entry_t *)pt2map_pt2pg(va), PAGE_SIZE);
949 * Setup L2 page table for given KVA.
950 * Used in pre-bootstrap epoch.
953 pmap_preboot_pt2_setup(vm_offset_t va)
956 vm_paddr_t pt2pg_pa, pt2_pa;
958 /* Setup PT2's page. */
959 pt2pg_pa = pmap_preboot_pt2pg_setup(va);
960 pt2_pa = page_pt2pa(pt2pg_pa, pte1_index(va));
962 /* Insert PT2 to PT1. */
963 pte1p = kern_pte1(va);
964 pte1_store(pte1p, PTE1_LINK(pt2_pa));
968 * Get L2 page entry associated with given KVA.
969 * Used in pre-bootstrap epoch.
971 static __inline pt2_entry_t*
972 pmap_preboot_vtopte2(vm_offset_t va)
976 /* Setup PT2 if needed. */
977 pte1p = kern_pte1(va);
978 if (!pte1_is_valid(pte1_load(pte1p))) /* XXX - sections ?! */
979 pmap_preboot_pt2_setup(va);
981 return (pt2map_entry(va));
985 * Pre-bootstrap epoch page(s) mapping(s).
988 pmap_preboot_map_pages(vm_paddr_t pa, vm_offset_t va, u_int num)
993 /* Map all the pages. */
994 for (i = 0; i < num; i++) {
995 pte2p = pmap_preboot_vtopte2(va);
996 pte2_store(pte2p, PTE2_KRW(pa));
1003 * Pre-bootstrap epoch virtual space alocator.
1006 pmap_preboot_reserve_pages(u_int num)
1009 vm_offset_t start, va;
1012 /* Allocate virtual space. */
1013 start = va = virtual_avail;
1014 virtual_avail += num * PAGE_SIZE;
1016 /* Zero the mapping. */
1017 for (i = 0; i < num; i++) {
1018 pte2p = pmap_preboot_vtopte2(va);
1019 pte2_store(pte2p, 0);
1027 * Pre-bootstrap epoch page(s) allocation and mapping(s).
1030 pmap_preboot_get_vpages(u_int num)
1035 /* Allocate physical page(s). */
1036 pa = pmap_preboot_get_pages(num);
1038 /* Allocate virtual space. */
1040 virtual_avail += num * PAGE_SIZE;
1042 /* Map and zero all. */
1043 pmap_preboot_map_pages(pa, va, num);
1044 bzero((void *)va, num * PAGE_SIZE);
1050 * Pre-bootstrap epoch page mapping(s) with attributes.
1053 pmap_preboot_map_attr(vm_paddr_t pa, vm_offset_t va, vm_size_t size,
1054 vm_prot_t prot, vm_memattr_t attr)
1057 u_int l1_attr, l1_prot, l2_prot, l2_attr;
1061 l2_prot = prot & VM_PROT_WRITE ? PTE2_AP_KRW : PTE2_AP_KR;
1062 l2_prot |= (prot & VM_PROT_EXECUTE) ? PTE2_X : PTE2_NX;
1063 l2_attr = vm_memattr_to_pte2(attr);
1064 l1_prot = ATTR_TO_L1(l2_prot);
1065 l1_attr = ATTR_TO_L1(l2_attr);
1067 /* Map all the pages. */
1068 num = round_page(size);
1070 if ((((va | pa) & PTE1_OFFSET) == 0) && (num >= PTE1_SIZE)) {
1071 pte1p = kern_pte1(va);
1072 pte1_store(pte1p, PTE1_KERN(pa, l1_prot, l1_attr));
1077 pte2p = pmap_preboot_vtopte2(va);
1078 pte2_store(pte2p, PTE2_KERN(pa, l2_prot, l2_attr));
1087 * Extract from the kernel page table the physical address
1088 * that is mapped by the given virtual address "va".
1091 pmap_kextract(vm_offset_t va)
1097 pte1 = pte1_load(kern_pte1(va));
1098 if (pte1_is_section(pte1)) {
1099 pa = pte1_pa(pte1) | (va & PTE1_OFFSET);
1100 } else if (pte1_is_link(pte1)) {
1102 * We should beware of concurrent promotion that changes
1103 * pte1 at this point. However, it's not a problem as PT2
1104 * page is preserved by promotion in PT2TAB. So even if
1105 * it happens, using of PT2MAP is still safe.
1107 * QQQ: However, concurrent removing is a problem which
1108 * ends in abort on PT2MAP space. Locking must be used
1109 * to deal with this.
1111 pte2 = pte2_load(pt2map_entry(va));
1112 pa = pte2_pa(pte2) | (va & PTE2_OFFSET);
1115 panic("%s: va %#x pte1 %#x", __func__, va, pte1);
1121 * Extract from the kernel page table the physical address
1122 * that is mapped by the given virtual address "va". Also
1123 * return L2 page table entry which maps the address.
1125 * This is only intended to be used for panic dumps.
1128 pmap_dump_kextract(vm_offset_t va, pt2_entry_t *pte2p)
1134 pte1 = pte1_load(kern_pte1(va));
1135 if (pte1_is_section(pte1)) {
1136 pa = pte1_pa(pte1) | (va & PTE1_OFFSET);
1137 pte2 = pa | ATTR_TO_L2(pte1) | PTE2_V;
1138 } else if (pte1_is_link(pte1)) {
1139 pte2 = pte2_load(pt2map_entry(va));
1150 /*****************************************************************************
1152 * PMAP second stage initialization and utility functions
1153 * for bootstrap epoch.
1155 * After pmap_bootstrap() is called, the following functions for
1156 * mappings can be used:
1158 * void pmap_kenter(vm_offset_t va, vm_paddr_t pa);
1159 * void pmap_kremove(vm_offset_t va);
1160 * vm_offset_t pmap_map(vm_offset_t *virt, vm_paddr_t start, vm_paddr_t end,
1163 * NOTE: This is not SMP coherent stage. And physical page allocation is not
1164 * allowed during this stage.
1166 *****************************************************************************/
1169 * Initialize kernel PMAP locks and lists, kernel_pmap itself, and
1170 * reserve various virtual spaces for temporary mappings.
1173 pmap_bootstrap(vm_offset_t firstaddr)
1175 pt2_entry_t *unused __unused;
1179 * Initialize the kernel pmap (which is statically allocated).
1181 PMAP_LOCK_INIT(kernel_pmap);
1182 kernel_l1pa = (vm_paddr_t)kern_pt1; /* for libkvm */
1183 kernel_pmap->pm_pt1 = kern_pt1;
1184 kernel_pmap->pm_pt2tab = kern_pt2tab;
1185 CPU_FILL(&kernel_pmap->pm_active); /* don't allow deactivation */
1186 TAILQ_INIT(&kernel_pmap->pm_pvchunk);
1189 * Initialize the global pv list lock.
1191 rw_init(&pvh_global_lock, "pmap pv global");
1193 LIST_INIT(&allpmaps);
1196 * Request a spin mutex so that changes to allpmaps cannot be
1197 * preempted by smp_rendezvous_cpus().
1199 mtx_init(&allpmaps_lock, "allpmaps", NULL, MTX_SPIN);
1200 mtx_lock_spin(&allpmaps_lock);
1201 LIST_INSERT_HEAD(&allpmaps, kernel_pmap, pm_list);
1202 mtx_unlock_spin(&allpmaps_lock);
1205 * Reserve some special page table entries/VA space for temporary
1208 #define SYSMAP(c, p, v, n) do { \
1209 v = (c)pmap_preboot_reserve_pages(n); \
1210 p = pt2map_entry((vm_offset_t)v); \
1214 * Local CMAP1/CMAP2 are used for zeroing and copying pages.
1215 * Local CMAP2 is also used for data cache cleaning.
1218 mtx_init(&pc->pc_cmap_lock, "SYSMAPS", NULL, MTX_DEF);
1219 SYSMAP(caddr_t, pc->pc_cmap1_pte2p, pc->pc_cmap1_addr, 1);
1220 SYSMAP(caddr_t, pc->pc_cmap2_pte2p, pc->pc_cmap2_addr, 1);
1221 SYSMAP(vm_offset_t, pc->pc_qmap_pte2p, pc->pc_qmap_addr, 1);
1226 SYSMAP(caddr_t, unused, crashdumpmap, MAXDUMPPGS);
1229 * _tmppt is used for reading arbitrary physical pages via /dev/mem.
1231 SYSMAP(caddr_t, unused, _tmppt, 1);
1234 * PADDR1 and PADDR2 are used by pmap_pte2_quick() and pmap_pte2(),
1235 * respectively. PADDR3 is used by pmap_pte2_ddb().
1237 SYSMAP(pt2_entry_t *, PMAP1, PADDR1, 1);
1238 SYSMAP(pt2_entry_t *, PMAP2, PADDR2, 1);
1240 SYSMAP(pt2_entry_t *, PMAP3, PADDR3, 1);
1242 mtx_init(&PMAP2mutex, "PMAP2", NULL, MTX_DEF);
1245 * Note that in very short time in initarm(), we are going to
1246 * initialize phys_avail[] array and no further page allocation
1247 * can happen after that until vm subsystem will be initialized.
1249 kernel_vm_end_new = kernel_vm_end;
1250 virtual_end = vm_max_kernel_address;
1254 pmap_init_reserved_pages(void)
1263 * Skip if the mapping has already been initialized,
1264 * i.e. this is the BSP.
1266 if (pc->pc_cmap1_addr != 0)
1268 mtx_init(&pc->pc_cmap_lock, "SYSMAPS", NULL, MTX_DEF);
1269 pages = kva_alloc(PAGE_SIZE * 3);
1271 panic("%s: unable to allocate KVA", __func__);
1272 pc->pc_cmap1_pte2p = pt2map_entry(pages);
1273 pc->pc_cmap2_pte2p = pt2map_entry(pages + PAGE_SIZE);
1274 pc->pc_qmap_pte2p = pt2map_entry(pages + (PAGE_SIZE * 2));
1275 pc->pc_cmap1_addr = (caddr_t)pages;
1276 pc->pc_cmap2_addr = (caddr_t)(pages + PAGE_SIZE);
1277 pc->pc_qmap_addr = pages + (PAGE_SIZE * 2);
1280 SYSINIT(rpages_init, SI_SUB_CPU, SI_ORDER_ANY, pmap_init_reserved_pages, NULL);
1283 * The function can already be use in second initialization stage.
1284 * As such, the function DOES NOT call pmap_growkernel() where PT2
1285 * allocation can happen. So if used, be sure that PT2 for given
1286 * virtual address is allocated already!
1288 * Add a wired page to the kva.
1289 * Note: not SMP coherent.
1291 static __inline void
1292 pmap_kenter_prot_attr(vm_offset_t va, vm_paddr_t pa, uint32_t prot,
1298 pte1p = kern_pte1(va);
1299 if (!pte1_is_valid(pte1_load(pte1p))) { /* XXX - sections ?! */
1301 * This is a very low level function, so PT2 and particularly
1302 * PT2PG associated with given virtual address must be already
1303 * allocated. It's a pain mainly during pmap initialization
1304 * stage. However, called after pmap initialization with
1305 * virtual address not under kernel_vm_end will lead to
1308 if (!pte2_is_valid(pte2_load(kern_pt2tab_entry(va))))
1309 panic("%s: kernel PT2 not allocated!", __func__);
1312 pte2p = pt2map_entry(va);
1313 pte2_store(pte2p, PTE2_KERN(pa, prot, attr));
1317 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
1320 pmap_kenter_prot_attr(va, pa, PTE2_AP_KRW, PTE2_ATTR_DEFAULT);
1324 * Remove a page from the kernel pagetables.
1325 * Note: not SMP coherent.
1328 pmap_kremove(vm_offset_t va)
1333 pte1p = kern_pte1(va);
1334 if (pte1_is_section(pte1_load(pte1p))) {
1337 pte2p = pt2map_entry(va);
1343 * Share new kernel PT2PG with all pmaps.
1344 * The caller is responsible for maintaining TLB consistency.
1347 pmap_kenter_pt2tab(vm_offset_t va, pt2_entry_t npte2)
1352 mtx_lock_spin(&allpmaps_lock);
1353 LIST_FOREACH(pmap, &allpmaps, pm_list) {
1354 pte2p = pmap_pt2tab_entry(pmap, va);
1355 pt2tab_store(pte2p, npte2);
1357 mtx_unlock_spin(&allpmaps_lock);
1361 * Share new kernel PTE1 with all pmaps.
1362 * The caller is responsible for maintaining TLB consistency.
1365 pmap_kenter_pte1(vm_offset_t va, pt1_entry_t npte1)
1370 mtx_lock_spin(&allpmaps_lock);
1371 LIST_FOREACH(pmap, &allpmaps, pm_list) {
1372 pte1p = pmap_pte1(pmap, va);
1373 pte1_store(pte1p, npte1);
1375 mtx_unlock_spin(&allpmaps_lock);
1379 * Used to map a range of physical addresses into kernel
1380 * virtual address space.
1382 * The value passed in '*virt' is a suggested virtual address for
1383 * the mapping. Architectures which can support a direct-mapped
1384 * physical to virtual region can return the appropriate address
1385 * within that region, leaving '*virt' unchanged. Other
1386 * architectures should map the pages starting at '*virt' and
1387 * update '*virt' with the first usable address after the mapped
1390 * NOTE: Read the comments above pmap_kenter_prot_attr() as
1391 * the function is used herein!
1394 pmap_map(vm_offset_t *virt, vm_paddr_t start, vm_paddr_t end, int prot)
1396 vm_offset_t va, sva;
1397 vm_paddr_t pte1_offset;
1399 uint32_t l1prot, l2prot;
1400 uint32_t l1attr, l2attr;
1402 PDEBUG(1, printf("%s: virt = %#x, start = %#x, end = %#x (size = %#x),"
1403 " prot = %d\n", __func__, *virt, start, end, end - start, prot));
1405 l2prot = (prot & VM_PROT_WRITE) ? PTE2_AP_KRW : PTE2_AP_KR;
1406 l2prot |= (prot & VM_PROT_EXECUTE) ? PTE2_X : PTE2_NX;
1407 l1prot = ATTR_TO_L1(l2prot);
1409 l2attr = PTE2_ATTR_DEFAULT;
1410 l1attr = ATTR_TO_L1(l2attr);
1414 * Does the physical address range's size and alignment permit at
1415 * least one section mapping to be created?
1417 pte1_offset = start & PTE1_OFFSET;
1418 if ((end - start) - ((PTE1_SIZE - pte1_offset) & PTE1_OFFSET) >=
1421 * Increase the starting virtual address so that its alignment
1422 * does not preclude the use of section mappings.
1424 if ((va & PTE1_OFFSET) < pte1_offset)
1425 va = pte1_trunc(va) + pte1_offset;
1426 else if ((va & PTE1_OFFSET) > pte1_offset)
1427 va = pte1_roundup(va) + pte1_offset;
1430 while (start < end) {
1431 if ((start & PTE1_OFFSET) == 0 && end - start >= PTE1_SIZE) {
1432 KASSERT((va & PTE1_OFFSET) == 0,
1433 ("%s: misaligned va %#x", __func__, va));
1434 npte1 = PTE1_KERN(start, l1prot, l1attr);
1435 pmap_kenter_pte1(va, npte1);
1439 pmap_kenter_prot_attr(va, start, l2prot, l2attr);
1444 tlb_flush_range(sva, va - sva);
1450 * Make a temporary mapping for a physical address.
1451 * This is only intended to be used for panic dumps.
1454 pmap_kenter_temporary(vm_paddr_t pa, int i)
1458 /* QQQ: 'i' should be less or equal to MAXDUMPPGS. */
1460 va = (vm_offset_t)crashdumpmap + (i * PAGE_SIZE);
1461 pmap_kenter(va, pa);
1462 tlb_flush_local(va);
1463 return ((void *)crashdumpmap);
1467 /*************************************
1469 * TLB & cache maintenance routines.
1471 *************************************/
1474 * We inline these within pmap.c for speed.
1477 pmap_tlb_flush(pmap_t pmap, vm_offset_t va)
1480 if (pmap == kernel_pmap || !CPU_EMPTY(&pmap->pm_active))
1485 pmap_tlb_flush_range(pmap_t pmap, vm_offset_t sva, vm_size_t size)
1488 if (pmap == kernel_pmap || !CPU_EMPTY(&pmap->pm_active))
1489 tlb_flush_range(sva, size);
1493 * Abuse the pte2 nodes for unmapped kva to thread a kva freelist through.
1495 * - Must deal with pages in order to ensure that none of the PTE2_* bits
1496 * are ever set, PTE2_V in particular.
1497 * - Assumes we can write to pte2s without pte2_store() atomic ops.
1498 * - Assumes nothing will ever test these addresses for 0 to indicate
1499 * no mapping instead of correctly checking PTE2_V.
1500 * - Assumes a vm_offset_t will fit in a pte2 (true for arm).
1501 * Because PTE2_V is never set, there can be no mappings to invalidate.
1504 pmap_pte2list_alloc(vm_offset_t *head)
1511 panic("pmap_ptelist_alloc: exhausted ptelist KVA");
1512 pte2p = pt2map_entry(va);
1515 panic("%s: va with PTE2_V set!", __func__);
1521 pmap_pte2list_free(vm_offset_t *head, vm_offset_t va)
1526 panic("%s: freeing va with PTE2_V set!", __func__);
1527 pte2p = pt2map_entry(va);
1528 *pte2p = *head; /* virtual! PTE2_V is 0 though */
1533 pmap_pte2list_init(vm_offset_t *head, void *base, int npages)
1539 for (i = npages - 1; i >= 0; i--) {
1540 va = (vm_offset_t)base + i * PAGE_SIZE;
1541 pmap_pte2list_free(head, va);
1545 /*****************************************************************************
1547 * PMAP third and final stage initialization.
1549 * After pmap_init() is called, PMAP subsystem is fully initialized.
1551 *****************************************************************************/
1553 SYSCTL_NODE(_vm, OID_AUTO, pmap, CTLFLAG_RD, 0, "VM/pmap parameters");
1555 SYSCTL_INT(_vm_pmap, OID_AUTO, pv_entry_max, CTLFLAG_RD, &pv_entry_max, 0,
1556 "Max number of PV entries");
1557 SYSCTL_INT(_vm_pmap, OID_AUTO, shpgperproc, CTLFLAG_RD, &shpgperproc, 0,
1558 "Page share factor per proc");
1560 static u_long nkpt2pg = NKPT2PG;
1561 SYSCTL_ULONG(_vm_pmap, OID_AUTO, nkpt2pg, CTLFLAG_RD,
1562 &nkpt2pg, 0, "Pre-allocated pages for kernel PT2s");
1564 static int sp_enabled = 1;
1565 SYSCTL_INT(_vm_pmap, OID_AUTO, sp_enabled, CTLFLAG_RDTUN | CTLFLAG_NOFETCH,
1566 &sp_enabled, 0, "Are large page mappings enabled?");
1569 pmap_ps_enabled(pmap_t pmap __unused)
1572 return (sp_enabled != 0);
1575 static SYSCTL_NODE(_vm_pmap, OID_AUTO, pte1, CTLFLAG_RD, 0,
1576 "1MB page mapping counters");
1578 static u_long pmap_pte1_demotions;
1579 SYSCTL_ULONG(_vm_pmap_pte1, OID_AUTO, demotions, CTLFLAG_RD,
1580 &pmap_pte1_demotions, 0, "1MB page demotions");
1582 static u_long pmap_pte1_mappings;
1583 SYSCTL_ULONG(_vm_pmap_pte1, OID_AUTO, mappings, CTLFLAG_RD,
1584 &pmap_pte1_mappings, 0, "1MB page mappings");
1586 static u_long pmap_pte1_p_failures;
1587 SYSCTL_ULONG(_vm_pmap_pte1, OID_AUTO, p_failures, CTLFLAG_RD,
1588 &pmap_pte1_p_failures, 0, "1MB page promotion failures");
1590 static u_long pmap_pte1_promotions;
1591 SYSCTL_ULONG(_vm_pmap_pte1, OID_AUTO, promotions, CTLFLAG_RD,
1592 &pmap_pte1_promotions, 0, "1MB page promotions");
1594 static u_long pmap_pte1_kern_demotions;
1595 SYSCTL_ULONG(_vm_pmap_pte1, OID_AUTO, kern_demotions, CTLFLAG_RD,
1596 &pmap_pte1_kern_demotions, 0, "1MB page kernel demotions");
1598 static u_long pmap_pte1_kern_promotions;
1599 SYSCTL_ULONG(_vm_pmap_pte1, OID_AUTO, kern_promotions, CTLFLAG_RD,
1600 &pmap_pte1_kern_promotions, 0, "1MB page kernel promotions");
1602 static __inline ttb_entry_t
1603 pmap_ttb_get(pmap_t pmap)
1606 return (vtophys(pmap->pm_pt1) | ttb_flags);
1610 * Initialize a vm_page's machine-dependent fields.
1613 * 1. Pages for L2 page tables are always not managed. So, pv_list and
1614 * pt2_wirecount can share same physical space. However, proper
1615 * initialization on a page alloc for page tables and reinitialization
1616 * on the page free must be ensured.
1619 pmap_page_init(vm_page_t m)
1622 TAILQ_INIT(&m->md.pv_list);
1623 pt2_wirecount_init(m);
1624 m->md.pat_mode = VM_MEMATTR_DEFAULT;
1628 * Virtualization for faster way how to zero whole page.
1630 static __inline void
1631 pagezero(void *page)
1634 bzero(page, PAGE_SIZE);
1638 * Zero L2 page table page.
1639 * Use same KVA as in pmap_zero_page().
1641 static __inline vm_paddr_t
1642 pmap_pt2pg_zero(vm_page_t m)
1644 pt2_entry_t *cmap2_pte2p;
1648 pa = VM_PAGE_TO_PHYS(m);
1651 * XXX: For now, we map whole page even if it's already zero,
1652 * to sync it even if the sync is only DSB.
1656 cmap2_pte2p = pc->pc_cmap2_pte2p;
1657 mtx_lock(&pc->pc_cmap_lock);
1658 if (pte2_load(cmap2_pte2p) != 0)
1659 panic("%s: CMAP2 busy", __func__);
1660 pte2_store(cmap2_pte2p, PTE2_KERN_NG(pa, PTE2_AP_KRW,
1661 vm_page_pte2_attr(m)));
1662 /* Even VM_ALLOC_ZERO request is only advisory. */
1663 if ((m->flags & PG_ZERO) == 0)
1664 pagezero(pc->pc_cmap2_addr);
1665 pte2_sync_range((pt2_entry_t *)pc->pc_cmap2_addr, PAGE_SIZE);
1666 pte2_clear(cmap2_pte2p);
1667 tlb_flush((vm_offset_t)pc->pc_cmap2_addr);
1670 * Unpin the thread before releasing the lock. Otherwise the thread
1671 * could be rescheduled while still bound to the current CPU, only
1672 * to unpin itself immediately upon resuming execution.
1675 mtx_unlock(&pc->pc_cmap_lock);
1681 * Init just allocated page as L2 page table(s) holder
1682 * and return its physical address.
1684 static __inline vm_paddr_t
1685 pmap_pt2pg_init(pmap_t pmap, vm_offset_t va, vm_page_t m)
1690 /* Check page attributes. */
1691 if (m->md.pat_mode != pt_memattr)
1692 pmap_page_set_memattr(m, pt_memattr);
1694 /* Zero page and init wire counts. */
1695 pa = pmap_pt2pg_zero(m);
1696 pt2_wirecount_init(m);
1699 * Map page to PT2MAP address space for given pmap.
1700 * Note that PT2MAP space is shared with all pmaps.
1702 if (pmap == kernel_pmap)
1703 pmap_kenter_pt2tab(va, PTE2_KPT(pa));
1705 pte2p = pmap_pt2tab_entry(pmap, va);
1706 pt2tab_store(pte2p, PTE2_KPT_NG(pa));
1713 * Initialize the pmap module.
1714 * Called by vm_init, to initialize any structures that the pmap
1715 * system needs to map virtual memory.
1721 pt2_entry_t *pte2p, pte2;
1722 u_int i, pte1_idx, pv_npg;
1724 PDEBUG(1, printf("%s: phys_start = %#x\n", __func__, PHYSADDR));
1727 * Initialize the vm page array entries for kernel pmap's
1728 * L2 page table pages allocated in advance.
1730 pte1_idx = pte1_index(KERNBASE - PT2MAP_SIZE);
1731 pte2p = kern_pt2tab_entry(KERNBASE - PT2MAP_SIZE);
1732 for (i = 0; i < nkpt2pg + NPG_IN_PT2TAB; i++, pte2p++) {
1736 pte2 = pte2_load(pte2p);
1737 KASSERT(pte2_is_valid(pte2), ("%s: no valid entry", __func__));
1740 m = PHYS_TO_VM_PAGE(pa);
1741 KASSERT(m >= vm_page_array &&
1742 m < &vm_page_array[vm_page_array_size],
1743 ("%s: L2 page table page is out of range", __func__));
1745 m->pindex = pte1_idx;
1747 pte1_idx += NPT2_IN_PG;
1751 * Initialize the address space (zone) for the pv entries. Set a
1752 * high water mark so that the system can recover from excessive
1753 * numbers of pv entries.
1755 TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
1756 pv_entry_max = shpgperproc * maxproc + vm_cnt.v_page_count;
1757 TUNABLE_INT_FETCH("vm.pmap.pv_entries", &pv_entry_max);
1758 pv_entry_max = roundup(pv_entry_max, _NPCPV);
1759 pv_entry_high_water = 9 * (pv_entry_max / 10);
1762 * Are large page mappings enabled?
1764 TUNABLE_INT_FETCH("vm.pmap.sp_enabled", &sp_enabled);
1766 KASSERT(MAXPAGESIZES > 1 && pagesizes[1] == 0,
1767 ("%s: can't assign to pagesizes[1]", __func__));
1768 pagesizes[1] = PTE1_SIZE;
1772 * Calculate the size of the pv head table for sections.
1773 * Handle the possibility that "vm_phys_segs[...].end" is zero.
1774 * Note that the table is only for sections which could be promoted.
1776 first_managed_pa = pte1_trunc(vm_phys_segs[0].start);
1777 pv_npg = (pte1_trunc(vm_phys_segs[vm_phys_nsegs - 1].end - PAGE_SIZE)
1778 - first_managed_pa) / PTE1_SIZE + 1;
1781 * Allocate memory for the pv head table for sections.
1783 s = (vm_size_t)(pv_npg * sizeof(struct md_page));
1785 pv_table = (struct md_page *)kmem_malloc(kernel_arena, s,
1787 for (i = 0; i < pv_npg; i++)
1788 TAILQ_INIT(&pv_table[i].pv_list);
1790 pv_maxchunks = MAX(pv_entry_max / _NPCPV, maxproc);
1791 pv_chunkbase = (struct pv_chunk *)kva_alloc(PAGE_SIZE * pv_maxchunks);
1792 if (pv_chunkbase == NULL)
1793 panic("%s: not enough kvm for pv chunks", __func__);
1794 pmap_pte2list_init(&pv_vafree, pv_chunkbase, pv_maxchunks);
1798 * Add a list of wired pages to the kva
1799 * this routine is only used for temporary
1800 * kernel mappings that do not need to have
1801 * page modification or references recorded.
1802 * Note that old mappings are simply written
1803 * over. The page *must* be wired.
1804 * Note: SMP coherent. Uses a ranged shootdown IPI.
1807 pmap_qenter(vm_offset_t sva, vm_page_t *ma, int count)
1810 pt2_entry_t *epte2p, *pte2p, pte2;
1815 pte2p = pt2map_entry(sva);
1816 epte2p = pte2p + count;
1817 while (pte2p < epte2p) {
1819 pa = VM_PAGE_TO_PHYS(m);
1820 pte2 = pte2_load(pte2p);
1821 if ((pte2_pa(pte2) != pa) ||
1822 (pte2_attr(pte2) != vm_page_pte2_attr(m))) {
1824 pte2_store(pte2p, PTE2_KERN(pa, PTE2_AP_KRW,
1825 vm_page_pte2_attr(m)));
1829 if (__predict_false(anychanged))
1830 tlb_flush_range(sva, count * PAGE_SIZE);
1834 * This routine tears out page mappings from the
1835 * kernel -- it is meant only for temporary mappings.
1836 * Note: SMP coherent. Uses a ranged shootdown IPI.
1839 pmap_qremove(vm_offset_t sva, int count)
1844 while (count-- > 0) {
1848 tlb_flush_range(sva, va - sva);
1852 * Are we current address space or kernel?
1855 pmap_is_current(pmap_t pmap)
1858 return (pmap == kernel_pmap ||
1859 (pmap == vmspace_pmap(curthread->td_proc->p_vmspace)));
1863 * If the given pmap is not the current or kernel pmap, the returned
1864 * pte2 must be released by passing it to pmap_pte2_release().
1866 static pt2_entry_t *
1867 pmap_pte2(pmap_t pmap, vm_offset_t va)
1870 vm_paddr_t pt2pg_pa;
1872 pte1 = pte1_load(pmap_pte1(pmap, va));
1873 if (pte1_is_section(pte1))
1874 panic("%s: attempt to map PTE1", __func__);
1875 if (pte1_is_link(pte1)) {
1876 /* Are we current address space or kernel? */
1877 if (pmap_is_current(pmap))
1878 return (pt2map_entry(va));
1879 /* Note that L2 page table size is not equal to PAGE_SIZE. */
1880 pt2pg_pa = trunc_page(pte1_link_pa(pte1));
1881 mtx_lock(&PMAP2mutex);
1882 if (pte2_pa(pte2_load(PMAP2)) != pt2pg_pa) {
1883 pte2_store(PMAP2, PTE2_KPT(pt2pg_pa));
1884 tlb_flush((vm_offset_t)PADDR2);
1886 return (PADDR2 + (arm32_btop(va) & (NPTE2_IN_PG - 1)));
1892 * Releases a pte2 that was obtained from pmap_pte2().
1893 * Be prepared for the pte2p being NULL.
1895 static __inline void
1896 pmap_pte2_release(pt2_entry_t *pte2p)
1899 if ((pt2_entry_t *)(trunc_page((vm_offset_t)pte2p)) == PADDR2) {
1900 mtx_unlock(&PMAP2mutex);
1905 * Super fast pmap_pte2 routine best used when scanning
1906 * the pv lists. This eliminates many coarse-grained
1907 * invltlb calls. Note that many of the pv list
1908 * scans are across different pmaps. It is very wasteful
1909 * to do an entire tlb flush for checking a single mapping.
1911 * If the given pmap is not the current pmap, pvh_global_lock
1912 * must be held and curthread pinned to a CPU.
1914 static pt2_entry_t *
1915 pmap_pte2_quick(pmap_t pmap, vm_offset_t va)
1918 vm_paddr_t pt2pg_pa;
1920 pte1 = pte1_load(pmap_pte1(pmap, va));
1921 if (pte1_is_section(pte1))
1922 panic("%s: attempt to map PTE1", __func__);
1923 if (pte1_is_link(pte1)) {
1924 /* Are we current address space or kernel? */
1925 if (pmap_is_current(pmap))
1926 return (pt2map_entry(va));
1927 rw_assert(&pvh_global_lock, RA_WLOCKED);
1928 KASSERT(curthread->td_pinned > 0,
1929 ("%s: curthread not pinned", __func__));
1930 /* Note that L2 page table size is not equal to PAGE_SIZE. */
1931 pt2pg_pa = trunc_page(pte1_link_pa(pte1));
1932 if (pte2_pa(pte2_load(PMAP1)) != pt2pg_pa) {
1933 pte2_store(PMAP1, PTE2_KPT(pt2pg_pa));
1935 PMAP1cpu = PCPU_GET(cpuid);
1937 tlb_flush_local((vm_offset_t)PADDR1);
1941 if (PMAP1cpu != PCPU_GET(cpuid)) {
1942 PMAP1cpu = PCPU_GET(cpuid);
1943 tlb_flush_local((vm_offset_t)PADDR1);
1948 return (PADDR1 + (arm32_btop(va) & (NPTE2_IN_PG - 1)));
1954 * Routine: pmap_extract
1956 * Extract the physical page address associated
1957 * with the given map/virtual_address pair.
1960 pmap_extract(pmap_t pmap, vm_offset_t va)
1967 pte1 = pte1_load(pmap_pte1(pmap, va));
1968 if (pte1_is_section(pte1))
1969 pa = pte1_pa(pte1) | (va & PTE1_OFFSET);
1970 else if (pte1_is_link(pte1)) {
1971 pte2p = pmap_pte2(pmap, va);
1972 pa = pte2_pa(pte2_load(pte2p)) | (va & PTE2_OFFSET);
1973 pmap_pte2_release(pte2p);
1981 * Routine: pmap_extract_and_hold
1983 * Atomically extract and hold the physical page
1984 * with the given pmap and virtual address pair
1985 * if that mapping permits the given protection.
1988 pmap_extract_and_hold(pmap_t pmap, vm_offset_t va, vm_prot_t prot)
1990 vm_paddr_t pa, lockpa;
1992 pt2_entry_t pte2, *pte2p;
1999 pte1 = pte1_load(pmap_pte1(pmap, va));
2000 if (pte1_is_section(pte1)) {
2001 if (!(pte1 & PTE1_RO) || !(prot & VM_PROT_WRITE)) {
2002 pa = pte1_pa(pte1) | (va & PTE1_OFFSET);
2003 if (vm_page_pa_tryrelock(pmap, pa, &lockpa))
2005 m = PHYS_TO_VM_PAGE(pa);
2008 } else if (pte1_is_link(pte1)) {
2009 pte2p = pmap_pte2(pmap, va);
2010 pte2 = pte2_load(pte2p);
2011 pmap_pte2_release(pte2p);
2012 if (pte2_is_valid(pte2) &&
2013 (!(pte2 & PTE2_RO) || !(prot & VM_PROT_WRITE))) {
2015 if (vm_page_pa_tryrelock(pmap, pa, &lockpa))
2017 m = PHYS_TO_VM_PAGE(pa);
2021 PA_UNLOCK_COND(lockpa);
2027 * Grow the number of kernel L2 page table entries, if needed.
2030 pmap_growkernel(vm_offset_t addr)
2033 vm_paddr_t pt2pg_pa, pt2_pa;
2037 PDEBUG(1, printf("%s: addr = %#x\n", __func__, addr));
2039 * All the time kernel_vm_end is first KVA for which underlying
2040 * L2 page table is either not allocated or linked from L1 page table
2041 * (not considering sections). Except for two possible cases:
2043 * (1) in the very beginning as long as pmap_growkernel() was
2044 * not called, it could be first unused KVA (which is not
2045 * rounded up to PTE1_SIZE),
2047 * (2) when all KVA space is mapped and kernel_map->max_offset
2048 * address is not rounded up to PTE1_SIZE. (For example,
2049 * it could be 0xFFFFFFFF.)
2051 kernel_vm_end = pte1_roundup(kernel_vm_end);
2052 mtx_assert(&kernel_map->system_mtx, MA_OWNED);
2053 addr = roundup2(addr, PTE1_SIZE);
2054 if (addr - 1 >= kernel_map->max_offset)
2055 addr = kernel_map->max_offset;
2056 while (kernel_vm_end < addr) {
2057 pte1 = pte1_load(kern_pte1(kernel_vm_end));
2058 if (pte1_is_valid(pte1)) {
2059 kernel_vm_end += PTE1_SIZE;
2060 if (kernel_vm_end - 1 >= kernel_map->max_offset) {
2061 kernel_vm_end = kernel_map->max_offset;
2068 * kernel_vm_end_new is used in pmap_pinit() when kernel
2069 * mappings are entered to new pmap all at once to avoid race
2070 * between pmap_kenter_pte1() and kernel_vm_end increase.
2071 * The same aplies to pmap_kenter_pt2tab().
2073 kernel_vm_end_new = kernel_vm_end + PTE1_SIZE;
2075 pte2 = pt2tab_load(kern_pt2tab_entry(kernel_vm_end));
2076 if (!pte2_is_valid(pte2)) {
2078 * Install new PT2s page into kernel PT2TAB.
2080 m = vm_page_alloc(NULL,
2081 pte1_index(kernel_vm_end) & ~PT2PG_MASK,
2082 VM_ALLOC_INTERRUPT | VM_ALLOC_NOOBJ |
2083 VM_ALLOC_WIRED | VM_ALLOC_ZERO);
2085 panic("%s: no memory to grow kernel", __func__);
2087 * QQQ: To link all new L2 page tables from L1 page
2088 * table now and so pmap_kenter_pte1() them
2089 * at once together with pmap_kenter_pt2tab()
2090 * could be nice speed up. However,
2091 * pmap_growkernel() does not happen so often...
2092 * QQQ: The other TTBR is another option.
2094 pt2pg_pa = pmap_pt2pg_init(kernel_pmap, kernel_vm_end,
2097 pt2pg_pa = pte2_pa(pte2);
2099 pt2_pa = page_pt2pa(pt2pg_pa, pte1_index(kernel_vm_end));
2100 pmap_kenter_pte1(kernel_vm_end, PTE1_LINK(pt2_pa));
2102 kernel_vm_end = kernel_vm_end_new;
2103 if (kernel_vm_end - 1 >= kernel_map->max_offset) {
2104 kernel_vm_end = kernel_map->max_offset;
2111 kvm_size(SYSCTL_HANDLER_ARGS)
2113 unsigned long ksize = vm_max_kernel_address - KERNBASE;
2115 return (sysctl_handle_long(oidp, &ksize, 0, req));
2117 SYSCTL_PROC(_vm, OID_AUTO, kvm_size, CTLTYPE_LONG|CTLFLAG_RD,
2118 0, 0, kvm_size, "IU", "Size of KVM");
2121 kvm_free(SYSCTL_HANDLER_ARGS)
2123 unsigned long kfree = vm_max_kernel_address - kernel_vm_end;
2125 return (sysctl_handle_long(oidp, &kfree, 0, req));
2127 SYSCTL_PROC(_vm, OID_AUTO, kvm_free, CTLTYPE_LONG|CTLFLAG_RD,
2128 0, 0, kvm_free, "IU", "Amount of KVM free");
2130 /***********************************************
2132 * Pmap allocation/deallocation routines.
2134 ***********************************************/
2137 * Initialize the pmap for the swapper process.
2140 pmap_pinit0(pmap_t pmap)
2142 PDEBUG(1, printf("%s: pmap = %p\n", __func__, pmap));
2144 PMAP_LOCK_INIT(pmap);
2147 * Kernel page table directory and pmap stuff around is already
2148 * initialized, we are using it right now and here. So, finish
2149 * only PMAP structures initialization for process0 ...
2151 * Since the L1 page table and PT2TAB is shared with the kernel pmap,
2152 * which is already included in the list "allpmaps", this pmap does
2153 * not need to be inserted into that list.
2155 pmap->pm_pt1 = kern_pt1;
2156 pmap->pm_pt2tab = kern_pt2tab;
2157 CPU_ZERO(&pmap->pm_active);
2158 PCPU_SET(curpmap, pmap);
2159 TAILQ_INIT(&pmap->pm_pvchunk);
2160 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
2161 CPU_SET(0, &pmap->pm_active);
2164 static __inline void
2165 pte1_copy_nosync(pt1_entry_t *spte1p, pt1_entry_t *dpte1p, vm_offset_t sva,
2170 idx = pte1_index(sva);
2171 count = (pte1_index(eva) - idx + 1) * sizeof(pt1_entry_t);
2172 bcopy(spte1p + idx, dpte1p + idx, count);
2175 static __inline void
2176 pt2tab_copy_nosync(pt2_entry_t *spte2p, pt2_entry_t *dpte2p, vm_offset_t sva,
2181 idx = pt2tab_index(sva);
2182 count = (pt2tab_index(eva) - idx + 1) * sizeof(pt2_entry_t);
2183 bcopy(spte2p + idx, dpte2p + idx, count);
2187 * Initialize a preallocated and zeroed pmap structure,
2188 * such as one in a vmspace structure.
2191 pmap_pinit(pmap_t pmap)
2195 vm_paddr_t pa, pt2tab_pa;
2198 PDEBUG(6, printf("%s: pmap = %p, pm_pt1 = %p\n", __func__, pmap,
2202 * No need to allocate L2 page table space yet but we do need
2203 * a valid L1 page table and PT2TAB table.
2205 * Install shared kernel mappings to these tables. It's a little
2206 * tricky as some parts of KVA are reserved for vectors, devices,
2207 * and whatever else. These parts are supposed to be above
2208 * vm_max_kernel_address. Thus two regions should be installed:
2210 * (1) <KERNBASE, kernel_vm_end),
2211 * (2) <vm_max_kernel_address, 0xFFFFFFFF>.
2213 * QQQ: The second region should be stable enough to be installed
2214 * only once in time when the tables are allocated.
2215 * QQQ: Maybe copy of both regions at once could be faster ...
2216 * QQQ: Maybe the other TTBR is an option.
2218 * Finally, install own PT2TAB table to these tables.
2221 if (pmap->pm_pt1 == NULL) {
2222 pmap->pm_pt1 = (pt1_entry_t *)kmem_alloc_contig(kernel_arena,
2223 NB_IN_PT1, M_NOWAIT | M_ZERO, 0, -1UL, NB_IN_PT1, 0,
2225 if (pmap->pm_pt1 == NULL)
2228 if (pmap->pm_pt2tab == NULL) {
2230 * QQQ: (1) PT2TAB must be contiguous. If PT2TAB is one page
2231 * only, what should be the only size for 32 bit systems,
2232 * then we could allocate it with vm_page_alloc() and all
2233 * the stuff needed as other L2 page table pages.
2234 * (2) Note that a process PT2TAB is special L2 page table
2235 * page. Its mapping in kernel_arena is permanent and can
2236 * be used no matter which process is current. Its mapping
2237 * in PT2MAP can be used only for current process.
2239 pmap->pm_pt2tab = (pt2_entry_t *)kmem_alloc_attr(kernel_arena,
2240 NB_IN_PT2TAB, M_NOWAIT | M_ZERO, 0, -1UL, pt_memattr);
2241 if (pmap->pm_pt2tab == NULL) {
2243 * QQQ: As struct pmap is allocated from UMA with
2244 * UMA_ZONE_NOFREE flag, it's important to leave
2245 * no allocation in pmap if initialization failed.
2247 kmem_free(kernel_arena, (vm_offset_t)pmap->pm_pt1,
2249 pmap->pm_pt1 = NULL;
2253 * QQQ: Each L2 page table page vm_page_t has pindex set to
2254 * pte1 index of virtual address mapped by this page.
2255 * It's not valid for non kernel PT2TABs themselves.
2256 * The pindex of these pages can not be altered because
2257 * of the way how they are allocated now. However, it
2258 * should not be a problem.
2262 mtx_lock_spin(&allpmaps_lock);
2264 * To avoid race with pmap_kenter_pte1() and pmap_kenter_pt2tab(),
2265 * kernel_vm_end_new is used here instead of kernel_vm_end.
2267 pte1_copy_nosync(kern_pt1, pmap->pm_pt1, KERNBASE,
2268 kernel_vm_end_new - 1);
2269 pte1_copy_nosync(kern_pt1, pmap->pm_pt1, vm_max_kernel_address,
2271 pt2tab_copy_nosync(kern_pt2tab, pmap->pm_pt2tab, KERNBASE,
2272 kernel_vm_end_new - 1);
2273 pt2tab_copy_nosync(kern_pt2tab, pmap->pm_pt2tab, vm_max_kernel_address,
2275 LIST_INSERT_HEAD(&allpmaps, pmap, pm_list);
2276 mtx_unlock_spin(&allpmaps_lock);
2279 * Store PT2MAP PT2 pages (a.k.a. PT2TAB) in PT2TAB itself.
2280 * I.e. self reference mapping. The PT2TAB is private, however mapped
2281 * into shared PT2MAP space, so the mapping should be not global.
2283 pt2tab_pa = vtophys(pmap->pm_pt2tab);
2284 pte2p = pmap_pt2tab_entry(pmap, (vm_offset_t)PT2MAP);
2285 for (pa = pt2tab_pa, i = 0; i < NPG_IN_PT2TAB; i++, pa += PTE2_SIZE) {
2286 pt2tab_store(pte2p++, PTE2_KPT_NG(pa));
2289 /* Insert PT2MAP PT2s into pmap PT1. */
2290 pte1p = pmap_pte1(pmap, (vm_offset_t)PT2MAP);
2291 for (pa = pt2tab_pa, i = 0; i < NPT2_IN_PT2TAB; i++, pa += NB_IN_PT2) {
2292 pte1_store(pte1p++, PTE1_LINK(pa));
2296 * Now synchronize new mapping which was made above.
2298 pte1_sync_range(pmap->pm_pt1, NB_IN_PT1);
2299 pte2_sync_range(pmap->pm_pt2tab, NB_IN_PT2TAB);
2301 CPU_ZERO(&pmap->pm_active);
2302 TAILQ_INIT(&pmap->pm_pvchunk);
2303 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
2310 pt2tab_user_is_empty(pt2_entry_t *tab)
2314 end = pt2tab_index(VM_MAXUSER_ADDRESS);
2315 for (i = 0; i < end; i++)
2316 if (tab[i] != 0) return (FALSE);
2321 * Release any resources held by the given physical map.
2322 * Called when a pmap initialized by pmap_pinit is being released.
2323 * Should only be called if the map contains no valid mappings.
2326 pmap_release(pmap_t pmap)
2329 vm_offset_t start, end;
2331 KASSERT(pmap->pm_stats.resident_count == 0,
2332 ("%s: pmap resident count %ld != 0", __func__,
2333 pmap->pm_stats.resident_count));
2334 KASSERT(pt2tab_user_is_empty(pmap->pm_pt2tab),
2335 ("%s: has allocated user PT2(s)", __func__));
2336 KASSERT(CPU_EMPTY(&pmap->pm_active),
2337 ("%s: pmap %p is active on some CPU(s)", __func__, pmap));
2339 mtx_lock_spin(&allpmaps_lock);
2340 LIST_REMOVE(pmap, pm_list);
2341 mtx_unlock_spin(&allpmaps_lock);
2344 start = pte1_index(KERNBASE) * sizeof(pt1_entry_t);
2345 end = (pte1_index(0xFFFFFFFF) + 1) * sizeof(pt1_entry_t);
2346 bzero((char *)pmap->pm_pt1 + start, end - start);
2348 start = pt2tab_index(KERNBASE) * sizeof(pt2_entry_t);
2349 end = (pt2tab_index(0xFFFFFFFF) + 1) * sizeof(pt2_entry_t);
2350 bzero((char *)pmap->pm_pt2tab + start, end - start);
2353 * We are leaving PT1 and PT2TAB allocated on released pmap,
2354 * so hopefully UMA vmspace_zone will always be inited with
2355 * UMA_ZONE_NOFREE flag.
2359 /*********************************************************
2361 * L2 table pages and their pages management routines.
2363 *********************************************************/
2366 * Virtual interface for L2 page table wire counting.
2368 * Each L2 page table in a page has own counter which counts a number of
2369 * valid mappings in a table. Global page counter counts mappings in all
2370 * tables in a page plus a single itself mapping in PT2TAB.
2372 * During a promotion we leave the associated L2 page table counter
2373 * untouched, so the table (strictly speaking a page which holds it)
2374 * is never freed if promoted.
2376 * If a page m->wire_count == 1 then no valid mappings exist in any L2 page
2377 * table in the page and the page itself is only mapped in PT2TAB.
2380 static __inline void
2381 pt2_wirecount_init(vm_page_t m)
2386 * Note: A page m is allocated with VM_ALLOC_WIRED flag and
2387 * m->wire_count should be already set correctly.
2388 * So, there is no need to set it again herein.
2390 for (i = 0; i < NPT2_IN_PG; i++)
2391 m->md.pt2_wirecount[i] = 0;
2394 static __inline void
2395 pt2_wirecount_inc(vm_page_t m, uint32_t pte1_idx)
2399 * Note: A just modificated pte2 (i.e. already allocated)
2400 * is acquiring one extra reference which must be
2401 * explicitly cleared. It influences the KASSERTs herein.
2402 * All L2 page tables in a page always belong to the same
2403 * pmap, so we allow only one extra reference for the page.
2405 KASSERT(m->md.pt2_wirecount[pte1_idx & PT2PG_MASK] < (NPTE2_IN_PT2 + 1),
2406 ("%s: PT2 is overflowing ...", __func__));
2407 KASSERT(m->wire_count <= (NPTE2_IN_PG + 1),
2408 ("%s: PT2PG is overflowing ...", __func__));
2411 m->md.pt2_wirecount[pte1_idx & PT2PG_MASK]++;
2414 static __inline void
2415 pt2_wirecount_dec(vm_page_t m, uint32_t pte1_idx)
2418 KASSERT(m->md.pt2_wirecount[pte1_idx & PT2PG_MASK] != 0,
2419 ("%s: PT2 is underflowing ...", __func__));
2420 KASSERT(m->wire_count > 1,
2421 ("%s: PT2PG is underflowing ...", __func__));
2424 m->md.pt2_wirecount[pte1_idx & PT2PG_MASK]--;
2427 static __inline void
2428 pt2_wirecount_set(vm_page_t m, uint32_t pte1_idx, uint16_t count)
2431 KASSERT(count <= NPTE2_IN_PT2,
2432 ("%s: invalid count %u", __func__, count));
2433 KASSERT(m->wire_count > m->md.pt2_wirecount[pte1_idx & PT2PG_MASK],
2434 ("%s: PT2PG corrupting (%u, %u) ...", __func__, m->wire_count,
2435 m->md.pt2_wirecount[pte1_idx & PT2PG_MASK]));
2437 m->wire_count -= m->md.pt2_wirecount[pte1_idx & PT2PG_MASK];
2438 m->wire_count += count;
2439 m->md.pt2_wirecount[pte1_idx & PT2PG_MASK] = count;
2441 KASSERT(m->wire_count <= (NPTE2_IN_PG + 1),
2442 ("%s: PT2PG is overflowed (%u) ...", __func__, m->wire_count));
2445 static __inline uint32_t
2446 pt2_wirecount_get(vm_page_t m, uint32_t pte1_idx)
2449 return (m->md.pt2_wirecount[pte1_idx & PT2PG_MASK]);
2452 static __inline boolean_t
2453 pt2_is_empty(vm_page_t m, vm_offset_t va)
2456 return (m->md.pt2_wirecount[pte1_index(va) & PT2PG_MASK] == 0);
2459 static __inline boolean_t
2460 pt2_is_full(vm_page_t m, vm_offset_t va)
2463 return (m->md.pt2_wirecount[pte1_index(va) & PT2PG_MASK] ==
2467 static __inline boolean_t
2468 pt2pg_is_empty(vm_page_t m)
2471 return (m->wire_count == 1);
2475 * This routine is called if the L2 page table
2476 * is not mapped correctly.
2479 _pmap_allocpte2(pmap_t pmap, vm_offset_t va, u_int flags)
2485 vm_paddr_t pt2pg_pa, pt2_pa;
2487 pte1_idx = pte1_index(va);
2488 pte1p = pmap->pm_pt1 + pte1_idx;
2490 KASSERT(pte1_load(pte1p) == 0,
2491 ("%s: pm_pt1[%#x] is not zero: %#x", __func__, pte1_idx,
2494 pte2 = pt2tab_load(pmap_pt2tab_entry(pmap, va));
2495 if (!pte2_is_valid(pte2)) {
2497 * Install new PT2s page into pmap PT2TAB.
2499 m = vm_page_alloc(NULL, pte1_idx & ~PT2PG_MASK,
2500 VM_ALLOC_NOOBJ | VM_ALLOC_WIRED | VM_ALLOC_ZERO);
2502 if ((flags & PMAP_ENTER_NOSLEEP) == 0) {
2504 rw_wunlock(&pvh_global_lock);
2506 rw_wlock(&pvh_global_lock);
2511 * Indicate the need to retry. While waiting,
2512 * the L2 page table page may have been allocated.
2516 pmap->pm_stats.resident_count++;
2517 pt2pg_pa = pmap_pt2pg_init(pmap, va, m);
2519 pt2pg_pa = pte2_pa(pte2);
2520 m = PHYS_TO_VM_PAGE(pt2pg_pa);
2523 pt2_wirecount_inc(m, pte1_idx);
2524 pt2_pa = page_pt2pa(pt2pg_pa, pte1_idx);
2525 pte1_store(pte1p, PTE1_LINK(pt2_pa));
2531 pmap_allocpte2(pmap_t pmap, vm_offset_t va, u_int flags)
2534 pt1_entry_t *pte1p, pte1;
2537 pte1_idx = pte1_index(va);
2539 pte1p = pmap->pm_pt1 + pte1_idx;
2540 pte1 = pte1_load(pte1p);
2543 * This supports switching from a 1MB page to a
2546 if (pte1_is_section(pte1)) {
2547 (void)pmap_demote_pte1(pmap, pte1p, va);
2549 * Reload pte1 after demotion.
2551 * Note: Demotion can even fail as either PT2 is not find for
2552 * the virtual address or PT2PG can not be allocated.
2554 pte1 = pte1_load(pte1p);
2558 * If the L2 page table page is mapped, we just increment the
2559 * hold count, and activate it.
2561 if (pte1_is_link(pte1)) {
2562 m = PHYS_TO_VM_PAGE(pte1_link_pa(pte1));
2563 pt2_wirecount_inc(m, pte1_idx);
2566 * Here if the PT2 isn't mapped, or if it has
2569 m = _pmap_allocpte2(pmap, va, flags);
2570 if (m == NULL && (flags & PMAP_ENTER_NOSLEEP) == 0)
2578 * Schedule the specified unused L2 page table page to be freed. Specifically,
2579 * add the page to the specified list of pages that will be released to the
2580 * physical memory manager after the TLB has been updated.
2582 static __inline void
2583 pmap_add_delayed_free_list(vm_page_t m, struct spglist *free)
2587 * Put page on a list so that it is released after
2588 * *ALL* TLB shootdown is done
2591 pmap_zero_page_check(m);
2593 m->flags |= PG_ZERO;
2594 SLIST_INSERT_HEAD(free, m, plinks.s.ss);
2598 * Unwire L2 page tables page.
2601 pmap_unwire_pt2pg(pmap_t pmap, vm_offset_t va, vm_page_t m)
2603 pt1_entry_t *pte1p, opte1 __unused;
2607 KASSERT(pt2pg_is_empty(m),
2608 ("%s: pmap %p PT2PG %p wired", __func__, pmap, m));
2611 * Unmap all L2 page tables in the page from L1 page table.
2613 * QQQ: Individual L2 page tables (except the last one) can be unmapped
2614 * earlier. However, we are doing that this way.
2616 KASSERT(m->pindex == (pte1_index(va) & ~PT2PG_MASK),
2617 ("%s: pmap %p va %#x PT2PG %p bad index", __func__, pmap, va, m));
2618 pte1p = pmap->pm_pt1 + m->pindex;
2619 for (i = 0; i < NPT2_IN_PG; i++, pte1p++) {
2620 KASSERT(m->md.pt2_wirecount[i] == 0,
2621 ("%s: pmap %p PT2 %u (PG %p) wired", __func__, pmap, i, m));
2622 opte1 = pte1_load(pte1p);
2623 if (pte1_is_link(opte1)) {
2626 * Flush intermediate TLB cache.
2628 pmap_tlb_flush(pmap, (m->pindex + i) << PTE1_SHIFT);
2632 KASSERT((opte1 == 0) || pte1_is_section(opte1),
2633 ("%s: pmap %p va %#x bad pte1 %x at %u", __func__,
2634 pmap, va, opte1, i));
2639 * Unmap the page from PT2TAB.
2641 pte2p = pmap_pt2tab_entry(pmap, va);
2642 (void)pt2tab_load_clear(pte2p);
2643 pmap_tlb_flush(pmap, pt2map_pt2pg(va));
2646 pmap->pm_stats.resident_count--;
2649 * This barrier is so that the ordinary store unmapping
2650 * the L2 page table page is globally performed before TLB shoot-
2658 * Decrements a L2 page table page's wire count, which is used to record the
2659 * number of valid page table entries within the page. If the wire count
2660 * drops to zero, then the page table page is unmapped. Returns TRUE if the
2661 * page table page was unmapped and FALSE otherwise.
2663 static __inline boolean_t
2664 pmap_unwire_pt2(pmap_t pmap, vm_offset_t va, vm_page_t m, struct spglist *free)
2666 pt2_wirecount_dec(m, pte1_index(va));
2667 if (pt2pg_is_empty(m)) {
2669 * QQQ: Wire count is zero, so whole page should be zero and
2670 * we can set PG_ZERO flag to it.
2671 * Note that when promotion is enabled, it takes some
2672 * more efforts. See pmap_unwire_pt2_all() below.
2674 pmap_unwire_pt2pg(pmap, va, m);
2675 pmap_add_delayed_free_list(m, free);
2682 * Drop a L2 page table page's wire count at once, which is used to record
2683 * the number of valid L2 page table entries within the page. If the wire
2684 * count drops to zero, then the L2 page table page is unmapped.
2686 static __inline void
2687 pmap_unwire_pt2_all(pmap_t pmap, vm_offset_t va, vm_page_t m,
2688 struct spglist *free)
2690 u_int pte1_idx = pte1_index(va);
2692 KASSERT(m->pindex == (pte1_idx & ~PT2PG_MASK),
2693 ("%s: PT2 page's pindex is wrong", __func__));
2694 KASSERT(m->wire_count > pt2_wirecount_get(m, pte1_idx),
2695 ("%s: bad pt2 wire count %u > %u", __func__, m->wire_count,
2696 pt2_wirecount_get(m, pte1_idx)));
2699 * It's possible that the L2 page table was never used.
2700 * It happened in case that a section was created without promotion.
2702 if (pt2_is_full(m, va)) {
2703 pt2_wirecount_set(m, pte1_idx, 0);
2706 * QQQ: We clear L2 page table now, so when L2 page table page
2707 * is going to be freed, we can set it PG_ZERO flag ...
2708 * This function is called only on section mappings, so
2709 * hopefully it's not to big overload.
2711 * XXX: If pmap is current, existing PT2MAP mapping could be
2714 pmap_zero_page_area(m, page_pt2off(pte1_idx), NB_IN_PT2);
2718 KASSERT(pt2_is_empty(m, va), ("%s: PT2 is not empty (%u)",
2719 __func__, pt2_wirecount_get(m, pte1_idx)));
2721 if (pt2pg_is_empty(m)) {
2722 pmap_unwire_pt2pg(pmap, va, m);
2723 pmap_add_delayed_free_list(m, free);
2728 * After removing a L2 page table entry, this routine is used to
2729 * conditionally free the page, and manage the hold/wire counts.
2732 pmap_unuse_pt2(pmap_t pmap, vm_offset_t va, struct spglist *free)
2737 if (va >= VM_MAXUSER_ADDRESS)
2739 pte1 = pte1_load(pmap_pte1(pmap, va));
2740 mpte = PHYS_TO_VM_PAGE(pte1_link_pa(pte1));
2741 return (pmap_unwire_pt2(pmap, va, mpte, free));
2744 /*************************************
2746 * Page management routines.
2748 *************************************/
2750 CTASSERT(sizeof(struct pv_chunk) == PAGE_SIZE);
2751 CTASSERT(_NPCM == 11);
2752 CTASSERT(_NPCPV == 336);
2754 static __inline struct pv_chunk *
2755 pv_to_chunk(pv_entry_t pv)
2758 return ((struct pv_chunk *)((uintptr_t)pv & ~(uintptr_t)PAGE_MASK));
2761 #define PV_PMAP(pv) (pv_to_chunk(pv)->pc_pmap)
2763 #define PC_FREE0_9 0xfffffffful /* Free values for index 0 through 9 */
2764 #define PC_FREE10 0x0000fffful /* Free values for index 10 */
2766 static const uint32_t pc_freemask[_NPCM] = {
2767 PC_FREE0_9, PC_FREE0_9, PC_FREE0_9,
2768 PC_FREE0_9, PC_FREE0_9, PC_FREE0_9,
2769 PC_FREE0_9, PC_FREE0_9, PC_FREE0_9,
2770 PC_FREE0_9, PC_FREE10
2773 SYSCTL_INT(_vm_pmap, OID_AUTO, pv_entry_count, CTLFLAG_RD, &pv_entry_count, 0,
2774 "Current number of pv entries");
2777 static int pc_chunk_count, pc_chunk_allocs, pc_chunk_frees, pc_chunk_tryfail;
2779 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_count, CTLFLAG_RD, &pc_chunk_count, 0,
2780 "Current number of pv entry chunks");
2781 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_allocs, CTLFLAG_RD, &pc_chunk_allocs, 0,
2782 "Current number of pv entry chunks allocated");
2783 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_frees, CTLFLAG_RD, &pc_chunk_frees, 0,
2784 "Current number of pv entry chunks frees");
2785 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_tryfail, CTLFLAG_RD, &pc_chunk_tryfail,
2786 0, "Number of times tried to get a chunk page but failed.");
2788 static long pv_entry_frees, pv_entry_allocs;
2789 static int pv_entry_spare;
2791 SYSCTL_LONG(_vm_pmap, OID_AUTO, pv_entry_frees, CTLFLAG_RD, &pv_entry_frees, 0,
2792 "Current number of pv entry frees");
2793 SYSCTL_LONG(_vm_pmap, OID_AUTO, pv_entry_allocs, CTLFLAG_RD, &pv_entry_allocs,
2794 0, "Current number of pv entry allocs");
2795 SYSCTL_INT(_vm_pmap, OID_AUTO, pv_entry_spare, CTLFLAG_RD, &pv_entry_spare, 0,
2796 "Current number of spare pv entries");
2800 * Is given page managed?
2802 static __inline bool
2803 is_managed(vm_paddr_t pa)
2807 m = PHYS_TO_VM_PAGE(pa);
2810 return ((m->oflags & VPO_UNMANAGED) == 0);
2813 static __inline bool
2814 pte1_is_managed(pt1_entry_t pte1)
2817 return (is_managed(pte1_pa(pte1)));
2820 static __inline bool
2821 pte2_is_managed(pt2_entry_t pte2)
2824 return (is_managed(pte2_pa(pte2)));
2828 * We are in a serious low memory condition. Resort to
2829 * drastic measures to free some pages so we can allocate
2830 * another pv entry chunk.
2833 pmap_pv_reclaim(pmap_t locked_pmap)
2836 struct pv_chunk *pc;
2837 struct md_page *pvh;
2840 pt2_entry_t *pte2p, tpte2;
2844 struct spglist free;
2846 int bit, field, freed;
2848 PMAP_LOCK_ASSERT(locked_pmap, MA_OWNED);
2852 TAILQ_INIT(&newtail);
2853 while ((pc = TAILQ_FIRST(&pv_chunks)) != NULL && (pv_vafree == 0 ||
2854 SLIST_EMPTY(&free))) {
2855 TAILQ_REMOVE(&pv_chunks, pc, pc_lru);
2856 if (pmap != pc->pc_pmap) {
2858 if (pmap != locked_pmap)
2862 /* Avoid deadlock and lock recursion. */
2863 if (pmap > locked_pmap)
2865 else if (pmap != locked_pmap && !PMAP_TRYLOCK(pmap)) {
2867 TAILQ_INSERT_TAIL(&newtail, pc, pc_lru);
2873 * Destroy every non-wired, 4 KB page mapping in the chunk.
2876 for (field = 0; field < _NPCM; field++) {
2877 for (inuse = ~pc->pc_map[field] & pc_freemask[field];
2878 inuse != 0; inuse &= ~(1UL << bit)) {
2879 bit = ffs(inuse) - 1;
2880 pv = &pc->pc_pventry[field * 32 + bit];
2882 pte1p = pmap_pte1(pmap, va);
2883 if (pte1_is_section(pte1_load(pte1p)))
2885 pte2p = pmap_pte2(pmap, va);
2886 tpte2 = pte2_load(pte2p);
2887 if ((tpte2 & PTE2_W) == 0)
2888 tpte2 = pte2_load_clear(pte2p);
2889 pmap_pte2_release(pte2p);
2890 if ((tpte2 & PTE2_W) != 0)
2893 ("pmap_pv_reclaim: pmap %p va %#x zero pte",
2895 pmap_tlb_flush(pmap, va);
2896 m = PHYS_TO_VM_PAGE(pte2_pa(tpte2));
2897 if (pte2_is_dirty(tpte2))
2899 if ((tpte2 & PTE2_A) != 0)
2900 vm_page_aflag_set(m, PGA_REFERENCED);
2901 TAILQ_REMOVE(&m->md.pv_list, pv, pv_next);
2902 if (TAILQ_EMPTY(&m->md.pv_list) &&
2903 (m->flags & PG_FICTITIOUS) == 0) {
2904 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
2905 if (TAILQ_EMPTY(&pvh->pv_list)) {
2906 vm_page_aflag_clear(m,
2910 pc->pc_map[field] |= 1UL << bit;
2911 pmap_unuse_pt2(pmap, va, &free);
2916 TAILQ_INSERT_TAIL(&newtail, pc, pc_lru);
2919 /* Every freed mapping is for a 4 KB page. */
2920 pmap->pm_stats.resident_count -= freed;
2921 PV_STAT(pv_entry_frees += freed);
2922 PV_STAT(pv_entry_spare += freed);
2923 pv_entry_count -= freed;
2924 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
2925 for (field = 0; field < _NPCM; field++)
2926 if (pc->pc_map[field] != pc_freemask[field]) {
2927 TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc,
2929 TAILQ_INSERT_TAIL(&newtail, pc, pc_lru);
2932 * One freed pv entry in locked_pmap is
2935 if (pmap == locked_pmap)
2939 if (field == _NPCM) {
2940 PV_STAT(pv_entry_spare -= _NPCPV);
2941 PV_STAT(pc_chunk_count--);
2942 PV_STAT(pc_chunk_frees++);
2943 /* Entire chunk is free; return it. */
2944 m_pc = PHYS_TO_VM_PAGE(pmap_kextract((vm_offset_t)pc));
2945 pmap_qremove((vm_offset_t)pc, 1);
2946 pmap_pte2list_free(&pv_vafree, (vm_offset_t)pc);
2951 TAILQ_CONCAT(&pv_chunks, &newtail, pc_lru);
2953 if (pmap != locked_pmap)
2956 if (m_pc == NULL && pv_vafree != 0 && SLIST_EMPTY(&free)) {
2957 m_pc = SLIST_FIRST(&free);
2958 SLIST_REMOVE_HEAD(&free, plinks.s.ss);
2959 /* Recycle a freed page table page. */
2960 m_pc->wire_count = 1;
2963 vm_page_free_pages_toq(&free, false);
2968 free_pv_chunk(struct pv_chunk *pc)
2972 TAILQ_REMOVE(&pv_chunks, pc, pc_lru);
2973 PV_STAT(pv_entry_spare -= _NPCPV);
2974 PV_STAT(pc_chunk_count--);
2975 PV_STAT(pc_chunk_frees++);
2976 /* entire chunk is free, return it */
2977 m = PHYS_TO_VM_PAGE(pmap_kextract((vm_offset_t)pc));
2978 pmap_qremove((vm_offset_t)pc, 1);
2979 vm_page_unwire(m, PQ_NONE);
2981 pmap_pte2list_free(&pv_vafree, (vm_offset_t)pc);
2985 * Free the pv_entry back to the free list.
2988 free_pv_entry(pmap_t pmap, pv_entry_t pv)
2990 struct pv_chunk *pc;
2991 int idx, field, bit;
2993 rw_assert(&pvh_global_lock, RA_WLOCKED);
2994 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2995 PV_STAT(pv_entry_frees++);
2996 PV_STAT(pv_entry_spare++);
2998 pc = pv_to_chunk(pv);
2999 idx = pv - &pc->pc_pventry[0];
3002 pc->pc_map[field] |= 1ul << bit;
3003 for (idx = 0; idx < _NPCM; idx++)
3004 if (pc->pc_map[idx] != pc_freemask[idx]) {
3006 * 98% of the time, pc is already at the head of the
3007 * list. If it isn't already, move it to the head.
3009 if (__predict_false(TAILQ_FIRST(&pmap->pm_pvchunk) !=
3011 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
3012 TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc,
3017 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
3022 * Get a new pv_entry, allocating a block from the system
3026 get_pv_entry(pmap_t pmap, boolean_t try)
3028 static const struct timeval printinterval = { 60, 0 };
3029 static struct timeval lastprint;
3032 struct pv_chunk *pc;
3035 rw_assert(&pvh_global_lock, RA_WLOCKED);
3036 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3037 PV_STAT(pv_entry_allocs++);
3039 if (pv_entry_count > pv_entry_high_water)
3040 if (ratecheck(&lastprint, &printinterval))
3041 printf("Approaching the limit on PV entries, consider "
3042 "increasing either the vm.pmap.shpgperproc or the "
3043 "vm.pmap.pv_entry_max tunable.\n");
3045 pc = TAILQ_FIRST(&pmap->pm_pvchunk);
3047 for (field = 0; field < _NPCM; field++) {
3048 if (pc->pc_map[field]) {
3049 bit = ffs(pc->pc_map[field]) - 1;
3053 if (field < _NPCM) {
3054 pv = &pc->pc_pventry[field * 32 + bit];
3055 pc->pc_map[field] &= ~(1ul << bit);
3056 /* If this was the last item, move it to tail */
3057 for (field = 0; field < _NPCM; field++)
3058 if (pc->pc_map[field] != 0) {
3059 PV_STAT(pv_entry_spare--);
3060 return (pv); /* not full, return */
3062 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
3063 TAILQ_INSERT_TAIL(&pmap->pm_pvchunk, pc, pc_list);
3064 PV_STAT(pv_entry_spare--);
3069 * Access to the pte2list "pv_vafree" is synchronized by the pvh
3070 * global lock. If "pv_vafree" is currently non-empty, it will
3071 * remain non-empty until pmap_pte2list_alloc() completes.
3073 if (pv_vafree == 0 || (m = vm_page_alloc(NULL, 0, VM_ALLOC_NORMAL |
3074 VM_ALLOC_NOOBJ | VM_ALLOC_WIRED)) == NULL) {
3077 PV_STAT(pc_chunk_tryfail++);
3080 m = pmap_pv_reclaim(pmap);
3084 PV_STAT(pc_chunk_count++);
3085 PV_STAT(pc_chunk_allocs++);
3086 pc = (struct pv_chunk *)pmap_pte2list_alloc(&pv_vafree);
3087 pmap_qenter((vm_offset_t)pc, &m, 1);
3089 pc->pc_map[0] = pc_freemask[0] & ~1ul; /* preallocated bit 0 */
3090 for (field = 1; field < _NPCM; field++)
3091 pc->pc_map[field] = pc_freemask[field];
3092 TAILQ_INSERT_TAIL(&pv_chunks, pc, pc_lru);
3093 pv = &pc->pc_pventry[0];
3094 TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc, pc_list);
3095 PV_STAT(pv_entry_spare += _NPCPV - 1);
3100 * Create a pv entry for page at pa for
3104 pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t m)
3108 rw_assert(&pvh_global_lock, RA_WLOCKED);
3109 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3110 pv = get_pv_entry(pmap, FALSE);
3112 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_next);
3115 static __inline pv_entry_t
3116 pmap_pvh_remove(struct md_page *pvh, pmap_t pmap, vm_offset_t va)
3120 rw_assert(&pvh_global_lock, RA_WLOCKED);
3121 TAILQ_FOREACH(pv, &pvh->pv_list, pv_next) {
3122 if (pmap == PV_PMAP(pv) && va == pv->pv_va) {
3123 TAILQ_REMOVE(&pvh->pv_list, pv, pv_next);
3131 pmap_pvh_free(struct md_page *pvh, pmap_t pmap, vm_offset_t va)
3135 pv = pmap_pvh_remove(pvh, pmap, va);
3136 KASSERT(pv != NULL, ("pmap_pvh_free: pv not found"));
3137 free_pv_entry(pmap, pv);
3141 pmap_remove_entry(pmap_t pmap, vm_page_t m, vm_offset_t va)
3143 struct md_page *pvh;
3145 rw_assert(&pvh_global_lock, RA_WLOCKED);
3146 pmap_pvh_free(&m->md, pmap, va);
3147 if (TAILQ_EMPTY(&m->md.pv_list) && (m->flags & PG_FICTITIOUS) == 0) {
3148 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
3149 if (TAILQ_EMPTY(&pvh->pv_list))
3150 vm_page_aflag_clear(m, PGA_WRITEABLE);
3155 pmap_pv_demote_pte1(pmap_t pmap, vm_offset_t va, vm_paddr_t pa)
3157 struct md_page *pvh;
3159 vm_offset_t va_last;
3162 rw_assert(&pvh_global_lock, RA_WLOCKED);
3163 KASSERT((pa & PTE1_OFFSET) == 0,
3164 ("pmap_pv_demote_pte1: pa is not 1mpage aligned"));
3167 * Transfer the 1mpage's pv entry for this mapping to the first
3170 pvh = pa_to_pvh(pa);
3171 va = pte1_trunc(va);
3172 pv = pmap_pvh_remove(pvh, pmap, va);
3173 KASSERT(pv != NULL, ("pmap_pv_demote_pte1: pv not found"));
3174 m = PHYS_TO_VM_PAGE(pa);
3175 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_next);
3176 /* Instantiate the remaining NPTE2_IN_PT2 - 1 pv entries. */
3177 va_last = va + PTE1_SIZE - PAGE_SIZE;
3180 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
3181 ("pmap_pv_demote_pte1: page %p is not managed", m));
3183 pmap_insert_entry(pmap, va, m);
3184 } while (va < va_last);
3187 #if VM_NRESERVLEVEL > 0
3189 pmap_pv_promote_pte1(pmap_t pmap, vm_offset_t va, vm_paddr_t pa)
3191 struct md_page *pvh;
3193 vm_offset_t va_last;
3196 rw_assert(&pvh_global_lock, RA_WLOCKED);
3197 KASSERT((pa & PTE1_OFFSET) == 0,
3198 ("pmap_pv_promote_pte1: pa is not 1mpage aligned"));
3201 * Transfer the first page's pv entry for this mapping to the
3202 * 1mpage's pv list. Aside from avoiding the cost of a call
3203 * to get_pv_entry(), a transfer avoids the possibility that
3204 * get_pv_entry() calls pmap_pv_reclaim() and that pmap_pv_reclaim()
3205 * removes one of the mappings that is being promoted.
3207 m = PHYS_TO_VM_PAGE(pa);
3208 va = pte1_trunc(va);
3209 pv = pmap_pvh_remove(&m->md, pmap, va);
3210 KASSERT(pv != NULL, ("pmap_pv_promote_pte1: pv not found"));
3211 pvh = pa_to_pvh(pa);
3212 TAILQ_INSERT_TAIL(&pvh->pv_list, pv, pv_next);
3213 /* Free the remaining NPTE2_IN_PT2 - 1 pv entries. */
3214 va_last = va + PTE1_SIZE - PAGE_SIZE;
3218 pmap_pvh_free(&m->md, pmap, va);
3219 } while (va < va_last);
3224 * Conditionally create a pv entry.
3227 pmap_try_insert_pv_entry(pmap_t pmap, vm_offset_t va, vm_page_t m)
3231 rw_assert(&pvh_global_lock, RA_WLOCKED);
3232 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3233 if (pv_entry_count < pv_entry_high_water &&
3234 (pv = get_pv_entry(pmap, TRUE)) != NULL) {
3236 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_next);
3243 * Create the pv entries for each of the pages within a section.
3246 pmap_pv_insert_pte1(pmap_t pmap, vm_offset_t va, pt1_entry_t pte1, u_int flags)
3248 struct md_page *pvh;
3252 rw_assert(&pvh_global_lock, RA_WLOCKED);
3253 noreclaim = (flags & PMAP_ENTER_NORECLAIM) != 0;
3254 if ((noreclaim && pv_entry_count >= pv_entry_high_water) ||
3255 (pv = get_pv_entry(pmap, noreclaim)) == NULL)
3258 pvh = pa_to_pvh(pte1_pa(pte1));
3259 TAILQ_INSERT_TAIL(&pvh->pv_list, pv, pv_next);
3264 pmap_tlb_flush_pte1(pmap_t pmap, vm_offset_t va, pt1_entry_t npte1)
3267 /* Kill all the small mappings or the big one only. */
3268 if (pte1_is_section(npte1))
3269 pmap_tlb_flush_range(pmap, pte1_trunc(va), PTE1_SIZE);
3271 pmap_tlb_flush(pmap, pte1_trunc(va));
3275 * Update kernel pte1 on all pmaps.
3277 * The following function is called only on one cpu with disabled interrupts.
3278 * In SMP case, smp_rendezvous_cpus() is used to stop other cpus. This way
3279 * nobody can invoke explicit hardware table walk during the update of pte1.
3280 * Unsolicited hardware table walk can still happen, invoked by speculative
3281 * data or instruction prefetch or even by speculative hardware table walk.
3283 * The break-before-make approach should be implemented here. However, it's
3284 * not so easy to do that for kernel mappings as it would be unhappy to unmap
3285 * itself unexpectedly but voluntarily.
3288 pmap_update_pte1_kernel(vm_offset_t va, pt1_entry_t npte1)
3294 * Get current pmap. Interrupts should be disabled here
3295 * so PCPU_GET() is done atomically.
3297 pmap = PCPU_GET(curpmap);
3302 * (1) Change pte1 on current pmap.
3303 * (2) Flush all obsolete TLB entries on current CPU.
3304 * (3) Change pte1 on all pmaps.
3305 * (4) Flush all obsolete TLB entries on all CPUs in SMP case.
3308 pte1p = pmap_pte1(pmap, va);
3309 pte1_store(pte1p, npte1);
3311 /* Kill all the small mappings or the big one only. */
3312 if (pte1_is_section(npte1)) {
3313 pmap_pte1_kern_promotions++;
3314 tlb_flush_range_local(pte1_trunc(va), PTE1_SIZE);
3316 pmap_pte1_kern_demotions++;
3317 tlb_flush_local(pte1_trunc(va));
3321 * In SMP case, this function is called when all cpus are at smp
3322 * rendezvous, so there is no need to use 'allpmaps_lock' lock here.
3323 * In UP case, the function is called with this lock locked.
3325 LIST_FOREACH(pmap, &allpmaps, pm_list) {
3326 pte1p = pmap_pte1(pmap, va);
3327 pte1_store(pte1p, npte1);
3331 /* Kill all the small mappings or the big one only. */
3332 if (pte1_is_section(npte1))
3333 tlb_flush_range(pte1_trunc(va), PTE1_SIZE);
3335 tlb_flush(pte1_trunc(va));
3340 struct pte1_action {
3343 u_int update; /* CPU that updates the PTE1 */
3347 pmap_update_pte1_action(void *arg)
3349 struct pte1_action *act = arg;
3351 if (act->update == PCPU_GET(cpuid))
3352 pmap_update_pte1_kernel(act->va, act->npte1);
3356 * Change pte1 on current pmap.
3357 * Note that kernel pte1 must be changed on all pmaps.
3359 * According to the architecture reference manual published by ARM,
3360 * the behaviour is UNPREDICTABLE when two or more TLB entries map the same VA.
3361 * According to this manual, UNPREDICTABLE behaviours must never happen in
3362 * a viable system. In contrast, on x86 processors, it is not specified which
3363 * TLB entry mapping the virtual address will be used, but the MMU doesn't
3364 * generate a bogus translation the way it does on Cortex-A8 rev 2 (Beaglebone
3367 * It's a problem when either promotion or demotion is being done. The pte1
3368 * update and appropriate TLB flush must be done atomically in general.
3371 pmap_change_pte1(pmap_t pmap, pt1_entry_t *pte1p, vm_offset_t va,
3375 if (pmap == kernel_pmap) {
3376 struct pte1_action act;
3381 act.update = PCPU_GET(cpuid);
3382 smp_rendezvous_cpus(all_cpus, smp_no_rendezvous_barrier,
3383 pmap_update_pte1_action, NULL, &act);
3389 * Use break-before-make approach for changing userland
3390 * mappings. It can cause L1 translation aborts on other
3391 * cores in SMP case. So, special treatment is implemented
3392 * in pmap_fault(). To reduce the likelihood that another core
3393 * will be affected by the broken mapping, disable interrupts
3394 * until the mapping change is completed.
3396 cspr = disable_interrupts(PSR_I | PSR_F);
3398 pmap_tlb_flush_pte1(pmap, va, npte1);
3399 pte1_store(pte1p, npte1);
3400 restore_interrupts(cspr);
3405 pmap_change_pte1(pmap_t pmap, pt1_entry_t *pte1p, vm_offset_t va,
3409 if (pmap == kernel_pmap) {
3410 mtx_lock_spin(&allpmaps_lock);
3411 pmap_update_pte1_kernel(va, npte1);
3412 mtx_unlock_spin(&allpmaps_lock);
3417 * Use break-before-make approach for changing userland
3418 * mappings. It's absolutely safe in UP case when interrupts
3421 cspr = disable_interrupts(PSR_I | PSR_F);
3423 pmap_tlb_flush_pte1(pmap, va, npte1);
3424 pte1_store(pte1p, npte1);
3425 restore_interrupts(cspr);
3430 #if VM_NRESERVLEVEL > 0
3432 * Tries to promote the NPTE2_IN_PT2, contiguous 4KB page mappings that are
3433 * within a single page table page (PT2) to a single 1MB page mapping.
3434 * For promotion to occur, two conditions must be met: (1) the 4KB page
3435 * mappings must map aligned, contiguous physical memory and (2) the 4KB page
3436 * mappings must have identical characteristics.
3438 * Managed (PG_MANAGED) mappings within the kernel address space are not
3439 * promoted. The reason is that kernel PTE1s are replicated in each pmap but
3440 * pmap_remove_write(), pmap_clear_modify(), and pmap_clear_reference() only
3441 * read the PTE1 from the kernel pmap.
3444 pmap_promote_pte1(pmap_t pmap, pt1_entry_t *pte1p, vm_offset_t va)
3447 pt2_entry_t *fpte2p, fpte2, fpte2_fav;
3448 pt2_entry_t *pte2p, pte2;
3449 vm_offset_t pteva __unused;
3450 vm_page_t m __unused;
3452 PDEBUG(6, printf("%s(%p): try for va %#x pte1 %#x at %p\n", __func__,
3453 pmap, va, pte1_load(pte1p), pte1p));
3455 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3458 * Examine the first PTE2 in the specified PT2. Abort if this PTE2 is
3459 * either invalid, unused, or does not map the first 4KB physical page
3460 * within a 1MB page.
3462 fpte2p = pmap_pte2_quick(pmap, pte1_trunc(va));
3463 fpte2 = pte2_load(fpte2p);
3464 if ((fpte2 & ((PTE2_FRAME & PTE1_OFFSET) | PTE2_A | PTE2_V)) !=
3465 (PTE2_A | PTE2_V)) {
3466 pmap_pte1_p_failures++;
3467 CTR3(KTR_PMAP, "%s: failure(1) for va %#x in pmap %p",
3468 __func__, va, pmap);
3471 if (pte2_is_managed(fpte2) && pmap == kernel_pmap) {
3472 pmap_pte1_p_failures++;
3473 CTR3(KTR_PMAP, "%s: failure(2) for va %#x in pmap %p",
3474 __func__, va, pmap);
3477 if ((fpte2 & (PTE2_NM | PTE2_RO)) == PTE2_NM) {
3479 * When page is not modified, PTE2_RO can be set without
3480 * a TLB invalidation.
3483 pte2_store(fpte2p, fpte2);
3487 * Examine each of the other PTE2s in the specified PT2. Abort if this
3488 * PTE2 maps an unexpected 4KB physical page or does not have identical
3489 * characteristics to the first PTE2.
3491 fpte2_fav = (fpte2 & (PTE2_FRAME | PTE2_A | PTE2_V));
3492 fpte2_fav += PTE1_SIZE - PTE2_SIZE; /* examine from the end */
3493 for (pte2p = fpte2p + NPTE2_IN_PT2 - 1; pte2p > fpte2p; pte2p--) {
3494 pte2 = pte2_load(pte2p);
3495 if ((pte2 & (PTE2_FRAME | PTE2_A | PTE2_V)) != fpte2_fav) {
3496 pmap_pte1_p_failures++;
3497 CTR3(KTR_PMAP, "%s: failure(3) for va %#x in pmap %p",
3498 __func__, va, pmap);
3501 if ((pte2 & (PTE2_NM | PTE2_RO)) == PTE2_NM) {
3503 * When page is not modified, PTE2_RO can be set
3504 * without a TLB invalidation. See note above.
3507 pte2_store(pte2p, pte2);
3508 pteva = pte1_trunc(va) | (pte2 & PTE1_OFFSET &
3510 CTR3(KTR_PMAP, "%s: protect for va %#x in pmap %p",
3511 __func__, pteva, pmap);
3513 if ((pte2 & PTE2_PROMOTE) != (fpte2 & PTE2_PROMOTE)) {
3514 pmap_pte1_p_failures++;
3515 CTR3(KTR_PMAP, "%s: failure(4) for va %#x in pmap %p",
3516 __func__, va, pmap);
3520 fpte2_fav -= PTE2_SIZE;
3523 * The page table page in its current state will stay in PT2TAB
3524 * until the PTE1 mapping the section is demoted by pmap_demote_pte1()
3525 * or destroyed by pmap_remove_pte1().
3527 * Note that L2 page table size is not equal to PAGE_SIZE.
3529 m = PHYS_TO_VM_PAGE(trunc_page(pte1_link_pa(pte1_load(pte1p))));
3530 KASSERT(m >= vm_page_array && m < &vm_page_array[vm_page_array_size],
3531 ("%s: PT2 page is out of range", __func__));
3532 KASSERT(m->pindex == (pte1_index(va) & ~PT2PG_MASK),
3533 ("%s: PT2 page's pindex is wrong", __func__));
3536 * Get pte1 from pte2 format.
3538 npte1 = (fpte2 & PTE1_FRAME) | ATTR_TO_L1(fpte2) | PTE1_V;
3541 * Promote the pv entries.
3543 if (pte2_is_managed(fpte2))
3544 pmap_pv_promote_pte1(pmap, va, pte1_pa(npte1));
3547 * Promote the mappings.
3549 pmap_change_pte1(pmap, pte1p, va, npte1);
3551 pmap_pte1_promotions++;
3552 CTR3(KTR_PMAP, "%s: success for va %#x in pmap %p",
3553 __func__, va, pmap);
3555 PDEBUG(6, printf("%s(%p): success for va %#x pte1 %#x(%#x) at %p\n",
3556 __func__, pmap, va, npte1, pte1_load(pte1p), pte1p));
3558 #endif /* VM_NRESERVLEVEL > 0 */
3561 * Zero L2 page table page.
3563 static __inline void
3564 pmap_clear_pt2(pt2_entry_t *fpte2p)
3568 for (pte2p = fpte2p; pte2p < fpte2p + NPTE2_IN_PT2; pte2p++)
3574 * Removes a 1MB page mapping from the kernel pmap.
3577 pmap_remove_kernel_pte1(pmap_t pmap, pt1_entry_t *pte1p, vm_offset_t va)
3581 pt2_entry_t *fpte2p;
3584 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3585 m = pmap_pt2_page(pmap, va);
3588 * QQQ: Is this function called only on promoted pte1?
3589 * We certainly do section mappings directly
3590 * (without promotion) in kernel !!!
3592 panic("%s: missing pt2 page", __func__);
3594 pte1_idx = pte1_index(va);
3597 * Initialize the L2 page table.
3599 fpte2p = page_pt2(pt2map_pt2pg(va), pte1_idx);
3600 pmap_clear_pt2(fpte2p);
3603 * Remove the mapping.
3605 pt2_pa = page_pt2pa(VM_PAGE_TO_PHYS(m), pte1_idx);
3606 pmap_kenter_pte1(va, PTE1_LINK(pt2_pa));
3609 * QQQ: We do not need to invalidate PT2MAP mapping
3610 * as we did not change it. I.e. the L2 page table page
3611 * was and still is mapped the same way.
3616 * Do the things to unmap a section in a process
3619 pmap_remove_pte1(pmap_t pmap, pt1_entry_t *pte1p, vm_offset_t sva,
3620 struct spglist *free)
3623 struct md_page *pvh;
3624 vm_offset_t eva, va;
3627 PDEBUG(6, printf("%s(%p): va %#x pte1 %#x at %p\n", __func__, pmap, sva,
3628 pte1_load(pte1p), pte1p));
3630 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3631 KASSERT((sva & PTE1_OFFSET) == 0,
3632 ("%s: sva is not 1mpage aligned", __func__));
3635 * Clear and invalidate the mapping. It should occupy one and only TLB
3636 * entry. So, pmap_tlb_flush() called with aligned address should be
3639 opte1 = pte1_load_clear(pte1p);
3640 pmap_tlb_flush(pmap, sva);
3642 if (pte1_is_wired(opte1))
3643 pmap->pm_stats.wired_count -= PTE1_SIZE / PAGE_SIZE;
3644 pmap->pm_stats.resident_count -= PTE1_SIZE / PAGE_SIZE;
3645 if (pte1_is_managed(opte1)) {
3646 pvh = pa_to_pvh(pte1_pa(opte1));
3647 pmap_pvh_free(pvh, pmap, sva);
3648 eva = sva + PTE1_SIZE;
3649 for (va = sva, m = PHYS_TO_VM_PAGE(pte1_pa(opte1));
3650 va < eva; va += PAGE_SIZE, m++) {
3651 if (pte1_is_dirty(opte1))
3654 vm_page_aflag_set(m, PGA_REFERENCED);
3655 if (TAILQ_EMPTY(&m->md.pv_list) &&
3656 TAILQ_EMPTY(&pvh->pv_list))
3657 vm_page_aflag_clear(m, PGA_WRITEABLE);
3660 if (pmap == kernel_pmap) {
3662 * L2 page table(s) can't be removed from kernel map as
3663 * kernel counts on it (stuff around pmap_growkernel()).
3665 pmap_remove_kernel_pte1(pmap, pte1p, sva);
3668 * Get associated L2 page table page.
3669 * It's possible that the page was never allocated.
3671 m = pmap_pt2_page(pmap, sva);
3673 pmap_unwire_pt2_all(pmap, sva, m, free);
3678 * Fills L2 page table page with mappings to consecutive physical pages.
3680 static __inline void
3681 pmap_fill_pt2(pt2_entry_t *fpte2p, pt2_entry_t npte2)
3685 for (pte2p = fpte2p; pte2p < fpte2p + NPTE2_IN_PT2; pte2p++) {
3686 pte2_store(pte2p, npte2);
3692 * Tries to demote a 1MB page mapping. If demotion fails, the
3693 * 1MB page mapping is invalidated.
3696 pmap_demote_pte1(pmap_t pmap, pt1_entry_t *pte1p, vm_offset_t va)
3698 pt1_entry_t opte1, npte1;
3699 pt2_entry_t *fpte2p, npte2;
3700 vm_paddr_t pt2pg_pa, pt2_pa;
3702 struct spglist free;
3703 uint32_t pte1_idx, isnew = 0;
3705 PDEBUG(6, printf("%s(%p): try for va %#x pte1 %#x at %p\n", __func__,
3706 pmap, va, pte1_load(pte1p), pte1p));
3708 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3710 opte1 = pte1_load(pte1p);
3711 KASSERT(pte1_is_section(opte1), ("%s: opte1 not a section", __func__));
3713 if ((opte1 & PTE1_A) == 0 || (m = pmap_pt2_page(pmap, va)) == NULL) {
3714 KASSERT(!pte1_is_wired(opte1),
3715 ("%s: PT2 page for a wired mapping is missing", __func__));
3718 * Invalidate the 1MB page mapping and return
3719 * "failure" if the mapping was never accessed or the
3720 * allocation of the new page table page fails.
3722 if ((opte1 & PTE1_A) == 0 || (m = vm_page_alloc(NULL,
3723 pte1_index(va) & ~PT2PG_MASK, VM_ALLOC_NOOBJ |
3724 VM_ALLOC_NORMAL | VM_ALLOC_WIRED)) == NULL) {
3726 pmap_remove_pte1(pmap, pte1p, pte1_trunc(va), &free);
3727 vm_page_free_pages_toq(&free, false);
3728 CTR3(KTR_PMAP, "%s: failure for va %#x in pmap %p",
3729 __func__, va, pmap);
3732 if (va < VM_MAXUSER_ADDRESS)
3733 pmap->pm_stats.resident_count++;
3738 * We init all L2 page tables in the page even if
3739 * we are going to change everything for one L2 page
3742 pt2pg_pa = pmap_pt2pg_init(pmap, va, m);
3744 if (va < VM_MAXUSER_ADDRESS) {
3745 if (pt2_is_empty(m, va))
3746 isnew = 1; /* Demoting section w/o promotion. */
3749 KASSERT(pt2_is_full(m, va), ("%s: bad PT2 wire"
3750 " count %u", __func__,
3751 pt2_wirecount_get(m, pte1_index(va))));
3756 pt2pg_pa = VM_PAGE_TO_PHYS(m);
3757 pte1_idx = pte1_index(va);
3759 * If the pmap is current, then the PT2MAP can provide access to
3760 * the page table page (promoted L2 page tables are not unmapped).
3761 * Otherwise, temporarily map the L2 page table page (m) into
3762 * the kernel's address space at either PADDR1 or PADDR2.
3764 * Note that L2 page table size is not equal to PAGE_SIZE.
3766 if (pmap_is_current(pmap))
3767 fpte2p = page_pt2(pt2map_pt2pg(va), pte1_idx);
3768 else if (curthread->td_pinned > 0 && rw_wowned(&pvh_global_lock)) {
3769 if (pte2_pa(pte2_load(PMAP1)) != pt2pg_pa) {
3770 pte2_store(PMAP1, PTE2_KPT(pt2pg_pa));
3772 PMAP1cpu = PCPU_GET(cpuid);
3774 tlb_flush_local((vm_offset_t)PADDR1);
3778 if (PMAP1cpu != PCPU_GET(cpuid)) {
3779 PMAP1cpu = PCPU_GET(cpuid);
3780 tlb_flush_local((vm_offset_t)PADDR1);
3785 fpte2p = page_pt2((vm_offset_t)PADDR1, pte1_idx);
3787 mtx_lock(&PMAP2mutex);
3788 if (pte2_pa(pte2_load(PMAP2)) != pt2pg_pa) {
3789 pte2_store(PMAP2, PTE2_KPT(pt2pg_pa));
3790 tlb_flush((vm_offset_t)PADDR2);
3792 fpte2p = page_pt2((vm_offset_t)PADDR2, pte1_idx);
3794 pt2_pa = page_pt2pa(pt2pg_pa, pte1_idx);
3795 npte1 = PTE1_LINK(pt2_pa);
3797 KASSERT((opte1 & PTE1_A) != 0,
3798 ("%s: opte1 is missing PTE1_A", __func__));
3799 KASSERT((opte1 & (PTE1_NM | PTE1_RO)) != PTE1_NM,
3800 ("%s: opte1 has PTE1_NM", __func__));
3803 * Get pte2 from pte1 format.
3805 npte2 = pte1_pa(opte1) | ATTR_TO_L2(opte1) | PTE2_V;
3808 * If the L2 page table page is new, initialize it. If the mapping
3809 * has changed attributes, update the page table entries.
3812 pt2_wirecount_set(m, pte1_idx, NPTE2_IN_PT2);
3813 pmap_fill_pt2(fpte2p, npte2);
3814 } else if ((pte2_load(fpte2p) & PTE2_PROMOTE) !=
3815 (npte2 & PTE2_PROMOTE))
3816 pmap_fill_pt2(fpte2p, npte2);
3818 KASSERT(pte2_pa(pte2_load(fpte2p)) == pte2_pa(npte2),
3819 ("%s: fpte2p and npte2 map different physical addresses",
3822 if (fpte2p == PADDR2)
3823 mtx_unlock(&PMAP2mutex);
3826 * Demote the mapping. This pmap is locked. The old PTE1 has
3827 * PTE1_A set. If the old PTE1 has not PTE1_RO set, it also
3828 * has not PTE1_NM set. Thus, there is no danger of a race with
3829 * another processor changing the setting of PTE1_A and/or PTE1_NM
3830 * between the read above and the store below.
3832 pmap_change_pte1(pmap, pte1p, va, npte1);
3835 * Demote the pv entry. This depends on the earlier demotion
3836 * of the mapping. Specifically, the (re)creation of a per-
3837 * page pv entry might trigger the execution of pmap_pv_reclaim(),
3838 * which might reclaim a newly (re)created per-page pv entry
3839 * and destroy the associated mapping. In order to destroy
3840 * the mapping, the PTE1 must have already changed from mapping
3841 * the 1mpage to referencing the page table page.
3843 if (pte1_is_managed(opte1))
3844 pmap_pv_demote_pte1(pmap, va, pte1_pa(opte1));
3846 pmap_pte1_demotions++;
3847 CTR3(KTR_PMAP, "%s: success for va %#x in pmap %p",
3848 __func__, va, pmap);
3850 PDEBUG(6, printf("%s(%p): success for va %#x pte1 %#x(%#x) at %p\n",
3851 __func__, pmap, va, npte1, pte1_load(pte1p), pte1p));
3856 * Insert the given physical page (p) at
3857 * the specified virtual address (v) in the
3858 * target physical map with the protection requested.
3860 * If specified, the page will be wired down, meaning
3861 * that the related pte can not be reclaimed.
3863 * NB: This is the only routine which MAY NOT lazy-evaluate
3864 * or lose information. That is, this routine must actually
3865 * insert this page into the given map NOW.
3868 pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
3869 u_int flags, int8_t psind)
3873 pt2_entry_t npte2, opte2;
3876 vm_page_t mpte2, om;
3879 va = trunc_page(va);
3880 KASSERT(va <= vm_max_kernel_address, ("%s: toobig", __func__));
3881 KASSERT(va < UPT2V_MIN_ADDRESS || va >= UPT2V_MAX_ADDRESS,
3882 ("%s: invalid to pmap_enter page table pages (va: 0x%x)", __func__,
3884 KASSERT((m->oflags & VPO_UNMANAGED) != 0 || va < kmi.clean_sva ||
3885 va >= kmi.clean_eva,
3886 ("%s: managed mapping within the clean submap", __func__));
3887 if ((m->oflags & VPO_UNMANAGED) == 0 && !vm_page_xbusied(m))
3888 VM_OBJECT_ASSERT_LOCKED(m->object);
3889 KASSERT((flags & PMAP_ENTER_RESERVED) == 0,
3890 ("%s: flags %u has reserved bits set", __func__, flags));
3891 pa = VM_PAGE_TO_PHYS(m);
3892 npte2 = PTE2(pa, PTE2_A, vm_page_pte2_attr(m));
3893 if ((flags & VM_PROT_WRITE) == 0)
3895 if ((prot & VM_PROT_WRITE) == 0)
3897 KASSERT((npte2 & (PTE2_NM | PTE2_RO)) != PTE2_RO,
3898 ("%s: flags includes VM_PROT_WRITE but prot doesn't", __func__));
3899 if ((prot & VM_PROT_EXECUTE) == 0)
3901 if ((flags & PMAP_ENTER_WIRED) != 0)
3903 if (va < VM_MAXUSER_ADDRESS)
3905 if (pmap != kernel_pmap)
3908 rw_wlock(&pvh_global_lock);
3912 /* Assert the required virtual and physical alignment. */
3913 KASSERT((va & PTE1_OFFSET) == 0,
3914 ("%s: va unaligned", __func__));
3915 KASSERT(m->psind > 0, ("%s: m->psind < psind", __func__));
3916 rv = pmap_enter_pte1(pmap, va, PTE1_PA(pa) | ATTR_TO_L1(npte2) |
3922 * In the case that a page table page is not
3923 * resident, we are creating it here.
3925 if (va < VM_MAXUSER_ADDRESS) {
3926 mpte2 = pmap_allocpte2(pmap, va, flags);
3927 if (mpte2 == NULL) {
3928 KASSERT((flags & PMAP_ENTER_NOSLEEP) != 0,
3929 ("pmap_allocpte2 failed with sleep allowed"));
3930 rv = KERN_RESOURCE_SHORTAGE;
3935 pte1p = pmap_pte1(pmap, va);
3936 if (pte1_is_section(pte1_load(pte1p)))
3937 panic("%s: attempted on 1MB page", __func__);
3938 pte2p = pmap_pte2_quick(pmap, va);
3940 panic("%s: invalid L1 page table entry va=%#x", __func__, va);
3943 opte2 = pte2_load(pte2p);
3944 opa = pte2_pa(opte2);
3946 * Mapping has not changed, must be protection or wiring change.
3948 if (pte2_is_valid(opte2) && (opa == pa)) {
3950 * Wiring change, just update stats. We don't worry about
3951 * wiring PT2 pages as they remain resident as long as there
3952 * are valid mappings in them. Hence, if a user page is wired,
3953 * the PT2 page will be also.
3955 if (pte2_is_wired(npte2) && !pte2_is_wired(opte2))
3956 pmap->pm_stats.wired_count++;
3957 else if (!pte2_is_wired(npte2) && pte2_is_wired(opte2))
3958 pmap->pm_stats.wired_count--;
3961 * Remove extra pte2 reference
3964 pt2_wirecount_dec(mpte2, pte1_index(va));
3965 if ((m->oflags & VPO_UNMANAGED) == 0)
3971 * QQQ: We think that changing physical address on writeable mapping
3972 * is not safe. Well, maybe on kernel address space with correct
3973 * locking, it can make a sense. However, we have no idea why
3974 * anyone should do that on user address space. Are we wrong?
3976 KASSERT((opa == 0) || (opa == pa) ||
3977 !pte2_is_valid(opte2) || ((opte2 & PTE2_RO) != 0),
3978 ("%s: pmap %p va %#x(%#x) opa %#x pa %#x - gotcha %#x %#x!",
3979 __func__, pmap, va, opte2, opa, pa, flags, prot));
3984 * Mapping has changed, invalidate old range and fall through to
3985 * handle validating new mapping.
3988 if (pte2_is_wired(opte2))
3989 pmap->pm_stats.wired_count--;
3990 om = PHYS_TO_VM_PAGE(opa);
3991 if (om != NULL && (om->oflags & VPO_UNMANAGED) != 0)
3994 pv = pmap_pvh_remove(&om->md, pmap, va);
3997 * Remove extra pte2 reference
4000 pt2_wirecount_dec(mpte2, va >> PTE1_SHIFT);
4002 pmap->pm_stats.resident_count++;
4005 * Enter on the PV list if part of our managed memory.
4007 if ((m->oflags & VPO_UNMANAGED) == 0) {
4009 pv = get_pv_entry(pmap, FALSE);
4012 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_next);
4013 } else if (pv != NULL)
4014 free_pv_entry(pmap, pv);
4017 * Increment counters
4019 if (pte2_is_wired(npte2))
4020 pmap->pm_stats.wired_count++;
4024 * Now validate mapping with desired protection/wiring.
4026 if (prot & VM_PROT_WRITE) {
4027 if ((m->oflags & VPO_UNMANAGED) == 0)
4028 vm_page_aflag_set(m, PGA_WRITEABLE);
4032 * If the mapping or permission bits are different, we need
4033 * to update the pte2.
4035 * QQQ: Think again and again what to do
4036 * if the mapping is going to be changed!
4038 if ((opte2 & ~(PTE2_NM | PTE2_A)) != (npte2 & ~(PTE2_NM | PTE2_A))) {
4040 * Sync icache if exec permission and attribute VM_MEMATTR_WB_WA
4041 * is set. Do it now, before the mapping is stored and made
4042 * valid for hardware table walk. If done later, there is a race
4043 * for other threads of current process in lazy loading case.
4044 * Don't do it for kernel memory which is mapped with exec
4045 * permission even if the memory isn't going to hold executable
4046 * code. The only time when icache sync is needed is after
4047 * kernel module is loaded and the relocation info is processed.
4048 * And it's done in elf_cpu_load_file().
4050 * QQQ: (1) Does it exist any better way where
4051 * or how to sync icache?
4052 * (2) Now, we do it on a page basis.
4054 if ((prot & VM_PROT_EXECUTE) && pmap != kernel_pmap &&
4055 m->md.pat_mode == VM_MEMATTR_WB_WA &&
4056 (opa != pa || (opte2 & PTE2_NX)))
4057 cache_icache_sync_fresh(va, pa, PAGE_SIZE);
4059 if (opte2 & PTE2_V) {
4060 /* Change mapping with break-before-make approach. */
4061 opte2 = pte2_load_clear(pte2p);
4062 pmap_tlb_flush(pmap, va);
4063 pte2_store(pte2p, npte2);
4065 KASSERT((om->oflags & VPO_UNMANAGED) == 0,
4066 ("%s: om %p unmanaged", __func__, om));
4067 if ((opte2 & PTE2_A) != 0)
4068 vm_page_aflag_set(om, PGA_REFERENCED);
4069 if (pte2_is_dirty(opte2))
4071 if (TAILQ_EMPTY(&om->md.pv_list) &&
4072 ((om->flags & PG_FICTITIOUS) != 0 ||
4073 TAILQ_EMPTY(&pa_to_pvh(opa)->pv_list)))
4074 vm_page_aflag_clear(om, PGA_WRITEABLE);
4077 pte2_store(pte2p, npte2);
4082 * QQQ: In time when both access and not mofified bits are
4083 * emulated by software, this should not happen. Some
4084 * analysis is need, if this really happen. Missing
4085 * tlb flush somewhere could be the reason.
4087 panic("%s: pmap %p va %#x opte2 %x npte2 %x !!", __func__, pmap,
4092 #if VM_NRESERVLEVEL > 0
4094 * If both the L2 page table page and the reservation are fully
4095 * populated, then attempt promotion.
4097 if ((mpte2 == NULL || pt2_is_full(mpte2, va)) &&
4098 sp_enabled && (m->flags & PG_FICTITIOUS) == 0 &&
4099 vm_reserv_level_iffullpop(m) == 0)
4100 pmap_promote_pte1(pmap, pte1p, va);
4106 rw_wunlock(&pvh_global_lock);
4112 * Do the things to unmap a page in a process.
4115 pmap_remove_pte2(pmap_t pmap, pt2_entry_t *pte2p, vm_offset_t va,
4116 struct spglist *free)
4121 rw_assert(&pvh_global_lock, RA_WLOCKED);
4122 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
4124 /* Clear and invalidate the mapping. */
4125 opte2 = pte2_load_clear(pte2p);
4126 pmap_tlb_flush(pmap, va);
4128 KASSERT(pte2_is_valid(opte2), ("%s: pmap %p va %#x not link pte2 %#x",
4129 __func__, pmap, va, opte2));
4132 pmap->pm_stats.wired_count -= 1;
4133 pmap->pm_stats.resident_count -= 1;
4134 if (pte2_is_managed(opte2)) {
4135 m = PHYS_TO_VM_PAGE(pte2_pa(opte2));
4136 if (pte2_is_dirty(opte2))
4139 vm_page_aflag_set(m, PGA_REFERENCED);
4140 pmap_remove_entry(pmap, m, va);
4142 return (pmap_unuse_pt2(pmap, va, free));
4146 * Remove a single page from a process address space.
4149 pmap_remove_page(pmap_t pmap, vm_offset_t va, struct spglist *free)
4153 rw_assert(&pvh_global_lock, RA_WLOCKED);
4154 KASSERT(curthread->td_pinned > 0,
4155 ("%s: curthread not pinned", __func__));
4156 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
4157 if ((pte2p = pmap_pte2_quick(pmap, va)) == NULL ||
4158 !pte2_is_valid(pte2_load(pte2p)))
4160 pmap_remove_pte2(pmap, pte2p, va, free);
4164 * Remove the given range of addresses from the specified map.
4166 * It is assumed that the start and end are properly
4167 * rounded to the page size.
4170 pmap_remove(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
4173 pt1_entry_t *pte1p, pte1;
4174 pt2_entry_t *pte2p, pte2;
4175 struct spglist free;
4178 * Perform an unsynchronized read. This is, however, safe.
4180 if (pmap->pm_stats.resident_count == 0)
4185 rw_wlock(&pvh_global_lock);
4190 * Special handling of removing one page. A very common
4191 * operation and easy to short circuit some code.
4193 if (sva + PAGE_SIZE == eva) {
4194 pte1 = pte1_load(pmap_pte1(pmap, sva));
4195 if (pte1_is_link(pte1)) {
4196 pmap_remove_page(pmap, sva, &free);
4201 for (; sva < eva; sva = nextva) {
4203 * Calculate address for next L2 page table.
4205 nextva = pte1_trunc(sva + PTE1_SIZE);
4208 if (pmap->pm_stats.resident_count == 0)
4211 pte1p = pmap_pte1(pmap, sva);
4212 pte1 = pte1_load(pte1p);
4215 * Weed out invalid mappings. Note: we assume that the L1 page
4216 * table is always allocated, and in kernel virtual.
4221 if (pte1_is_section(pte1)) {
4223 * Are we removing the entire large page? If not,
4224 * demote the mapping and fall through.
4226 if (sva + PTE1_SIZE == nextva && eva >= nextva) {
4227 pmap_remove_pte1(pmap, pte1p, sva, &free);
4229 } else if (!pmap_demote_pte1(pmap, pte1p, sva)) {
4230 /* The large page mapping was destroyed. */
4235 /* Update pte1 after demotion. */
4236 pte1 = pte1_load(pte1p);
4241 KASSERT(pte1_is_link(pte1), ("%s: pmap %p va %#x pte1 %#x at %p"
4242 " is not link", __func__, pmap, sva, pte1, pte1p));
4245 * Limit our scan to either the end of the va represented
4246 * by the current L2 page table page, or to the end of the
4247 * range being removed.
4252 for (pte2p = pmap_pte2_quick(pmap, sva); sva != nextva;
4253 pte2p++, sva += PAGE_SIZE) {
4254 pte2 = pte2_load(pte2p);
4255 if (!pte2_is_valid(pte2))
4257 if (pmap_remove_pte2(pmap, pte2p, sva, &free))
4263 rw_wunlock(&pvh_global_lock);
4265 vm_page_free_pages_toq(&free, false);
4269 * Routine: pmap_remove_all
4271 * Removes this physical page from
4272 * all physical maps in which it resides.
4273 * Reflects back modify bits to the pager.
4276 * Original versions of this routine were very
4277 * inefficient because they iteratively called
4278 * pmap_remove (slow...)
4282 pmap_remove_all(vm_page_t m)
4284 struct md_page *pvh;
4287 pt2_entry_t *pte2p, opte2;
4290 struct spglist free;
4292 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4293 ("%s: page %p is not managed", __func__, m));
4295 rw_wlock(&pvh_global_lock);
4297 if ((m->flags & PG_FICTITIOUS) != 0)
4298 goto small_mappings;
4299 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
4300 while ((pv = TAILQ_FIRST(&pvh->pv_list)) != NULL) {
4304 pte1p = pmap_pte1(pmap, va);
4305 (void)pmap_demote_pte1(pmap, pte1p, va);
4309 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
4312 pmap->pm_stats.resident_count--;
4313 pte1p = pmap_pte1(pmap, pv->pv_va);
4314 KASSERT(!pte1_is_section(pte1_load(pte1p)), ("%s: found "
4315 "a 1mpage in page %p's pv list", __func__, m));
4316 pte2p = pmap_pte2_quick(pmap, pv->pv_va);
4317 opte2 = pte2_load_clear(pte2p);
4318 pmap_tlb_flush(pmap, pv->pv_va);
4319 KASSERT(pte2_is_valid(opte2), ("%s: pmap %p va %x zero pte2",
4320 __func__, pmap, pv->pv_va));
4321 if (pte2_is_wired(opte2))
4322 pmap->pm_stats.wired_count--;
4324 vm_page_aflag_set(m, PGA_REFERENCED);
4327 * Update the vm_page_t clean and reference bits.
4329 if (pte2_is_dirty(opte2))
4331 pmap_unuse_pt2(pmap, pv->pv_va, &free);
4332 TAILQ_REMOVE(&m->md.pv_list, pv, pv_next);
4333 free_pv_entry(pmap, pv);
4336 vm_page_aflag_clear(m, PGA_WRITEABLE);
4338 rw_wunlock(&pvh_global_lock);
4339 vm_page_free_pages_toq(&free, false);
4343 * Just subroutine for pmap_remove_pages() to reasonably satisfy
4344 * good coding style, a.k.a. 80 character line width limit hell.
4346 static __inline void
4347 pmap_remove_pte1_quick(pmap_t pmap, pt1_entry_t pte1, pv_entry_t pv,
4348 struct spglist *free)
4351 vm_page_t m, mt, mpt2pg;
4352 struct md_page *pvh;
4355 m = PHYS_TO_VM_PAGE(pa);
4357 KASSERT(m->phys_addr == pa, ("%s: vm_page_t %p addr mismatch %#x %#x",
4358 __func__, m, m->phys_addr, pa));
4359 KASSERT((m->flags & PG_FICTITIOUS) != 0 ||
4360 m < &vm_page_array[vm_page_array_size],
4361 ("%s: bad pte1 %#x", __func__, pte1));
4363 if (pte1_is_dirty(pte1)) {
4364 for (mt = m; mt < &m[PTE1_SIZE / PAGE_SIZE]; mt++)
4368 pmap->pm_stats.resident_count -= PTE1_SIZE / PAGE_SIZE;
4369 pvh = pa_to_pvh(pa);
4370 TAILQ_REMOVE(&pvh->pv_list, pv, pv_next);
4371 if (TAILQ_EMPTY(&pvh->pv_list)) {
4372 for (mt = m; mt < &m[PTE1_SIZE / PAGE_SIZE]; mt++)
4373 if (TAILQ_EMPTY(&mt->md.pv_list))
4374 vm_page_aflag_clear(mt, PGA_WRITEABLE);
4376 mpt2pg = pmap_pt2_page(pmap, pv->pv_va);
4378 pmap_unwire_pt2_all(pmap, pv->pv_va, mpt2pg, free);
4382 * Just subroutine for pmap_remove_pages() to reasonably satisfy
4383 * good coding style, a.k.a. 80 character line width limit hell.
4385 static __inline void
4386 pmap_remove_pte2_quick(pmap_t pmap, pt2_entry_t pte2, pv_entry_t pv,
4387 struct spglist *free)
4391 struct md_page *pvh;
4394 m = PHYS_TO_VM_PAGE(pa);
4396 KASSERT(m->phys_addr == pa, ("%s: vm_page_t %p addr mismatch %#x %#x",
4397 __func__, m, m->phys_addr, pa));
4398 KASSERT((m->flags & PG_FICTITIOUS) != 0 ||
4399 m < &vm_page_array[vm_page_array_size],
4400 ("%s: bad pte2 %#x", __func__, pte2));
4402 if (pte2_is_dirty(pte2))
4405 pmap->pm_stats.resident_count--;
4406 TAILQ_REMOVE(&m->md.pv_list, pv, pv_next);
4407 if (TAILQ_EMPTY(&m->md.pv_list) && (m->flags & PG_FICTITIOUS) == 0) {
4408 pvh = pa_to_pvh(pa);
4409 if (TAILQ_EMPTY(&pvh->pv_list))
4410 vm_page_aflag_clear(m, PGA_WRITEABLE);
4412 pmap_unuse_pt2(pmap, pv->pv_va, free);
4416 * Remove all pages from specified address space this aids process
4417 * exit speeds. Also, this code is special cased for current process
4418 * only, but can have the more generic (and slightly slower) mode enabled.
4419 * This is much faster than pmap_remove in the case of running down
4420 * an entire address space.
4423 pmap_remove_pages(pmap_t pmap)
4425 pt1_entry_t *pte1p, pte1;
4426 pt2_entry_t *pte2p, pte2;
4428 struct pv_chunk *pc, *npc;
4429 struct spglist free;
4432 uint32_t inuse, bitmask;
4436 * Assert that the given pmap is only active on the current
4437 * CPU. Unfortunately, we cannot block another CPU from
4438 * activating the pmap while this function is executing.
4440 KASSERT(pmap == vmspace_pmap(curthread->td_proc->p_vmspace),
4441 ("%s: non-current pmap %p", __func__, pmap));
4442 #if defined(SMP) && defined(INVARIANTS)
4444 cpuset_t other_cpus;
4447 other_cpus = pmap->pm_active;
4448 CPU_CLR(PCPU_GET(cpuid), &other_cpus);
4450 KASSERT(CPU_EMPTY(&other_cpus),
4451 ("%s: pmap %p active on other cpus", __func__, pmap));
4455 rw_wlock(&pvh_global_lock);
4458 TAILQ_FOREACH_SAFE(pc, &pmap->pm_pvchunk, pc_list, npc) {
4459 KASSERT(pc->pc_pmap == pmap, ("%s: wrong pmap %p %p",
4460 __func__, pmap, pc->pc_pmap));
4462 for (field = 0; field < _NPCM; field++) {
4463 inuse = (~(pc->pc_map[field])) & pc_freemask[field];
4464 while (inuse != 0) {
4465 bit = ffs(inuse) - 1;
4466 bitmask = 1UL << bit;
4467 idx = field * 32 + bit;
4468 pv = &pc->pc_pventry[idx];
4472 * Note that we cannot remove wired pages
4473 * from a process' mapping at this time
4475 pte1p = pmap_pte1(pmap, pv->pv_va);
4476 pte1 = pte1_load(pte1p);
4477 if (pte1_is_section(pte1)) {
4478 if (pte1_is_wired(pte1)) {
4483 pmap_remove_pte1_quick(pmap, pte1, pv,
4486 else if (pte1_is_link(pte1)) {
4487 pte2p = pt2map_entry(pv->pv_va);
4488 pte2 = pte2_load(pte2p);
4490 if (!pte2_is_valid(pte2)) {
4491 printf("%s: pmap %p va %#x "
4492 "pte2 %#x\n", __func__,
4493 pmap, pv->pv_va, pte2);
4497 if (pte2_is_wired(pte2)) {
4502 pmap_remove_pte2_quick(pmap, pte2, pv,
4505 printf("%s: pmap %p va %#x pte1 %#x\n",
4506 __func__, pmap, pv->pv_va, pte1);
4511 PV_STAT(pv_entry_frees++);
4512 PV_STAT(pv_entry_spare++);
4514 pc->pc_map[field] |= bitmask;
4518 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
4522 tlb_flush_all_ng_local();
4524 rw_wunlock(&pvh_global_lock);
4526 vm_page_free_pages_toq(&free, false);
4530 * This code makes some *MAJOR* assumptions:
4531 * 1. Current pmap & pmap exists.
4534 * 4. No L2 page table pages.
4535 * but is *MUCH* faster than pmap_enter...
4538 pmap_enter_quick_locked(pmap_t pmap, vm_offset_t va, vm_page_t m,
4539 vm_prot_t prot, vm_page_t mpt2pg)
4541 pt2_entry_t *pte2p, pte2;
4543 struct spglist free;
4546 KASSERT(va < kmi.clean_sva || va >= kmi.clean_eva ||
4547 (m->oflags & VPO_UNMANAGED) != 0,
4548 ("%s: managed mapping within the clean submap", __func__));
4549 rw_assert(&pvh_global_lock, RA_WLOCKED);
4550 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
4553 * In the case that a L2 page table page is not
4554 * resident, we are creating it here.
4556 if (va < VM_MAXUSER_ADDRESS) {
4558 pt1_entry_t pte1, *pte1p;
4562 * Get L1 page table things.
4564 pte1_idx = pte1_index(va);
4565 pte1p = pmap_pte1(pmap, va);
4566 pte1 = pte1_load(pte1p);
4568 if (mpt2pg && (mpt2pg->pindex == (pte1_idx & ~PT2PG_MASK))) {
4570 * Each of NPT2_IN_PG L2 page tables on the page can
4571 * come here. Make sure that associated L1 page table
4572 * link is established.
4574 * QQQ: It comes that we don't establish all links to
4575 * L2 page tables for newly allocated L2 page
4578 KASSERT(!pte1_is_section(pte1),
4579 ("%s: pte1 %#x is section", __func__, pte1));
4580 if (!pte1_is_link(pte1)) {
4581 pt2_pa = page_pt2pa(VM_PAGE_TO_PHYS(mpt2pg),
4583 pte1_store(pte1p, PTE1_LINK(pt2_pa));
4585 pt2_wirecount_inc(mpt2pg, pte1_idx);
4588 * If the L2 page table page is mapped, we just
4589 * increment the hold count, and activate it.
4591 if (pte1_is_section(pte1)) {
4593 } else if (pte1_is_link(pte1)) {
4594 mpt2pg = PHYS_TO_VM_PAGE(pte1_link_pa(pte1));
4595 pt2_wirecount_inc(mpt2pg, pte1_idx);
4597 mpt2pg = _pmap_allocpte2(pmap, va,
4598 PMAP_ENTER_NOSLEEP);
4608 * This call to pt2map_entry() makes the assumption that we are
4609 * entering the page into the current pmap. In order to support
4610 * quick entry into any pmap, one would likely use pmap_pte2_quick().
4611 * But that isn't as quick as pt2map_entry().
4613 pte2p = pt2map_entry(va);
4614 pte2 = pte2_load(pte2p);
4615 if (pte2_is_valid(pte2)) {
4616 if (mpt2pg != NULL) {
4618 * Remove extra pte2 reference
4620 pt2_wirecount_dec(mpt2pg, pte1_index(va));
4627 * Enter on the PV list if part of our managed memory.
4629 if ((m->oflags & VPO_UNMANAGED) == 0 &&
4630 !pmap_try_insert_pv_entry(pmap, va, m)) {
4631 if (mpt2pg != NULL) {
4633 if (pmap_unwire_pt2(pmap, va, mpt2pg, &free)) {
4634 pmap_tlb_flush(pmap, va);
4635 vm_page_free_pages_toq(&free, false);
4644 * Increment counters
4646 pmap->pm_stats.resident_count++;
4649 * Now validate mapping with RO protection
4651 pa = VM_PAGE_TO_PHYS(m);
4652 l2prot = PTE2_RO | PTE2_NM;
4653 if (va < VM_MAXUSER_ADDRESS)
4654 l2prot |= PTE2_U | PTE2_NG;
4655 if ((prot & VM_PROT_EXECUTE) == 0)
4657 else if (m->md.pat_mode == VM_MEMATTR_WB_WA && pmap != kernel_pmap) {
4659 * Sync icache if exec permission and attribute VM_MEMATTR_WB_WA
4660 * is set. QQQ: For more info, see comments in pmap_enter().
4662 cache_icache_sync_fresh(va, pa, PAGE_SIZE);
4664 pte2_store(pte2p, PTE2(pa, l2prot, vm_page_pte2_attr(m)));
4670 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot)
4673 rw_wlock(&pvh_global_lock);
4675 (void)pmap_enter_quick_locked(pmap, va, m, prot, NULL);
4676 rw_wunlock(&pvh_global_lock);
4681 * Tries to create a read- and/or execute-only 1 MB page mapping. Returns
4682 * true if successful. Returns false if (1) a mapping already exists at the
4683 * specified virtual address or (2) a PV entry cannot be allocated without
4684 * reclaiming another PV entry.
4687 pmap_enter_1mpage(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot)
4692 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
4693 pa = VM_PAGE_TO_PHYS(m);
4694 pte1 = PTE1(pa, PTE1_NM | PTE1_RO, ATTR_TO_L1(vm_page_pte2_attr(m)));
4695 if ((prot & VM_PROT_EXECUTE) == 0)
4697 if (va < VM_MAXUSER_ADDRESS)
4699 if (pmap != kernel_pmap)
4701 return (pmap_enter_pte1(pmap, va, pte1, PMAP_ENTER_NOSLEEP |
4702 PMAP_ENTER_NOREPLACE | PMAP_ENTER_NORECLAIM, m) == KERN_SUCCESS);
4706 * Tries to create the specified 1 MB page mapping. Returns KERN_SUCCESS if
4707 * the mapping was created, and either KERN_FAILURE or KERN_RESOURCE_SHORTAGE
4708 * otherwise. Returns KERN_FAILURE if PMAP_ENTER_NOREPLACE was specified and
4709 * a mapping already exists at the specified virtual address. Returns
4710 * KERN_RESOURCE_SHORTAGE if PMAP_ENTER_NORECLAIM was specified and PV entry
4711 * allocation failed.
4714 pmap_enter_pte1(pmap_t pmap, vm_offset_t va, pt1_entry_t pte1, u_int flags,
4717 struct spglist free;
4718 pt1_entry_t opte1, *pte1p;
4719 pt2_entry_t pte2, *pte2p;
4720 vm_offset_t cur, end;
4723 rw_assert(&pvh_global_lock, RA_WLOCKED);
4724 KASSERT((pte1 & (PTE1_NM | PTE1_RO)) == 0 ||
4725 (pte1 & (PTE1_NM | PTE1_RO)) == (PTE1_NM | PTE1_RO),
4726 ("%s: pte1 has inconsistent NM and RO attributes", __func__));
4727 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
4728 pte1p = pmap_pte1(pmap, va);
4729 opte1 = pte1_load(pte1p);
4730 if (pte1_is_valid(opte1)) {
4731 if ((flags & PMAP_ENTER_NOREPLACE) != 0) {
4732 CTR3(KTR_PMAP, "%s: failure for va %#lx in pmap %p",
4733 __func__, va, pmap);
4734 return (KERN_FAILURE);
4736 /* Break the existing mapping(s). */
4738 if (pte1_is_section(opte1)) {
4740 * If the section resulted from a promotion, then a
4741 * reserved PT page could be freed.
4743 pmap_remove_pte1(pmap, pte1p, va, &free);
4746 end = va + PTE1_SIZE;
4747 for (cur = va, pte2p = pmap_pte2_quick(pmap, va);
4748 cur != end; cur += PAGE_SIZE, pte2p++) {
4749 pte2 = pte2_load(pte2p);
4750 if (!pte2_is_valid(pte2))
4752 if (pmap_remove_pte2(pmap, pte2p, cur, &free))
4757 vm_page_free_pages_toq(&free, false);
4759 if ((m->oflags & VPO_UNMANAGED) == 0) {
4761 * Abort this mapping if its PV entry could not be created.
4763 if (!pmap_pv_insert_pte1(pmap, va, pte1, flags)) {
4764 CTR3(KTR_PMAP, "%s: failure for va %#lx in pmap %p",
4765 __func__, va, pmap);
4766 return (KERN_RESOURCE_SHORTAGE);
4768 if ((pte1 & PTE1_RO) == 0) {
4769 for (mt = m; mt < &m[PTE1_SIZE / PAGE_SIZE]; mt++)
4770 vm_page_aflag_set(mt, PGA_WRITEABLE);
4775 * Increment counters.
4777 if (pte1_is_wired(pte1))
4778 pmap->pm_stats.wired_count += PTE1_SIZE / PAGE_SIZE;
4779 pmap->pm_stats.resident_count += PTE1_SIZE / PAGE_SIZE;
4782 * Sync icache if exec permission and attribute VM_MEMATTR_WB_WA
4783 * is set. QQQ: For more info, see comments in pmap_enter().
4785 if ((pte1 & PTE1_NX) == 0 && m->md.pat_mode == VM_MEMATTR_WB_WA &&
4786 pmap != kernel_pmap && (!pte1_is_section(opte1) ||
4787 pte1_pa(opte1) != VM_PAGE_TO_PHYS(m) || (opte1 & PTE2_NX) != 0))
4788 cache_icache_sync_fresh(va, VM_PAGE_TO_PHYS(m), PTE1_SIZE);
4793 pte1_store(pte1p, pte1);
4795 pmap_pte1_mappings++;
4796 CTR3(KTR_PMAP, "%s: success for va %#lx in pmap %p", __func__, va,
4798 return (KERN_SUCCESS);
4802 * Maps a sequence of resident pages belonging to the same object.
4803 * The sequence begins with the given page m_start. This page is
4804 * mapped at the given virtual address start. Each subsequent page is
4805 * mapped at a virtual address that is offset from start by the same
4806 * amount as the page is offset from m_start within the object. The
4807 * last page in the sequence is the page with the largest offset from
4808 * m_start that can be mapped at a virtual address less than the given
4809 * virtual address end. Not every virtual page between start and end
4810 * is mapped; only those for which a resident page exists with the
4811 * corresponding offset from m_start are mapped.
4814 pmap_enter_object(pmap_t pmap, vm_offset_t start, vm_offset_t end,
4815 vm_page_t m_start, vm_prot_t prot)
4818 vm_page_t m, mpt2pg;
4819 vm_pindex_t diff, psize;
4821 PDEBUG(6, printf("%s: pmap %p start %#x end %#x m %p prot %#x\n",
4822 __func__, pmap, start, end, m_start, prot));
4824 VM_OBJECT_ASSERT_LOCKED(m_start->object);
4825 psize = atop(end - start);
4828 rw_wlock(&pvh_global_lock);
4830 while (m != NULL && (diff = m->pindex - m_start->pindex) < psize) {
4831 va = start + ptoa(diff);
4832 if ((va & PTE1_OFFSET) == 0 && va + PTE1_SIZE <= end &&
4833 m->psind == 1 && sp_enabled &&
4834 pmap_enter_1mpage(pmap, va, m, prot))
4835 m = &m[PTE1_SIZE / PAGE_SIZE - 1];
4837 mpt2pg = pmap_enter_quick_locked(pmap, va, m, prot,
4839 m = TAILQ_NEXT(m, listq);
4841 rw_wunlock(&pvh_global_lock);
4846 * This code maps large physical mmap regions into the
4847 * processor address space. Note that some shortcuts
4848 * are taken, but the code works.
4851 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr, vm_object_t object,
4852 vm_pindex_t pindex, vm_size_t size)
4855 vm_paddr_t pa, pte2_pa;
4857 vm_memattr_t pat_mode;
4858 u_int l1attr, l1prot;
4860 VM_OBJECT_ASSERT_WLOCKED(object);
4861 KASSERT(object->type == OBJT_DEVICE || object->type == OBJT_SG,
4862 ("%s: non-device object", __func__));
4863 if ((addr & PTE1_OFFSET) == 0 && (size & PTE1_OFFSET) == 0) {
4864 if (!vm_object_populate(object, pindex, pindex + atop(size)))
4866 p = vm_page_lookup(object, pindex);
4867 KASSERT(p->valid == VM_PAGE_BITS_ALL,
4868 ("%s: invalid page %p", __func__, p));
4869 pat_mode = p->md.pat_mode;
4872 * Abort the mapping if the first page is not physically
4873 * aligned to a 1MB page boundary.
4875 pte2_pa = VM_PAGE_TO_PHYS(p);
4876 if (pte2_pa & PTE1_OFFSET)
4880 * Skip the first page. Abort the mapping if the rest of
4881 * the pages are not physically contiguous or have differing
4882 * memory attributes.
4884 p = TAILQ_NEXT(p, listq);
4885 for (pa = pte2_pa + PAGE_SIZE; pa < pte2_pa + size;
4887 KASSERT(p->valid == VM_PAGE_BITS_ALL,
4888 ("%s: invalid page %p", __func__, p));
4889 if (pa != VM_PAGE_TO_PHYS(p) ||
4890 pat_mode != p->md.pat_mode)
4892 p = TAILQ_NEXT(p, listq);
4896 * Map using 1MB pages.
4898 * QQQ: Well, we are mapping a section, so same condition must
4899 * be hold like during promotion. It looks that only RW mapping
4900 * is done here, so readonly mapping must be done elsewhere.
4902 l1prot = PTE1_U | PTE1_NG | PTE1_RW | PTE1_M | PTE1_A;
4903 l1attr = ATTR_TO_L1(vm_memattr_to_pte2(pat_mode));
4905 for (pa = pte2_pa; pa < pte2_pa + size; pa += PTE1_SIZE) {
4906 pte1p = pmap_pte1(pmap, addr);
4907 if (!pte1_is_valid(pte1_load(pte1p))) {
4908 pte1_store(pte1p, PTE1(pa, l1prot, l1attr));
4909 pmap->pm_stats.resident_count += PTE1_SIZE /
4911 pmap_pte1_mappings++;
4913 /* Else continue on if the PTE1 is already valid. */
4921 * Do the things to protect a 1mpage in a process.
4924 pmap_protect_pte1(pmap_t pmap, pt1_entry_t *pte1p, vm_offset_t sva,
4927 pt1_entry_t npte1, opte1;
4928 vm_offset_t eva, va;
4931 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
4932 KASSERT((sva & PTE1_OFFSET) == 0,
4933 ("%s: sva is not 1mpage aligned", __func__));
4935 opte1 = npte1 = pte1_load(pte1p);
4936 if (pte1_is_managed(opte1) && pte1_is_dirty(opte1)) {
4937 eva = sva + PTE1_SIZE;
4938 for (va = sva, m = PHYS_TO_VM_PAGE(pte1_pa(opte1));
4939 va < eva; va += PAGE_SIZE, m++)
4942 if ((prot & VM_PROT_WRITE) == 0)
4943 npte1 |= PTE1_RO | PTE1_NM;
4944 if ((prot & VM_PROT_EXECUTE) == 0)
4948 * QQQ: Herein, execute permission is never set.
4949 * It only can be cleared. So, no icache
4950 * syncing is needed.
4953 if (npte1 != opte1) {
4954 pte1_store(pte1p, npte1);
4955 pmap_tlb_flush(pmap, sva);
4960 * Set the physical protection on the
4961 * specified range of this map as requested.
4964 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
4966 boolean_t pv_lists_locked;
4968 pt1_entry_t *pte1p, pte1;
4969 pt2_entry_t *pte2p, opte2, npte2;
4971 KASSERT((prot & ~VM_PROT_ALL) == 0, ("invalid prot %x", prot));
4972 if (prot == VM_PROT_NONE) {
4973 pmap_remove(pmap, sva, eva);
4977 if ((prot & (VM_PROT_WRITE | VM_PROT_EXECUTE)) ==
4978 (VM_PROT_WRITE | VM_PROT_EXECUTE))
4981 if (pmap_is_current(pmap))
4982 pv_lists_locked = FALSE;
4984 pv_lists_locked = TRUE;
4986 rw_wlock(&pvh_global_lock);
4991 for (; sva < eva; sva = nextva) {
4993 * Calculate address for next L2 page table.
4995 nextva = pte1_trunc(sva + PTE1_SIZE);
4999 pte1p = pmap_pte1(pmap, sva);
5000 pte1 = pte1_load(pte1p);
5003 * Weed out invalid mappings. Note: we assume that L1 page
5004 * page table is always allocated, and in kernel virtual.
5009 if (pte1_is_section(pte1)) {
5011 * Are we protecting the entire large page? If not,
5012 * demote the mapping and fall through.
5014 if (sva + PTE1_SIZE == nextva && eva >= nextva) {
5015 pmap_protect_pte1(pmap, pte1p, sva, prot);
5018 if (!pv_lists_locked) {
5019 pv_lists_locked = TRUE;
5020 if (!rw_try_wlock(&pvh_global_lock)) {
5026 if (!pmap_demote_pte1(pmap, pte1p, sva)) {
5028 * The large page mapping
5035 /* Update pte1 after demotion */
5036 pte1 = pte1_load(pte1p);
5042 KASSERT(pte1_is_link(pte1), ("%s: pmap %p va %#x pte1 %#x at %p"
5043 " is not link", __func__, pmap, sva, pte1, pte1p));
5046 * Limit our scan to either the end of the va represented
5047 * by the current L2 page table page, or to the end of the
5048 * range being protected.
5053 for (pte2p = pmap_pte2_quick(pmap, sva); sva != nextva; pte2p++,
5057 opte2 = npte2 = pte2_load(pte2p);
5058 if (!pte2_is_valid(opte2))
5061 if ((prot & VM_PROT_WRITE) == 0) {
5062 if (pte2_is_managed(opte2) &&
5063 pte2_is_dirty(opte2)) {
5064 m = PHYS_TO_VM_PAGE(pte2_pa(opte2));
5067 npte2 |= PTE2_RO | PTE2_NM;
5070 if ((prot & VM_PROT_EXECUTE) == 0)
5074 * QQQ: Herein, execute permission is never set.
5075 * It only can be cleared. So, no icache
5076 * syncing is needed.
5079 if (npte2 != opte2) {
5080 pte2_store(pte2p, npte2);
5081 pmap_tlb_flush(pmap, sva);
5085 if (pv_lists_locked) {
5087 rw_wunlock(&pvh_global_lock);
5093 * pmap_pvh_wired_mappings:
5095 * Return the updated number "count" of managed mappings that are wired.
5098 pmap_pvh_wired_mappings(struct md_page *pvh, int count)
5105 rw_assert(&pvh_global_lock, RA_WLOCKED);
5107 TAILQ_FOREACH(pv, &pvh->pv_list, pv_next) {
5110 pte1 = pte1_load(pmap_pte1(pmap, pv->pv_va));
5111 if (pte1_is_section(pte1)) {
5112 if (pte1_is_wired(pte1))
5115 KASSERT(pte1_is_link(pte1),
5116 ("%s: pte1 %#x is not link", __func__, pte1));
5117 pte2 = pte2_load(pmap_pte2_quick(pmap, pv->pv_va));
5118 if (pte2_is_wired(pte2))
5128 * pmap_page_wired_mappings:
5130 * Return the number of managed mappings to the given physical page
5134 pmap_page_wired_mappings(vm_page_t m)
5139 if ((m->oflags & VPO_UNMANAGED) != 0)
5141 rw_wlock(&pvh_global_lock);
5142 count = pmap_pvh_wired_mappings(&m->md, count);
5143 if ((m->flags & PG_FICTITIOUS) == 0) {
5144 count = pmap_pvh_wired_mappings(pa_to_pvh(VM_PAGE_TO_PHYS(m)),
5147 rw_wunlock(&pvh_global_lock);
5152 * Returns TRUE if any of the given mappings were used to modify
5153 * physical memory. Otherwise, returns FALSE. Both page and 1mpage
5154 * mappings are supported.
5157 pmap_is_modified_pvh(struct md_page *pvh)
5165 rw_assert(&pvh_global_lock, RA_WLOCKED);
5168 TAILQ_FOREACH(pv, &pvh->pv_list, pv_next) {
5171 pte1 = pte1_load(pmap_pte1(pmap, pv->pv_va));
5172 if (pte1_is_section(pte1)) {
5173 rv = pte1_is_dirty(pte1);
5175 KASSERT(pte1_is_link(pte1),
5176 ("%s: pte1 %#x is not link", __func__, pte1));
5177 pte2 = pte2_load(pmap_pte2_quick(pmap, pv->pv_va));
5178 rv = pte2_is_dirty(pte2);
5191 * Return whether or not the specified physical page was modified
5192 * in any physical maps.
5195 pmap_is_modified(vm_page_t m)
5199 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
5200 ("%s: page %p is not managed", __func__, m));
5203 * If the page is not exclusive busied, then PGA_WRITEABLE cannot be
5204 * concurrently set while the object is locked. Thus, if PGA_WRITEABLE
5205 * is clear, no PTE2s can have PG_M set.
5207 VM_OBJECT_ASSERT_WLOCKED(m->object);
5208 if (!vm_page_xbusied(m) && (m->aflags & PGA_WRITEABLE) == 0)
5210 rw_wlock(&pvh_global_lock);
5211 rv = pmap_is_modified_pvh(&m->md) ||
5212 ((m->flags & PG_FICTITIOUS) == 0 &&
5213 pmap_is_modified_pvh(pa_to_pvh(VM_PAGE_TO_PHYS(m))));
5214 rw_wunlock(&pvh_global_lock);
5219 * pmap_is_prefaultable:
5221 * Return whether or not the specified virtual address is eligible
5225 pmap_is_prefaultable(pmap_t pmap, vm_offset_t addr)
5233 pte1 = pte1_load(pmap_pte1(pmap, addr));
5234 if (pte1_is_link(pte1)) {
5235 pte2 = pte2_load(pt2map_entry(addr));
5236 rv = !pte2_is_valid(pte2) ;
5243 * Returns TRUE if any of the given mappings were referenced and FALSE
5244 * otherwise. Both page and 1mpage mappings are supported.
5247 pmap_is_referenced_pvh(struct md_page *pvh)
5256 rw_assert(&pvh_global_lock, RA_WLOCKED);
5259 TAILQ_FOREACH(pv, &pvh->pv_list, pv_next) {
5262 pte1 = pte1_load(pmap_pte1(pmap, pv->pv_va));
5263 if (pte1_is_section(pte1)) {
5264 rv = (pte1 & (PTE1_A | PTE1_V)) == (PTE1_A | PTE1_V);
5266 pte2 = pte2_load(pmap_pte2_quick(pmap, pv->pv_va));
5267 rv = (pte2 & (PTE2_A | PTE2_V)) == (PTE2_A | PTE2_V);
5278 * pmap_is_referenced:
5280 * Return whether or not the specified physical page was referenced
5281 * in any physical maps.
5284 pmap_is_referenced(vm_page_t m)
5288 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
5289 ("%s: page %p is not managed", __func__, m));
5290 rw_wlock(&pvh_global_lock);
5291 rv = pmap_is_referenced_pvh(&m->md) ||
5292 ((m->flags & PG_FICTITIOUS) == 0 &&
5293 pmap_is_referenced_pvh(pa_to_pvh(VM_PAGE_TO_PHYS(m))));
5294 rw_wunlock(&pvh_global_lock);
5299 * pmap_ts_referenced:
5301 * Return a count of reference bits for a page, clearing those bits.
5302 * It is not necessary for every reference bit to be cleared, but it
5303 * is necessary that 0 only be returned when there are truly no
5304 * reference bits set.
5306 * As an optimization, update the page's dirty field if a modified bit is
5307 * found while counting reference bits. This opportunistic update can be
5308 * performed at low cost and can eliminate the need for some future calls
5309 * to pmap_is_modified(). However, since this function stops after
5310 * finding PMAP_TS_REFERENCED_MAX reference bits, it may not detect some
5311 * dirty pages. Those dirty pages will only be detected by a future call
5312 * to pmap_is_modified().
5315 pmap_ts_referenced(vm_page_t m)
5317 struct md_page *pvh;
5320 pt1_entry_t *pte1p, opte1;
5321 pt2_entry_t *pte2p, opte2;
5325 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
5326 ("%s: page %p is not managed", __func__, m));
5327 pa = VM_PAGE_TO_PHYS(m);
5328 pvh = pa_to_pvh(pa);
5329 rw_wlock(&pvh_global_lock);
5331 if ((m->flags & PG_FICTITIOUS) != 0 ||
5332 (pvf = TAILQ_FIRST(&pvh->pv_list)) == NULL)
5333 goto small_mappings;
5338 pte1p = pmap_pte1(pmap, pv->pv_va);
5339 opte1 = pte1_load(pte1p);
5340 if (pte1_is_dirty(opte1)) {
5342 * Although "opte1" is mapping a 1MB page, because
5343 * this function is called at a 4KB page granularity,
5344 * we only update the 4KB page under test.
5348 if ((opte1 & PTE1_A) != 0) {
5350 * Since this reference bit is shared by 256 4KB pages,
5351 * it should not be cleared every time it is tested.
5352 * Apply a simple "hash" function on the physical page
5353 * number, the virtual section number, and the pmap
5354 * address to select one 4KB page out of the 256
5355 * on which testing the reference bit will result
5356 * in clearing that bit. This function is designed
5357 * to avoid the selection of the same 4KB page
5358 * for every 1MB page mapping.
5360 * On demotion, a mapping that hasn't been referenced
5361 * is simply destroyed. To avoid the possibility of a
5362 * subsequent page fault on a demoted wired mapping,
5363 * always leave its reference bit set. Moreover,
5364 * since the section is wired, the current state of
5365 * its reference bit won't affect page replacement.
5367 if ((((pa >> PAGE_SHIFT) ^ (pv->pv_va >> PTE1_SHIFT) ^
5368 (uintptr_t)pmap) & (NPTE2_IN_PG - 1)) == 0 &&
5369 !pte1_is_wired(opte1)) {
5370 pte1_clear_bit(pte1p, PTE1_A);
5371 pmap_tlb_flush(pmap, pv->pv_va);
5376 /* Rotate the PV list if it has more than one entry. */
5377 if (TAILQ_NEXT(pv, pv_next) != NULL) {
5378 TAILQ_REMOVE(&pvh->pv_list, pv, pv_next);
5379 TAILQ_INSERT_TAIL(&pvh->pv_list, pv, pv_next);
5381 if (rtval >= PMAP_TS_REFERENCED_MAX)
5383 } while ((pv = TAILQ_FIRST(&pvh->pv_list)) != pvf);
5385 if ((pvf = TAILQ_FIRST(&m->md.pv_list)) == NULL)
5391 pte1p = pmap_pte1(pmap, pv->pv_va);
5392 KASSERT(pte1_is_link(pte1_load(pte1p)),
5393 ("%s: not found a link in page %p's pv list", __func__, m));
5395 pte2p = pmap_pte2_quick(pmap, pv->pv_va);
5396 opte2 = pte2_load(pte2p);
5397 if (pte2_is_dirty(opte2))
5399 if ((opte2 & PTE2_A) != 0) {
5400 pte2_clear_bit(pte2p, PTE2_A);
5401 pmap_tlb_flush(pmap, pv->pv_va);
5405 /* Rotate the PV list if it has more than one entry. */
5406 if (TAILQ_NEXT(pv, pv_next) != NULL) {
5407 TAILQ_REMOVE(&m->md.pv_list, pv, pv_next);
5408 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_next);
5410 } while ((pv = TAILQ_FIRST(&m->md.pv_list)) != pvf && rtval <
5411 PMAP_TS_REFERENCED_MAX);
5414 rw_wunlock(&pvh_global_lock);
5419 * Clear the wired attribute from the mappings for the specified range of
5420 * addresses in the given pmap. Every valid mapping within that range
5421 * must have the wired attribute set. In contrast, invalid mappings
5422 * cannot have the wired attribute set, so they are ignored.
5424 * The wired attribute of the page table entry is not a hardware feature,
5425 * so there is no need to invalidate any TLB entries.
5428 pmap_unwire(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
5431 pt1_entry_t *pte1p, pte1;
5432 pt2_entry_t *pte2p, pte2;
5433 boolean_t pv_lists_locked;
5435 if (pmap_is_current(pmap))
5436 pv_lists_locked = FALSE;
5438 pv_lists_locked = TRUE;
5440 rw_wlock(&pvh_global_lock);
5444 for (; sva < eva; sva = nextva) {
5445 nextva = pte1_trunc(sva + PTE1_SIZE);
5449 pte1p = pmap_pte1(pmap, sva);
5450 pte1 = pte1_load(pte1p);
5453 * Weed out invalid mappings. Note: we assume that L1 page
5454 * page table is always allocated, and in kernel virtual.
5459 if (pte1_is_section(pte1)) {
5460 if (!pte1_is_wired(pte1))
5461 panic("%s: pte1 %#x not wired", __func__, pte1);
5464 * Are we unwiring the entire large page? If not,
5465 * demote the mapping and fall through.
5467 if (sva + PTE1_SIZE == nextva && eva >= nextva) {
5468 pte1_clear_bit(pte1p, PTE1_W);
5469 pmap->pm_stats.wired_count -= PTE1_SIZE /
5473 if (!pv_lists_locked) {
5474 pv_lists_locked = TRUE;
5475 if (!rw_try_wlock(&pvh_global_lock)) {
5482 if (!pmap_demote_pte1(pmap, pte1p, sva))
5483 panic("%s: demotion failed", __func__);
5486 /* Update pte1 after demotion */
5487 pte1 = pte1_load(pte1p);
5493 KASSERT(pte1_is_link(pte1), ("%s: pmap %p va %#x pte1 %#x at %p"
5494 " is not link", __func__, pmap, sva, pte1, pte1p));
5497 * Limit our scan to either the end of the va represented
5498 * by the current L2 page table page, or to the end of the
5499 * range being protected.
5504 for (pte2p = pmap_pte2_quick(pmap, sva); sva != nextva; pte2p++,
5506 pte2 = pte2_load(pte2p);
5507 if (!pte2_is_valid(pte2))
5509 if (!pte2_is_wired(pte2))
5510 panic("%s: pte2 %#x is missing PTE2_W",
5514 * PTE2_W must be cleared atomically. Although the pmap
5515 * lock synchronizes access to PTE2_W, another processor
5516 * could be changing PTE2_NM and/or PTE2_A concurrently.
5518 pte2_clear_bit(pte2p, PTE2_W);
5519 pmap->pm_stats.wired_count--;
5522 if (pv_lists_locked) {
5524 rw_wunlock(&pvh_global_lock);
5530 * Clear the write and modified bits in each of the given page's mappings.
5533 pmap_remove_write(vm_page_t m)
5535 struct md_page *pvh;
5536 pv_entry_t next_pv, pv;
5539 pt2_entry_t *pte2p, opte2;
5542 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
5543 ("%s: page %p is not managed", __func__, m));
5546 * If the page is not exclusive busied, then PGA_WRITEABLE cannot be
5547 * set by another thread while the object is locked. Thus,
5548 * if PGA_WRITEABLE is clear, no page table entries need updating.
5550 VM_OBJECT_ASSERT_WLOCKED(m->object);
5551 if (!vm_page_xbusied(m) && (m->aflags & PGA_WRITEABLE) == 0)
5553 rw_wlock(&pvh_global_lock);
5555 if ((m->flags & PG_FICTITIOUS) != 0)
5556 goto small_mappings;
5557 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
5558 TAILQ_FOREACH_SAFE(pv, &pvh->pv_list, pv_next, next_pv) {
5562 pte1p = pmap_pte1(pmap, va);
5563 if (!(pte1_load(pte1p) & PTE1_RO))
5564 (void)pmap_demote_pte1(pmap, pte1p, va);
5568 TAILQ_FOREACH(pv, &m->md.pv_list, pv_next) {
5571 pte1p = pmap_pte1(pmap, pv->pv_va);
5572 KASSERT(!pte1_is_section(pte1_load(pte1p)), ("%s: found"
5573 " a section in page %p's pv list", __func__, m));
5574 pte2p = pmap_pte2_quick(pmap, pv->pv_va);
5575 opte2 = pte2_load(pte2p);
5576 if (!(opte2 & PTE2_RO)) {
5577 pte2_store(pte2p, opte2 | PTE2_RO | PTE2_NM);
5578 if (pte2_is_dirty(opte2))
5580 pmap_tlb_flush(pmap, pv->pv_va);
5584 vm_page_aflag_clear(m, PGA_WRITEABLE);
5586 rw_wunlock(&pvh_global_lock);
5590 * Apply the given advice to the specified range of addresses within the
5591 * given pmap. Depending on the advice, clear the referenced and/or
5592 * modified flags in each mapping and set the mapped page's dirty field.
5595 pmap_advise(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, int advice)
5597 pt1_entry_t *pte1p, opte1;
5598 pt2_entry_t *pte2p, pte2;
5601 boolean_t pv_lists_locked;
5603 if (advice != MADV_DONTNEED && advice != MADV_FREE)
5605 if (pmap_is_current(pmap))
5606 pv_lists_locked = FALSE;
5608 pv_lists_locked = TRUE;
5610 rw_wlock(&pvh_global_lock);
5614 for (; sva < eva; sva = pdnxt) {
5615 pdnxt = pte1_trunc(sva + PTE1_SIZE);
5618 pte1p = pmap_pte1(pmap, sva);
5619 opte1 = pte1_load(pte1p);
5620 if (!pte1_is_valid(opte1)) /* XXX */
5622 else if (pte1_is_section(opte1)) {
5623 if (!pte1_is_managed(opte1))
5625 if (!pv_lists_locked) {
5626 pv_lists_locked = TRUE;
5627 if (!rw_try_wlock(&pvh_global_lock)) {
5633 if (!pmap_demote_pte1(pmap, pte1p, sva)) {
5635 * The large page mapping was destroyed.
5641 * Unless the page mappings are wired, remove the
5642 * mapping to a single page so that a subsequent
5643 * access may repromote. Since the underlying L2 page
5644 * table is fully populated, this removal never
5645 * frees a L2 page table page.
5647 if (!pte1_is_wired(opte1)) {
5648 pte2p = pmap_pte2_quick(pmap, sva);
5649 KASSERT(pte2_is_valid(pte2_load(pte2p)),
5650 ("%s: invalid PTE2", __func__));
5651 pmap_remove_pte2(pmap, pte2p, sva, NULL);
5656 for (pte2p = pmap_pte2_quick(pmap, sva); sva != pdnxt; pte2p++,
5658 pte2 = pte2_load(pte2p);
5659 if (!pte2_is_valid(pte2) || !pte2_is_managed(pte2))
5661 else if (pte2_is_dirty(pte2)) {
5662 if (advice == MADV_DONTNEED) {
5664 * Future calls to pmap_is_modified()
5665 * can be avoided by making the page
5668 m = PHYS_TO_VM_PAGE(pte2_pa(pte2));
5671 pte2_set_bit(pte2p, PTE2_NM);
5672 pte2_clear_bit(pte2p, PTE2_A);
5673 } else if ((pte2 & PTE2_A) != 0)
5674 pte2_clear_bit(pte2p, PTE2_A);
5677 pmap_tlb_flush(pmap, sva);
5680 if (pv_lists_locked) {
5682 rw_wunlock(&pvh_global_lock);
5688 * Clear the modify bits on the specified physical page.
5691 pmap_clear_modify(vm_page_t m)
5693 struct md_page *pvh;
5694 pv_entry_t next_pv, pv;
5696 pt1_entry_t *pte1p, opte1;
5697 pt2_entry_t *pte2p, opte2;
5700 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
5701 ("%s: page %p is not managed", __func__, m));
5702 VM_OBJECT_ASSERT_WLOCKED(m->object);
5703 KASSERT(!vm_page_xbusied(m),
5704 ("%s: page %p is exclusive busy", __func__, m));
5707 * If the page is not PGA_WRITEABLE, then no PTE2s can have PTE2_NM
5708 * cleared. If the object containing the page is locked and the page
5709 * is not exclusive busied, then PGA_WRITEABLE cannot be concurrently
5712 if ((m->flags & PGA_WRITEABLE) == 0)
5714 rw_wlock(&pvh_global_lock);
5716 if ((m->flags & PG_FICTITIOUS) != 0)
5717 goto small_mappings;
5718 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
5719 TAILQ_FOREACH_SAFE(pv, &pvh->pv_list, pv_next, next_pv) {
5723 pte1p = pmap_pte1(pmap, va);
5724 opte1 = pte1_load(pte1p);
5725 if (!(opte1 & PTE1_RO)) {
5726 if (pmap_demote_pte1(pmap, pte1p, va) &&
5727 !pte1_is_wired(opte1)) {
5729 * Write protect the mapping to a
5730 * single page so that a subsequent
5731 * write access may repromote.
5733 va += VM_PAGE_TO_PHYS(m) - pte1_pa(opte1);
5734 pte2p = pmap_pte2_quick(pmap, va);
5735 opte2 = pte2_load(pte2p);
5736 if ((opte2 & PTE2_V)) {
5737 pte2_set_bit(pte2p, PTE2_NM | PTE2_RO);
5739 pmap_tlb_flush(pmap, va);
5746 TAILQ_FOREACH(pv, &m->md.pv_list, pv_next) {
5749 pte1p = pmap_pte1(pmap, pv->pv_va);
5750 KASSERT(!pte1_is_section(pte1_load(pte1p)), ("%s: found"
5751 " a section in page %p's pv list", __func__, m));
5752 pte2p = pmap_pte2_quick(pmap, pv->pv_va);
5753 if (pte2_is_dirty(pte2_load(pte2p))) {
5754 pte2_set_bit(pte2p, PTE2_NM);
5755 pmap_tlb_flush(pmap, pv->pv_va);
5760 rw_wunlock(&pvh_global_lock);
5765 * Sets the memory attribute for the specified page.
5768 pmap_page_set_memattr(vm_page_t m, vm_memattr_t ma)
5770 pt2_entry_t *cmap2_pte2p;
5775 oma = m->md.pat_mode;
5776 m->md.pat_mode = ma;
5778 CTR5(KTR_PMAP, "%s: page %p - 0x%08X oma: %d, ma: %d", __func__, m,
5779 VM_PAGE_TO_PHYS(m), oma, ma);
5780 if ((m->flags & PG_FICTITIOUS) != 0)
5784 * If "m" is a normal page, flush it from the cache.
5786 * First, try to find an existing mapping of the page by sf
5787 * buffer. sf_buf_invalidate_cache() modifies mapping and
5788 * flushes the cache.
5790 if (sf_buf_invalidate_cache(m, oma))
5794 * If page is not mapped by sf buffer, map the page
5795 * transient and do invalidation.
5798 pa = VM_PAGE_TO_PHYS(m);
5801 cmap2_pte2p = pc->pc_cmap2_pte2p;
5802 mtx_lock(&pc->pc_cmap_lock);
5803 if (pte2_load(cmap2_pte2p) != 0)
5804 panic("%s: CMAP2 busy", __func__);
5805 pte2_store(cmap2_pte2p, PTE2_KERN_NG(pa, PTE2_AP_KRW,
5806 vm_memattr_to_pte2(ma)));
5807 dcache_wbinv_poc((vm_offset_t)pc->pc_cmap2_addr, pa, PAGE_SIZE);
5808 pte2_clear(cmap2_pte2p);
5809 tlb_flush((vm_offset_t)pc->pc_cmap2_addr);
5811 mtx_unlock(&pc->pc_cmap_lock);
5816 * Miscellaneous support routines follow
5820 * Returns TRUE if the given page is mapped individually or as part of
5821 * a 1mpage. Otherwise, returns FALSE.
5824 pmap_page_is_mapped(vm_page_t m)
5828 if ((m->oflags & VPO_UNMANAGED) != 0)
5830 rw_wlock(&pvh_global_lock);
5831 rv = !TAILQ_EMPTY(&m->md.pv_list) ||
5832 ((m->flags & PG_FICTITIOUS) == 0 &&
5833 !TAILQ_EMPTY(&pa_to_pvh(VM_PAGE_TO_PHYS(m))->pv_list));
5834 rw_wunlock(&pvh_global_lock);
5839 * Returns true if the pmap's pv is one of the first
5840 * 16 pvs linked to from this page. This count may
5841 * be changed upwards or downwards in the future; it
5842 * is only necessary that true be returned for a small
5843 * subset of pmaps for proper page aging.
5846 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
5848 struct md_page *pvh;
5853 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
5854 ("%s: page %p is not managed", __func__, m));
5856 rw_wlock(&pvh_global_lock);
5857 TAILQ_FOREACH(pv, &m->md.pv_list, pv_next) {
5858 if (PV_PMAP(pv) == pmap) {
5866 if (!rv && loops < 16 && (m->flags & PG_FICTITIOUS) == 0) {
5867 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
5868 TAILQ_FOREACH(pv, &pvh->pv_list, pv_next) {
5869 if (PV_PMAP(pv) == pmap) {
5878 rw_wunlock(&pvh_global_lock);
5883 * pmap_zero_page zeros the specified hardware page by mapping
5884 * the page into KVM and using bzero to clear its contents.
5887 pmap_zero_page(vm_page_t m)
5889 pt2_entry_t *cmap2_pte2p;
5894 cmap2_pte2p = pc->pc_cmap2_pte2p;
5895 mtx_lock(&pc->pc_cmap_lock);
5896 if (pte2_load(cmap2_pte2p) != 0)
5897 panic("%s: CMAP2 busy", __func__);
5898 pte2_store(cmap2_pte2p, PTE2_KERN_NG(VM_PAGE_TO_PHYS(m), PTE2_AP_KRW,
5899 vm_page_pte2_attr(m)));
5900 pagezero(pc->pc_cmap2_addr);
5901 pte2_clear(cmap2_pte2p);
5902 tlb_flush((vm_offset_t)pc->pc_cmap2_addr);
5904 mtx_unlock(&pc->pc_cmap_lock);
5908 * pmap_zero_page_area zeros the specified hardware page by mapping
5909 * the page into KVM and using bzero to clear its contents.
5911 * off and size may not cover an area beyond a single hardware page.
5914 pmap_zero_page_area(vm_page_t m, int off, int size)
5916 pt2_entry_t *cmap2_pte2p;
5921 cmap2_pte2p = pc->pc_cmap2_pte2p;
5922 mtx_lock(&pc->pc_cmap_lock);
5923 if (pte2_load(cmap2_pte2p) != 0)
5924 panic("%s: CMAP2 busy", __func__);
5925 pte2_store(cmap2_pte2p, PTE2_KERN_NG(VM_PAGE_TO_PHYS(m), PTE2_AP_KRW,
5926 vm_page_pte2_attr(m)));
5927 if (off == 0 && size == PAGE_SIZE)
5928 pagezero(pc->pc_cmap2_addr);
5930 bzero(pc->pc_cmap2_addr + off, size);
5931 pte2_clear(cmap2_pte2p);
5932 tlb_flush((vm_offset_t)pc->pc_cmap2_addr);
5934 mtx_unlock(&pc->pc_cmap_lock);
5938 * pmap_copy_page copies the specified (machine independent)
5939 * page by mapping the page into virtual memory and using
5940 * bcopy to copy the page, one machine dependent page at a
5944 pmap_copy_page(vm_page_t src, vm_page_t dst)
5946 pt2_entry_t *cmap1_pte2p, *cmap2_pte2p;
5951 cmap1_pte2p = pc->pc_cmap1_pte2p;
5952 cmap2_pte2p = pc->pc_cmap2_pte2p;
5953 mtx_lock(&pc->pc_cmap_lock);
5954 if (pte2_load(cmap1_pte2p) != 0)
5955 panic("%s: CMAP1 busy", __func__);
5956 if (pte2_load(cmap2_pte2p) != 0)
5957 panic("%s: CMAP2 busy", __func__);
5958 pte2_store(cmap1_pte2p, PTE2_KERN_NG(VM_PAGE_TO_PHYS(src),
5959 PTE2_AP_KR | PTE2_NM, vm_page_pte2_attr(src)));
5960 pte2_store(cmap2_pte2p, PTE2_KERN_NG(VM_PAGE_TO_PHYS(dst),
5961 PTE2_AP_KRW, vm_page_pte2_attr(dst)));
5962 bcopy(pc->pc_cmap1_addr, pc->pc_cmap2_addr, PAGE_SIZE);
5963 pte2_clear(cmap1_pte2p);
5964 tlb_flush((vm_offset_t)pc->pc_cmap1_addr);
5965 pte2_clear(cmap2_pte2p);
5966 tlb_flush((vm_offset_t)pc->pc_cmap2_addr);
5968 mtx_unlock(&pc->pc_cmap_lock);
5971 int unmapped_buf_allowed = 1;
5974 pmap_copy_pages(vm_page_t ma[], vm_offset_t a_offset, vm_page_t mb[],
5975 vm_offset_t b_offset, int xfersize)
5977 pt2_entry_t *cmap1_pte2p, *cmap2_pte2p;
5978 vm_page_t a_pg, b_pg;
5980 vm_offset_t a_pg_offset, b_pg_offset;
5986 cmap1_pte2p = pc->pc_cmap1_pte2p;
5987 cmap2_pte2p = pc->pc_cmap2_pte2p;
5988 mtx_lock(&pc->pc_cmap_lock);
5989 if (pte2_load(cmap1_pte2p) != 0)
5990 panic("pmap_copy_pages: CMAP1 busy");
5991 if (pte2_load(cmap2_pte2p) != 0)
5992 panic("pmap_copy_pages: CMAP2 busy");
5993 while (xfersize > 0) {
5994 a_pg = ma[a_offset >> PAGE_SHIFT];
5995 a_pg_offset = a_offset & PAGE_MASK;
5996 cnt = min(xfersize, PAGE_SIZE - a_pg_offset);
5997 b_pg = mb[b_offset >> PAGE_SHIFT];
5998 b_pg_offset = b_offset & PAGE_MASK;
5999 cnt = min(cnt, PAGE_SIZE - b_pg_offset);
6000 pte2_store(cmap1_pte2p, PTE2_KERN_NG(VM_PAGE_TO_PHYS(a_pg),
6001 PTE2_AP_KR | PTE2_NM, vm_page_pte2_attr(a_pg)));
6002 tlb_flush_local((vm_offset_t)pc->pc_cmap1_addr);
6003 pte2_store(cmap2_pte2p, PTE2_KERN_NG(VM_PAGE_TO_PHYS(b_pg),
6004 PTE2_AP_KRW, vm_page_pte2_attr(b_pg)));
6005 tlb_flush_local((vm_offset_t)pc->pc_cmap2_addr);
6006 a_cp = pc->pc_cmap1_addr + a_pg_offset;
6007 b_cp = pc->pc_cmap2_addr + b_pg_offset;
6008 bcopy(a_cp, b_cp, cnt);
6013 pte2_clear(cmap1_pte2p);
6014 tlb_flush((vm_offset_t)pc->pc_cmap1_addr);
6015 pte2_clear(cmap2_pte2p);
6016 tlb_flush((vm_offset_t)pc->pc_cmap2_addr);
6018 mtx_unlock(&pc->pc_cmap_lock);
6022 pmap_quick_enter_page(vm_page_t m)
6029 pte2p = pc->pc_qmap_pte2p;
6031 KASSERT(pte2_load(pte2p) == 0, ("%s: PTE2 busy", __func__));
6033 pte2_store(pte2p, PTE2_KERN_NG(VM_PAGE_TO_PHYS(m), PTE2_AP_KRW,
6034 vm_page_pte2_attr(m)));
6035 return (pc->pc_qmap_addr);
6039 pmap_quick_remove_page(vm_offset_t addr)
6045 pte2p = pc->pc_qmap_pte2p;
6047 KASSERT(addr == pc->pc_qmap_addr, ("%s: invalid address", __func__));
6048 KASSERT(pte2_load(pte2p) != 0, ("%s: PTE2 not in use", __func__));
6051 tlb_flush(pc->pc_qmap_addr);
6056 * Copy the range specified by src_addr/len
6057 * from the source map to the range dst_addr/len
6058 * in the destination map.
6060 * This routine is only advisory and need not do anything.
6063 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr, vm_size_t len,
6064 vm_offset_t src_addr)
6066 struct spglist free;
6068 vm_offset_t end_addr = src_addr + len;
6071 if (dst_addr != src_addr)
6074 if (!pmap_is_current(src_pmap))
6077 rw_wlock(&pvh_global_lock);
6078 if (dst_pmap < src_pmap) {
6079 PMAP_LOCK(dst_pmap);
6080 PMAP_LOCK(src_pmap);
6082 PMAP_LOCK(src_pmap);
6083 PMAP_LOCK(dst_pmap);
6086 for (addr = src_addr; addr < end_addr; addr = nextva) {
6087 pt2_entry_t *src_pte2p, *dst_pte2p;
6088 vm_page_t dst_mpt2pg, src_mpt2pg;
6089 pt1_entry_t src_pte1;
6092 KASSERT(addr < VM_MAXUSER_ADDRESS,
6093 ("%s: invalid to pmap_copy page tables", __func__));
6095 nextva = pte1_trunc(addr + PTE1_SIZE);
6099 pte1_idx = pte1_index(addr);
6100 src_pte1 = src_pmap->pm_pt1[pte1_idx];
6101 if (pte1_is_section(src_pte1)) {
6102 if ((addr & PTE1_OFFSET) != 0 ||
6103 (addr + PTE1_SIZE) > end_addr)
6105 if (dst_pmap->pm_pt1[pte1_idx] == 0 &&
6106 (!pte1_is_managed(src_pte1) ||
6107 pmap_pv_insert_pte1(dst_pmap, addr, src_pte1,
6108 PMAP_ENTER_NORECLAIM))) {
6109 dst_pmap->pm_pt1[pte1_idx] = src_pte1 &
6111 dst_pmap->pm_stats.resident_count +=
6112 PTE1_SIZE / PAGE_SIZE;
6113 pmap_pte1_mappings++;
6116 } else if (!pte1_is_link(src_pte1))
6119 src_mpt2pg = PHYS_TO_VM_PAGE(pte1_link_pa(src_pte1));
6122 * We leave PT2s to be linked from PT1 even if they are not
6123 * referenced until all PT2s in a page are without reference.
6125 * QQQ: It could be changed ...
6127 #if 0 /* single_pt2_link_is_cleared */
6128 KASSERT(pt2_wirecount_get(src_mpt2pg, pte1_idx) > 0,
6129 ("%s: source page table page is unused", __func__));
6131 if (pt2_wirecount_get(src_mpt2pg, pte1_idx) == 0)
6134 if (nextva > end_addr)
6137 src_pte2p = pt2map_entry(addr);
6138 while (addr < nextva) {
6139 pt2_entry_t temp_pte2;
6140 temp_pte2 = pte2_load(src_pte2p);
6142 * we only virtual copy managed pages
6144 if (pte2_is_managed(temp_pte2)) {
6145 dst_mpt2pg = pmap_allocpte2(dst_pmap, addr,
6146 PMAP_ENTER_NOSLEEP);
6147 if (dst_mpt2pg == NULL)
6149 dst_pte2p = pmap_pte2_quick(dst_pmap, addr);
6150 if (!pte2_is_valid(pte2_load(dst_pte2p)) &&
6151 pmap_try_insert_pv_entry(dst_pmap, addr,
6152 PHYS_TO_VM_PAGE(pte2_pa(temp_pte2)))) {
6154 * Clear the wired, modified, and
6155 * accessed (referenced) bits
6158 temp_pte2 &= ~(PTE2_W | PTE2_A);
6159 temp_pte2 |= PTE2_NM;
6160 pte2_store(dst_pte2p, temp_pte2);
6161 dst_pmap->pm_stats.resident_count++;
6164 if (pmap_unwire_pt2(dst_pmap, addr,
6165 dst_mpt2pg, &free)) {
6166 pmap_tlb_flush(dst_pmap, addr);
6167 vm_page_free_pages_toq(&free,
6172 if (pt2_wirecount_get(dst_mpt2pg, pte1_idx) >=
6173 pt2_wirecount_get(src_mpt2pg, pte1_idx))
6182 rw_wunlock(&pvh_global_lock);
6183 PMAP_UNLOCK(src_pmap);
6184 PMAP_UNLOCK(dst_pmap);
6188 * Increase the starting virtual address of the given mapping if a
6189 * different alignment might result in more section mappings.
6192 pmap_align_superpage(vm_object_t object, vm_ooffset_t offset,
6193 vm_offset_t *addr, vm_size_t size)
6195 vm_offset_t pte1_offset;
6197 if (size < PTE1_SIZE)
6199 if (object != NULL && (object->flags & OBJ_COLORED) != 0)
6200 offset += ptoa(object->pg_color);
6201 pte1_offset = offset & PTE1_OFFSET;
6202 if (size - ((PTE1_SIZE - pte1_offset) & PTE1_OFFSET) < PTE1_SIZE ||
6203 (*addr & PTE1_OFFSET) == pte1_offset)
6205 if ((*addr & PTE1_OFFSET) < pte1_offset)
6206 *addr = pte1_trunc(*addr) + pte1_offset;
6208 *addr = pte1_roundup(*addr) + pte1_offset;
6212 pmap_activate(struct thread *td)
6214 pmap_t pmap, oldpmap;
6217 PDEBUG(9, printf("%s: td = %08x\n", __func__, (uint32_t)td));
6220 pmap = vmspace_pmap(td->td_proc->p_vmspace);
6221 oldpmap = PCPU_GET(curpmap);
6222 cpuid = PCPU_GET(cpuid);
6225 CPU_CLR_ATOMIC(cpuid, &oldpmap->pm_active);
6226 CPU_SET_ATOMIC(cpuid, &pmap->pm_active);
6228 CPU_CLR(cpuid, &oldpmap->pm_active);
6229 CPU_SET(cpuid, &pmap->pm_active);
6232 ttb = pmap_ttb_get(pmap);
6235 * pmap_activate is for the current thread on the current cpu
6237 td->td_pcb->pcb_pagedir = ttb;
6239 PCPU_SET(curpmap, pmap);
6244 * Perform the pmap work for mincore.
6247 pmap_mincore(pmap_t pmap, vm_offset_t addr, vm_paddr_t *locked_pa)
6249 pt1_entry_t *pte1p, pte1;
6250 pt2_entry_t *pte2p, pte2;
6257 pte1p = pmap_pte1(pmap, addr);
6258 pte1 = pte1_load(pte1p);
6259 if (pte1_is_section(pte1)) {
6260 pa = trunc_page(pte1_pa(pte1) | (addr & PTE1_OFFSET));
6261 managed = pte1_is_managed(pte1);
6262 val = MINCORE_SUPER | MINCORE_INCORE;
6263 if (pte1_is_dirty(pte1))
6264 val |= MINCORE_MODIFIED | MINCORE_MODIFIED_OTHER;
6266 val |= MINCORE_REFERENCED | MINCORE_REFERENCED_OTHER;
6267 } else if (pte1_is_link(pte1)) {
6268 pte2p = pmap_pte2(pmap, addr);
6269 pte2 = pte2_load(pte2p);
6270 pmap_pte2_release(pte2p);
6272 managed = pte2_is_managed(pte2);
6273 val = MINCORE_INCORE;
6274 if (pte2_is_dirty(pte2))
6275 val |= MINCORE_MODIFIED | MINCORE_MODIFIED_OTHER;
6277 val |= MINCORE_REFERENCED | MINCORE_REFERENCED_OTHER;
6282 if ((val & (MINCORE_MODIFIED_OTHER | MINCORE_REFERENCED_OTHER)) !=
6283 (MINCORE_MODIFIED_OTHER | MINCORE_REFERENCED_OTHER) && managed) {
6284 /* Ensure that "PHYS_TO_VM_PAGE(pa)->object" doesn't change. */
6285 if (vm_page_pa_tryrelock(pmap, pa, locked_pa))
6288 PA_UNLOCK_COND(*locked_pa);
6294 pmap_kenter_device(vm_offset_t va, vm_size_t size, vm_paddr_t pa)
6299 KASSERT((size & PAGE_MASK) == 0,
6300 ("%s: device mapping not page-sized", __func__));
6303 l2attr = vm_memattr_to_pte2(VM_MEMATTR_DEVICE);
6305 pmap_kenter_prot_attr(va, pa, PTE2_AP_KRW, l2attr);
6310 tlb_flush_range(sva, va - sva);
6314 pmap_kremove_device(vm_offset_t va, vm_size_t size)
6318 KASSERT((size & PAGE_MASK) == 0,
6319 ("%s: device mapping not page-sized", __func__));
6327 tlb_flush_range(sva, va - sva);
6331 pmap_set_pcb_pagedir(pmap_t pmap, struct pcb *pcb)
6334 pcb->pcb_pagedir = pmap_ttb_get(pmap);
6339 * Clean L1 data cache range by physical address.
6340 * The range must be within a single page.
6343 pmap_dcache_wb_pou(vm_paddr_t pa, vm_size_t size, uint32_t attr)
6345 pt2_entry_t *cmap2_pte2p;
6348 KASSERT(((pa & PAGE_MASK) + size) <= PAGE_SIZE,
6349 ("%s: not on single page", __func__));
6353 cmap2_pte2p = pc->pc_cmap2_pte2p;
6354 mtx_lock(&pc->pc_cmap_lock);
6355 if (pte2_load(cmap2_pte2p) != 0)
6356 panic("%s: CMAP2 busy", __func__);
6357 pte2_store(cmap2_pte2p, PTE2_KERN_NG(pa, PTE2_AP_KRW, attr));
6358 dcache_wb_pou((vm_offset_t)pc->pc_cmap2_addr + (pa & PAGE_MASK), size);
6359 pte2_clear(cmap2_pte2p);
6360 tlb_flush((vm_offset_t)pc->pc_cmap2_addr);
6362 mtx_unlock(&pc->pc_cmap_lock);
6366 * Sync instruction cache range which is not mapped yet.
6369 cache_icache_sync_fresh(vm_offset_t va, vm_paddr_t pa, vm_size_t size)
6371 uint32_t len, offset;
6374 /* Write back d-cache on given address range. */
6375 offset = pa & PAGE_MASK;
6376 for ( ; size != 0; size -= len, pa += len, offset = 0) {
6377 len = min(PAGE_SIZE - offset, size);
6378 m = PHYS_TO_VM_PAGE(pa);
6379 KASSERT(m != NULL, ("%s: vm_page_t is null for %#x",
6381 pmap_dcache_wb_pou(pa, len, vm_page_pte2_attr(m));
6384 * I-cache is VIPT. Only way how to flush all virtual mappings
6385 * on given physical address is to invalidate all i-cache.
6391 pmap_sync_icache(pmap_t pmap, vm_offset_t va, vm_size_t size)
6394 /* Write back d-cache on given address range. */
6395 if (va >= VM_MIN_KERNEL_ADDRESS) {
6396 dcache_wb_pou(va, size);
6398 uint32_t len, offset;
6402 offset = va & PAGE_MASK;
6403 for ( ; size != 0; size -= len, va += len, offset = 0) {
6404 pa = pmap_extract(pmap, va); /* offset is preserved */
6405 len = min(PAGE_SIZE - offset, size);
6406 m = PHYS_TO_VM_PAGE(pa);
6407 KASSERT(m != NULL, ("%s: vm_page_t is null for %#x",
6409 pmap_dcache_wb_pou(pa, len, vm_page_pte2_attr(m));
6413 * I-cache is VIPT. Only way how to flush all virtual mappings
6414 * on given physical address is to invalidate all i-cache.
6420 * The implementation of pmap_fault() uses IN_RANGE2() macro which
6421 * depends on the fact that given range size is a power of 2.
6423 CTASSERT(powerof2(NB_IN_PT1));
6424 CTASSERT(powerof2(PT2MAP_SIZE));
6426 #define IN_RANGE2(addr, start, size) \
6427 ((vm_offset_t)(start) == ((vm_offset_t)(addr) & ~((size) - 1)))
6430 * Handle access and R/W emulation faults.
6433 pmap_fault(pmap_t pmap, vm_offset_t far, uint32_t fsr, int idx, bool usermode)
6435 pt1_entry_t *pte1p, pte1;
6436 pt2_entry_t *pte2p, pte2;
6442 * In kernel, we should never get abort with FAR which is in range of
6443 * pmap->pm_pt1 or PT2MAP address spaces. If it happens, stop here
6444 * and print out a useful abort message and even get to the debugger
6445 * otherwise it likely ends with never ending loop of aborts.
6447 if (__predict_false(IN_RANGE2(far, pmap->pm_pt1, NB_IN_PT1))) {
6449 * All L1 tables should always be mapped and present.
6450 * However, we check only current one herein. For user mode,
6451 * only permission abort from malicious user is not fatal.
6452 * And alignment abort as it may have higher priority.
6454 if (!usermode || (idx != FAULT_ALIGN && idx != FAULT_PERM_L2)) {
6455 CTR4(KTR_PMAP, "%s: pmap %#x pm_pt1 %#x far %#x",
6456 __func__, pmap, pmap->pm_pt1, far);
6457 panic("%s: pm_pt1 abort", __func__);
6459 return (KERN_INVALID_ADDRESS);
6461 if (__predict_false(IN_RANGE2(far, PT2MAP, PT2MAP_SIZE))) {
6463 * PT2MAP should be always mapped and present in current
6464 * L1 table. However, only existing L2 tables are mapped
6465 * in PT2MAP. For user mode, only L2 translation abort and
6466 * permission abort from malicious user is not fatal.
6467 * And alignment abort as it may have higher priority.
6469 if (!usermode || (idx != FAULT_ALIGN &&
6470 idx != FAULT_TRAN_L2 && idx != FAULT_PERM_L2)) {
6471 CTR4(KTR_PMAP, "%s: pmap %#x PT2MAP %#x far %#x",
6472 __func__, pmap, PT2MAP, far);
6473 panic("%s: PT2MAP abort", __func__);
6475 return (KERN_INVALID_ADDRESS);
6479 * A pmap lock is used below for handling of access and R/W emulation
6480 * aborts. They were handled by atomic operations before so some
6481 * analysis of new situation is needed to answer the following question:
6482 * Is it safe to use the lock even for these aborts?
6484 * There may happen two cases in general:
6486 * (1) Aborts while the pmap lock is locked already - this should not
6487 * happen as pmap lock is not recursive. However, under pmap lock only
6488 * internal kernel data should be accessed and such data should be
6489 * mapped with A bit set and NM bit cleared. If double abort happens,
6490 * then a mapping of data which has caused it must be fixed. Further,
6491 * all new mappings are always made with A bit set and the bit can be
6492 * cleared only on managed mappings.
6494 * (2) Aborts while another lock(s) is/are locked - this already can
6495 * happen. However, there is no difference here if it's either access or
6496 * R/W emulation abort, or if it's some other abort.
6501 pte1 = pte1_load(pmap_pte1(pmap, far));
6502 if (pte1_is_link(pte1)) {
6504 * Check in advance that associated L2 page table is mapped into
6505 * PT2MAP space. Note that faulty access to not mapped L2 page
6506 * table is caught in more general check above where "far" is
6507 * checked that it does not lay in PT2MAP space. Note also that
6508 * L1 page table and PT2TAB always exist and are mapped.
6510 pte2 = pt2tab_load(pmap_pt2tab_entry(pmap, far));
6511 if (!pte2_is_valid(pte2))
6512 panic("%s: missing L2 page table (%p, %#x)",
6513 __func__, pmap, far);
6518 * Special treatment is due to break-before-make approach done when
6519 * pte1 is updated for userland mapping during section promotion or
6520 * demotion. If not caught here, pmap_enter() can find a section
6521 * mapping on faulting address. That is not allowed.
6523 if (idx == FAULT_TRAN_L1 && usermode && cp15_ats1cur_check(far) == 0) {
6525 return (KERN_SUCCESS);
6529 * Accesss bits for page and section. Note that the entry
6530 * is not in TLB yet, so TLB flush is not necessary.
6532 * QQQ: This is hardware emulation, we do not call userret()
6533 * for aborts from user mode.
6535 if (idx == FAULT_ACCESS_L2) {
6536 pte1 = pte1_load(pmap_pte1(pmap, far));
6537 if (pte1_is_link(pte1)) {
6538 /* L2 page table should exist and be mapped. */
6539 pte2p = pt2map_entry(far);
6540 pte2 = pte2_load(pte2p);
6541 if (pte2_is_valid(pte2)) {
6542 pte2_store(pte2p, pte2 | PTE2_A);
6544 return (KERN_SUCCESS);
6548 * We got L2 access fault but PTE1 is not a link.
6549 * Probably some race happened, do nothing.
6551 CTR3(KTR_PMAP, "%s: FAULT_ACCESS_L2 - pmap %#x far %#x",
6552 __func__, pmap, far);
6554 return (KERN_SUCCESS);
6557 if (idx == FAULT_ACCESS_L1) {
6558 pte1p = pmap_pte1(pmap, far);
6559 pte1 = pte1_load(pte1p);
6560 if (pte1_is_section(pte1)) {
6561 pte1_store(pte1p, pte1 | PTE1_A);
6563 return (KERN_SUCCESS);
6566 * We got L1 access fault but PTE1 is not section
6567 * mapping. Probably some race happened, do nothing.
6569 CTR3(KTR_PMAP, "%s: FAULT_ACCESS_L1 - pmap %#x far %#x",
6570 __func__, pmap, far);
6572 return (KERN_SUCCESS);
6577 * Handle modify bits for page and section. Note that the modify
6578 * bit is emulated by software. So PTEx_RO is software read only
6579 * bit and PTEx_NM flag is real hardware read only bit.
6581 * QQQ: This is hardware emulation, we do not call userret()
6582 * for aborts from user mode.
6584 if ((fsr & FSR_WNR) && (idx == FAULT_PERM_L2)) {
6585 pte1 = pte1_load(pmap_pte1(pmap, far));
6586 if (pte1_is_link(pte1)) {
6587 /* L2 page table should exist and be mapped. */
6588 pte2p = pt2map_entry(far);
6589 pte2 = pte2_load(pte2p);
6590 if (pte2_is_valid(pte2) && !(pte2 & PTE2_RO) &&
6592 pte2_store(pte2p, pte2 & ~PTE2_NM);
6593 tlb_flush(trunc_page(far));
6595 return (KERN_SUCCESS);
6599 * We got L2 permission fault but PTE1 is not a link.
6600 * Probably some race happened, do nothing.
6602 CTR3(KTR_PMAP, "%s: FAULT_PERM_L2 - pmap %#x far %#x",
6603 __func__, pmap, far);
6605 return (KERN_SUCCESS);
6608 if ((fsr & FSR_WNR) && (idx == FAULT_PERM_L1)) {
6609 pte1p = pmap_pte1(pmap, far);
6610 pte1 = pte1_load(pte1p);
6611 if (pte1_is_section(pte1)) {
6612 if (!(pte1 & PTE1_RO) && (pte1 & PTE1_NM)) {
6613 pte1_store(pte1p, pte1 & ~PTE1_NM);
6614 tlb_flush(pte1_trunc(far));
6616 return (KERN_SUCCESS);
6620 * We got L1 permission fault but PTE1 is not section
6621 * mapping. Probably some race happened, do nothing.
6623 CTR3(KTR_PMAP, "%s: FAULT_PERM_L1 - pmap %#x far %#x",
6624 __func__, pmap, far);
6626 return (KERN_SUCCESS);
6631 * QQQ: The previous code, mainly fast handling of access and
6632 * modify bits aborts, could be moved to ASM. Now we are
6633 * starting to deal with not fast aborts.
6636 return (KERN_FAILURE);
6639 #if defined(PMAP_DEBUG)
6641 * Reusing of KVA used in pmap_zero_page function !!!
6644 pmap_zero_page_check(vm_page_t m)
6646 pt2_entry_t *cmap2_pte2p;
6652 cmap2_pte2p = pc->pc_cmap2_pte2p;
6653 mtx_lock(&pc->pc_cmap_lock);
6654 if (pte2_load(cmap2_pte2p) != 0)
6655 panic("%s: CMAP2 busy", __func__);
6656 pte2_store(cmap2_pte2p, PTE2_KERN_NG(VM_PAGE_TO_PHYS(m), PTE2_AP_KRW,
6657 vm_page_pte2_attr(m)));
6658 end = (uint32_t*)(pc->pc_cmap2_addr + PAGE_SIZE);
6659 for (p = (uint32_t*)pc->pc_cmap2_addr; p < end; p++)
6661 panic("%s: page %p not zero, va: %p", __func__, m,
6663 pte2_clear(cmap2_pte2p);
6664 tlb_flush((vm_offset_t)pc->pc_cmap2_addr);
6666 mtx_unlock(&pc->pc_cmap_lock);
6670 pmap_pid_dump(int pid)
6677 sx_slock(&allproc_lock);
6678 FOREACH_PROC_IN_SYSTEM(p) {
6679 if (p->p_pid != pid || p->p_vmspace == NULL)
6682 pmap = vmspace_pmap(p->p_vmspace);
6683 for (i = 0; i < NPTE1_IN_PT1; i++) {
6685 pt2_entry_t *pte2p, pte2;
6686 vm_offset_t base, va;
6690 base = i << PTE1_SHIFT;
6691 pte1 = pte1_load(&pmap->pm_pt1[i]);
6693 if (pte1_is_section(pte1)) {
6695 * QQQ: Do something here!
6697 } else if (pte1_is_link(pte1)) {
6698 for (j = 0; j < NPTE2_IN_PT2; j++) {
6699 va = base + (j << PAGE_SHIFT);
6700 if (va >= VM_MIN_KERNEL_ADDRESS) {
6705 sx_sunlock(&allproc_lock);
6708 pte2p = pmap_pte2(pmap, va);
6709 pte2 = pte2_load(pte2p);
6710 pmap_pte2_release(pte2p);
6711 if (!pte2_is_valid(pte2))
6715 m = PHYS_TO_VM_PAGE(pa);
6716 printf("va: 0x%x, pa: 0x%x, h: %d, w:"
6717 " %d, f: 0x%x", va, pa,
6718 m->hold_count, m->wire_count,
6732 sx_sunlock(&allproc_lock);
6739 static pt2_entry_t *
6740 pmap_pte2_ddb(pmap_t pmap, vm_offset_t va)
6743 vm_paddr_t pt2pg_pa;
6745 pte1 = pte1_load(pmap_pte1(pmap, va));
6746 if (!pte1_is_link(pte1))
6749 if (pmap_is_current(pmap))
6750 return (pt2map_entry(va));
6752 /* Note that L2 page table size is not equal to PAGE_SIZE. */
6753 pt2pg_pa = trunc_page(pte1_link_pa(pte1));
6754 if (pte2_pa(pte2_load(PMAP3)) != pt2pg_pa) {
6755 pte2_store(PMAP3, PTE2_KPT(pt2pg_pa));
6757 PMAP3cpu = PCPU_GET(cpuid);
6759 tlb_flush_local((vm_offset_t)PADDR3);
6762 else if (PMAP3cpu != PCPU_GET(cpuid)) {
6763 PMAP3cpu = PCPU_GET(cpuid);
6764 tlb_flush_local((vm_offset_t)PADDR3);
6767 return (PADDR3 + (arm32_btop(va) & (NPTE2_IN_PG - 1)));
6771 dump_pmap(pmap_t pmap)
6774 printf("pmap %p\n", pmap);
6775 printf(" pm_pt1: %p\n", pmap->pm_pt1);
6776 printf(" pm_pt2tab: %p\n", pmap->pm_pt2tab);
6777 printf(" pm_active: 0x%08lX\n", pmap->pm_active.__bits[0]);
6780 DB_SHOW_COMMAND(pmaps, pmap_list_pmaps)
6784 LIST_FOREACH(pmap, &allpmaps, pm_list) {
6790 pte2_class(pt2_entry_t pte2)
6794 cls = (pte2 >> 2) & 0x03;
6795 cls |= (pte2 >> 4) & 0x04;
6800 dump_section(pmap_t pmap, uint32_t pte1_idx)
6805 dump_link(pmap_t pmap, uint32_t pte1_idx, boolean_t invalid_ok)
6809 pt2_entry_t *pte2p, pte2;
6812 va = pte1_idx << PTE1_SHIFT;
6813 pte2p = pmap_pte2_ddb(pmap, va);
6814 for (i = 0; i < NPTE2_IN_PT2; i++, pte2p++, va += PAGE_SIZE) {
6815 pte2 = pte2_load(pte2p);
6818 if (!pte2_is_valid(pte2)) {
6819 printf(" 0x%08X: 0x%08X", va, pte2);
6821 printf(" - not valid !!!");
6825 m = PHYS_TO_VM_PAGE(pte2_pa(pte2));
6826 printf(" 0x%08X: 0x%08X, TEX%d, s:%d, g:%d, m:%p", va , pte2,
6827 pte2_class(pte2), !!(pte2 & PTE2_S), !(pte2 & PTE2_NG), m);
6829 printf(" v:%d h:%d w:%d f:0x%04X\n", m->valid,
6830 m->hold_count, m->wire_count, m->flags);
6837 static __inline boolean_t
6838 is_pv_chunk_space(vm_offset_t va)
6841 if ((((vm_offset_t)pv_chunkbase) <= va) &&
6842 (va < ((vm_offset_t)pv_chunkbase + PAGE_SIZE * pv_maxchunks)))
6847 DB_SHOW_COMMAND(pmap, pmap_pmap_print)
6849 /* XXX convert args. */
6850 pmap_t pmap = (pmap_t)addr;
6853 vm_offset_t va, eva;
6856 boolean_t invalid_ok, dump_link_ok, dump_pv_chunk;
6861 LIST_FOREACH(pm, &allpmaps, pm_list)
6862 if (pm == pmap) break;
6864 printf("given pmap %p is not in allpmaps list\n", pmap);
6868 pmap = PCPU_GET(curpmap);
6870 eva = (modif[0] == 'u') ? VM_MAXUSER_ADDRESS : 0xFFFFFFFF;
6871 dump_pv_chunk = FALSE; /* XXX evaluate from modif[] */
6873 printf("pmap: 0x%08X\n", (uint32_t)pmap);
6874 printf("PT2MAP: 0x%08X\n", (uint32_t)PT2MAP);
6875 printf("pt2tab: 0x%08X\n", (uint32_t)pmap->pm_pt2tab);
6877 for(i = 0; i < NPTE1_IN_PT1; i++) {
6878 pte1 = pte1_load(&pmap->pm_pt1[i]);
6881 va = i << PTE1_SHIFT;
6885 if (pte1_is_section(pte1)) {
6886 printf("0x%08X: Section 0x%08X, s:%d g:%d\n", va, pte1,
6887 !!(pte1 & PTE1_S), !(pte1 & PTE1_NG));
6888 dump_section(pmap, i);
6889 } else if (pte1_is_link(pte1)) {
6890 dump_link_ok = TRUE;
6892 pte2 = pte2_load(pmap_pt2tab_entry(pmap, va));
6893 m = PHYS_TO_VM_PAGE(pte1_link_pa(pte1));
6894 printf("0x%08X: Link 0x%08X, pt2tab: 0x%08X m: %p",
6896 if (is_pv_chunk_space(va)) {
6897 printf(" - pv_chunk space");
6901 dump_link_ok = FALSE;
6904 printf(" w:%d w2:%u", m->wire_count,
6905 pt2_wirecount_get(m, pte1_index(va)));
6907 printf(" !!! pt2tab entry is ZERO");
6908 else if (pte2_pa(pte1) != pte2_pa(pte2))
6909 printf(" !!! pt2tab entry is DIFFERENT - m: %p",
6910 PHYS_TO_VM_PAGE(pte2_pa(pte2)));
6913 dump_link(pmap, i, invalid_ok);
6915 printf("0x%08X: Invalid entry 0x%08X\n", va, pte1);
6920 dump_pt2tab(pmap_t pmap)
6928 printf("PT2TAB:\n");
6929 for (i = 0; i < PT2TAB_ENTRIES; i++) {
6930 pte2 = pte2_load(&pmap->pm_pt2tab[i]);
6931 if (!pte2_is_valid(pte2))
6933 va = i << PT2TAB_SHIFT;
6935 m = PHYS_TO_VM_PAGE(pa);
6936 printf(" 0x%08X: 0x%08X, TEX%d, s:%d, m:%p", va, pte2,
6937 pte2_class(pte2), !!(pte2 & PTE2_S), m);
6939 printf(" , h: %d, w: %d, f: 0x%04X pidx: %lld",
6940 m->hold_count, m->wire_count, m->flags, m->pindex);
6945 DB_SHOW_COMMAND(pmap_pt2tab, pmap_pt2tab_print)
6947 /* XXX convert args. */
6948 pmap_t pmap = (pmap_t)addr;
6955 printf("supported only on current pmap\n");
6959 pmap = PCPU_GET(curpmap);
6960 printf("curpmap: 0x%08X\n", (uint32_t)pmap);
6961 printf("PT2MAP: 0x%08X\n", (uint32_t)PT2MAP);
6962 printf("pt2tab: 0x%08X\n", (uint32_t)pmap->pm_pt2tab);
6964 start = pte1_index((vm_offset_t)PT2MAP);
6965 for (i = start; i < (start + NPT2_IN_PT2TAB); i++) {
6966 pte1 = pte1_load(&pmap->pm_pt1[i]);
6969 va = i << PTE1_SHIFT;
6970 if (pte1_is_section(pte1)) {
6971 printf("0x%08X: Section 0x%08X, s:%d\n", va, pte1,
6973 dump_section(pmap, i);
6974 } else if (pte1_is_link(pte1)) {
6975 pte2 = pte2_load(pmap_pt2tab_entry(pmap, va));
6976 printf("0x%08X: Link 0x%08X, pt2tab: 0x%08X\n", va,
6979 printf(" !!! pt2tab entry is ZERO\n");
6981 printf("0x%08X: Invalid entry 0x%08X\n", va, pte1);