2 * Copyright (c) 1991 Regents of the University of California.
3 * Copyright (c) 1994 John S. Dyson
4 * Copyright (c) 1994 David Greenman
5 * Copyright (c) 2005-2010 Alan L. Cox <alc@cs.rice.edu>
6 * Copyright (c) 2014-2016 Svatopluk Kraus <skra@FreeBSD.org>
7 * Copyright (c) 2014-2016 Michal Meloun <mmel@FreeBSD.org>
10 * This code is derived from software contributed to Berkeley by
11 * the Systems Programming Group of the University of Utah Computer
12 * Science Department and William Jolitz of UUNET Technologies Inc.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * from: @(#)pmap.c 7.7 (Berkeley) 5/12/91
41 * Copyright (c) 2003 Networks Associates Technology, Inc.
42 * All rights reserved.
44 * This software was developed for the FreeBSD Project by Jake Burkholder,
45 * Safeport Network Services, and Network Associates Laboratories, the
46 * Security Research Division of Network Associates, Inc. under
47 * DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA
48 * CHATS research program.
50 * Redistribution and use in source and binary forms, with or without
51 * modification, are permitted provided that the following conditions
53 * 1. Redistributions of source code must retain the above copyright
54 * notice, this list of conditions and the following disclaimer.
55 * 2. Redistributions in binary form must reproduce the above copyright
56 * notice, this list of conditions and the following disclaimer in the
57 * documentation and/or other materials provided with the distribution.
59 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72 #include <sys/cdefs.h>
73 __FBSDID("$FreeBSD$");
76 * Manages physical address maps.
78 * Since the information managed by this module is
79 * also stored by the logical address mapping module,
80 * this module may throw away valid virtual-to-physical
81 * mappings at almost any time. However, invalidations
82 * of virtual-to-physical mappings must be done as
85 * In order to cope with hardware architectures which
86 * make virtual-to-physical map invalidates expensive,
87 * this module may delay invalidate or reduced protection
88 * operations until such time as they are actually
89 * necessary. This module is given full information as
90 * to which processors are currently using which maps,
91 * and to when physical maps must be made correct.
98 #include <sys/param.h>
99 #include <sys/systm.h>
100 #include <sys/kernel.h>
102 #include <sys/lock.h>
103 #include <sys/proc.h>
104 #include <sys/rwlock.h>
105 #include <sys/malloc.h>
106 #include <sys/vmmeter.h>
107 #include <sys/malloc.h>
108 #include <sys/mman.h>
109 #include <sys/sf_buf.h>
111 #include <sys/sched.h>
112 #include <sys/sysctl.h>
118 #include <machine/physmem.h>
123 #include <vm/vm_param.h>
124 #include <vm/vm_kern.h>
125 #include <vm/vm_object.h>
126 #include <vm/vm_map.h>
127 #include <vm/vm_page.h>
128 #include <vm/vm_pageout.h>
129 #include <vm/vm_phys.h>
130 #include <vm/vm_extern.h>
131 #include <vm/vm_reserv.h>
132 #include <sys/lock.h>
133 #include <sys/mutex.h>
135 #include <machine/md_var.h>
136 #include <machine/pmap_var.h>
137 #include <machine/cpu.h>
138 #include <machine/pcb.h>
139 #include <machine/sf_buf.h>
141 #include <machine/smp.h>
143 #ifndef PMAP_SHPGPERPROC
144 #define PMAP_SHPGPERPROC 200
148 #define PMAP_INLINE __inline
154 static void pmap_zero_page_check(vm_page_t m);
155 void pmap_debug(int level);
156 int pmap_pid_dump(int pid);
158 #define PDEBUG(_lev_,_stat_) \
159 if (pmap_debug_level >= (_lev_)) \
161 #define dprintf printf
162 int pmap_debug_level = 1;
163 #else /* PMAP_DEBUG */
164 #define PDEBUG(_lev_,_stat_) /* Nothing */
165 #define dprintf(x, arg...)
166 #endif /* PMAP_DEBUG */
169 * Level 2 page tables map definion ('max' is excluded).
172 #define PT2V_MIN_ADDRESS ((vm_offset_t)PT2MAP)
173 #define PT2V_MAX_ADDRESS ((vm_offset_t)PT2MAP + PT2MAP_SIZE)
175 #define UPT2V_MIN_ADDRESS ((vm_offset_t)PT2MAP)
176 #define UPT2V_MAX_ADDRESS \
177 ((vm_offset_t)(PT2MAP + (KERNBASE >> PT2MAP_SHIFT)))
180 * Promotion to a 1MB (PTE1) page mapping requires that the corresponding
181 * 4KB (PTE2) page mappings have identical settings for the following fields:
183 #define PTE2_PROMOTE (PTE2_V | PTE2_A | PTE2_NM | PTE2_S | PTE2_NG | \
184 PTE2_NX | PTE2_RO | PTE2_U | PTE2_W | \
187 #define PTE1_PROMOTE (PTE1_V | PTE1_A | PTE1_NM | PTE1_S | PTE1_NG | \
188 PTE1_NX | PTE1_RO | PTE1_U | PTE1_W | \
191 #define ATTR_TO_L1(l2_attr) ((((l2_attr) & L2_TEX0) ? L1_S_TEX0 : 0) | \
192 (((l2_attr) & L2_C) ? L1_S_C : 0) | \
193 (((l2_attr) & L2_B) ? L1_S_B : 0) | \
194 (((l2_attr) & PTE2_A) ? PTE1_A : 0) | \
195 (((l2_attr) & PTE2_NM) ? PTE1_NM : 0) | \
196 (((l2_attr) & PTE2_S) ? PTE1_S : 0) | \
197 (((l2_attr) & PTE2_NG) ? PTE1_NG : 0) | \
198 (((l2_attr) & PTE2_NX) ? PTE1_NX : 0) | \
199 (((l2_attr) & PTE2_RO) ? PTE1_RO : 0) | \
200 (((l2_attr) & PTE2_U) ? PTE1_U : 0) | \
201 (((l2_attr) & PTE2_W) ? PTE1_W : 0))
203 #define ATTR_TO_L2(l1_attr) ((((l1_attr) & L1_S_TEX0) ? L2_TEX0 : 0) | \
204 (((l1_attr) & L1_S_C) ? L2_C : 0) | \
205 (((l1_attr) & L1_S_B) ? L2_B : 0) | \
206 (((l1_attr) & PTE1_A) ? PTE2_A : 0) | \
207 (((l1_attr) & PTE1_NM) ? PTE2_NM : 0) | \
208 (((l1_attr) & PTE1_S) ? PTE2_S : 0) | \
209 (((l1_attr) & PTE1_NG) ? PTE2_NG : 0) | \
210 (((l1_attr) & PTE1_NX) ? PTE2_NX : 0) | \
211 (((l1_attr) & PTE1_RO) ? PTE2_RO : 0) | \
212 (((l1_attr) & PTE1_U) ? PTE2_U : 0) | \
213 (((l1_attr) & PTE1_W) ? PTE2_W : 0))
216 * PTE2 descriptors creation macros.
218 #define PTE2_ATTR_DEFAULT vm_memattr_to_pte2(VM_MEMATTR_DEFAULT)
219 #define PTE2_ATTR_PT vm_memattr_to_pte2(pt_memattr)
221 #define PTE2_KPT(pa) PTE2_KERN(pa, PTE2_AP_KRW, PTE2_ATTR_PT)
222 #define PTE2_KPT_NG(pa) PTE2_KERN_NG(pa, PTE2_AP_KRW, PTE2_ATTR_PT)
224 #define PTE2_KRW(pa) PTE2_KERN(pa, PTE2_AP_KRW, PTE2_ATTR_DEFAULT)
225 #define PTE2_KRO(pa) PTE2_KERN(pa, PTE2_AP_KR, PTE2_ATTR_DEFAULT)
229 #define PV_STAT(x) do { x ; } while (0)
231 #define PV_STAT(x) do { } while (0)
235 * The boot_pt1 is used temporary in very early boot stage as L1 page table.
236 * We can init many things with no memory allocation thanks to its static
237 * allocation and this brings two main advantages:
238 * (1) other cores can be started very simply,
239 * (2) various boot loaders can be supported as its arguments can be processed
240 * in virtual address space and can be moved to safe location before
241 * first allocation happened.
242 * Only disadvantage is that boot_pt1 is used only in very early boot stage.
243 * However, the table is uninitialized and so lays in bss. Therefore kernel
244 * image size is not influenced.
246 * QQQ: In the future, maybe, boot_pt1 can be used for soft reset and
247 * CPU suspend/resume game.
249 extern pt1_entry_t boot_pt1[];
252 pt1_entry_t *kern_pt1;
253 pt2_entry_t *kern_pt2tab;
256 static uint32_t ttb_flags;
257 static vm_memattr_t pt_memattr;
258 ttb_entry_t pmap_kern_ttb;
260 struct pmap kernel_pmap_store;
261 LIST_HEAD(pmaplist, pmap);
262 static struct pmaplist allpmaps;
263 static struct mtx allpmaps_lock;
265 vm_offset_t virtual_avail; /* VA of first avail page (after kernel bss) */
266 vm_offset_t virtual_end; /* VA of last avail page (end of kernel AS) */
268 static vm_offset_t kernel_vm_end_new;
269 vm_offset_t kernel_vm_end = KERNBASE + NKPT2PG * NPT2_IN_PG * PTE1_SIZE;
270 vm_offset_t vm_max_kernel_address;
271 vm_paddr_t kernel_l1pa;
273 static struct rwlock __aligned(CACHE_LINE_SIZE) pvh_global_lock;
276 * Data for the pv entry allocation mechanism
278 static TAILQ_HEAD(pch, pv_chunk) pv_chunks = TAILQ_HEAD_INITIALIZER(pv_chunks);
279 static int pv_entry_count = 0, pv_entry_max = 0, pv_entry_high_water = 0;
280 static struct md_page *pv_table; /* XXX: Is it used only the list in md_page? */
281 static int shpgperproc = PMAP_SHPGPERPROC;
283 struct pv_chunk *pv_chunkbase; /* KVA block for pv_chunks */
284 int pv_maxchunks; /* How many chunks we have KVA for */
285 vm_offset_t pv_vafree; /* freelist stored in the PTE */
287 vm_paddr_t first_managed_pa;
288 #define pa_to_pvh(pa) (&pv_table[pte1_index(pa - first_managed_pa)])
291 * All those kernel PT submaps that BSD is so fond of
295 struct msgbuf *msgbufp = NULL; /* XXX move it to machdep.c */
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;
326 static __inline void pt2_wirecount_init(vm_page_t m);
327 static boolean_t pmap_demote_pte1(pmap_t pmap, pt1_entry_t *pte1p,
329 void cache_icache_sync_fresh(vm_offset_t va, vm_paddr_t pa, vm_size_t size);
332 * Function to set the debug level of the pmap code.
336 pmap_debug(int level)
339 pmap_debug_level = level;
340 dprintf("pmap_debug: level=%d\n", pmap_debug_level);
342 #endif /* PMAP_DEBUG */
345 * This table must corespond with memory attribute configuration in vm.h.
346 * First entry is used for normal system mapping.
348 * Device memory is always marked as shared.
349 * Normal memory is shared only in SMP .
350 * Not outer shareable bits are not used yet.
351 * Class 6 cannot be used on ARM11.
353 #define TEXDEF_TYPE_SHIFT 0
354 #define TEXDEF_TYPE_MASK 0x3
355 #define TEXDEF_INNER_SHIFT 2
356 #define TEXDEF_INNER_MASK 0x3
357 #define TEXDEF_OUTER_SHIFT 4
358 #define TEXDEF_OUTER_MASK 0x3
359 #define TEXDEF_NOS_SHIFT 6
360 #define TEXDEF_NOS_MASK 0x1
362 #define TEX(t, i, o, s) \
363 ((t) << TEXDEF_TYPE_SHIFT) | \
364 ((i) << TEXDEF_INNER_SHIFT) | \
365 ((o) << TEXDEF_OUTER_SHIFT | \
366 ((s) << TEXDEF_NOS_SHIFT))
368 static uint32_t tex_class[8] = {
369 /* type inner cache outer cache */
370 TEX(PRRR_MEM, NMRR_WB_WA, NMRR_WB_WA, 0), /* 0 - ATTR_WB_WA */
371 TEX(PRRR_MEM, NMRR_NC, NMRR_NC, 0), /* 1 - ATTR_NOCACHE */
372 TEX(PRRR_DEV, NMRR_NC, NMRR_NC, 0), /* 2 - ATTR_DEVICE */
373 TEX(PRRR_SO, NMRR_NC, NMRR_NC, 0), /* 3 - ATTR_SO */
374 TEX(PRRR_MEM, NMRR_WT, NMRR_WT, 0), /* 4 - ATTR_WT */
375 TEX(PRRR_MEM, NMRR_NC, NMRR_NC, 0), /* 5 - NOT USED YET */
376 TEX(PRRR_MEM, NMRR_NC, NMRR_NC, 0), /* 6 - NOT USED YET */
377 TEX(PRRR_MEM, NMRR_NC, NMRR_NC, 0), /* 7 - NOT USED YET */
381 static uint32_t pte2_attr_tab[8] = {
382 PTE2_ATTR_WB_WA, /* 0 - VM_MEMATTR_WB_WA */
383 PTE2_ATTR_NOCACHE, /* 1 - VM_MEMATTR_NOCACHE */
384 PTE2_ATTR_DEVICE, /* 2 - VM_MEMATTR_DEVICE */
385 PTE2_ATTR_SO, /* 3 - VM_MEMATTR_SO */
386 PTE2_ATTR_WT, /* 4 - VM_MEMATTR_WRITE_THROUGH */
387 0, /* 5 - NOT USED YET */
388 0, /* 6 - NOT USED YET */
389 0 /* 7 - NOT USED YET */
391 CTASSERT(VM_MEMATTR_WB_WA == 0);
392 CTASSERT(VM_MEMATTR_NOCACHE == 1);
393 CTASSERT(VM_MEMATTR_DEVICE == 2);
394 CTASSERT(VM_MEMATTR_SO == 3);
395 CTASSERT(VM_MEMATTR_WRITE_THROUGH == 4);
397 static inline uint32_t
398 vm_memattr_to_pte2(vm_memattr_t ma)
401 KASSERT((u_int)ma < 5, ("%s: bad vm_memattr_t %d", __func__, ma));
402 return (pte2_attr_tab[(u_int)ma]);
405 static inline uint32_t
406 vm_page_pte2_attr(vm_page_t m)
409 return (vm_memattr_to_pte2(m->md.pat_mode));
413 * Convert TEX definition entry to TTB flags.
416 encode_ttb_flags(int idx)
418 uint32_t inner, outer, nos, reg;
420 inner = (tex_class[idx] >> TEXDEF_INNER_SHIFT) &
422 outer = (tex_class[idx] >> TEXDEF_OUTER_SHIFT) &
424 nos = (tex_class[idx] >> TEXDEF_NOS_SHIFT) &
429 if (cpuinfo.coherent_walk)
430 reg |= (inner & 0x1) << 6;
431 reg |= (inner & 0x2) >> 1;
441 * Set TEX remapping registers in current CPU.
447 uint32_t type, inner, outer, nos;
450 #ifdef PMAP_PTE_NOCACHE
452 if (cpuinfo.coherent_walk) {
453 pt_memattr = VM_MEMATTR_WB_WA;
454 ttb_flags = encode_ttb_flags(0);
457 pt_memattr = VM_MEMATTR_NOCACHE;
458 ttb_flags = encode_ttb_flags(1);
461 pt_memattr = VM_MEMATTR_WB_WA;
462 ttb_flags = encode_ttb_flags(0);
468 /* Build remapping register from TEX classes. */
469 for (i = 0; i < 8; i++) {
470 type = (tex_class[i] >> TEXDEF_TYPE_SHIFT) &
472 inner = (tex_class[i] >> TEXDEF_INNER_SHIFT) &
474 outer = (tex_class[i] >> TEXDEF_OUTER_SHIFT) &
476 nos = (tex_class[i] >> TEXDEF_NOS_SHIFT) &
479 prrr |= type << (i * 2);
480 prrr |= nos << (i + 24);
481 nmrr |= inner << (i * 2);
482 nmrr |= outer << (i * 2 + 16);
484 /* Add shareable bits for device memory. */
485 prrr |= PRRR_DS0 | PRRR_DS1;
487 /* Add shareable bits for normal memory in SMP case. */
496 /* Caches are disabled, so full TLB flush should be enough. */
497 tlb_flush_all_local();
501 * Remap one vm_meattr class to another one. This can be useful as
502 * workaround for SOC errata, e.g. if devices must be accessed using
505 * !!! Please note that this function is absolutely last resort thing.
506 * It should not be used under normal circumstances. !!!
509 * - it shall be called after pmap_bootstrap_prepare() and before
510 * cpu_mp_start() (thus only on boot CPU). In practice, it's expected
511 * to be called from platform_attach() or platform_late_init().
513 * - if remapping doesn't change caching mode, or until uncached class
514 * is remapped to any kind of cached one, then no other restriction exists.
516 * - if pmap_remap_vm_attr() changes caching mode, but both (original and
517 * remapped) remain cached, then caller is resposible for calling
518 * of dcache_wbinv_poc_all().
520 * - remapping of any kind of cached class to uncached is not permitted.
523 pmap_remap_vm_attr(vm_memattr_t old_attr, vm_memattr_t new_attr)
525 int old_idx, new_idx;
527 /* Map VM memattrs to indexes to tex_class table. */
528 old_idx = PTE2_ATTR2IDX(pte2_attr_tab[(int)old_attr]);
529 new_idx = PTE2_ATTR2IDX(pte2_attr_tab[(int)new_attr]);
531 /* Replace TEX attribute and apply it. */
532 tex_class[old_idx] = tex_class[new_idx];
537 * KERNBASE must be multiple of NPT2_IN_PG * PTE1_SIZE. In other words,
538 * KERNBASE is mapped by first L2 page table in L2 page table page. It
539 * meets same constrain due to PT2MAP being placed just under KERNBASE.
541 CTASSERT((KERNBASE & (NPT2_IN_PG * PTE1_SIZE - 1)) == 0);
542 CTASSERT((KERNBASE - VM_MAXUSER_ADDRESS) >= PT2MAP_SIZE);
545 * In crazy dreams, PAGE_SIZE could be a multiple of PTE2_SIZE in general.
546 * For now, anyhow, the following check must be fulfilled.
548 CTASSERT(PAGE_SIZE == PTE2_SIZE);
550 * We don't want to mess up MI code with all MMU and PMAP definitions,
551 * so some things, which depend on other ones, are defined independently.
552 * Now, it is time to check that we don't screw up something.
554 CTASSERT(PDRSHIFT == PTE1_SHIFT);
556 * Check L1 and L2 page table entries definitions consistency.
558 CTASSERT(NB_IN_PT1 == (sizeof(pt1_entry_t) * NPTE1_IN_PT1));
559 CTASSERT(NB_IN_PT2 == (sizeof(pt2_entry_t) * NPTE2_IN_PT2));
561 * Check L2 page tables page consistency.
563 CTASSERT(PAGE_SIZE == (NPT2_IN_PG * NB_IN_PT2));
564 CTASSERT((1 << PT2PG_SHIFT) == NPT2_IN_PG);
566 * Check PT2TAB consistency.
567 * PT2TAB_ENTRIES is defined as a division of NPTE1_IN_PT1 by NPT2_IN_PG.
568 * This should be done without remainder.
570 CTASSERT(NPTE1_IN_PT1 == (PT2TAB_ENTRIES * NPT2_IN_PG));
575 * All level 2 page tables (PT2s) are mapped continuously and accordingly
576 * into PT2MAP address space. As PT2 size is less than PAGE_SIZE, this can
577 * be done only if PAGE_SIZE is a multiple of PT2 size. All PT2s in one page
578 * must be used together, but not necessary at once. The first PT2 in a page
579 * must map things on correctly aligned address and the others must follow
582 #define NB_IN_PT2TAB (PT2TAB_ENTRIES * sizeof(pt2_entry_t))
583 #define NPT2_IN_PT2TAB (NB_IN_PT2TAB / NB_IN_PT2)
584 #define NPG_IN_PT2TAB (NB_IN_PT2TAB / PAGE_SIZE)
587 * Check PT2TAB consistency.
588 * NPT2_IN_PT2TAB is defined as a division of NB_IN_PT2TAB by NB_IN_PT2.
589 * NPG_IN_PT2TAB is defined as a division of NB_IN_PT2TAB by PAGE_SIZE.
590 * The both should be done without remainder.
592 CTASSERT(NB_IN_PT2TAB == (NPT2_IN_PT2TAB * NB_IN_PT2));
593 CTASSERT(NB_IN_PT2TAB == (NPG_IN_PT2TAB * PAGE_SIZE));
595 * The implementation was made general, however, with the assumption
596 * bellow in mind. In case of another value of NPG_IN_PT2TAB,
597 * the code should be once more rechecked.
599 CTASSERT(NPG_IN_PT2TAB == 1);
602 * Get offset of PT2 in a page
603 * associated with given PT1 index.
605 static __inline u_int
606 page_pt2off(u_int pt1_idx)
609 return ((pt1_idx & PT2PG_MASK) * NB_IN_PT2);
613 * Get physical address of PT2
614 * associated with given PT2s page and PT1 index.
616 static __inline vm_paddr_t
617 page_pt2pa(vm_paddr_t pgpa, u_int pt1_idx)
620 return (pgpa + page_pt2off(pt1_idx));
624 * Get first entry of PT2
625 * associated with given PT2s page and PT1 index.
627 static __inline pt2_entry_t *
628 page_pt2(vm_offset_t pgva, u_int pt1_idx)
631 return ((pt2_entry_t *)(pgva + page_pt2off(pt1_idx)));
635 * Get virtual address of PT2s page (mapped in PT2MAP)
636 * which holds PT2 which holds entry which maps given virtual address.
638 static __inline vm_offset_t
639 pt2map_pt2pg(vm_offset_t va)
642 va &= ~(NPT2_IN_PG * PTE1_SIZE - 1);
643 return ((vm_offset_t)pt2map_entry(va));
646 /*****************************************************************************
648 * THREE pmap initialization milestones exist:
651 * -> fundamental init (including MMU) in ASM
654 * -> fundamental init continues in C
655 * -> first available physical address is known
657 * pmap_bootstrap_prepare() -> FIRST PMAP MILESTONE (first epoch begins)
658 * -> basic (safe) interface for physical address allocation is made
659 * -> basic (safe) interface for virtual mapping is made
660 * -> limited not SMP coherent work is possible
662 * -> more fundamental init continues in C
663 * -> locks and some more things are available
664 * -> all fundamental allocations and mappings are done
666 * pmap_bootstrap() -> SECOND PMAP MILESTONE (second epoch begins)
667 * -> phys_avail[] and virtual_avail is set
668 * -> control is passed to vm subsystem
669 * -> physical and virtual address allocation are off limit
670 * -> low level mapping functions, some SMP coherent,
671 * are available, which cannot be used before vm subsystem
675 * -> vm subsystem is being inited
677 * pmap_init() -> THIRD PMAP MILESTONE (third epoch begins)
678 * -> pmap is fully inited
680 *****************************************************************************/
682 /*****************************************************************************
684 * PMAP first stage initialization and utility functions
685 * for pre-bootstrap epoch.
687 * After pmap_bootstrap_prepare() is called, the following functions
690 * (1) strictly only for this stage functions for physical page allocations,
691 * virtual space allocations, and mappings:
693 * vm_paddr_t pmap_preboot_get_pages(u_int num);
694 * void pmap_preboot_map_pages(vm_paddr_t pa, vm_offset_t va, u_int num);
695 * vm_offset_t pmap_preboot_reserve_pages(u_int num);
696 * vm_offset_t pmap_preboot_get_vpages(u_int num);
697 * void pmap_preboot_map_attr(vm_paddr_t pa, vm_offset_t va, vm_size_t size,
698 * vm_prot_t prot, vm_memattr_t attr);
700 * (2) for all stages:
702 * vm_paddr_t pmap_kextract(vm_offset_t va);
704 * NOTE: This is not SMP coherent stage.
706 *****************************************************************************/
708 #define KERNEL_P2V(pa) \
709 ((vm_offset_t)((pa) - arm_physmem_kernaddr + KERNVIRTADDR))
710 #define KERNEL_V2P(va) \
711 ((vm_paddr_t)((va) - KERNVIRTADDR + arm_physmem_kernaddr))
713 static vm_paddr_t last_paddr;
716 * Pre-bootstrap epoch page allocator.
719 pmap_preboot_get_pages(u_int num)
724 last_paddr += num * PAGE_SIZE;
730 * The fundamental initialization of PMAP stuff.
732 * Some things already happened in locore.S and some things could happen
733 * before pmap_bootstrap_prepare() is called, so let's recall what is done:
734 * 1. Caches are disabled.
735 * 2. We are running on virtual addresses already with 'boot_pt1'
737 * 3. So far, all virtual addresses can be converted to physical ones and
738 * vice versa by the following macros:
739 * KERNEL_P2V(pa) .... physical to virtual ones,
740 * KERNEL_V2P(va) .... virtual to physical ones.
742 * What is done herein:
743 * 1. The 'boot_pt1' is replaced by real kernel L1 page table 'kern_pt1'.
744 * 2. PT2MAP magic is brought to live.
745 * 3. Basic preboot functions for page allocations and mappings can be used.
746 * 4. Everything is prepared for L1 cache enabling.
749 * 1. To use second TTB register, so kernel and users page tables will be
750 * separated. This way process forking - pmap_pinit() - could be faster,
751 * it saves physical pages and KVA per a process, and it's simple change.
752 * However, it will lead, due to hardware matter, to the following:
753 * (a) 2G space for kernel and 2G space for users.
754 * (b) 1G space for kernel in low addresses and 3G for users above it.
755 * A question is: Is the case (b) really an option? Note that case (b)
756 * does save neither physical memory and KVA.
759 pmap_bootstrap_prepare(vm_paddr_t last)
761 vm_paddr_t pt2pg_pa, pt2tab_pa, pa, size;
762 vm_offset_t pt2pg_va;
769 * Now, we are going to make real kernel mapping. Note that we are
770 * already running on some mapping made in locore.S and we expect
771 * that it's large enough to ensure nofault access to physical memory
772 * allocated herein before switch.
774 * As kernel image and everything needed before are and will be mapped
775 * by section mappings, we align last physical address to PTE1_SIZE.
777 last_paddr = pte1_roundup(last);
780 * Allocate and zero page(s) for kernel L1 page table.
782 * Note that it's first allocation on space which was PTE1_SIZE
783 * aligned and as such base_pt1 is aligned to NB_IN_PT1 too.
785 base_pt1 = pmap_preboot_get_pages(NPG_IN_PT1);
786 kern_pt1 = (pt1_entry_t *)KERNEL_P2V(base_pt1);
787 bzero((void*)kern_pt1, NB_IN_PT1);
788 pte1_sync_range(kern_pt1, NB_IN_PT1);
790 /* Allocate and zero page(s) for kernel PT2TAB. */
791 pt2tab_pa = pmap_preboot_get_pages(NPG_IN_PT2TAB);
792 kern_pt2tab = (pt2_entry_t *)KERNEL_P2V(pt2tab_pa);
793 bzero(kern_pt2tab, NB_IN_PT2TAB);
794 pte2_sync_range(kern_pt2tab, NB_IN_PT2TAB);
796 /* Allocate and zero page(s) for kernel L2 page tables. */
797 pt2pg_pa = pmap_preboot_get_pages(NKPT2PG);
798 pt2pg_va = KERNEL_P2V(pt2pg_pa);
799 size = NKPT2PG * PAGE_SIZE;
800 bzero((void*)pt2pg_va, size);
801 pte2_sync_range((pt2_entry_t *)pt2pg_va, size);
804 * Add a physical memory segment (vm_phys_seg) corresponding to the
805 * preallocated pages for kernel L2 page tables so that vm_page
806 * structures representing these pages will be created. The vm_page
807 * structures are required for promotion of the corresponding kernel
808 * virtual addresses to section mappings.
810 vm_phys_add_seg(pt2tab_pa, pmap_preboot_get_pages(0));
813 * Insert allocated L2 page table pages to PT2TAB and make
814 * link to all PT2s in L1 page table. See how kernel_vm_end
817 * We play simple and safe. So every KVA will have underlaying
818 * L2 page table, even kernel image mapped by sections.
820 pte2p = kern_pt2tab_entry(KERNBASE);
821 for (pa = pt2pg_pa; pa < pt2pg_pa + size; pa += PTE2_SIZE)
822 pt2tab_store(pte2p++, PTE2_KPT(pa));
824 pte1p = kern_pte1(KERNBASE);
825 for (pa = pt2pg_pa; pa < pt2pg_pa + size; pa += NB_IN_PT2)
826 pte1_store(pte1p++, PTE1_LINK(pa));
828 /* Make section mappings for kernel. */
829 l1_attr = ATTR_TO_L1(PTE2_ATTR_DEFAULT);
830 pte1p = kern_pte1(KERNBASE);
831 for (pa = KERNEL_V2P(KERNBASE); pa < last; pa += PTE1_SIZE)
832 pte1_store(pte1p++, PTE1_KERN(pa, PTE1_AP_KRW, l1_attr));
835 * Get free and aligned space for PT2MAP and make L1 page table links
836 * to L2 page tables held in PT2TAB.
838 * Note that pages holding PT2s are stored in PT2TAB as pt2_entry_t
839 * descriptors and PT2TAB page(s) itself is(are) used as PT2s. Thus
840 * each entry in PT2TAB maps all PT2s in a page. This implies that
841 * virtual address of PT2MAP must be aligned to NPT2_IN_PG * PTE1_SIZE.
843 PT2MAP = (pt2_entry_t *)(KERNBASE - PT2MAP_SIZE);
844 pte1p = kern_pte1((vm_offset_t)PT2MAP);
845 for (pa = pt2tab_pa, i = 0; i < NPT2_IN_PT2TAB; i++, pa += NB_IN_PT2) {
846 pte1_store(pte1p++, PTE1_LINK(pa));
850 * Store PT2TAB in PT2TAB itself, i.e. self reference mapping.
851 * Each pmap will hold own PT2TAB, so the mapping should be not global.
853 pte2p = kern_pt2tab_entry((vm_offset_t)PT2MAP);
854 for (pa = pt2tab_pa, i = 0; i < NPG_IN_PT2TAB; i++, pa += PTE2_SIZE) {
855 pt2tab_store(pte2p++, PTE2_KPT_NG(pa));
859 * Choose correct L2 page table and make mappings for allocations
860 * made herein which replaces temporary locore.S mappings after a while.
861 * Note that PT2MAP cannot be used until we switch to kern_pt1.
863 * Note, that these allocations started aligned on 1M section and
864 * kernel PT1 was allocated first. Making of mappings must follow
865 * order of physical allocations as we've used KERNEL_P2V() macro
866 * for virtual addresses resolution.
868 pte2p = kern_pt2tab_entry((vm_offset_t)kern_pt1);
869 pt2pg_va = KERNEL_P2V(pte2_pa(pte2_load(pte2p)));
871 pte2p = page_pt2(pt2pg_va, pte1_index((vm_offset_t)kern_pt1));
873 /* Make mapping for kernel L1 page table. */
874 for (pa = base_pt1, i = 0; i < NPG_IN_PT1; i++, pa += PTE2_SIZE)
875 pte2_store(pte2p++, PTE2_KPT(pa));
877 /* Make mapping for kernel PT2TAB. */
878 for (pa = pt2tab_pa, i = 0; i < NPG_IN_PT2TAB; i++, pa += PTE2_SIZE)
879 pte2_store(pte2p++, PTE2_KPT(pa));
881 /* Finally, switch from 'boot_pt1' to 'kern_pt1'. */
882 pmap_kern_ttb = base_pt1 | ttb_flags;
883 cpuinfo_reinit_mmu(pmap_kern_ttb);
885 * Initialize the first available KVA. As kernel image is mapped by
886 * sections, we are leaving some gap behind.
888 virtual_avail = (vm_offset_t)kern_pt2tab + NPG_IN_PT2TAB * PAGE_SIZE;
892 * Setup L2 page table page for given KVA.
893 * Used in pre-bootstrap epoch.
895 * Note that we have allocated NKPT2PG pages for L2 page tables in advance
896 * and used them for mapping KVA starting from KERNBASE. However, this is not
897 * enough. Vectors and devices need L2 page tables too. Note that they are
898 * even above VM_MAX_KERNEL_ADDRESS.
900 static __inline vm_paddr_t
901 pmap_preboot_pt2pg_setup(vm_offset_t va)
903 pt2_entry_t *pte2p, pte2;
906 /* Get associated entry in PT2TAB. */
907 pte2p = kern_pt2tab_entry(va);
909 /* Just return, if PT2s page exists already. */
910 pte2 = pt2tab_load(pte2p);
911 if (pte2_is_valid(pte2))
912 return (pte2_pa(pte2));
914 KASSERT(va >= VM_MAX_KERNEL_ADDRESS,
915 ("%s: NKPT2PG too small", __func__));
918 * Allocate page for PT2s and insert it to PT2TAB.
919 * In other words, map it into PT2MAP space.
921 pt2pg_pa = pmap_preboot_get_pages(1);
922 pt2tab_store(pte2p, PTE2_KPT(pt2pg_pa));
924 /* Zero all PT2s in allocated page. */
925 bzero((void*)pt2map_pt2pg(va), PAGE_SIZE);
926 pte2_sync_range((pt2_entry_t *)pt2map_pt2pg(va), PAGE_SIZE);
932 * Setup L2 page table for given KVA.
933 * Used in pre-bootstrap epoch.
936 pmap_preboot_pt2_setup(vm_offset_t va)
939 vm_paddr_t pt2pg_pa, pt2_pa;
941 /* Setup PT2's page. */
942 pt2pg_pa = pmap_preboot_pt2pg_setup(va);
943 pt2_pa = page_pt2pa(pt2pg_pa, pte1_index(va));
945 /* Insert PT2 to PT1. */
946 pte1p = kern_pte1(va);
947 pte1_store(pte1p, PTE1_LINK(pt2_pa));
951 * Get L2 page entry associated with given KVA.
952 * Used in pre-bootstrap epoch.
954 static __inline pt2_entry_t*
955 pmap_preboot_vtopte2(vm_offset_t va)
959 /* Setup PT2 if needed. */
960 pte1p = kern_pte1(va);
961 if (!pte1_is_valid(pte1_load(pte1p))) /* XXX - sections ?! */
962 pmap_preboot_pt2_setup(va);
964 return (pt2map_entry(va));
968 * Pre-bootstrap epoch page(s) mapping(s).
971 pmap_preboot_map_pages(vm_paddr_t pa, vm_offset_t va, u_int num)
976 /* Map all the pages. */
977 for (i = 0; i < num; i++) {
978 pte2p = pmap_preboot_vtopte2(va);
979 pte2_store(pte2p, PTE2_KRW(pa));
986 * Pre-bootstrap epoch virtual space alocator.
989 pmap_preboot_reserve_pages(u_int num)
992 vm_offset_t start, va;
995 /* Allocate virtual space. */
996 start = va = virtual_avail;
997 virtual_avail += num * PAGE_SIZE;
999 /* Zero the mapping. */
1000 for (i = 0; i < num; i++) {
1001 pte2p = pmap_preboot_vtopte2(va);
1002 pte2_store(pte2p, 0);
1010 * Pre-bootstrap epoch page(s) allocation and mapping(s).
1013 pmap_preboot_get_vpages(u_int num)
1018 /* Allocate physical page(s). */
1019 pa = pmap_preboot_get_pages(num);
1021 /* Allocate virtual space. */
1023 virtual_avail += num * PAGE_SIZE;
1025 /* Map and zero all. */
1026 pmap_preboot_map_pages(pa, va, num);
1027 bzero((void *)va, num * PAGE_SIZE);
1033 * Pre-bootstrap epoch page mapping(s) with attributes.
1036 pmap_preboot_map_attr(vm_paddr_t pa, vm_offset_t va, vm_size_t size,
1037 vm_prot_t prot, vm_memattr_t attr)
1040 u_int l1_attr, l1_prot, l2_prot, l2_attr;
1044 l2_prot = prot & VM_PROT_WRITE ? PTE2_AP_KRW : PTE2_AP_KR;
1045 l2_prot |= (prot & VM_PROT_EXECUTE) ? PTE2_X : PTE2_NX;
1046 l2_attr = vm_memattr_to_pte2(attr);
1047 l1_prot = ATTR_TO_L1(l2_prot);
1048 l1_attr = ATTR_TO_L1(l2_attr);
1050 /* Map all the pages. */
1051 num = round_page(size);
1053 if ((((va | pa) & PTE1_OFFSET) == 0) && (num >= PTE1_SIZE)) {
1054 pte1p = kern_pte1(va);
1055 pte1_store(pte1p, PTE1_KERN(pa, l1_prot, l1_attr));
1060 pte2p = pmap_preboot_vtopte2(va);
1061 pte2_store(pte2p, PTE2_KERN(pa, l2_prot, l2_attr));
1070 * Extract from the kernel page table the physical address
1071 * that is mapped by the given virtual address "va".
1074 pmap_kextract(vm_offset_t va)
1080 pte1 = pte1_load(kern_pte1(va));
1081 if (pte1_is_section(pte1)) {
1082 pa = pte1_pa(pte1) | (va & PTE1_OFFSET);
1083 } else if (pte1_is_link(pte1)) {
1085 * We should beware of concurrent promotion that changes
1086 * pte1 at this point. However, it's not a problem as PT2
1087 * page is preserved by promotion in PT2TAB. So even if
1088 * it happens, using of PT2MAP is still safe.
1090 * QQQ: However, concurrent removing is a problem which
1091 * ends in abort on PT2MAP space. Locking must be used
1092 * to deal with this.
1094 pte2 = pte2_load(pt2map_entry(va));
1095 pa = pte2_pa(pte2) | (va & PTE2_OFFSET);
1098 panic("%s: va %#x pte1 %#x", __func__, va, pte1);
1104 * Extract from the kernel page table the physical address
1105 * that is mapped by the given virtual address "va". Also
1106 * return L2 page table entry which maps the address.
1108 * This is only intended to be used for panic dumps.
1111 pmap_dump_kextract(vm_offset_t va, pt2_entry_t *pte2p)
1117 pte1 = pte1_load(kern_pte1(va));
1118 if (pte1_is_section(pte1)) {
1119 pa = pte1_pa(pte1) | (va & PTE1_OFFSET);
1120 pte2 = pa | ATTR_TO_L2(pte1) | PTE2_V;
1121 } else if (pte1_is_link(pte1)) {
1122 pte2 = pte2_load(pt2map_entry(va));
1133 /*****************************************************************************
1135 * PMAP second stage initialization and utility functions
1136 * for bootstrap epoch.
1138 * After pmap_bootstrap() is called, the following functions for
1139 * mappings can be used:
1141 * void pmap_kenter(vm_offset_t va, vm_paddr_t pa);
1142 * void pmap_kremove(vm_offset_t va);
1143 * vm_offset_t pmap_map(vm_offset_t *virt, vm_paddr_t start, vm_paddr_t end,
1146 * NOTE: This is not SMP coherent stage. And physical page allocation is not
1147 * allowed during this stage.
1149 *****************************************************************************/
1152 * Initialize kernel PMAP locks and lists, kernel_pmap itself, and
1153 * reserve various virtual spaces for temporary mappings.
1156 pmap_bootstrap(vm_offset_t firstaddr)
1158 pt2_entry_t *unused __unused;
1162 * Initialize the kernel pmap (which is statically allocated).
1164 PMAP_LOCK_INIT(kernel_pmap);
1165 kernel_l1pa = (vm_paddr_t)kern_pt1; /* for libkvm */
1166 kernel_pmap->pm_pt1 = kern_pt1;
1167 kernel_pmap->pm_pt2tab = kern_pt2tab;
1168 CPU_FILL(&kernel_pmap->pm_active); /* don't allow deactivation */
1169 TAILQ_INIT(&kernel_pmap->pm_pvchunk);
1172 * Initialize the global pv list lock.
1174 rw_init(&pvh_global_lock, "pmap pv global");
1176 LIST_INIT(&allpmaps);
1179 * Request a spin mutex so that changes to allpmaps cannot be
1180 * preempted by smp_rendezvous_cpus().
1182 mtx_init(&allpmaps_lock, "allpmaps", NULL, MTX_SPIN);
1183 mtx_lock_spin(&allpmaps_lock);
1184 LIST_INSERT_HEAD(&allpmaps, kernel_pmap, pm_list);
1185 mtx_unlock_spin(&allpmaps_lock);
1188 * Reserve some special page table entries/VA space for temporary
1191 #define SYSMAP(c, p, v, n) do { \
1192 v = (c)pmap_preboot_reserve_pages(n); \
1193 p = pt2map_entry((vm_offset_t)v); \
1197 * Local CMAP1/CMAP2 are used for zeroing and copying pages.
1198 * Local CMAP2 is also used for data cache cleaning.
1201 mtx_init(&pc->pc_cmap_lock, "SYSMAPS", NULL, MTX_DEF);
1202 SYSMAP(caddr_t, pc->pc_cmap1_pte2p, pc->pc_cmap1_addr, 1);
1203 SYSMAP(caddr_t, pc->pc_cmap2_pte2p, pc->pc_cmap2_addr, 1);
1204 SYSMAP(vm_offset_t, pc->pc_qmap_pte2p, pc->pc_qmap_addr, 1);
1209 SYSMAP(caddr_t, unused, crashdumpmap, MAXDUMPPGS);
1212 * _tmppt is used for reading arbitrary physical pages via /dev/mem.
1214 SYSMAP(caddr_t, unused, _tmppt, 1);
1217 * PADDR1 and PADDR2 are used by pmap_pte2_quick() and pmap_pte2(),
1218 * respectively. PADDR3 is used by pmap_pte2_ddb().
1220 SYSMAP(pt2_entry_t *, PMAP1, PADDR1, 1);
1221 SYSMAP(pt2_entry_t *, PMAP2, PADDR2, 1);
1223 SYSMAP(pt2_entry_t *, PMAP3, PADDR3, 1);
1225 mtx_init(&PMAP2mutex, "PMAP2", NULL, MTX_DEF);
1228 * Note that in very short time in initarm(), we are going to
1229 * initialize phys_avail[] array and no further page allocation
1230 * can happen after that until vm subsystem will be initialized.
1232 kernel_vm_end_new = kernel_vm_end;
1233 virtual_end = vm_max_kernel_address;
1237 pmap_init_reserved_pages(void)
1246 * Skip if the mapping has already been initialized,
1247 * i.e. this is the BSP.
1249 if (pc->pc_cmap1_addr != 0)
1251 mtx_init(&pc->pc_cmap_lock, "SYSMAPS", NULL, MTX_DEF);
1252 pages = kva_alloc(PAGE_SIZE * 3);
1254 panic("%s: unable to allocate KVA", __func__);
1255 pc->pc_cmap1_pte2p = pt2map_entry(pages);
1256 pc->pc_cmap2_pte2p = pt2map_entry(pages + PAGE_SIZE);
1257 pc->pc_qmap_pte2p = pt2map_entry(pages + (PAGE_SIZE * 2));
1258 pc->pc_cmap1_addr = (caddr_t)pages;
1259 pc->pc_cmap2_addr = (caddr_t)(pages + PAGE_SIZE);
1260 pc->pc_qmap_addr = pages + (PAGE_SIZE * 2);
1263 SYSINIT(rpages_init, SI_SUB_CPU, SI_ORDER_ANY, pmap_init_reserved_pages, NULL);
1266 * The function can already be use in second initialization stage.
1267 * As such, the function DOES NOT call pmap_growkernel() where PT2
1268 * allocation can happen. So if used, be sure that PT2 for given
1269 * virtual address is allocated already!
1271 * Add a wired page to the kva.
1272 * Note: not SMP coherent.
1274 static __inline void
1275 pmap_kenter_prot_attr(vm_offset_t va, vm_paddr_t pa, uint32_t prot,
1281 pte1p = kern_pte1(va);
1282 if (!pte1_is_valid(pte1_load(pte1p))) { /* XXX - sections ?! */
1284 * This is a very low level function, so PT2 and particularly
1285 * PT2PG associated with given virtual address must be already
1286 * allocated. It's a pain mainly during pmap initialization
1287 * stage. However, called after pmap initialization with
1288 * virtual address not under kernel_vm_end will lead to
1291 if (!pte2_is_valid(pte2_load(kern_pt2tab_entry(va))))
1292 panic("%s: kernel PT2 not allocated!", __func__);
1295 pte2p = pt2map_entry(va);
1296 pte2_store(pte2p, PTE2_KERN(pa, prot, attr));
1300 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
1303 pmap_kenter_prot_attr(va, pa, PTE2_AP_KRW, PTE2_ATTR_DEFAULT);
1307 * Remove a page from the kernel pagetables.
1308 * Note: not SMP coherent.
1311 pmap_kremove(vm_offset_t va)
1315 pte2p = pt2map_entry(va);
1320 * Share new kernel PT2PG with all pmaps.
1321 * The caller is responsible for maintaining TLB consistency.
1324 pmap_kenter_pt2tab(vm_offset_t va, pt2_entry_t npte2)
1329 mtx_lock_spin(&allpmaps_lock);
1330 LIST_FOREACH(pmap, &allpmaps, pm_list) {
1331 pte2p = pmap_pt2tab_entry(pmap, va);
1332 pt2tab_store(pte2p, npte2);
1334 mtx_unlock_spin(&allpmaps_lock);
1338 * Share new kernel PTE1 with all pmaps.
1339 * The caller is responsible for maintaining TLB consistency.
1342 pmap_kenter_pte1(vm_offset_t va, pt1_entry_t npte1)
1347 mtx_lock_spin(&allpmaps_lock);
1348 LIST_FOREACH(pmap, &allpmaps, pm_list) {
1349 pte1p = pmap_pte1(pmap, va);
1350 pte1_store(pte1p, npte1);
1352 mtx_unlock_spin(&allpmaps_lock);
1356 * Used to map a range of physical addresses into kernel
1357 * virtual address space.
1359 * The value passed in '*virt' is a suggested virtual address for
1360 * the mapping. Architectures which can support a direct-mapped
1361 * physical to virtual region can return the appropriate address
1362 * within that region, leaving '*virt' unchanged. Other
1363 * architectures should map the pages starting at '*virt' and
1364 * update '*virt' with the first usable address after the mapped
1367 * NOTE: Read the comments above pmap_kenter_prot_attr() as
1368 * the function is used herein!
1371 pmap_map(vm_offset_t *virt, vm_paddr_t start, vm_paddr_t end, int prot)
1373 vm_offset_t va, sva;
1374 vm_paddr_t pte1_offset;
1376 uint32_t l1prot, l2prot;
1377 uint32_t l1attr, l2attr;
1379 PDEBUG(1, printf("%s: virt = %#x, start = %#x, end = %#x (size = %#x),"
1380 " prot = %d\n", __func__, *virt, start, end, end - start, prot));
1382 l2prot = (prot & VM_PROT_WRITE) ? PTE2_AP_KRW : PTE2_AP_KR;
1383 l2prot |= (prot & VM_PROT_EXECUTE) ? PTE2_X : PTE2_NX;
1384 l1prot = ATTR_TO_L1(l2prot);
1386 l2attr = PTE2_ATTR_DEFAULT;
1387 l1attr = ATTR_TO_L1(l2attr);
1391 * Does the physical address range's size and alignment permit at
1392 * least one section mapping to be created?
1394 pte1_offset = start & PTE1_OFFSET;
1395 if ((end - start) - ((PTE1_SIZE - pte1_offset) & PTE1_OFFSET) >=
1398 * Increase the starting virtual address so that its alignment
1399 * does not preclude the use of section mappings.
1401 if ((va & PTE1_OFFSET) < pte1_offset)
1402 va = pte1_trunc(va) + pte1_offset;
1403 else if ((va & PTE1_OFFSET) > pte1_offset)
1404 va = pte1_roundup(va) + pte1_offset;
1407 while (start < end) {
1408 if ((start & PTE1_OFFSET) == 0 && end - start >= PTE1_SIZE) {
1409 KASSERT((va & PTE1_OFFSET) == 0,
1410 ("%s: misaligned va %#x", __func__, va));
1411 npte1 = PTE1_KERN(start, l1prot, l1attr);
1412 pmap_kenter_pte1(va, npte1);
1416 pmap_kenter_prot_attr(va, start, l2prot, l2attr);
1421 tlb_flush_range(sva, va - sva);
1427 * Make a temporary mapping for a physical address.
1428 * This is only intended to be used for panic dumps.
1431 pmap_kenter_temporary(vm_paddr_t pa, int i)
1435 /* QQQ: 'i' should be less or equal to MAXDUMPPGS. */
1437 va = (vm_offset_t)crashdumpmap + (i * PAGE_SIZE);
1438 pmap_kenter(va, pa);
1439 tlb_flush_local(va);
1440 return ((void *)crashdumpmap);
1444 /*************************************
1446 * TLB & cache maintenance routines.
1448 *************************************/
1451 * We inline these within pmap.c for speed.
1454 pmap_tlb_flush(pmap_t pmap, vm_offset_t va)
1457 if (pmap == kernel_pmap || !CPU_EMPTY(&pmap->pm_active))
1462 pmap_tlb_flush_range(pmap_t pmap, vm_offset_t sva, vm_size_t size)
1465 if (pmap == kernel_pmap || !CPU_EMPTY(&pmap->pm_active))
1466 tlb_flush_range(sva, size);
1470 * Abuse the pte2 nodes for unmapped kva to thread a kva freelist through.
1472 * - Must deal with pages in order to ensure that none of the PTE2_* bits
1473 * are ever set, PTE2_V in particular.
1474 * - Assumes we can write to pte2s without pte2_store() atomic ops.
1475 * - Assumes nothing will ever test these addresses for 0 to indicate
1476 * no mapping instead of correctly checking PTE2_V.
1477 * - Assumes a vm_offset_t will fit in a pte2 (true for arm).
1478 * Because PTE2_V is never set, there can be no mappings to invalidate.
1481 pmap_pte2list_alloc(vm_offset_t *head)
1488 panic("pmap_ptelist_alloc: exhausted ptelist KVA");
1489 pte2p = pt2map_entry(va);
1492 panic("%s: va with PTE2_V set!", __func__);
1498 pmap_pte2list_free(vm_offset_t *head, vm_offset_t va)
1503 panic("%s: freeing va with PTE2_V set!", __func__);
1504 pte2p = pt2map_entry(va);
1505 *pte2p = *head; /* virtual! PTE2_V is 0 though */
1510 pmap_pte2list_init(vm_offset_t *head, void *base, int npages)
1516 for (i = npages - 1; i >= 0; i--) {
1517 va = (vm_offset_t)base + i * PAGE_SIZE;
1518 pmap_pte2list_free(head, va);
1522 /*****************************************************************************
1524 * PMAP third and final stage initialization.
1526 * After pmap_init() is called, PMAP subsystem is fully initialized.
1528 *****************************************************************************/
1530 SYSCTL_NODE(_vm, OID_AUTO, pmap, CTLFLAG_RD, 0, "VM/pmap parameters");
1532 SYSCTL_INT(_vm_pmap, OID_AUTO, pv_entry_max, CTLFLAG_RD, &pv_entry_max, 0,
1533 "Max number of PV entries");
1534 SYSCTL_INT(_vm_pmap, OID_AUTO, shpgperproc, CTLFLAG_RD, &shpgperproc, 0,
1535 "Page share factor per proc");
1537 static u_long nkpt2pg = NKPT2PG;
1538 SYSCTL_ULONG(_vm_pmap, OID_AUTO, nkpt2pg, CTLFLAG_RD,
1539 &nkpt2pg, 0, "Pre-allocated pages for kernel PT2s");
1541 static int sp_enabled = 1;
1542 SYSCTL_INT(_vm_pmap, OID_AUTO, sp_enabled, CTLFLAG_RDTUN | CTLFLAG_NOFETCH,
1543 &sp_enabled, 0, "Are large page mappings enabled?");
1545 static SYSCTL_NODE(_vm_pmap, OID_AUTO, pte1, CTLFLAG_RD, 0,
1546 "1MB page mapping counters");
1548 static u_long pmap_pte1_demotions;
1549 SYSCTL_ULONG(_vm_pmap_pte1, OID_AUTO, demotions, CTLFLAG_RD,
1550 &pmap_pte1_demotions, 0, "1MB page demotions");
1552 static u_long pmap_pte1_mappings;
1553 SYSCTL_ULONG(_vm_pmap_pte1, OID_AUTO, mappings, CTLFLAG_RD,
1554 &pmap_pte1_mappings, 0, "1MB page mappings");
1556 static u_long pmap_pte1_p_failures;
1557 SYSCTL_ULONG(_vm_pmap_pte1, OID_AUTO, p_failures, CTLFLAG_RD,
1558 &pmap_pte1_p_failures, 0, "1MB page promotion failures");
1560 static u_long pmap_pte1_promotions;
1561 SYSCTL_ULONG(_vm_pmap_pte1, OID_AUTO, promotions, CTLFLAG_RD,
1562 &pmap_pte1_promotions, 0, "1MB page promotions");
1564 static u_long pmap_pte1_kern_demotions;
1565 SYSCTL_ULONG(_vm_pmap_pte1, OID_AUTO, kern_demotions, CTLFLAG_RD,
1566 &pmap_pte1_kern_demotions, 0, "1MB page kernel demotions");
1568 static u_long pmap_pte1_kern_promotions;
1569 SYSCTL_ULONG(_vm_pmap_pte1, OID_AUTO, kern_promotions, CTLFLAG_RD,
1570 &pmap_pte1_kern_promotions, 0, "1MB page kernel promotions");
1572 static __inline ttb_entry_t
1573 pmap_ttb_get(pmap_t pmap)
1576 return (vtophys(pmap->pm_pt1) | ttb_flags);
1580 * Initialize a vm_page's machine-dependent fields.
1583 * 1. Pages for L2 page tables are always not managed. So, pv_list and
1584 * pt2_wirecount can share same physical space. However, proper
1585 * initialization on a page alloc for page tables and reinitialization
1586 * on the page free must be ensured.
1589 pmap_page_init(vm_page_t m)
1592 TAILQ_INIT(&m->md.pv_list);
1593 pt2_wirecount_init(m);
1594 m->md.pat_mode = VM_MEMATTR_DEFAULT;
1598 * Virtualization for faster way how to zero whole page.
1600 static __inline void
1601 pagezero(void *page)
1604 bzero(page, PAGE_SIZE);
1608 * Zero L2 page table page.
1609 * Use same KVA as in pmap_zero_page().
1611 static __inline vm_paddr_t
1612 pmap_pt2pg_zero(vm_page_t m)
1614 pt2_entry_t *cmap2_pte2p;
1618 pa = VM_PAGE_TO_PHYS(m);
1621 * XXX: For now, we map whole page even if it's already zero,
1622 * to sync it even if the sync is only DSB.
1626 cmap2_pte2p = pc->pc_cmap2_pte2p;
1627 mtx_lock(&pc->pc_cmap_lock);
1628 if (pte2_load(cmap2_pte2p) != 0)
1629 panic("%s: CMAP2 busy", __func__);
1630 pte2_store(cmap2_pte2p, PTE2_KERN_NG(pa, PTE2_AP_KRW,
1631 vm_page_pte2_attr(m)));
1632 /* Even VM_ALLOC_ZERO request is only advisory. */
1633 if ((m->flags & PG_ZERO) == 0)
1634 pagezero(pc->pc_cmap2_addr);
1635 pte2_sync_range((pt2_entry_t *)pc->pc_cmap2_addr, PAGE_SIZE);
1636 pte2_clear(cmap2_pte2p);
1637 tlb_flush((vm_offset_t)pc->pc_cmap2_addr);
1640 * Unpin the thread before releasing the lock. Otherwise the thread
1641 * could be rescheduled while still bound to the current CPU, only
1642 * to unpin itself immediately upon resuming execution.
1645 mtx_unlock(&pc->pc_cmap_lock);
1651 * Init just allocated page as L2 page table(s) holder
1652 * and return its physical address.
1654 static __inline vm_paddr_t
1655 pmap_pt2pg_init(pmap_t pmap, vm_offset_t va, vm_page_t m)
1660 /* Check page attributes. */
1661 if (m->md.pat_mode != pt_memattr)
1662 pmap_page_set_memattr(m, pt_memattr);
1664 /* Zero page and init wire counts. */
1665 pa = pmap_pt2pg_zero(m);
1666 pt2_wirecount_init(m);
1669 * Map page to PT2MAP address space for given pmap.
1670 * Note that PT2MAP space is shared with all pmaps.
1672 if (pmap == kernel_pmap)
1673 pmap_kenter_pt2tab(va, PTE2_KPT(pa));
1675 pte2p = pmap_pt2tab_entry(pmap, va);
1676 pt2tab_store(pte2p, PTE2_KPT_NG(pa));
1683 * Initialize the pmap module.
1684 * Called by vm_init, to initialize any structures that the pmap
1685 * system needs to map virtual memory.
1691 pt2_entry_t *pte2p, pte2;
1692 u_int i, pte1_idx, pv_npg;
1694 PDEBUG(1, printf("%s: phys_start = %#x\n", __func__, PHYSADDR));
1697 * Initialize the vm page array entries for kernel pmap's
1698 * L2 page table pages allocated in advance.
1700 pte1_idx = pte1_index(KERNBASE - PT2MAP_SIZE);
1701 pte2p = kern_pt2tab_entry(KERNBASE - PT2MAP_SIZE);
1702 for (i = 0; i < nkpt2pg + NPG_IN_PT2TAB; i++, pte2p++) {
1706 pte2 = pte2_load(pte2p);
1707 KASSERT(pte2_is_valid(pte2), ("%s: no valid entry", __func__));
1710 m = PHYS_TO_VM_PAGE(pa);
1711 KASSERT(m >= vm_page_array &&
1712 m < &vm_page_array[vm_page_array_size],
1713 ("%s: L2 page table page is out of range", __func__));
1715 m->pindex = pte1_idx;
1717 pte1_idx += NPT2_IN_PG;
1721 * Initialize the address space (zone) for the pv entries. Set a
1722 * high water mark so that the system can recover from excessive
1723 * numbers of pv entries.
1725 TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
1726 pv_entry_max = shpgperproc * maxproc + vm_cnt.v_page_count;
1727 TUNABLE_INT_FETCH("vm.pmap.pv_entries", &pv_entry_max);
1728 pv_entry_max = roundup(pv_entry_max, _NPCPV);
1729 pv_entry_high_water = 9 * (pv_entry_max / 10);
1732 * Are large page mappings enabled?
1734 TUNABLE_INT_FETCH("vm.pmap.sp_enabled", &sp_enabled);
1736 KASSERT(MAXPAGESIZES > 1 && pagesizes[1] == 0,
1737 ("%s: can't assign to pagesizes[1]", __func__));
1738 pagesizes[1] = PTE1_SIZE;
1742 * Calculate the size of the pv head table for sections.
1743 * Handle the possibility that "vm_phys_segs[...].end" is zero.
1744 * Note that the table is only for sections which could be promoted.
1746 first_managed_pa = pte1_trunc(vm_phys_segs[0].start);
1747 pv_npg = (pte1_trunc(vm_phys_segs[vm_phys_nsegs - 1].end - PAGE_SIZE)
1748 - first_managed_pa) / PTE1_SIZE + 1;
1751 * Allocate memory for the pv head table for sections.
1753 s = (vm_size_t)(pv_npg * sizeof(struct md_page));
1755 pv_table = (struct md_page *)kmem_malloc(kernel_arena, s,
1757 for (i = 0; i < pv_npg; i++)
1758 TAILQ_INIT(&pv_table[i].pv_list);
1760 pv_maxchunks = MAX(pv_entry_max / _NPCPV, maxproc);
1761 pv_chunkbase = (struct pv_chunk *)kva_alloc(PAGE_SIZE * pv_maxchunks);
1762 if (pv_chunkbase == NULL)
1763 panic("%s: not enough kvm for pv chunks", __func__);
1764 pmap_pte2list_init(&pv_vafree, pv_chunkbase, pv_maxchunks);
1768 * Add a list of wired pages to the kva
1769 * this routine is only used for temporary
1770 * kernel mappings that do not need to have
1771 * page modification or references recorded.
1772 * Note that old mappings are simply written
1773 * over. The page *must* be wired.
1774 * Note: SMP coherent. Uses a ranged shootdown IPI.
1777 pmap_qenter(vm_offset_t sva, vm_page_t *ma, int count)
1780 pt2_entry_t *epte2p, *pte2p, pte2;
1785 pte2p = pt2map_entry(sva);
1786 epte2p = pte2p + count;
1787 while (pte2p < epte2p) {
1789 pa = VM_PAGE_TO_PHYS(m);
1790 pte2 = pte2_load(pte2p);
1791 if ((pte2_pa(pte2) != pa) ||
1792 (pte2_attr(pte2) != vm_page_pte2_attr(m))) {
1794 pte2_store(pte2p, PTE2_KERN(pa, PTE2_AP_KRW,
1795 vm_page_pte2_attr(m)));
1799 if (__predict_false(anychanged))
1800 tlb_flush_range(sva, count * PAGE_SIZE);
1804 * This routine tears out page mappings from the
1805 * kernel -- it is meant only for temporary mappings.
1806 * Note: SMP coherent. Uses a ranged shootdown IPI.
1809 pmap_qremove(vm_offset_t sva, int count)
1814 while (count-- > 0) {
1818 tlb_flush_range(sva, va - sva);
1822 * Are we current address space or kernel?
1825 pmap_is_current(pmap_t pmap)
1828 return (pmap == kernel_pmap ||
1829 (pmap == vmspace_pmap(curthread->td_proc->p_vmspace)));
1833 * If the given pmap is not the current or kernel pmap, the returned
1834 * pte2 must be released by passing it to pmap_pte2_release().
1836 static pt2_entry_t *
1837 pmap_pte2(pmap_t pmap, vm_offset_t va)
1840 vm_paddr_t pt2pg_pa;
1842 pte1 = pte1_load(pmap_pte1(pmap, va));
1843 if (pte1_is_section(pte1))
1844 panic("%s: attempt to map PTE1", __func__);
1845 if (pte1_is_link(pte1)) {
1846 /* Are we current address space or kernel? */
1847 if (pmap_is_current(pmap))
1848 return (pt2map_entry(va));
1849 /* Note that L2 page table size is not equal to PAGE_SIZE. */
1850 pt2pg_pa = trunc_page(pte1_link_pa(pte1));
1851 mtx_lock(&PMAP2mutex);
1852 if (pte2_pa(pte2_load(PMAP2)) != pt2pg_pa) {
1853 pte2_store(PMAP2, PTE2_KPT(pt2pg_pa));
1854 tlb_flush((vm_offset_t)PADDR2);
1856 return (PADDR2 + (arm32_btop(va) & (NPTE2_IN_PG - 1)));
1862 * Releases a pte2 that was obtained from pmap_pte2().
1863 * Be prepared for the pte2p being NULL.
1865 static __inline void
1866 pmap_pte2_release(pt2_entry_t *pte2p)
1869 if ((pt2_entry_t *)(trunc_page((vm_offset_t)pte2p)) == PADDR2) {
1870 mtx_unlock(&PMAP2mutex);
1875 * Super fast pmap_pte2 routine best used when scanning
1876 * the pv lists. This eliminates many coarse-grained
1877 * invltlb calls. Note that many of the pv list
1878 * scans are across different pmaps. It is very wasteful
1879 * to do an entire tlb flush for checking a single mapping.
1881 * If the given pmap is not the current pmap, pvh_global_lock
1882 * must be held and curthread pinned to a CPU.
1884 static pt2_entry_t *
1885 pmap_pte2_quick(pmap_t pmap, vm_offset_t va)
1888 vm_paddr_t pt2pg_pa;
1890 pte1 = pte1_load(pmap_pte1(pmap, va));
1891 if (pte1_is_section(pte1))
1892 panic("%s: attempt to map PTE1", __func__);
1893 if (pte1_is_link(pte1)) {
1894 /* Are we current address space or kernel? */
1895 if (pmap_is_current(pmap))
1896 return (pt2map_entry(va));
1897 rw_assert(&pvh_global_lock, RA_WLOCKED);
1898 KASSERT(curthread->td_pinned > 0,
1899 ("%s: curthread not pinned", __func__));
1900 /* Note that L2 page table size is not equal to PAGE_SIZE. */
1901 pt2pg_pa = trunc_page(pte1_link_pa(pte1));
1902 if (pte2_pa(pte2_load(PMAP1)) != pt2pg_pa) {
1903 pte2_store(PMAP1, PTE2_KPT(pt2pg_pa));
1905 PMAP1cpu = PCPU_GET(cpuid);
1907 tlb_flush_local((vm_offset_t)PADDR1);
1911 if (PMAP1cpu != PCPU_GET(cpuid)) {
1912 PMAP1cpu = PCPU_GET(cpuid);
1913 tlb_flush_local((vm_offset_t)PADDR1);
1918 return (PADDR1 + (arm32_btop(va) & (NPTE2_IN_PG - 1)));
1924 * Routine: pmap_extract
1926 * Extract the physical page address associated
1927 * with the given map/virtual_address pair.
1930 pmap_extract(pmap_t pmap, vm_offset_t va)
1937 pte1 = pte1_load(pmap_pte1(pmap, va));
1938 if (pte1_is_section(pte1))
1939 pa = pte1_pa(pte1) | (va & PTE1_OFFSET);
1940 else if (pte1_is_link(pte1)) {
1941 pte2p = pmap_pte2(pmap, va);
1942 pa = pte2_pa(pte2_load(pte2p)) | (va & PTE2_OFFSET);
1943 pmap_pte2_release(pte2p);
1951 * Routine: pmap_extract_and_hold
1953 * Atomically extract and hold the physical page
1954 * with the given pmap and virtual address pair
1955 * if that mapping permits the given protection.
1958 pmap_extract_and_hold(pmap_t pmap, vm_offset_t va, vm_prot_t prot)
1960 vm_paddr_t pa, lockpa;
1962 pt2_entry_t pte2, *pte2p;
1969 pte1 = pte1_load(pmap_pte1(pmap, va));
1970 if (pte1_is_section(pte1)) {
1971 if (!(pte1 & PTE1_RO) || !(prot & VM_PROT_WRITE)) {
1972 pa = pte1_pa(pte1) | (va & PTE1_OFFSET);
1973 if (vm_page_pa_tryrelock(pmap, pa, &lockpa))
1975 m = PHYS_TO_VM_PAGE(pa);
1978 } else if (pte1_is_link(pte1)) {
1979 pte2p = pmap_pte2(pmap, va);
1980 pte2 = pte2_load(pte2p);
1981 pmap_pte2_release(pte2p);
1982 if (pte2_is_valid(pte2) &&
1983 (!(pte2 & PTE2_RO) || !(prot & VM_PROT_WRITE))) {
1985 if (vm_page_pa_tryrelock(pmap, pa, &lockpa))
1987 m = PHYS_TO_VM_PAGE(pa);
1991 PA_UNLOCK_COND(lockpa);
1997 * Grow the number of kernel L2 page table entries, if needed.
2000 pmap_growkernel(vm_offset_t addr)
2003 vm_paddr_t pt2pg_pa, pt2_pa;
2007 PDEBUG(1, printf("%s: addr = %#x\n", __func__, addr));
2009 * All the time kernel_vm_end is first KVA for which underlying
2010 * L2 page table is either not allocated or linked from L1 page table
2011 * (not considering sections). Except for two possible cases:
2013 * (1) in the very beginning as long as pmap_growkernel() was
2014 * not called, it could be first unused KVA (which is not
2015 * rounded up to PTE1_SIZE),
2017 * (2) when all KVA space is mapped and kernel_map->max_offset
2018 * address is not rounded up to PTE1_SIZE. (For example,
2019 * it could be 0xFFFFFFFF.)
2021 kernel_vm_end = pte1_roundup(kernel_vm_end);
2022 mtx_assert(&kernel_map->system_mtx, MA_OWNED);
2023 addr = roundup2(addr, PTE1_SIZE);
2024 if (addr - 1 >= kernel_map->max_offset)
2025 addr = kernel_map->max_offset;
2026 while (kernel_vm_end < addr) {
2027 pte1 = pte1_load(kern_pte1(kernel_vm_end));
2028 if (pte1_is_valid(pte1)) {
2029 kernel_vm_end += PTE1_SIZE;
2030 if (kernel_vm_end - 1 >= kernel_map->max_offset) {
2031 kernel_vm_end = kernel_map->max_offset;
2038 * kernel_vm_end_new is used in pmap_pinit() when kernel
2039 * mappings are entered to new pmap all at once to avoid race
2040 * between pmap_kenter_pte1() and kernel_vm_end increase.
2041 * The same aplies to pmap_kenter_pt2tab().
2043 kernel_vm_end_new = kernel_vm_end + PTE1_SIZE;
2045 pte2 = pt2tab_load(kern_pt2tab_entry(kernel_vm_end));
2046 if (!pte2_is_valid(pte2)) {
2048 * Install new PT2s page into kernel PT2TAB.
2050 m = vm_page_alloc(NULL,
2051 pte1_index(kernel_vm_end) & ~PT2PG_MASK,
2052 VM_ALLOC_INTERRUPT | VM_ALLOC_NOOBJ |
2053 VM_ALLOC_WIRED | VM_ALLOC_ZERO);
2055 panic("%s: no memory to grow kernel", __func__);
2057 * QQQ: To link all new L2 page tables from L1 page
2058 * table now and so pmap_kenter_pte1() them
2059 * at once together with pmap_kenter_pt2tab()
2060 * could be nice speed up. However,
2061 * pmap_growkernel() does not happen so often...
2062 * QQQ: The other TTBR is another option.
2064 pt2pg_pa = pmap_pt2pg_init(kernel_pmap, kernel_vm_end,
2067 pt2pg_pa = pte2_pa(pte2);
2069 pt2_pa = page_pt2pa(pt2pg_pa, pte1_index(kernel_vm_end));
2070 pmap_kenter_pte1(kernel_vm_end, PTE1_LINK(pt2_pa));
2072 kernel_vm_end = kernel_vm_end_new;
2073 if (kernel_vm_end - 1 >= kernel_map->max_offset) {
2074 kernel_vm_end = kernel_map->max_offset;
2081 kvm_size(SYSCTL_HANDLER_ARGS)
2083 unsigned long ksize = vm_max_kernel_address - KERNBASE;
2085 return (sysctl_handle_long(oidp, &ksize, 0, req));
2087 SYSCTL_PROC(_vm, OID_AUTO, kvm_size, CTLTYPE_LONG|CTLFLAG_RD,
2088 0, 0, kvm_size, "IU", "Size of KVM");
2091 kvm_free(SYSCTL_HANDLER_ARGS)
2093 unsigned long kfree = vm_max_kernel_address - kernel_vm_end;
2095 return (sysctl_handle_long(oidp, &kfree, 0, req));
2097 SYSCTL_PROC(_vm, OID_AUTO, kvm_free, CTLTYPE_LONG|CTLFLAG_RD,
2098 0, 0, kvm_free, "IU", "Amount of KVM free");
2100 /***********************************************
2102 * Pmap allocation/deallocation routines.
2104 ***********************************************/
2107 * Initialize the pmap for the swapper process.
2110 pmap_pinit0(pmap_t pmap)
2112 PDEBUG(1, printf("%s: pmap = %p\n", __func__, pmap));
2114 PMAP_LOCK_INIT(pmap);
2117 * Kernel page table directory and pmap stuff around is already
2118 * initialized, we are using it right now and here. So, finish
2119 * only PMAP structures initialization for process0 ...
2121 * Since the L1 page table and PT2TAB is shared with the kernel pmap,
2122 * which is already included in the list "allpmaps", this pmap does
2123 * not need to be inserted into that list.
2125 pmap->pm_pt1 = kern_pt1;
2126 pmap->pm_pt2tab = kern_pt2tab;
2127 CPU_ZERO(&pmap->pm_active);
2128 PCPU_SET(curpmap, pmap);
2129 TAILQ_INIT(&pmap->pm_pvchunk);
2130 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
2131 CPU_SET(0, &pmap->pm_active);
2134 static __inline void
2135 pte1_copy_nosync(pt1_entry_t *spte1p, pt1_entry_t *dpte1p, vm_offset_t sva,
2140 idx = pte1_index(sva);
2141 count = (pte1_index(eva) - idx + 1) * sizeof(pt1_entry_t);
2142 bcopy(spte1p + idx, dpte1p + idx, count);
2145 static __inline void
2146 pt2tab_copy_nosync(pt2_entry_t *spte2p, pt2_entry_t *dpte2p, vm_offset_t sva,
2151 idx = pt2tab_index(sva);
2152 count = (pt2tab_index(eva) - idx + 1) * sizeof(pt2_entry_t);
2153 bcopy(spte2p + idx, dpte2p + idx, count);
2157 * Initialize a preallocated and zeroed pmap structure,
2158 * such as one in a vmspace structure.
2161 pmap_pinit(pmap_t pmap)
2165 vm_paddr_t pa, pt2tab_pa;
2168 PDEBUG(6, printf("%s: pmap = %p, pm_pt1 = %p\n", __func__, pmap,
2172 * No need to allocate L2 page table space yet but we do need
2173 * a valid L1 page table and PT2TAB table.
2175 * Install shared kernel mappings to these tables. It's a little
2176 * tricky as some parts of KVA are reserved for vectors, devices,
2177 * and whatever else. These parts are supposed to be above
2178 * vm_max_kernel_address. Thus two regions should be installed:
2180 * (1) <KERNBASE, kernel_vm_end),
2181 * (2) <vm_max_kernel_address, 0xFFFFFFFF>.
2183 * QQQ: The second region should be stable enough to be installed
2184 * only once in time when the tables are allocated.
2185 * QQQ: Maybe copy of both regions at once could be faster ...
2186 * QQQ: Maybe the other TTBR is an option.
2188 * Finally, install own PT2TAB table to these tables.
2191 if (pmap->pm_pt1 == NULL) {
2192 pmap->pm_pt1 = (pt1_entry_t *)kmem_alloc_contig(kernel_arena,
2193 NB_IN_PT1, M_NOWAIT | M_ZERO, 0, -1UL, NB_IN_PT1, 0,
2195 if (pmap->pm_pt1 == NULL)
2198 if (pmap->pm_pt2tab == NULL) {
2200 * QQQ: (1) PT2TAB must be contiguous. If PT2TAB is one page
2201 * only, what should be the only size for 32 bit systems,
2202 * then we could allocate it with vm_page_alloc() and all
2203 * the stuff needed as other L2 page table pages.
2204 * (2) Note that a process PT2TAB is special L2 page table
2205 * page. Its mapping in kernel_arena is permanent and can
2206 * be used no matter which process is current. Its mapping
2207 * in PT2MAP can be used only for current process.
2209 pmap->pm_pt2tab = (pt2_entry_t *)kmem_alloc_attr(kernel_arena,
2210 NB_IN_PT2TAB, M_NOWAIT | M_ZERO, 0, -1UL, pt_memattr);
2211 if (pmap->pm_pt2tab == NULL) {
2213 * QQQ: As struct pmap is allocated from UMA with
2214 * UMA_ZONE_NOFREE flag, it's important to leave
2215 * no allocation in pmap if initialization failed.
2217 kmem_free(kernel_arena, (vm_offset_t)pmap->pm_pt1,
2219 pmap->pm_pt1 = NULL;
2223 * QQQ: Each L2 page table page vm_page_t has pindex set to
2224 * pte1 index of virtual address mapped by this page.
2225 * It's not valid for non kernel PT2TABs themselves.
2226 * The pindex of these pages can not be altered because
2227 * of the way how they are allocated now. However, it
2228 * should not be a problem.
2232 mtx_lock_spin(&allpmaps_lock);
2234 * To avoid race with pmap_kenter_pte1() and pmap_kenter_pt2tab(),
2235 * kernel_vm_end_new is used here instead of kernel_vm_end.
2237 pte1_copy_nosync(kern_pt1, pmap->pm_pt1, KERNBASE,
2238 kernel_vm_end_new - 1);
2239 pte1_copy_nosync(kern_pt1, pmap->pm_pt1, vm_max_kernel_address,
2241 pt2tab_copy_nosync(kern_pt2tab, pmap->pm_pt2tab, KERNBASE,
2242 kernel_vm_end_new - 1);
2243 pt2tab_copy_nosync(kern_pt2tab, pmap->pm_pt2tab, vm_max_kernel_address,
2245 LIST_INSERT_HEAD(&allpmaps, pmap, pm_list);
2246 mtx_unlock_spin(&allpmaps_lock);
2249 * Store PT2MAP PT2 pages (a.k.a. PT2TAB) in PT2TAB itself.
2250 * I.e. self reference mapping. The PT2TAB is private, however mapped
2251 * into shared PT2MAP space, so the mapping should be not global.
2253 pt2tab_pa = vtophys(pmap->pm_pt2tab);
2254 pte2p = pmap_pt2tab_entry(pmap, (vm_offset_t)PT2MAP);
2255 for (pa = pt2tab_pa, i = 0; i < NPG_IN_PT2TAB; i++, pa += PTE2_SIZE) {
2256 pt2tab_store(pte2p++, PTE2_KPT_NG(pa));
2259 /* Insert PT2MAP PT2s into pmap PT1. */
2260 pte1p = pmap_pte1(pmap, (vm_offset_t)PT2MAP);
2261 for (pa = pt2tab_pa, i = 0; i < NPT2_IN_PT2TAB; i++, pa += NB_IN_PT2) {
2262 pte1_store(pte1p++, PTE1_LINK(pa));
2266 * Now synchronize new mapping which was made above.
2268 pte1_sync_range(pmap->pm_pt1, NB_IN_PT1);
2269 pte2_sync_range(pmap->pm_pt2tab, NB_IN_PT2TAB);
2271 CPU_ZERO(&pmap->pm_active);
2272 TAILQ_INIT(&pmap->pm_pvchunk);
2273 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
2280 pt2tab_user_is_empty(pt2_entry_t *tab)
2284 end = pt2tab_index(VM_MAXUSER_ADDRESS);
2285 for (i = 0; i < end; i++)
2286 if (tab[i] != 0) return (FALSE);
2291 * Release any resources held by the given physical map.
2292 * Called when a pmap initialized by pmap_pinit is being released.
2293 * Should only be called if the map contains no valid mappings.
2296 pmap_release(pmap_t pmap)
2299 vm_offset_t start, end;
2301 KASSERT(pmap->pm_stats.resident_count == 0,
2302 ("%s: pmap resident count %ld != 0", __func__,
2303 pmap->pm_stats.resident_count));
2304 KASSERT(pt2tab_user_is_empty(pmap->pm_pt2tab),
2305 ("%s: has allocated user PT2(s)", __func__));
2306 KASSERT(CPU_EMPTY(&pmap->pm_active),
2307 ("%s: pmap %p is active on some CPU(s)", __func__, pmap));
2309 mtx_lock_spin(&allpmaps_lock);
2310 LIST_REMOVE(pmap, pm_list);
2311 mtx_unlock_spin(&allpmaps_lock);
2314 start = pte1_index(KERNBASE) * sizeof(pt1_entry_t);
2315 end = (pte1_index(0xFFFFFFFF) + 1) * sizeof(pt1_entry_t);
2316 bzero((char *)pmap->pm_pt1 + start, end - start);
2318 start = pt2tab_index(KERNBASE) * sizeof(pt2_entry_t);
2319 end = (pt2tab_index(0xFFFFFFFF) + 1) * sizeof(pt2_entry_t);
2320 bzero((char *)pmap->pm_pt2tab + start, end - start);
2323 * We are leaving PT1 and PT2TAB allocated on released pmap,
2324 * so hopefully UMA vmspace_zone will always be inited with
2325 * UMA_ZONE_NOFREE flag.
2329 /*********************************************************
2331 * L2 table pages and their pages management routines.
2333 *********************************************************/
2336 * Virtual interface for L2 page table wire counting.
2338 * Each L2 page table in a page has own counter which counts a number of
2339 * valid mappings in a table. Global page counter counts mappings in all
2340 * tables in a page plus a single itself mapping in PT2TAB.
2342 * During a promotion we leave the associated L2 page table counter
2343 * untouched, so the table (strictly speaking a page which holds it)
2344 * is never freed if promoted.
2346 * If a page m->wire_count == 1 then no valid mappings exist in any L2 page
2347 * table in the page and the page itself is only mapped in PT2TAB.
2350 static __inline void
2351 pt2_wirecount_init(vm_page_t m)
2356 * Note: A page m is allocated with VM_ALLOC_WIRED flag and
2357 * m->wire_count should be already set correctly.
2358 * So, there is no need to set it again herein.
2360 for (i = 0; i < NPT2_IN_PG; i++)
2361 m->md.pt2_wirecount[i] = 0;
2364 static __inline void
2365 pt2_wirecount_inc(vm_page_t m, uint32_t pte1_idx)
2369 * Note: A just modificated pte2 (i.e. already allocated)
2370 * is acquiring one extra reference which must be
2371 * explicitly cleared. It influences the KASSERTs herein.
2372 * All L2 page tables in a page always belong to the same
2373 * pmap, so we allow only one extra reference for the page.
2375 KASSERT(m->md.pt2_wirecount[pte1_idx & PT2PG_MASK] < (NPTE2_IN_PT2 + 1),
2376 ("%s: PT2 is overflowing ...", __func__));
2377 KASSERT(m->wire_count <= (NPTE2_IN_PG + 1),
2378 ("%s: PT2PG is overflowing ...", __func__));
2381 m->md.pt2_wirecount[pte1_idx & PT2PG_MASK]++;
2384 static __inline void
2385 pt2_wirecount_dec(vm_page_t m, uint32_t pte1_idx)
2388 KASSERT(m->md.pt2_wirecount[pte1_idx & PT2PG_MASK] != 0,
2389 ("%s: PT2 is underflowing ...", __func__));
2390 KASSERT(m->wire_count > 1,
2391 ("%s: PT2PG is underflowing ...", __func__));
2394 m->md.pt2_wirecount[pte1_idx & PT2PG_MASK]--;
2397 static __inline void
2398 pt2_wirecount_set(vm_page_t m, uint32_t pte1_idx, uint16_t count)
2401 KASSERT(count <= NPTE2_IN_PT2,
2402 ("%s: invalid count %u", __func__, count));
2403 KASSERT(m->wire_count > m->md.pt2_wirecount[pte1_idx & PT2PG_MASK],
2404 ("%s: PT2PG corrupting (%u, %u) ...", __func__, m->wire_count,
2405 m->md.pt2_wirecount[pte1_idx & PT2PG_MASK]));
2407 m->wire_count -= m->md.pt2_wirecount[pte1_idx & PT2PG_MASK];
2408 m->wire_count += count;
2409 m->md.pt2_wirecount[pte1_idx & PT2PG_MASK] = count;
2411 KASSERT(m->wire_count <= (NPTE2_IN_PG + 1),
2412 ("%s: PT2PG is overflowed (%u) ...", __func__, m->wire_count));
2415 static __inline uint32_t
2416 pt2_wirecount_get(vm_page_t m, uint32_t pte1_idx)
2419 return (m->md.pt2_wirecount[pte1_idx & PT2PG_MASK]);
2422 static __inline boolean_t
2423 pt2_is_empty(vm_page_t m, vm_offset_t va)
2426 return (m->md.pt2_wirecount[pte1_index(va) & PT2PG_MASK] == 0);
2429 static __inline boolean_t
2430 pt2_is_full(vm_page_t m, vm_offset_t va)
2433 return (m->md.pt2_wirecount[pte1_index(va) & PT2PG_MASK] ==
2437 static __inline boolean_t
2438 pt2pg_is_empty(vm_page_t m)
2441 return (m->wire_count == 1);
2445 * This routine is called if the L2 page table
2446 * is not mapped correctly.
2449 _pmap_allocpte2(pmap_t pmap, vm_offset_t va, u_int flags)
2455 vm_paddr_t pt2pg_pa, pt2_pa;
2457 pte1_idx = pte1_index(va);
2458 pte1p = pmap->pm_pt1 + pte1_idx;
2460 KASSERT(pte1_load(pte1p) == 0,
2461 ("%s: pm_pt1[%#x] is not zero: %#x", __func__, pte1_idx,
2464 pte2 = pt2tab_load(pmap_pt2tab_entry(pmap, va));
2465 if (!pte2_is_valid(pte2)) {
2467 * Install new PT2s page into pmap PT2TAB.
2469 m = vm_page_alloc(NULL, pte1_idx & ~PT2PG_MASK,
2470 VM_ALLOC_NOOBJ | VM_ALLOC_WIRED | VM_ALLOC_ZERO);
2472 if ((flags & PMAP_ENTER_NOSLEEP) == 0) {
2474 rw_wunlock(&pvh_global_lock);
2476 rw_wlock(&pvh_global_lock);
2481 * Indicate the need to retry. While waiting,
2482 * the L2 page table page may have been allocated.
2486 pmap->pm_stats.resident_count++;
2487 pt2pg_pa = pmap_pt2pg_init(pmap, va, m);
2489 pt2pg_pa = pte2_pa(pte2);
2490 m = PHYS_TO_VM_PAGE(pt2pg_pa);
2493 pt2_wirecount_inc(m, pte1_idx);
2494 pt2_pa = page_pt2pa(pt2pg_pa, pte1_idx);
2495 pte1_store(pte1p, PTE1_LINK(pt2_pa));
2501 pmap_allocpte2(pmap_t pmap, vm_offset_t va, u_int flags)
2504 pt1_entry_t *pte1p, pte1;
2507 pte1_idx = pte1_index(va);
2509 pte1p = pmap->pm_pt1 + pte1_idx;
2510 pte1 = pte1_load(pte1p);
2513 * This supports switching from a 1MB page to a
2516 if (pte1_is_section(pte1)) {
2517 (void)pmap_demote_pte1(pmap, pte1p, va);
2519 * Reload pte1 after demotion.
2521 * Note: Demotion can even fail as either PT2 is not find for
2522 * the virtual address or PT2PG can not be allocated.
2524 pte1 = pte1_load(pte1p);
2528 * If the L2 page table page is mapped, we just increment the
2529 * hold count, and activate it.
2531 if (pte1_is_link(pte1)) {
2532 m = PHYS_TO_VM_PAGE(pte1_link_pa(pte1));
2533 pt2_wirecount_inc(m, pte1_idx);
2536 * Here if the PT2 isn't mapped, or if it has
2539 m = _pmap_allocpte2(pmap, va, flags);
2540 if (m == NULL && (flags & PMAP_ENTER_NOSLEEP) == 0)
2547 static __inline void
2548 pmap_free_zero_pages(struct spglist *free)
2552 while ((m = SLIST_FIRST(free)) != NULL) {
2553 SLIST_REMOVE_HEAD(free, plinks.s.ss);
2554 /* Preserve the page's PG_ZERO setting. */
2555 vm_page_free_toq(m);
2560 * Schedule the specified unused L2 page table page to be freed. Specifically,
2561 * add the page to the specified list of pages that will be released to the
2562 * physical memory manager after the TLB has been updated.
2564 static __inline void
2565 pmap_add_delayed_free_list(vm_page_t m, struct spglist *free)
2569 * Put page on a list so that it is released after
2570 * *ALL* TLB shootdown is done
2573 pmap_zero_page_check(m);
2575 m->flags |= PG_ZERO;
2576 SLIST_INSERT_HEAD(free, m, plinks.s.ss);
2580 * Unwire L2 page tables page.
2583 pmap_unwire_pt2pg(pmap_t pmap, vm_offset_t va, vm_page_t m)
2585 pt1_entry_t *pte1p, opte1 __unused;
2589 KASSERT(pt2pg_is_empty(m),
2590 ("%s: pmap %p PT2PG %p wired", __func__, pmap, m));
2593 * Unmap all L2 page tables in the page from L1 page table.
2595 * QQQ: Individual L2 page tables (except the last one) can be unmapped
2596 * earlier. However, we are doing that this way.
2598 KASSERT(m->pindex == (pte1_index(va) & ~PT2PG_MASK),
2599 ("%s: pmap %p va %#x PT2PG %p bad index", __func__, pmap, va, m));
2600 pte1p = pmap->pm_pt1 + m->pindex;
2601 for (i = 0; i < NPT2_IN_PG; i++, pte1p++) {
2602 KASSERT(m->md.pt2_wirecount[i] == 0,
2603 ("%s: pmap %p PT2 %u (PG %p) wired", __func__, pmap, i, m));
2604 opte1 = pte1_load(pte1p);
2605 if (pte1_is_link(opte1)) {
2608 * Flush intermediate TLB cache.
2610 pmap_tlb_flush(pmap, (m->pindex + i) << PTE1_SHIFT);
2614 KASSERT((opte1 == 0) || pte1_is_section(opte1),
2615 ("%s: pmap %p va %#x bad pte1 %x at %u", __func__,
2616 pmap, va, opte1, i));
2621 * Unmap the page from PT2TAB.
2623 pte2p = pmap_pt2tab_entry(pmap, va);
2624 (void)pt2tab_load_clear(pte2p);
2625 pmap_tlb_flush(pmap, pt2map_pt2pg(va));
2628 pmap->pm_stats.resident_count--;
2631 * This is a release store so that the ordinary store unmapping
2632 * the L2 page table page is globally performed before TLB shoot-
2635 atomic_subtract_rel_int(&vm_cnt.v_wire_count, 1);
2639 * Decrements a L2 page table page's wire count, which is used to record the
2640 * number of valid page table entries within the page. If the wire count
2641 * drops to zero, then the page table page is unmapped. Returns TRUE if the
2642 * page table page was unmapped and FALSE otherwise.
2644 static __inline boolean_t
2645 pmap_unwire_pt2(pmap_t pmap, vm_offset_t va, vm_page_t m, struct spglist *free)
2647 pt2_wirecount_dec(m, pte1_index(va));
2648 if (pt2pg_is_empty(m)) {
2650 * QQQ: Wire count is zero, so whole page should be zero and
2651 * we can set PG_ZERO flag to it.
2652 * Note that when promotion is enabled, it takes some
2653 * more efforts. See pmap_unwire_pt2_all() below.
2655 pmap_unwire_pt2pg(pmap, va, m);
2656 pmap_add_delayed_free_list(m, free);
2663 * Drop a L2 page table page's wire count at once, which is used to record
2664 * the number of valid L2 page table entries within the page. If the wire
2665 * count drops to zero, then the L2 page table page is unmapped.
2667 static __inline void
2668 pmap_unwire_pt2_all(pmap_t pmap, vm_offset_t va, vm_page_t m,
2669 struct spglist *free)
2671 u_int pte1_idx = pte1_index(va);
2673 KASSERT(m->pindex == (pte1_idx & ~PT2PG_MASK),
2674 ("%s: PT2 page's pindex is wrong", __func__));
2675 KASSERT(m->wire_count > pt2_wirecount_get(m, pte1_idx),
2676 ("%s: bad pt2 wire count %u > %u", __func__, m->wire_count,
2677 pt2_wirecount_get(m, pte1_idx)));
2680 * It's possible that the L2 page table was never used.
2681 * It happened in case that a section was created without promotion.
2683 if (pt2_is_full(m, va)) {
2684 pt2_wirecount_set(m, pte1_idx, 0);
2687 * QQQ: We clear L2 page table now, so when L2 page table page
2688 * is going to be freed, we can set it PG_ZERO flag ...
2689 * This function is called only on section mappings, so
2690 * hopefully it's not to big overload.
2692 * XXX: If pmap is current, existing PT2MAP mapping could be
2695 pmap_zero_page_area(m, page_pt2off(pte1_idx), NB_IN_PT2);
2699 KASSERT(pt2_is_empty(m, va), ("%s: PT2 is not empty (%u)",
2700 __func__, pt2_wirecount_get(m, pte1_idx)));
2702 if (pt2pg_is_empty(m)) {
2703 pmap_unwire_pt2pg(pmap, va, m);
2704 pmap_add_delayed_free_list(m, free);
2709 * After removing a L2 page table entry, this routine is used to
2710 * conditionally free the page, and manage the hold/wire counts.
2713 pmap_unuse_pt2(pmap_t pmap, vm_offset_t va, struct spglist *free)
2718 if (va >= VM_MAXUSER_ADDRESS)
2720 pte1 = pte1_load(pmap_pte1(pmap, va));
2721 mpte = PHYS_TO_VM_PAGE(pte1_link_pa(pte1));
2722 return (pmap_unwire_pt2(pmap, va, mpte, free));
2725 /*************************************
2727 * Page management routines.
2729 *************************************/
2731 CTASSERT(sizeof(struct pv_chunk) == PAGE_SIZE);
2732 CTASSERT(_NPCM == 11);
2733 CTASSERT(_NPCPV == 336);
2735 static __inline struct pv_chunk *
2736 pv_to_chunk(pv_entry_t pv)
2739 return ((struct pv_chunk *)((uintptr_t)pv & ~(uintptr_t)PAGE_MASK));
2742 #define PV_PMAP(pv) (pv_to_chunk(pv)->pc_pmap)
2744 #define PC_FREE0_9 0xfffffffful /* Free values for index 0 through 9 */
2745 #define PC_FREE10 0x0000fffful /* Free values for index 10 */
2747 static const uint32_t pc_freemask[_NPCM] = {
2748 PC_FREE0_9, PC_FREE0_9, PC_FREE0_9,
2749 PC_FREE0_9, PC_FREE0_9, PC_FREE0_9,
2750 PC_FREE0_9, PC_FREE0_9, PC_FREE0_9,
2751 PC_FREE0_9, PC_FREE10
2754 SYSCTL_INT(_vm_pmap, OID_AUTO, pv_entry_count, CTLFLAG_RD, &pv_entry_count, 0,
2755 "Current number of pv entries");
2758 static int pc_chunk_count, pc_chunk_allocs, pc_chunk_frees, pc_chunk_tryfail;
2760 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_count, CTLFLAG_RD, &pc_chunk_count, 0,
2761 "Current number of pv entry chunks");
2762 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_allocs, CTLFLAG_RD, &pc_chunk_allocs, 0,
2763 "Current number of pv entry chunks allocated");
2764 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_frees, CTLFLAG_RD, &pc_chunk_frees, 0,
2765 "Current number of pv entry chunks frees");
2766 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_tryfail, CTLFLAG_RD, &pc_chunk_tryfail,
2767 0, "Number of times tried to get a chunk page but failed.");
2769 static long pv_entry_frees, pv_entry_allocs;
2770 static int pv_entry_spare;
2772 SYSCTL_LONG(_vm_pmap, OID_AUTO, pv_entry_frees, CTLFLAG_RD, &pv_entry_frees, 0,
2773 "Current number of pv entry frees");
2774 SYSCTL_LONG(_vm_pmap, OID_AUTO, pv_entry_allocs, CTLFLAG_RD, &pv_entry_allocs,
2775 0, "Current number of pv entry allocs");
2776 SYSCTL_INT(_vm_pmap, OID_AUTO, pv_entry_spare, CTLFLAG_RD, &pv_entry_spare, 0,
2777 "Current number of spare pv entries");
2781 * Is given page managed?
2783 static __inline bool
2784 is_managed(vm_paddr_t pa)
2788 m = PHYS_TO_VM_PAGE(pa);
2791 return ((m->oflags & VPO_UNMANAGED) == 0);
2794 static __inline bool
2795 pte1_is_managed(pt1_entry_t pte1)
2798 return (is_managed(pte1_pa(pte1)));
2801 static __inline bool
2802 pte2_is_managed(pt2_entry_t pte2)
2805 return (is_managed(pte2_pa(pte2)));
2809 * We are in a serious low memory condition. Resort to
2810 * drastic measures to free some pages so we can allocate
2811 * another pv entry chunk.
2814 pmap_pv_reclaim(pmap_t locked_pmap)
2817 struct pv_chunk *pc;
2818 struct md_page *pvh;
2821 pt2_entry_t *pte2p, tpte2;
2825 struct spglist free;
2827 int bit, field, freed;
2829 PMAP_LOCK_ASSERT(locked_pmap, MA_OWNED);
2833 TAILQ_INIT(&newtail);
2834 while ((pc = TAILQ_FIRST(&pv_chunks)) != NULL && (pv_vafree == 0 ||
2835 SLIST_EMPTY(&free))) {
2836 TAILQ_REMOVE(&pv_chunks, pc, pc_lru);
2837 if (pmap != pc->pc_pmap) {
2839 if (pmap != locked_pmap)
2843 /* Avoid deadlock and lock recursion. */
2844 if (pmap > locked_pmap)
2846 else if (pmap != locked_pmap && !PMAP_TRYLOCK(pmap)) {
2848 TAILQ_INSERT_TAIL(&newtail, pc, pc_lru);
2854 * Destroy every non-wired, 4 KB page mapping in the chunk.
2857 for (field = 0; field < _NPCM; field++) {
2858 for (inuse = ~pc->pc_map[field] & pc_freemask[field];
2859 inuse != 0; inuse &= ~(1UL << bit)) {
2860 bit = ffs(inuse) - 1;
2861 pv = &pc->pc_pventry[field * 32 + bit];
2863 pte1p = pmap_pte1(pmap, va);
2864 if (pte1_is_section(pte1_load(pte1p)))
2866 pte2p = pmap_pte2(pmap, va);
2867 tpte2 = pte2_load(pte2p);
2868 if ((tpte2 & PTE2_W) == 0)
2869 tpte2 = pte2_load_clear(pte2p);
2870 pmap_pte2_release(pte2p);
2871 if ((tpte2 & PTE2_W) != 0)
2874 ("pmap_pv_reclaim: pmap %p va %#x zero pte",
2876 pmap_tlb_flush(pmap, va);
2877 m = PHYS_TO_VM_PAGE(pte2_pa(tpte2));
2878 if (pte2_is_dirty(tpte2))
2880 if ((tpte2 & PTE2_A) != 0)
2881 vm_page_aflag_set(m, PGA_REFERENCED);
2882 TAILQ_REMOVE(&m->md.pv_list, pv, pv_next);
2883 if (TAILQ_EMPTY(&m->md.pv_list) &&
2884 (m->flags & PG_FICTITIOUS) == 0) {
2885 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
2886 if (TAILQ_EMPTY(&pvh->pv_list)) {
2887 vm_page_aflag_clear(m,
2891 pc->pc_map[field] |= 1UL << bit;
2892 pmap_unuse_pt2(pmap, va, &free);
2897 TAILQ_INSERT_TAIL(&newtail, pc, pc_lru);
2900 /* Every freed mapping is for a 4 KB page. */
2901 pmap->pm_stats.resident_count -= freed;
2902 PV_STAT(pv_entry_frees += freed);
2903 PV_STAT(pv_entry_spare += freed);
2904 pv_entry_count -= freed;
2905 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
2906 for (field = 0; field < _NPCM; field++)
2907 if (pc->pc_map[field] != pc_freemask[field]) {
2908 TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc,
2910 TAILQ_INSERT_TAIL(&newtail, pc, pc_lru);
2913 * One freed pv entry in locked_pmap is
2916 if (pmap == locked_pmap)
2920 if (field == _NPCM) {
2921 PV_STAT(pv_entry_spare -= _NPCPV);
2922 PV_STAT(pc_chunk_count--);
2923 PV_STAT(pc_chunk_frees++);
2924 /* Entire chunk is free; return it. */
2925 m_pc = PHYS_TO_VM_PAGE(pmap_kextract((vm_offset_t)pc));
2926 pmap_qremove((vm_offset_t)pc, 1);
2927 pmap_pte2list_free(&pv_vafree, (vm_offset_t)pc);
2932 TAILQ_CONCAT(&pv_chunks, &newtail, pc_lru);
2934 if (pmap != locked_pmap)
2937 if (m_pc == NULL && pv_vafree != 0 && SLIST_EMPTY(&free)) {
2938 m_pc = SLIST_FIRST(&free);
2939 SLIST_REMOVE_HEAD(&free, plinks.s.ss);
2940 /* Recycle a freed page table page. */
2941 m_pc->wire_count = 1;
2942 atomic_add_int(&vm_cnt.v_wire_count, 1);
2944 pmap_free_zero_pages(&free);
2949 free_pv_chunk(struct pv_chunk *pc)
2953 TAILQ_REMOVE(&pv_chunks, pc, pc_lru);
2954 PV_STAT(pv_entry_spare -= _NPCPV);
2955 PV_STAT(pc_chunk_count--);
2956 PV_STAT(pc_chunk_frees++);
2957 /* entire chunk is free, return it */
2958 m = PHYS_TO_VM_PAGE(pmap_kextract((vm_offset_t)pc));
2959 pmap_qremove((vm_offset_t)pc, 1);
2960 vm_page_unwire(m, PQ_NONE);
2962 pmap_pte2list_free(&pv_vafree, (vm_offset_t)pc);
2966 * Free the pv_entry back to the free list.
2969 free_pv_entry(pmap_t pmap, pv_entry_t pv)
2971 struct pv_chunk *pc;
2972 int idx, field, bit;
2974 rw_assert(&pvh_global_lock, RA_WLOCKED);
2975 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2976 PV_STAT(pv_entry_frees++);
2977 PV_STAT(pv_entry_spare++);
2979 pc = pv_to_chunk(pv);
2980 idx = pv - &pc->pc_pventry[0];
2983 pc->pc_map[field] |= 1ul << bit;
2984 for (idx = 0; idx < _NPCM; idx++)
2985 if (pc->pc_map[idx] != pc_freemask[idx]) {
2987 * 98% of the time, pc is already at the head of the
2988 * list. If it isn't already, move it to the head.
2990 if (__predict_false(TAILQ_FIRST(&pmap->pm_pvchunk) !=
2992 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
2993 TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc,
2998 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
3003 * Get a new pv_entry, allocating a block from the system
3007 get_pv_entry(pmap_t pmap, boolean_t try)
3009 static const struct timeval printinterval = { 60, 0 };
3010 static struct timeval lastprint;
3013 struct pv_chunk *pc;
3016 rw_assert(&pvh_global_lock, RA_WLOCKED);
3017 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3018 PV_STAT(pv_entry_allocs++);
3020 if (pv_entry_count > pv_entry_high_water)
3021 if (ratecheck(&lastprint, &printinterval))
3022 printf("Approaching the limit on PV entries, consider "
3023 "increasing either the vm.pmap.shpgperproc or the "
3024 "vm.pmap.pv_entry_max tunable.\n");
3026 pc = TAILQ_FIRST(&pmap->pm_pvchunk);
3028 for (field = 0; field < _NPCM; field++) {
3029 if (pc->pc_map[field]) {
3030 bit = ffs(pc->pc_map[field]) - 1;
3034 if (field < _NPCM) {
3035 pv = &pc->pc_pventry[field * 32 + bit];
3036 pc->pc_map[field] &= ~(1ul << bit);
3037 /* If this was the last item, move it to tail */
3038 for (field = 0; field < _NPCM; field++)
3039 if (pc->pc_map[field] != 0) {
3040 PV_STAT(pv_entry_spare--);
3041 return (pv); /* not full, return */
3043 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
3044 TAILQ_INSERT_TAIL(&pmap->pm_pvchunk, pc, pc_list);
3045 PV_STAT(pv_entry_spare--);
3050 * Access to the pte2list "pv_vafree" is synchronized by the pvh
3051 * global lock. If "pv_vafree" is currently non-empty, it will
3052 * remain non-empty until pmap_pte2list_alloc() completes.
3054 if (pv_vafree == 0 || (m = vm_page_alloc(NULL, 0, VM_ALLOC_NORMAL |
3055 VM_ALLOC_NOOBJ | VM_ALLOC_WIRED)) == NULL) {
3058 PV_STAT(pc_chunk_tryfail++);
3061 m = pmap_pv_reclaim(pmap);
3065 PV_STAT(pc_chunk_count++);
3066 PV_STAT(pc_chunk_allocs++);
3067 pc = (struct pv_chunk *)pmap_pte2list_alloc(&pv_vafree);
3068 pmap_qenter((vm_offset_t)pc, &m, 1);
3070 pc->pc_map[0] = pc_freemask[0] & ~1ul; /* preallocated bit 0 */
3071 for (field = 1; field < _NPCM; field++)
3072 pc->pc_map[field] = pc_freemask[field];
3073 TAILQ_INSERT_TAIL(&pv_chunks, pc, pc_lru);
3074 pv = &pc->pc_pventry[0];
3075 TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc, pc_list);
3076 PV_STAT(pv_entry_spare += _NPCPV - 1);
3081 * Create a pv entry for page at pa for
3085 pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t m)
3089 rw_assert(&pvh_global_lock, RA_WLOCKED);
3090 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3091 pv = get_pv_entry(pmap, FALSE);
3093 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_next);
3096 static __inline pv_entry_t
3097 pmap_pvh_remove(struct md_page *pvh, pmap_t pmap, vm_offset_t va)
3101 rw_assert(&pvh_global_lock, RA_WLOCKED);
3102 TAILQ_FOREACH(pv, &pvh->pv_list, pv_next) {
3103 if (pmap == PV_PMAP(pv) && va == pv->pv_va) {
3104 TAILQ_REMOVE(&pvh->pv_list, pv, pv_next);
3112 pmap_pvh_free(struct md_page *pvh, pmap_t pmap, vm_offset_t va)
3116 pv = pmap_pvh_remove(pvh, pmap, va);
3117 KASSERT(pv != NULL, ("pmap_pvh_free: pv not found"));
3118 free_pv_entry(pmap, pv);
3122 pmap_remove_entry(pmap_t pmap, vm_page_t m, vm_offset_t va)
3124 struct md_page *pvh;
3126 rw_assert(&pvh_global_lock, RA_WLOCKED);
3127 pmap_pvh_free(&m->md, pmap, va);
3128 if (TAILQ_EMPTY(&m->md.pv_list) && (m->flags & PG_FICTITIOUS) == 0) {
3129 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
3130 if (TAILQ_EMPTY(&pvh->pv_list))
3131 vm_page_aflag_clear(m, PGA_WRITEABLE);
3136 pmap_pv_demote_pte1(pmap_t pmap, vm_offset_t va, vm_paddr_t pa)
3138 struct md_page *pvh;
3140 vm_offset_t va_last;
3143 rw_assert(&pvh_global_lock, RA_WLOCKED);
3144 KASSERT((pa & PTE1_OFFSET) == 0,
3145 ("pmap_pv_demote_pte1: pa is not 1mpage aligned"));
3148 * Transfer the 1mpage's pv entry for this mapping to the first
3151 pvh = pa_to_pvh(pa);
3152 va = pte1_trunc(va);
3153 pv = pmap_pvh_remove(pvh, pmap, va);
3154 KASSERT(pv != NULL, ("pmap_pv_demote_pte1: pv not found"));
3155 m = PHYS_TO_VM_PAGE(pa);
3156 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_next);
3157 /* Instantiate the remaining NPTE2_IN_PT2 - 1 pv entries. */
3158 va_last = va + PTE1_SIZE - PAGE_SIZE;
3161 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
3162 ("pmap_pv_demote_pte1: page %p is not managed", m));
3164 pmap_insert_entry(pmap, va, m);
3165 } while (va < va_last);
3168 #if VM_NRESERVLEVEL > 0
3170 pmap_pv_promote_pte1(pmap_t pmap, vm_offset_t va, vm_paddr_t pa)
3172 struct md_page *pvh;
3174 vm_offset_t va_last;
3177 rw_assert(&pvh_global_lock, RA_WLOCKED);
3178 KASSERT((pa & PTE1_OFFSET) == 0,
3179 ("pmap_pv_promote_pte1: pa is not 1mpage aligned"));
3182 * Transfer the first page's pv entry for this mapping to the
3183 * 1mpage's pv list. Aside from avoiding the cost of a call
3184 * to get_pv_entry(), a transfer avoids the possibility that
3185 * get_pv_entry() calls pmap_pv_reclaim() and that pmap_pv_reclaim()
3186 * removes one of the mappings that is being promoted.
3188 m = PHYS_TO_VM_PAGE(pa);
3189 va = pte1_trunc(va);
3190 pv = pmap_pvh_remove(&m->md, pmap, va);
3191 KASSERT(pv != NULL, ("pmap_pv_promote_pte1: pv not found"));
3192 pvh = pa_to_pvh(pa);
3193 TAILQ_INSERT_TAIL(&pvh->pv_list, pv, pv_next);
3194 /* Free the remaining NPTE2_IN_PT2 - 1 pv entries. */
3195 va_last = va + PTE1_SIZE - PAGE_SIZE;
3199 pmap_pvh_free(&m->md, pmap, va);
3200 } while (va < va_last);
3205 * Conditionally create a pv entry.
3208 pmap_try_insert_pv_entry(pmap_t pmap, vm_offset_t va, vm_page_t m)
3212 rw_assert(&pvh_global_lock, RA_WLOCKED);
3213 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3214 if (pv_entry_count < pv_entry_high_water &&
3215 (pv = get_pv_entry(pmap, TRUE)) != NULL) {
3217 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_next);
3224 * Create the pv entries for each of the pages within a section.
3227 pmap_pv_insert_pte1(pmap_t pmap, vm_offset_t va, vm_paddr_t pa)
3229 struct md_page *pvh;
3232 rw_assert(&pvh_global_lock, RA_WLOCKED);
3233 if (pv_entry_count < pv_entry_high_water &&
3234 (pv = get_pv_entry(pmap, TRUE)) != NULL) {
3236 pvh = pa_to_pvh(pa);
3237 TAILQ_INSERT_TAIL(&pvh->pv_list, pv, pv_next);
3244 pmap_tlb_flush_pte1(pmap_t pmap, vm_offset_t va, pt1_entry_t npte1)
3247 /* Kill all the small mappings or the big one only. */
3248 if (pte1_is_section(npte1))
3249 pmap_tlb_flush_range(pmap, pte1_trunc(va), PTE1_SIZE);
3251 pmap_tlb_flush(pmap, pte1_trunc(va));
3255 * Update kernel pte1 on all pmaps.
3257 * The following function is called only on one cpu with disabled interrupts.
3258 * In SMP case, smp_rendezvous_cpus() is used to stop other cpus. This way
3259 * nobody can invoke explicit hardware table walk during the update of pte1.
3260 * Unsolicited hardware table walk can still happen, invoked by speculative
3261 * data or instruction prefetch or even by speculative hardware table walk.
3263 * The break-before-make approach should be implemented here. However, it's
3264 * not so easy to do that for kernel mappings as it would be unhappy to unmap
3265 * itself unexpectedly but voluntarily.
3268 pmap_update_pte1_kernel(vm_offset_t va, pt1_entry_t npte1)
3274 * Get current pmap. Interrupts should be disabled here
3275 * so PCPU_GET() is done atomically.
3277 pmap = PCPU_GET(curpmap);
3282 * (1) Change pte1 on current pmap.
3283 * (2) Flush all obsolete TLB entries on current CPU.
3284 * (3) Change pte1 on all pmaps.
3285 * (4) Flush all obsolete TLB entries on all CPUs in SMP case.
3288 pte1p = pmap_pte1(pmap, va);
3289 pte1_store(pte1p, npte1);
3291 /* Kill all the small mappings or the big one only. */
3292 if (pte1_is_section(npte1)) {
3293 pmap_pte1_kern_promotions++;
3294 tlb_flush_range_local(pte1_trunc(va), PTE1_SIZE);
3296 pmap_pte1_kern_demotions++;
3297 tlb_flush_local(pte1_trunc(va));
3301 * In SMP case, this function is called when all cpus are at smp
3302 * rendezvous, so there is no need to use 'allpmaps_lock' lock here.
3303 * In UP case, the function is called with this lock locked.
3305 LIST_FOREACH(pmap, &allpmaps, pm_list) {
3306 pte1p = pmap_pte1(pmap, va);
3307 pte1_store(pte1p, npte1);
3311 /* Kill all the small mappings or the big one only. */
3312 if (pte1_is_section(npte1))
3313 tlb_flush_range(pte1_trunc(va), PTE1_SIZE);
3315 tlb_flush(pte1_trunc(va));
3320 struct pte1_action {
3323 u_int update; /* CPU that updates the PTE1 */
3327 pmap_update_pte1_action(void *arg)
3329 struct pte1_action *act = arg;
3331 if (act->update == PCPU_GET(cpuid))
3332 pmap_update_pte1_kernel(act->va, act->npte1);
3336 * Change pte1 on current pmap.
3337 * Note that kernel pte1 must be changed on all pmaps.
3339 * According to the architecture reference manual published by ARM,
3340 * the behaviour is UNPREDICTABLE when two or more TLB entries map the same VA.
3341 * According to this manual, UNPREDICTABLE behaviours must never happen in
3342 * a viable system. In contrast, on x86 processors, it is not specified which
3343 * TLB entry mapping the virtual address will be used, but the MMU doesn't
3344 * generate a bogus translation the way it does on Cortex-A8 rev 2 (Beaglebone
3347 * It's a problem when either promotion or demotion is being done. The pte1
3348 * update and appropriate TLB flush must be done atomically in general.
3351 pmap_change_pte1(pmap_t pmap, pt1_entry_t *pte1p, vm_offset_t va,
3355 if (pmap == kernel_pmap) {
3356 struct pte1_action act;
3361 act.update = PCPU_GET(cpuid);
3362 smp_rendezvous_cpus(all_cpus, smp_no_rendezvous_barrier,
3363 pmap_update_pte1_action, NULL, &act);
3369 * Use break-before-make approach for changing userland
3370 * mappings. It can cause L1 translation aborts on other
3371 * cores in SMP case. So, special treatment is implemented
3372 * in pmap_fault(). To reduce the likelihood that another core
3373 * will be affected by the broken mapping, disable interrupts
3374 * until the mapping change is completed.
3376 cspr = disable_interrupts(PSR_I | PSR_F);
3378 pmap_tlb_flush_pte1(pmap, va, npte1);
3379 pte1_store(pte1p, npte1);
3380 restore_interrupts(cspr);
3385 pmap_change_pte1(pmap_t pmap, pt1_entry_t *pte1p, vm_offset_t va,
3389 if (pmap == kernel_pmap) {
3390 mtx_lock_spin(&allpmaps_lock);
3391 pmap_update_pte1_kernel(va, npte1);
3392 mtx_unlock_spin(&allpmaps_lock);
3397 * Use break-before-make approach for changing userland
3398 * mappings. It's absolutely safe in UP case when interrupts
3401 cspr = disable_interrupts(PSR_I | PSR_F);
3403 pmap_tlb_flush_pte1(pmap, va, npte1);
3404 pte1_store(pte1p, npte1);
3405 restore_interrupts(cspr);
3410 #if VM_NRESERVLEVEL > 0
3412 * Tries to promote the NPTE2_IN_PT2, contiguous 4KB page mappings that are
3413 * within a single page table page (PT2) to a single 1MB page mapping.
3414 * For promotion to occur, two conditions must be met: (1) the 4KB page
3415 * mappings must map aligned, contiguous physical memory and (2) the 4KB page
3416 * mappings must have identical characteristics.
3418 * Managed (PG_MANAGED) mappings within the kernel address space are not
3419 * promoted. The reason is that kernel PTE1s are replicated in each pmap but
3420 * pmap_remove_write(), pmap_clear_modify(), and pmap_clear_reference() only
3421 * read the PTE1 from the kernel pmap.
3424 pmap_promote_pte1(pmap_t pmap, pt1_entry_t *pte1p, vm_offset_t va)
3427 pt2_entry_t *fpte2p, fpte2, fpte2_fav;
3428 pt2_entry_t *pte2p, pte2;
3429 vm_offset_t pteva __unused;
3430 vm_page_t m __unused;
3432 PDEBUG(6, printf("%s(%p): try for va %#x pte1 %#x at %p\n", __func__,
3433 pmap, va, pte1_load(pte1p), pte1p));
3435 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3438 * Examine the first PTE2 in the specified PT2. Abort if this PTE2 is
3439 * either invalid, unused, or does not map the first 4KB physical page
3440 * within a 1MB page.
3442 fpte2p = pmap_pte2_quick(pmap, pte1_trunc(va));
3443 fpte2 = pte2_load(fpte2p);
3444 if ((fpte2 & ((PTE2_FRAME & PTE1_OFFSET) | PTE2_A | PTE2_V)) !=
3445 (PTE2_A | PTE2_V)) {
3446 pmap_pte1_p_failures++;
3447 CTR3(KTR_PMAP, "%s: failure(1) for va %#x in pmap %p",
3448 __func__, va, pmap);
3451 if (pte2_is_managed(fpte2) && pmap == kernel_pmap) {
3452 pmap_pte1_p_failures++;
3453 CTR3(KTR_PMAP, "%s: failure(2) for va %#x in pmap %p",
3454 __func__, va, pmap);
3457 if ((fpte2 & (PTE2_NM | PTE2_RO)) == PTE2_NM) {
3459 * When page is not modified, PTE2_RO can be set without
3460 * a TLB invalidation.
3463 pte2_store(fpte2p, fpte2);
3467 * Examine each of the other PTE2s in the specified PT2. Abort if this
3468 * PTE2 maps an unexpected 4KB physical page or does not have identical
3469 * characteristics to the first PTE2.
3471 fpte2_fav = (fpte2 & (PTE2_FRAME | PTE2_A | PTE2_V));
3472 fpte2_fav += PTE1_SIZE - PTE2_SIZE; /* examine from the end */
3473 for (pte2p = fpte2p + NPTE2_IN_PT2 - 1; pte2p > fpte2p; pte2p--) {
3474 pte2 = pte2_load(pte2p);
3475 if ((pte2 & (PTE2_FRAME | PTE2_A | PTE2_V)) != fpte2_fav) {
3476 pmap_pte1_p_failures++;
3477 CTR3(KTR_PMAP, "%s: failure(3) for va %#x in pmap %p",
3478 __func__, va, pmap);
3481 if ((pte2 & (PTE2_NM | PTE2_RO)) == PTE2_NM) {
3483 * When page is not modified, PTE2_RO can be set
3484 * without a TLB invalidation. See note above.
3487 pte2_store(pte2p, pte2);
3488 pteva = pte1_trunc(va) | (pte2 & PTE1_OFFSET &
3490 CTR3(KTR_PMAP, "%s: protect for va %#x in pmap %p",
3491 __func__, pteva, pmap);
3493 if ((pte2 & PTE2_PROMOTE) != (fpte2 & PTE2_PROMOTE)) {
3494 pmap_pte1_p_failures++;
3495 CTR3(KTR_PMAP, "%s: failure(4) for va %#x in pmap %p",
3496 __func__, va, pmap);
3500 fpte2_fav -= PTE2_SIZE;
3503 * The page table page in its current state will stay in PT2TAB
3504 * until the PTE1 mapping the section is demoted by pmap_demote_pte1()
3505 * or destroyed by pmap_remove_pte1().
3507 * Note that L2 page table size is not equal to PAGE_SIZE.
3509 m = PHYS_TO_VM_PAGE(trunc_page(pte1_link_pa(pte1_load(pte1p))));
3510 KASSERT(m >= vm_page_array && m < &vm_page_array[vm_page_array_size],
3511 ("%s: PT2 page is out of range", __func__));
3512 KASSERT(m->pindex == (pte1_index(va) & ~PT2PG_MASK),
3513 ("%s: PT2 page's pindex is wrong", __func__));
3516 * Get pte1 from pte2 format.
3518 npte1 = (fpte2 & PTE1_FRAME) | ATTR_TO_L1(fpte2) | PTE1_V;
3521 * Promote the pv entries.
3523 if (pte2_is_managed(fpte2))
3524 pmap_pv_promote_pte1(pmap, va, pte1_pa(npte1));
3527 * Promote the mappings.
3529 pmap_change_pte1(pmap, pte1p, va, npte1);
3531 pmap_pte1_promotions++;
3532 CTR3(KTR_PMAP, "%s: success for va %#x in pmap %p",
3533 __func__, va, pmap);
3535 PDEBUG(6, printf("%s(%p): success for va %#x pte1 %#x(%#x) at %p\n",
3536 __func__, pmap, va, npte1, pte1_load(pte1p), pte1p));
3538 #endif /* VM_NRESERVLEVEL > 0 */
3541 * Zero L2 page table page.
3543 static __inline void
3544 pmap_clear_pt2(pt2_entry_t *fpte2p)
3548 for (pte2p = fpte2p; pte2p < fpte2p + NPTE2_IN_PT2; pte2p++)
3554 * Removes a 1MB page mapping from the kernel pmap.
3557 pmap_remove_kernel_pte1(pmap_t pmap, pt1_entry_t *pte1p, vm_offset_t va)
3561 pt2_entry_t *fpte2p;
3564 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3565 m = pmap_pt2_page(pmap, va);
3568 * QQQ: Is this function called only on promoted pte1?
3569 * We certainly do section mappings directly
3570 * (without promotion) in kernel !!!
3572 panic("%s: missing pt2 page", __func__);
3574 pte1_idx = pte1_index(va);
3577 * Initialize the L2 page table.
3579 fpte2p = page_pt2(pt2map_pt2pg(va), pte1_idx);
3580 pmap_clear_pt2(fpte2p);
3583 * Remove the mapping.
3585 pt2_pa = page_pt2pa(VM_PAGE_TO_PHYS(m), pte1_idx);
3586 pmap_kenter_pte1(va, PTE1_LINK(pt2_pa));
3589 * QQQ: We do not need to invalidate PT2MAP mapping
3590 * as we did not change it. I.e. the L2 page table page
3591 * was and still is mapped the same way.
3596 * Do the things to unmap a section in a process
3599 pmap_remove_pte1(pmap_t pmap, pt1_entry_t *pte1p, vm_offset_t sva,
3600 struct spglist *free)
3603 struct md_page *pvh;
3604 vm_offset_t eva, va;
3607 PDEBUG(6, printf("%s(%p): va %#x pte1 %#x at %p\n", __func__, pmap, sva,
3608 pte1_load(pte1p), pte1p));
3610 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3611 KASSERT((sva & PTE1_OFFSET) == 0,
3612 ("%s: sva is not 1mpage aligned", __func__));
3615 * Clear and invalidate the mapping. It should occupy one and only TLB
3616 * entry. So, pmap_tlb_flush() called with aligned address should be
3619 opte1 = pte1_load_clear(pte1p);
3620 pmap_tlb_flush(pmap, sva);
3622 if (pte1_is_wired(opte1))
3623 pmap->pm_stats.wired_count -= PTE1_SIZE / PAGE_SIZE;
3624 pmap->pm_stats.resident_count -= PTE1_SIZE / PAGE_SIZE;
3625 if (pte1_is_managed(opte1)) {
3626 pvh = pa_to_pvh(pte1_pa(opte1));
3627 pmap_pvh_free(pvh, pmap, sva);
3628 eva = sva + PTE1_SIZE;
3629 for (va = sva, m = PHYS_TO_VM_PAGE(pte1_pa(opte1));
3630 va < eva; va += PAGE_SIZE, m++) {
3631 if (pte1_is_dirty(opte1))
3634 vm_page_aflag_set(m, PGA_REFERENCED);
3635 if (TAILQ_EMPTY(&m->md.pv_list) &&
3636 TAILQ_EMPTY(&pvh->pv_list))
3637 vm_page_aflag_clear(m, PGA_WRITEABLE);
3640 if (pmap == kernel_pmap) {
3642 * L2 page table(s) can't be removed from kernel map as
3643 * kernel counts on it (stuff around pmap_growkernel()).
3645 pmap_remove_kernel_pte1(pmap, pte1p, sva);
3648 * Get associated L2 page table page.
3649 * It's possible that the page was never allocated.
3651 m = pmap_pt2_page(pmap, sva);
3653 pmap_unwire_pt2_all(pmap, sva, m, free);
3658 * Fills L2 page table page with mappings to consecutive physical pages.
3660 static __inline void
3661 pmap_fill_pt2(pt2_entry_t *fpte2p, pt2_entry_t npte2)
3665 for (pte2p = fpte2p; pte2p < fpte2p + NPTE2_IN_PT2; pte2p++) {
3666 pte2_store(pte2p, npte2);
3672 * Tries to demote a 1MB page mapping. If demotion fails, the
3673 * 1MB page mapping is invalidated.
3676 pmap_demote_pte1(pmap_t pmap, pt1_entry_t *pte1p, vm_offset_t va)
3678 pt1_entry_t opte1, npte1;
3679 pt2_entry_t *fpte2p, npte2;
3680 vm_paddr_t pt2pg_pa, pt2_pa;
3682 struct spglist free;
3683 uint32_t pte1_idx, isnew = 0;
3685 PDEBUG(6, printf("%s(%p): try for va %#x pte1 %#x at %p\n", __func__,
3686 pmap, va, pte1_load(pte1p), pte1p));
3688 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3690 opte1 = pte1_load(pte1p);
3691 KASSERT(pte1_is_section(opte1), ("%s: opte1 not a section", __func__));
3693 if ((opte1 & PTE1_A) == 0 || (m = pmap_pt2_page(pmap, va)) == NULL) {
3694 KASSERT(!pte1_is_wired(opte1),
3695 ("%s: PT2 page for a wired mapping is missing", __func__));
3698 * Invalidate the 1MB page mapping and return
3699 * "failure" if the mapping was never accessed or the
3700 * allocation of the new page table page fails.
3702 if ((opte1 & PTE1_A) == 0 || (m = vm_page_alloc(NULL,
3703 pte1_index(va) & ~PT2PG_MASK, VM_ALLOC_NOOBJ |
3704 VM_ALLOC_NORMAL | VM_ALLOC_WIRED)) == NULL) {
3706 pmap_remove_pte1(pmap, pte1p, pte1_trunc(va), &free);
3707 pmap_free_zero_pages(&free);
3708 CTR3(KTR_PMAP, "%s: failure for va %#x in pmap %p",
3709 __func__, va, pmap);
3712 if (va < VM_MAXUSER_ADDRESS)
3713 pmap->pm_stats.resident_count++;
3718 * We init all L2 page tables in the page even if
3719 * we are going to change everything for one L2 page
3722 pt2pg_pa = pmap_pt2pg_init(pmap, va, m);
3724 if (va < VM_MAXUSER_ADDRESS) {
3725 if (pt2_is_empty(m, va))
3726 isnew = 1; /* Demoting section w/o promotion. */
3729 KASSERT(pt2_is_full(m, va), ("%s: bad PT2 wire"
3730 " count %u", __func__,
3731 pt2_wirecount_get(m, pte1_index(va))));
3736 pt2pg_pa = VM_PAGE_TO_PHYS(m);
3737 pte1_idx = pte1_index(va);
3739 * If the pmap is current, then the PT2MAP can provide access to
3740 * the page table page (promoted L2 page tables are not unmapped).
3741 * Otherwise, temporarily map the L2 page table page (m) into
3742 * the kernel's address space at either PADDR1 or PADDR2.
3744 * Note that L2 page table size is not equal to PAGE_SIZE.
3746 if (pmap_is_current(pmap))
3747 fpte2p = page_pt2(pt2map_pt2pg(va), pte1_idx);
3748 else if (curthread->td_pinned > 0 && rw_wowned(&pvh_global_lock)) {
3749 if (pte2_pa(pte2_load(PMAP1)) != pt2pg_pa) {
3750 pte2_store(PMAP1, PTE2_KPT(pt2pg_pa));
3752 PMAP1cpu = PCPU_GET(cpuid);
3754 tlb_flush_local((vm_offset_t)PADDR1);
3758 if (PMAP1cpu != PCPU_GET(cpuid)) {
3759 PMAP1cpu = PCPU_GET(cpuid);
3760 tlb_flush_local((vm_offset_t)PADDR1);
3765 fpte2p = page_pt2((vm_offset_t)PADDR1, pte1_idx);
3767 mtx_lock(&PMAP2mutex);
3768 if (pte2_pa(pte2_load(PMAP2)) != pt2pg_pa) {
3769 pte2_store(PMAP2, PTE2_KPT(pt2pg_pa));
3770 tlb_flush((vm_offset_t)PADDR2);
3772 fpte2p = page_pt2((vm_offset_t)PADDR2, pte1_idx);
3774 pt2_pa = page_pt2pa(pt2pg_pa, pte1_idx);
3775 npte1 = PTE1_LINK(pt2_pa);
3777 KASSERT((opte1 & PTE1_A) != 0,
3778 ("%s: opte1 is missing PTE1_A", __func__));
3779 KASSERT((opte1 & (PTE1_NM | PTE1_RO)) != PTE1_NM,
3780 ("%s: opte1 has PTE1_NM", __func__));
3783 * Get pte2 from pte1 format.
3785 npte2 = pte1_pa(opte1) | ATTR_TO_L2(opte1) | PTE2_V;
3788 * If the L2 page table page is new, initialize it. If the mapping
3789 * has changed attributes, update the page table entries.
3792 pt2_wirecount_set(m, pte1_idx, NPTE2_IN_PT2);
3793 pmap_fill_pt2(fpte2p, npte2);
3794 } else if ((pte2_load(fpte2p) & PTE2_PROMOTE) !=
3795 (npte2 & PTE2_PROMOTE))
3796 pmap_fill_pt2(fpte2p, npte2);
3798 KASSERT(pte2_pa(pte2_load(fpte2p)) == pte2_pa(npte2),
3799 ("%s: fpte2p and npte2 map different physical addresses",
3802 if (fpte2p == PADDR2)
3803 mtx_unlock(&PMAP2mutex);
3806 * Demote the mapping. This pmap is locked. The old PTE1 has
3807 * PTE1_A set. If the old PTE1 has not PTE1_RO set, it also
3808 * has not PTE1_NM set. Thus, there is no danger of a race with
3809 * another processor changing the setting of PTE1_A and/or PTE1_NM
3810 * between the read above and the store below.
3812 pmap_change_pte1(pmap, pte1p, va, npte1);
3815 * Demote the pv entry. This depends on the earlier demotion
3816 * of the mapping. Specifically, the (re)creation of a per-
3817 * page pv entry might trigger the execution of pmap_pv_reclaim(),
3818 * which might reclaim a newly (re)created per-page pv entry
3819 * and destroy the associated mapping. In order to destroy
3820 * the mapping, the PTE1 must have already changed from mapping
3821 * the 1mpage to referencing the page table page.
3823 if (pte1_is_managed(opte1))
3824 pmap_pv_demote_pte1(pmap, va, pte1_pa(opte1));
3826 pmap_pte1_demotions++;
3827 CTR3(KTR_PMAP, "%s: success for va %#x in pmap %p",
3828 __func__, va, pmap);
3830 PDEBUG(6, printf("%s(%p): success for va %#x pte1 %#x(%#x) at %p\n",
3831 __func__, pmap, va, npte1, pte1_load(pte1p), pte1p));
3836 * Insert the given physical page (p) at
3837 * the specified virtual address (v) in the
3838 * target physical map with the protection requested.
3840 * If specified, the page will be wired down, meaning
3841 * that the related pte can not be reclaimed.
3843 * NB: This is the only routine which MAY NOT lazy-evaluate
3844 * or lose information. That is, this routine must actually
3845 * insert this page into the given map NOW.
3848 pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
3849 u_int flags, int8_t psind)
3853 pt2_entry_t npte2, opte2;
3856 vm_page_t mpte2, om;
3859 va = trunc_page(va);
3861 wired = (flags & PMAP_ENTER_WIRED) != 0;
3863 KASSERT(va <= vm_max_kernel_address, ("%s: toobig", __func__));
3864 KASSERT(va < UPT2V_MIN_ADDRESS || va >= UPT2V_MAX_ADDRESS,
3865 ("%s: invalid to pmap_enter page table pages (va: 0x%x)", __func__,
3867 if ((m->oflags & VPO_UNMANAGED) == 0 && !vm_page_xbusied(m))
3868 VM_OBJECT_ASSERT_LOCKED(m->object);
3870 rw_wlock(&pvh_global_lock);
3875 * In the case that a page table page is not
3876 * resident, we are creating it here.
3878 if (va < VM_MAXUSER_ADDRESS) {
3879 mpte2 = pmap_allocpte2(pmap, va, flags);
3880 if (mpte2 == NULL) {
3881 KASSERT((flags & PMAP_ENTER_NOSLEEP) != 0,
3882 ("pmap_allocpte2 failed with sleep allowed"));
3884 rw_wunlock(&pvh_global_lock);
3886 return (KERN_RESOURCE_SHORTAGE);
3889 pte1p = pmap_pte1(pmap, va);
3890 if (pte1_is_section(pte1_load(pte1p)))
3891 panic("%s: attempted on 1MB page", __func__);
3892 pte2p = pmap_pte2_quick(pmap, va);
3894 panic("%s: invalid L1 page table entry va=%#x", __func__, va);
3897 pa = VM_PAGE_TO_PHYS(m);
3898 opte2 = pte2_load(pte2p);
3899 opa = pte2_pa(opte2);
3901 * Mapping has not changed, must be protection or wiring change.
3903 if (pte2_is_valid(opte2) && (opa == pa)) {
3905 * Wiring change, just update stats. We don't worry about
3906 * wiring PT2 pages as they remain resident as long as there
3907 * are valid mappings in them. Hence, if a user page is wired,
3908 * the PT2 page will be also.
3910 if (wired && !pte2_is_wired(opte2))
3911 pmap->pm_stats.wired_count++;
3912 else if (!wired && pte2_is_wired(opte2))
3913 pmap->pm_stats.wired_count--;
3916 * Remove extra pte2 reference
3919 pt2_wirecount_dec(mpte2, pte1_index(va));
3920 if (pte2_is_managed(opte2))
3926 * QQQ: We think that changing physical address on writeable mapping
3927 * is not safe. Well, maybe on kernel address space with correct
3928 * locking, it can make a sense. However, we have no idea why
3929 * anyone should do that on user address space. Are we wrong?
3931 KASSERT((opa == 0) || (opa == pa) ||
3932 !pte2_is_valid(opte2) || ((opte2 & PTE2_RO) != 0),
3933 ("%s: pmap %p va %#x(%#x) opa %#x pa %#x - gotcha %#x %#x!",
3934 __func__, pmap, va, opte2, opa, pa, flags, prot));
3939 * Mapping has changed, invalidate old range and fall through to
3940 * handle validating new mapping.
3943 if (pte2_is_wired(opte2))
3944 pmap->pm_stats.wired_count--;
3945 if (pte2_is_managed(opte2)) {
3946 om = PHYS_TO_VM_PAGE(opa);
3947 pv = pmap_pvh_remove(&om->md, pmap, va);
3950 * Remove extra pte2 reference
3953 pt2_wirecount_dec(mpte2, va >> PTE1_SHIFT);
3955 pmap->pm_stats.resident_count++;
3958 * Enter on the PV list if part of our managed memory.
3960 if ((m->oflags & VPO_UNMANAGED) == 0) {
3961 KASSERT(va < kmi.clean_sva || va >= kmi.clean_eva,
3962 ("%s: managed mapping within the clean submap", __func__));
3964 pv = get_pv_entry(pmap, FALSE);
3966 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_next);
3967 } else if (pv != NULL)
3968 free_pv_entry(pmap, pv);
3971 * Increment counters
3974 pmap->pm_stats.wired_count++;
3978 * Now validate mapping with desired protection/wiring.
3980 npte2 = PTE2(pa, PTE2_NM, vm_page_pte2_attr(m));
3981 if (prot & VM_PROT_WRITE) {
3982 if (pte2_is_managed(npte2))
3983 vm_page_aflag_set(m, PGA_WRITEABLE);
3987 if ((prot & VM_PROT_EXECUTE) == 0)
3991 if (va < VM_MAXUSER_ADDRESS)
3993 if (pmap != kernel_pmap)
3997 * If the mapping or permission bits are different, we need
3998 * to update the pte2.
4000 * QQQ: Think again and again what to do
4001 * if the mapping is going to be changed!
4003 if ((opte2 & ~(PTE2_NM | PTE2_A)) != (npte2 & ~(PTE2_NM | PTE2_A))) {
4005 * Sync icache if exec permission and attribute VM_MEMATTR_WB_WA
4006 * is set. Do it now, before the mapping is stored and made
4007 * valid for hardware table walk. If done later, there is a race
4008 * for other threads of current process in lazy loading case.
4009 * Don't do it for kernel memory which is mapped with exec
4010 * permission even if the memory isn't going to hold executable
4011 * code. The only time when icache sync is needed is after
4012 * kernel module is loaded and the relocation info is processed.
4013 * And it's done in elf_cpu_load_file().
4015 * QQQ: (1) Does it exist any better way where
4016 * or how to sync icache?
4017 * (2) Now, we do it on a page basis.
4019 if ((prot & VM_PROT_EXECUTE) && pmap != kernel_pmap &&
4020 m->md.pat_mode == VM_MEMATTR_WB_WA &&
4021 (opa != pa || (opte2 & PTE2_NX)))
4022 cache_icache_sync_fresh(va, pa, PAGE_SIZE);
4025 if (flags & VM_PROT_WRITE)
4027 if (opte2 & PTE2_V) {
4028 /* Change mapping with break-before-make approach. */
4029 opte2 = pte2_load_clear(pte2p);
4030 pmap_tlb_flush(pmap, va);
4031 pte2_store(pte2p, npte2);
4032 if (opte2 & PTE2_A) {
4033 if (pte2_is_managed(opte2))
4034 vm_page_aflag_set(om, PGA_REFERENCED);
4036 if (pte2_is_dirty(opte2)) {
4037 if (pte2_is_managed(opte2))
4040 if (pte2_is_managed(opte2) &&
4041 TAILQ_EMPTY(&om->md.pv_list) &&
4042 ((om->flags & PG_FICTITIOUS) != 0 ||
4043 TAILQ_EMPTY(&pa_to_pvh(opa)->pv_list)))
4044 vm_page_aflag_clear(om, PGA_WRITEABLE);
4046 pte2_store(pte2p, npte2);
4051 * QQQ: In time when both access and not mofified bits are
4052 * emulated by software, this should not happen. Some
4053 * analysis is need, if this really happen. Missing
4054 * tlb flush somewhere could be the reason.
4056 panic("%s: pmap %p va %#x opte2 %x npte2 %x !!", __func__, pmap,
4061 #if VM_NRESERVLEVEL > 0
4063 * If both the L2 page table page and the reservation are fully
4064 * populated, then attempt promotion.
4066 if ((mpte2 == NULL || pt2_is_full(mpte2, va)) &&
4067 sp_enabled && (m->flags & PG_FICTITIOUS) == 0 &&
4068 vm_reserv_level_iffullpop(m) == 0)
4069 pmap_promote_pte1(pmap, pte1p, va);
4072 rw_wunlock(&pvh_global_lock);
4074 return (KERN_SUCCESS);
4078 * Do the things to unmap a page in a process.
4081 pmap_remove_pte2(pmap_t pmap, pt2_entry_t *pte2p, vm_offset_t va,
4082 struct spglist *free)
4087 rw_assert(&pvh_global_lock, RA_WLOCKED);
4088 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
4090 /* Clear and invalidate the mapping. */
4091 opte2 = pte2_load_clear(pte2p);
4092 pmap_tlb_flush(pmap, va);
4094 KASSERT(pte2_is_valid(opte2), ("%s: pmap %p va %#x not link pte2 %#x",
4095 __func__, pmap, va, opte2));
4098 pmap->pm_stats.wired_count -= 1;
4099 pmap->pm_stats.resident_count -= 1;
4100 if (pte2_is_managed(opte2)) {
4101 m = PHYS_TO_VM_PAGE(pte2_pa(opte2));
4102 if (pte2_is_dirty(opte2))
4105 vm_page_aflag_set(m, PGA_REFERENCED);
4106 pmap_remove_entry(pmap, m, va);
4108 return (pmap_unuse_pt2(pmap, va, free));
4112 * Remove a single page from a process address space.
4115 pmap_remove_page(pmap_t pmap, vm_offset_t va, struct spglist *free)
4119 rw_assert(&pvh_global_lock, RA_WLOCKED);
4120 KASSERT(curthread->td_pinned > 0,
4121 ("%s: curthread not pinned", __func__));
4122 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
4123 if ((pte2p = pmap_pte2_quick(pmap, va)) == NULL ||
4124 !pte2_is_valid(pte2_load(pte2p)))
4126 pmap_remove_pte2(pmap, pte2p, va, free);
4130 * Remove the given range of addresses from the specified map.
4132 * It is assumed that the start and end are properly
4133 * rounded to the page size.
4136 pmap_remove(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
4139 pt1_entry_t *pte1p, pte1;
4140 pt2_entry_t *pte2p, pte2;
4141 struct spglist free;
4144 * Perform an unsynchronized read. This is, however, safe.
4146 if (pmap->pm_stats.resident_count == 0)
4151 rw_wlock(&pvh_global_lock);
4156 * Special handling of removing one page. A very common
4157 * operation and easy to short circuit some code.
4159 if (sva + PAGE_SIZE == eva) {
4160 pte1 = pte1_load(pmap_pte1(pmap, sva));
4161 if (pte1_is_link(pte1)) {
4162 pmap_remove_page(pmap, sva, &free);
4167 for (; sva < eva; sva = nextva) {
4169 * Calculate address for next L2 page table.
4171 nextva = pte1_trunc(sva + PTE1_SIZE);
4174 if (pmap->pm_stats.resident_count == 0)
4177 pte1p = pmap_pte1(pmap, sva);
4178 pte1 = pte1_load(pte1p);
4181 * Weed out invalid mappings. Note: we assume that the L1 page
4182 * table is always allocated, and in kernel virtual.
4187 if (pte1_is_section(pte1)) {
4189 * Are we removing the entire large page? If not,
4190 * demote the mapping and fall through.
4192 if (sva + PTE1_SIZE == nextva && eva >= nextva) {
4193 pmap_remove_pte1(pmap, pte1p, sva, &free);
4195 } else if (!pmap_demote_pte1(pmap, pte1p, sva)) {
4196 /* The large page mapping was destroyed. */
4201 /* Update pte1 after demotion. */
4202 pte1 = pte1_load(pte1p);
4207 KASSERT(pte1_is_link(pte1), ("%s: pmap %p va %#x pte1 %#x at %p"
4208 " is not link", __func__, pmap, sva, pte1, pte1p));
4211 * Limit our scan to either the end of the va represented
4212 * by the current L2 page table page, or to the end of the
4213 * range being removed.
4218 for (pte2p = pmap_pte2_quick(pmap, sva); sva != nextva;
4219 pte2p++, sva += PAGE_SIZE) {
4220 pte2 = pte2_load(pte2p);
4221 if (!pte2_is_valid(pte2))
4223 if (pmap_remove_pte2(pmap, pte2p, sva, &free))
4229 rw_wunlock(&pvh_global_lock);
4231 pmap_free_zero_pages(&free);
4235 * Routine: pmap_remove_all
4237 * Removes this physical page from
4238 * all physical maps in which it resides.
4239 * Reflects back modify bits to the pager.
4242 * Original versions of this routine were very
4243 * inefficient because they iteratively called
4244 * pmap_remove (slow...)
4248 pmap_remove_all(vm_page_t m)
4250 struct md_page *pvh;
4253 pt2_entry_t *pte2p, opte2;
4256 struct spglist free;
4258 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4259 ("%s: page %p is not managed", __func__, m));
4261 rw_wlock(&pvh_global_lock);
4263 if ((m->flags & PG_FICTITIOUS) != 0)
4264 goto small_mappings;
4265 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
4266 while ((pv = TAILQ_FIRST(&pvh->pv_list)) != NULL) {
4270 pte1p = pmap_pte1(pmap, va);
4271 (void)pmap_demote_pte1(pmap, pte1p, va);
4275 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
4278 pmap->pm_stats.resident_count--;
4279 pte1p = pmap_pte1(pmap, pv->pv_va);
4280 KASSERT(!pte1_is_section(pte1_load(pte1p)), ("%s: found "
4281 "a 1mpage in page %p's pv list", __func__, m));
4282 pte2p = pmap_pte2_quick(pmap, pv->pv_va);
4283 opte2 = pte2_load_clear(pte2p);
4284 pmap_tlb_flush(pmap, pv->pv_va);
4285 KASSERT(pte2_is_valid(opte2), ("%s: pmap %p va %x zero pte2",
4286 __func__, pmap, pv->pv_va));
4287 if (pte2_is_wired(opte2))
4288 pmap->pm_stats.wired_count--;
4290 vm_page_aflag_set(m, PGA_REFERENCED);
4293 * Update the vm_page_t clean and reference bits.
4295 if (pte2_is_dirty(opte2))
4297 pmap_unuse_pt2(pmap, pv->pv_va, &free);
4298 TAILQ_REMOVE(&m->md.pv_list, pv, pv_next);
4299 free_pv_entry(pmap, pv);
4302 vm_page_aflag_clear(m, PGA_WRITEABLE);
4304 rw_wunlock(&pvh_global_lock);
4305 pmap_free_zero_pages(&free);
4309 * Just subroutine for pmap_remove_pages() to reasonably satisfy
4310 * good coding style, a.k.a. 80 character line width limit hell.
4312 static __inline void
4313 pmap_remove_pte1_quick(pmap_t pmap, pt1_entry_t pte1, pv_entry_t pv,
4314 struct spglist *free)
4317 vm_page_t m, mt, mpt2pg;
4318 struct md_page *pvh;
4321 m = PHYS_TO_VM_PAGE(pa);
4323 KASSERT(m->phys_addr == pa, ("%s: vm_page_t %p addr mismatch %#x %#x",
4324 __func__, m, m->phys_addr, pa));
4325 KASSERT((m->flags & PG_FICTITIOUS) != 0 ||
4326 m < &vm_page_array[vm_page_array_size],
4327 ("%s: bad pte1 %#x", __func__, pte1));
4329 if (pte1_is_dirty(pte1)) {
4330 for (mt = m; mt < &m[PTE1_SIZE / PAGE_SIZE]; mt++)
4334 pmap->pm_stats.resident_count -= PTE1_SIZE / PAGE_SIZE;
4335 pvh = pa_to_pvh(pa);
4336 TAILQ_REMOVE(&pvh->pv_list, pv, pv_next);
4337 if (TAILQ_EMPTY(&pvh->pv_list)) {
4338 for (mt = m; mt < &m[PTE1_SIZE / PAGE_SIZE]; mt++)
4339 if (TAILQ_EMPTY(&mt->md.pv_list))
4340 vm_page_aflag_clear(mt, PGA_WRITEABLE);
4342 mpt2pg = pmap_pt2_page(pmap, pv->pv_va);
4344 pmap_unwire_pt2_all(pmap, pv->pv_va, mpt2pg, free);
4348 * Just subroutine for pmap_remove_pages() to reasonably satisfy
4349 * good coding style, a.k.a. 80 character line width limit hell.
4351 static __inline void
4352 pmap_remove_pte2_quick(pmap_t pmap, pt2_entry_t pte2, pv_entry_t pv,
4353 struct spglist *free)
4357 struct md_page *pvh;
4360 m = PHYS_TO_VM_PAGE(pa);
4362 KASSERT(m->phys_addr == pa, ("%s: vm_page_t %p addr mismatch %#x %#x",
4363 __func__, m, m->phys_addr, pa));
4364 KASSERT((m->flags & PG_FICTITIOUS) != 0 ||
4365 m < &vm_page_array[vm_page_array_size],
4366 ("%s: bad pte2 %#x", __func__, pte2));
4368 if (pte2_is_dirty(pte2))
4371 pmap->pm_stats.resident_count--;
4372 TAILQ_REMOVE(&m->md.pv_list, pv, pv_next);
4373 if (TAILQ_EMPTY(&m->md.pv_list) && (m->flags & PG_FICTITIOUS) == 0) {
4374 pvh = pa_to_pvh(pa);
4375 if (TAILQ_EMPTY(&pvh->pv_list))
4376 vm_page_aflag_clear(m, PGA_WRITEABLE);
4378 pmap_unuse_pt2(pmap, pv->pv_va, free);
4382 * Remove all pages from specified address space this aids process
4383 * exit speeds. Also, this code is special cased for current process
4384 * only, but can have the more generic (and slightly slower) mode enabled.
4385 * This is much faster than pmap_remove in the case of running down
4386 * an entire address space.
4389 pmap_remove_pages(pmap_t pmap)
4391 pt1_entry_t *pte1p, pte1;
4392 pt2_entry_t *pte2p, pte2;
4394 struct pv_chunk *pc, *npc;
4395 struct spglist free;
4398 uint32_t inuse, bitmask;
4402 * Assert that the given pmap is only active on the current
4403 * CPU. Unfortunately, we cannot block another CPU from
4404 * activating the pmap while this function is executing.
4406 KASSERT(pmap == vmspace_pmap(curthread->td_proc->p_vmspace),
4407 ("%s: non-current pmap %p", __func__, pmap));
4408 #if defined(SMP) && defined(INVARIANTS)
4410 cpuset_t other_cpus;
4413 other_cpus = pmap->pm_active;
4414 CPU_CLR(PCPU_GET(cpuid), &other_cpus);
4416 KASSERT(CPU_EMPTY(&other_cpus),
4417 ("%s: pmap %p active on other cpus", __func__, pmap));
4421 rw_wlock(&pvh_global_lock);
4424 TAILQ_FOREACH_SAFE(pc, &pmap->pm_pvchunk, pc_list, npc) {
4425 KASSERT(pc->pc_pmap == pmap, ("%s: wrong pmap %p %p",
4426 __func__, pmap, pc->pc_pmap));
4428 for (field = 0; field < _NPCM; field++) {
4429 inuse = (~(pc->pc_map[field])) & pc_freemask[field];
4430 while (inuse != 0) {
4431 bit = ffs(inuse) - 1;
4432 bitmask = 1UL << bit;
4433 idx = field * 32 + bit;
4434 pv = &pc->pc_pventry[idx];
4438 * Note that we cannot remove wired pages
4439 * from a process' mapping at this time
4441 pte1p = pmap_pte1(pmap, pv->pv_va);
4442 pte1 = pte1_load(pte1p);
4443 if (pte1_is_section(pte1)) {
4444 if (pte1_is_wired(pte1)) {
4449 pmap_remove_pte1_quick(pmap, pte1, pv,
4452 else if (pte1_is_link(pte1)) {
4453 pte2p = pt2map_entry(pv->pv_va);
4454 pte2 = pte2_load(pte2p);
4456 if (!pte2_is_valid(pte2)) {
4457 printf("%s: pmap %p va %#x "
4458 "pte2 %#x\n", __func__,
4459 pmap, pv->pv_va, pte2);
4463 if (pte2_is_wired(pte2)) {
4468 pmap_remove_pte2_quick(pmap, pte2, pv,
4471 printf("%s: pmap %p va %#x pte1 %#x\n",
4472 __func__, pmap, pv->pv_va, pte1);
4477 PV_STAT(pv_entry_frees++);
4478 PV_STAT(pv_entry_spare++);
4480 pc->pc_map[field] |= bitmask;
4484 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
4488 tlb_flush_all_ng_local();
4490 rw_wunlock(&pvh_global_lock);
4492 pmap_free_zero_pages(&free);
4496 * This code makes some *MAJOR* assumptions:
4497 * 1. Current pmap & pmap exists.
4500 * 4. No L2 page table pages.
4501 * but is *MUCH* faster than pmap_enter...
4504 pmap_enter_quick_locked(pmap_t pmap, vm_offset_t va, vm_page_t m,
4505 vm_prot_t prot, vm_page_t mpt2pg)
4507 pt2_entry_t *pte2p, pte2;
4509 struct spglist free;
4512 KASSERT(va < kmi.clean_sva || va >= kmi.clean_eva ||
4513 (m->oflags & VPO_UNMANAGED) != 0,
4514 ("%s: managed mapping within the clean submap", __func__));
4515 rw_assert(&pvh_global_lock, RA_WLOCKED);
4516 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
4519 * In the case that a L2 page table page is not
4520 * resident, we are creating it here.
4522 if (va < VM_MAXUSER_ADDRESS) {
4524 pt1_entry_t pte1, *pte1p;
4528 * Get L1 page table things.
4530 pte1_idx = pte1_index(va);
4531 pte1p = pmap_pte1(pmap, va);
4532 pte1 = pte1_load(pte1p);
4534 if (mpt2pg && (mpt2pg->pindex == (pte1_idx & ~PT2PG_MASK))) {
4536 * Each of NPT2_IN_PG L2 page tables on the page can
4537 * come here. Make sure that associated L1 page table
4538 * link is established.
4540 * QQQ: It comes that we don't establish all links to
4541 * L2 page tables for newly allocated L2 page
4544 KASSERT(!pte1_is_section(pte1),
4545 ("%s: pte1 %#x is section", __func__, pte1));
4546 if (!pte1_is_link(pte1)) {
4547 pt2_pa = page_pt2pa(VM_PAGE_TO_PHYS(mpt2pg),
4549 pte1_store(pte1p, PTE1_LINK(pt2_pa));
4551 pt2_wirecount_inc(mpt2pg, pte1_idx);
4554 * If the L2 page table page is mapped, we just
4555 * increment the hold count, and activate it.
4557 if (pte1_is_section(pte1)) {
4559 } else if (pte1_is_link(pte1)) {
4560 mpt2pg = PHYS_TO_VM_PAGE(pte1_link_pa(pte1));
4561 pt2_wirecount_inc(mpt2pg, pte1_idx);
4563 mpt2pg = _pmap_allocpte2(pmap, va,
4564 PMAP_ENTER_NOSLEEP);
4574 * This call to pt2map_entry() makes the assumption that we are
4575 * entering the page into the current pmap. In order to support
4576 * quick entry into any pmap, one would likely use pmap_pte2_quick().
4577 * But that isn't as quick as pt2map_entry().
4579 pte2p = pt2map_entry(va);
4580 pte2 = pte2_load(pte2p);
4581 if (pte2_is_valid(pte2)) {
4582 if (mpt2pg != NULL) {
4584 * Remove extra pte2 reference
4586 pt2_wirecount_dec(mpt2pg, pte1_index(va));
4593 * Enter on the PV list if part of our managed memory.
4595 if ((m->oflags & VPO_UNMANAGED) == 0 &&
4596 !pmap_try_insert_pv_entry(pmap, va, m)) {
4597 if (mpt2pg != NULL) {
4599 if (pmap_unwire_pt2(pmap, va, mpt2pg, &free)) {
4600 pmap_tlb_flush(pmap, va);
4601 pmap_free_zero_pages(&free);
4610 * Increment counters
4612 pmap->pm_stats.resident_count++;
4615 * Now validate mapping with RO protection
4617 pa = VM_PAGE_TO_PHYS(m);
4618 l2prot = PTE2_RO | PTE2_NM;
4619 if (va < VM_MAXUSER_ADDRESS)
4620 l2prot |= PTE2_U | PTE2_NG;
4621 if ((prot & VM_PROT_EXECUTE) == 0)
4623 else if (m->md.pat_mode == VM_MEMATTR_WB_WA && pmap != kernel_pmap) {
4625 * Sync icache if exec permission and attribute VM_MEMATTR_WB_WA
4626 * is set. QQQ: For more info, see comments in pmap_enter().
4628 cache_icache_sync_fresh(va, pa, PAGE_SIZE);
4630 pte2_store(pte2p, PTE2(pa, l2prot, vm_page_pte2_attr(m)));
4636 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot)
4639 rw_wlock(&pvh_global_lock);
4641 (void)pmap_enter_quick_locked(pmap, va, m, prot, NULL);
4642 rw_wunlock(&pvh_global_lock);
4647 * Tries to create 1MB page mapping. Returns TRUE if successful and
4648 * FALSE otherwise. Fails if (1) a page table page cannot be allocated without
4649 * blocking, (2) a mapping already exists at the specified virtual address, or
4650 * (3) a pv entry cannot be allocated without reclaiming another pv entry.
4653 pmap_enter_pte1(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot)
4659 rw_assert(&pvh_global_lock, RA_WLOCKED);
4660 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
4661 pte1p = pmap_pte1(pmap, va);
4662 if (pte1_is_valid(pte1_load(pte1p))) {
4663 CTR3(KTR_PMAP, "%s: failure for va %#lx in pmap %p", __func__,
4667 if ((m->oflags & VPO_UNMANAGED) == 0) {
4669 * Abort this mapping if its PV entry could not be created.
4671 if (!pmap_pv_insert_pte1(pmap, va, VM_PAGE_TO_PHYS(m))) {
4672 CTR3(KTR_PMAP, "%s: failure for va %#lx in pmap %p",
4673 __func__, va, pmap);
4678 * Increment counters.
4680 pmap->pm_stats.resident_count += PTE1_SIZE / PAGE_SIZE;
4685 * QQQ: Why VM_PROT_WRITE is not evaluated and the mapping is
4688 pa = VM_PAGE_TO_PHYS(m);
4689 l1prot = PTE1_RO | PTE1_NM;
4690 if (va < VM_MAXUSER_ADDRESS)
4691 l1prot |= PTE1_U | PTE1_NG;
4692 if ((prot & VM_PROT_EXECUTE) == 0)
4694 else if (m->md.pat_mode == VM_MEMATTR_WB_WA && pmap != kernel_pmap) {
4696 * Sync icache if exec permission and attribute VM_MEMATTR_WB_WA
4697 * is set. QQQ: For more info, see comments in pmap_enter().
4699 cache_icache_sync_fresh(va, pa, PTE1_SIZE);
4701 pte1_store(pte1p, PTE1(pa, l1prot, ATTR_TO_L1(vm_page_pte2_attr(m))));
4703 pmap_pte1_mappings++;
4704 CTR3(KTR_PMAP, "%s: success for va %#lx in pmap %p", __func__, va,
4710 * Maps a sequence of resident pages belonging to the same object.
4711 * The sequence begins with the given page m_start. This page is
4712 * mapped at the given virtual address start. Each subsequent page is
4713 * mapped at a virtual address that is offset from start by the same
4714 * amount as the page is offset from m_start within the object. The
4715 * last page in the sequence is the page with the largest offset from
4716 * m_start that can be mapped at a virtual address less than the given
4717 * virtual address end. Not every virtual page between start and end
4718 * is mapped; only those for which a resident page exists with the
4719 * corresponding offset from m_start are mapped.
4722 pmap_enter_object(pmap_t pmap, vm_offset_t start, vm_offset_t end,
4723 vm_page_t m_start, vm_prot_t prot)
4726 vm_page_t m, mpt2pg;
4727 vm_pindex_t diff, psize;
4729 PDEBUG(6, printf("%s: pmap %p start %#x end %#x m %p prot %#x\n",
4730 __func__, pmap, start, end, m_start, prot));
4732 VM_OBJECT_ASSERT_LOCKED(m_start->object);
4733 psize = atop(end - start);
4736 rw_wlock(&pvh_global_lock);
4738 while (m != NULL && (diff = m->pindex - m_start->pindex) < psize) {
4739 va = start + ptoa(diff);
4740 if ((va & PTE1_OFFSET) == 0 && va + PTE1_SIZE <= end &&
4741 m->psind == 1 && sp_enabled &&
4742 pmap_enter_pte1(pmap, va, m, prot))
4743 m = &m[PTE1_SIZE / PAGE_SIZE - 1];
4745 mpt2pg = pmap_enter_quick_locked(pmap, va, m, prot,
4747 m = TAILQ_NEXT(m, listq);
4749 rw_wunlock(&pvh_global_lock);
4754 * This code maps large physical mmap regions into the
4755 * processor address space. Note that some shortcuts
4756 * are taken, but the code works.
4759 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr, vm_object_t object,
4760 vm_pindex_t pindex, vm_size_t size)
4763 vm_paddr_t pa, pte2_pa;
4765 vm_memattr_t pat_mode;
4766 u_int l1attr, l1prot;
4768 VM_OBJECT_ASSERT_WLOCKED(object);
4769 KASSERT(object->type == OBJT_DEVICE || object->type == OBJT_SG,
4770 ("%s: non-device object", __func__));
4771 if ((addr & PTE1_OFFSET) == 0 && (size & PTE1_OFFSET) == 0) {
4772 if (!vm_object_populate(object, pindex, pindex + atop(size)))
4774 p = vm_page_lookup(object, pindex);
4775 KASSERT(p->valid == VM_PAGE_BITS_ALL,
4776 ("%s: invalid page %p", __func__, p));
4777 pat_mode = p->md.pat_mode;
4780 * Abort the mapping if the first page is not physically
4781 * aligned to a 1MB page boundary.
4783 pte2_pa = VM_PAGE_TO_PHYS(p);
4784 if (pte2_pa & PTE1_OFFSET)
4788 * Skip the first page. Abort the mapping if the rest of
4789 * the pages are not physically contiguous or have differing
4790 * memory attributes.
4792 p = TAILQ_NEXT(p, listq);
4793 for (pa = pte2_pa + PAGE_SIZE; pa < pte2_pa + size;
4795 KASSERT(p->valid == VM_PAGE_BITS_ALL,
4796 ("%s: invalid page %p", __func__, p));
4797 if (pa != VM_PAGE_TO_PHYS(p) ||
4798 pat_mode != p->md.pat_mode)
4800 p = TAILQ_NEXT(p, listq);
4804 * Map using 1MB pages.
4806 * QQQ: Well, we are mapping a section, so same condition must
4807 * be hold like during promotion. It looks that only RW mapping
4808 * is done here, so readonly mapping must be done elsewhere.
4810 l1prot = PTE1_U | PTE1_NG | PTE1_RW | PTE1_M | PTE1_A;
4811 l1attr = ATTR_TO_L1(vm_memattr_to_pte2(pat_mode));
4813 for (pa = pte2_pa; pa < pte2_pa + size; pa += PTE1_SIZE) {
4814 pte1p = pmap_pte1(pmap, addr);
4815 if (!pte1_is_valid(pte1_load(pte1p))) {
4816 pte1_store(pte1p, PTE1(pa, l1prot, l1attr));
4817 pmap->pm_stats.resident_count += PTE1_SIZE /
4819 pmap_pte1_mappings++;
4821 /* Else continue on if the PTE1 is already valid. */
4829 * Do the things to protect a 1mpage in a process.
4832 pmap_protect_pte1(pmap_t pmap, pt1_entry_t *pte1p, vm_offset_t sva,
4835 pt1_entry_t npte1, opte1;
4836 vm_offset_t eva, va;
4839 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
4840 KASSERT((sva & PTE1_OFFSET) == 0,
4841 ("%s: sva is not 1mpage aligned", __func__));
4843 opte1 = npte1 = pte1_load(pte1p);
4844 if (pte1_is_managed(opte1) && pte1_is_dirty(opte1)) {
4845 eva = sva + PTE1_SIZE;
4846 for (va = sva, m = PHYS_TO_VM_PAGE(pte1_pa(opte1));
4847 va < eva; va += PAGE_SIZE, m++)
4850 if ((prot & VM_PROT_WRITE) == 0)
4851 npte1 |= PTE1_RO | PTE1_NM;
4852 if ((prot & VM_PROT_EXECUTE) == 0)
4856 * QQQ: Herein, execute permission is never set.
4857 * It only can be cleared. So, no icache
4858 * syncing is needed.
4861 if (npte1 != opte1) {
4862 pte1_store(pte1p, npte1);
4863 pmap_tlb_flush(pmap, sva);
4868 * Set the physical protection on the
4869 * specified range of this map as requested.
4872 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
4874 boolean_t pv_lists_locked;
4876 pt1_entry_t *pte1p, pte1;
4877 pt2_entry_t *pte2p, opte2, npte2;
4879 KASSERT((prot & ~VM_PROT_ALL) == 0, ("invalid prot %x", prot));
4880 if (prot == VM_PROT_NONE) {
4881 pmap_remove(pmap, sva, eva);
4885 if ((prot & (VM_PROT_WRITE | VM_PROT_EXECUTE)) ==
4886 (VM_PROT_WRITE | VM_PROT_EXECUTE))
4889 if (pmap_is_current(pmap))
4890 pv_lists_locked = FALSE;
4892 pv_lists_locked = TRUE;
4894 rw_wlock(&pvh_global_lock);
4899 for (; sva < eva; sva = nextva) {
4901 * Calculate address for next L2 page table.
4903 nextva = pte1_trunc(sva + PTE1_SIZE);
4907 pte1p = pmap_pte1(pmap, sva);
4908 pte1 = pte1_load(pte1p);
4911 * Weed out invalid mappings. Note: we assume that L1 page
4912 * page table is always allocated, and in kernel virtual.
4917 if (pte1_is_section(pte1)) {
4919 * Are we protecting the entire large page? If not,
4920 * demote the mapping and fall through.
4922 if (sva + PTE1_SIZE == nextva && eva >= nextva) {
4923 pmap_protect_pte1(pmap, pte1p, sva, prot);
4926 if (!pv_lists_locked) {
4927 pv_lists_locked = TRUE;
4928 if (!rw_try_wlock(&pvh_global_lock)) {
4934 if (!pmap_demote_pte1(pmap, pte1p, sva)) {
4936 * The large page mapping
4943 /* Update pte1 after demotion */
4944 pte1 = pte1_load(pte1p);
4950 KASSERT(pte1_is_link(pte1), ("%s: pmap %p va %#x pte1 %#x at %p"
4951 " is not link", __func__, pmap, sva, pte1, pte1p));
4954 * Limit our scan to either the end of the va represented
4955 * by the current L2 page table page, or to the end of the
4956 * range being protected.
4961 for (pte2p = pmap_pte2_quick(pmap, sva); sva != nextva; pte2p++,
4965 opte2 = npte2 = pte2_load(pte2p);
4966 if (!pte2_is_valid(opte2))
4969 if ((prot & VM_PROT_WRITE) == 0) {
4970 if (pte2_is_managed(opte2) &&
4971 pte2_is_dirty(opte2)) {
4972 m = PHYS_TO_VM_PAGE(pte2_pa(opte2));
4975 npte2 |= PTE2_RO | PTE2_NM;
4978 if ((prot & VM_PROT_EXECUTE) == 0)
4982 * QQQ: Herein, execute permission is never set.
4983 * It only can be cleared. So, no icache
4984 * syncing is needed.
4987 if (npte2 != opte2) {
4988 pte2_store(pte2p, npte2);
4989 pmap_tlb_flush(pmap, sva);
4993 if (pv_lists_locked) {
4995 rw_wunlock(&pvh_global_lock);
5001 * pmap_pvh_wired_mappings:
5003 * Return the updated number "count" of managed mappings that are wired.
5006 pmap_pvh_wired_mappings(struct md_page *pvh, int count)
5013 rw_assert(&pvh_global_lock, RA_WLOCKED);
5015 TAILQ_FOREACH(pv, &pvh->pv_list, pv_next) {
5018 pte1 = pte1_load(pmap_pte1(pmap, pv->pv_va));
5019 if (pte1_is_section(pte1)) {
5020 if (pte1_is_wired(pte1))
5023 KASSERT(pte1_is_link(pte1),
5024 ("%s: pte1 %#x is not link", __func__, pte1));
5025 pte2 = pte2_load(pmap_pte2_quick(pmap, pv->pv_va));
5026 if (pte2_is_wired(pte2))
5036 * pmap_page_wired_mappings:
5038 * Return the number of managed mappings to the given physical page
5042 pmap_page_wired_mappings(vm_page_t m)
5047 if ((m->oflags & VPO_UNMANAGED) != 0)
5049 rw_wlock(&pvh_global_lock);
5050 count = pmap_pvh_wired_mappings(&m->md, count);
5051 if ((m->flags & PG_FICTITIOUS) == 0) {
5052 count = pmap_pvh_wired_mappings(pa_to_pvh(VM_PAGE_TO_PHYS(m)),
5055 rw_wunlock(&pvh_global_lock);
5060 * Returns TRUE if any of the given mappings were used to modify
5061 * physical memory. Otherwise, returns FALSE. Both page and 1mpage
5062 * mappings are supported.
5065 pmap_is_modified_pvh(struct md_page *pvh)
5073 rw_assert(&pvh_global_lock, RA_WLOCKED);
5076 TAILQ_FOREACH(pv, &pvh->pv_list, pv_next) {
5079 pte1 = pte1_load(pmap_pte1(pmap, pv->pv_va));
5080 if (pte1_is_section(pte1)) {
5081 rv = pte1_is_dirty(pte1);
5083 KASSERT(pte1_is_link(pte1),
5084 ("%s: pte1 %#x is not link", __func__, pte1));
5085 pte2 = pte2_load(pmap_pte2_quick(pmap, pv->pv_va));
5086 rv = pte2_is_dirty(pte2);
5099 * Return whether or not the specified physical page was modified
5100 * in any physical maps.
5103 pmap_is_modified(vm_page_t m)
5107 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
5108 ("%s: page %p is not managed", __func__, m));
5111 * If the page is not exclusive busied, then PGA_WRITEABLE cannot be
5112 * concurrently set while the object is locked. Thus, if PGA_WRITEABLE
5113 * is clear, no PTE2s can have PG_M set.
5115 VM_OBJECT_ASSERT_WLOCKED(m->object);
5116 if (!vm_page_xbusied(m) && (m->aflags & PGA_WRITEABLE) == 0)
5118 rw_wlock(&pvh_global_lock);
5119 rv = pmap_is_modified_pvh(&m->md) ||
5120 ((m->flags & PG_FICTITIOUS) == 0 &&
5121 pmap_is_modified_pvh(pa_to_pvh(VM_PAGE_TO_PHYS(m))));
5122 rw_wunlock(&pvh_global_lock);
5127 * pmap_is_prefaultable:
5129 * Return whether or not the specified virtual address is eligible
5133 pmap_is_prefaultable(pmap_t pmap, vm_offset_t addr)
5141 pte1 = pte1_load(pmap_pte1(pmap, addr));
5142 if (pte1_is_link(pte1)) {
5143 pte2 = pte2_load(pt2map_entry(addr));
5144 rv = !pte2_is_valid(pte2) ;
5151 * Returns TRUE if any of the given mappings were referenced and FALSE
5152 * otherwise. Both page and 1mpage mappings are supported.
5155 pmap_is_referenced_pvh(struct md_page *pvh)
5164 rw_assert(&pvh_global_lock, RA_WLOCKED);
5167 TAILQ_FOREACH(pv, &pvh->pv_list, pv_next) {
5170 pte1 = pte1_load(pmap_pte1(pmap, pv->pv_va));
5171 if (pte1_is_section(pte1)) {
5172 rv = (pte1 & (PTE1_A | PTE1_V)) == (PTE1_A | PTE1_V);
5174 pte2 = pte2_load(pmap_pte2_quick(pmap, pv->pv_va));
5175 rv = (pte2 & (PTE2_A | PTE2_V)) == (PTE2_A | PTE2_V);
5186 * pmap_is_referenced:
5188 * Return whether or not the specified physical page was referenced
5189 * in any physical maps.
5192 pmap_is_referenced(vm_page_t m)
5196 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
5197 ("%s: page %p is not managed", __func__, m));
5198 rw_wlock(&pvh_global_lock);
5199 rv = pmap_is_referenced_pvh(&m->md) ||
5200 ((m->flags & PG_FICTITIOUS) == 0 &&
5201 pmap_is_referenced_pvh(pa_to_pvh(VM_PAGE_TO_PHYS(m))));
5202 rw_wunlock(&pvh_global_lock);
5207 * pmap_ts_referenced:
5209 * Return a count of reference bits for a page, clearing those bits.
5210 * It is not necessary for every reference bit to be cleared, but it
5211 * is necessary that 0 only be returned when there are truly no
5212 * reference bits set.
5214 * As an optimization, update the page's dirty field if a modified bit is
5215 * found while counting reference bits. This opportunistic update can be
5216 * performed at low cost and can eliminate the need for some future calls
5217 * to pmap_is_modified(). However, since this function stops after
5218 * finding PMAP_TS_REFERENCED_MAX reference bits, it may not detect some
5219 * dirty pages. Those dirty pages will only be detected by a future call
5220 * to pmap_is_modified().
5223 pmap_ts_referenced(vm_page_t m)
5225 struct md_page *pvh;
5228 pt1_entry_t *pte1p, opte1;
5229 pt2_entry_t *pte2p, opte2;
5233 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
5234 ("%s: page %p is not managed", __func__, m));
5235 pa = VM_PAGE_TO_PHYS(m);
5236 pvh = pa_to_pvh(pa);
5237 rw_wlock(&pvh_global_lock);
5239 if ((m->flags & PG_FICTITIOUS) != 0 ||
5240 (pvf = TAILQ_FIRST(&pvh->pv_list)) == NULL)
5241 goto small_mappings;
5246 pte1p = pmap_pte1(pmap, pv->pv_va);
5247 opte1 = pte1_load(pte1p);
5248 if (pte1_is_dirty(opte1)) {
5250 * Although "opte1" is mapping a 1MB page, because
5251 * this function is called at a 4KB page granularity,
5252 * we only update the 4KB page under test.
5256 if ((opte1 & PTE1_A) != 0) {
5258 * Since this reference bit is shared by 256 4KB pages,
5259 * it should not be cleared every time it is tested.
5260 * Apply a simple "hash" function on the physical page
5261 * number, the virtual section number, and the pmap
5262 * address to select one 4KB page out of the 256
5263 * on which testing the reference bit will result
5264 * in clearing that bit. This function is designed
5265 * to avoid the selection of the same 4KB page
5266 * for every 1MB page mapping.
5268 * On demotion, a mapping that hasn't been referenced
5269 * is simply destroyed. To avoid the possibility of a
5270 * subsequent page fault on a demoted wired mapping,
5271 * always leave its reference bit set. Moreover,
5272 * since the section is wired, the current state of
5273 * its reference bit won't affect page replacement.
5275 if ((((pa >> PAGE_SHIFT) ^ (pv->pv_va >> PTE1_SHIFT) ^
5276 (uintptr_t)pmap) & (NPTE2_IN_PG - 1)) == 0 &&
5277 !pte1_is_wired(opte1)) {
5278 pte1_clear_bit(pte1p, PTE1_A);
5279 pmap_tlb_flush(pmap, pv->pv_va);
5284 /* Rotate the PV list if it has more than one entry. */
5285 if (TAILQ_NEXT(pv, pv_next) != NULL) {
5286 TAILQ_REMOVE(&pvh->pv_list, pv, pv_next);
5287 TAILQ_INSERT_TAIL(&pvh->pv_list, pv, pv_next);
5289 if (rtval >= PMAP_TS_REFERENCED_MAX)
5291 } while ((pv = TAILQ_FIRST(&pvh->pv_list)) != pvf);
5293 if ((pvf = TAILQ_FIRST(&m->md.pv_list)) == NULL)
5299 pte1p = pmap_pte1(pmap, pv->pv_va);
5300 KASSERT(pte1_is_link(pte1_load(pte1p)),
5301 ("%s: not found a link in page %p's pv list", __func__, m));
5303 pte2p = pmap_pte2_quick(pmap, pv->pv_va);
5304 opte2 = pte2_load(pte2p);
5305 if (pte2_is_dirty(opte2))
5307 if ((opte2 & PTE2_A) != 0) {
5308 pte2_clear_bit(pte2p, PTE2_A);
5309 pmap_tlb_flush(pmap, pv->pv_va);
5313 /* Rotate the PV list if it has more than one entry. */
5314 if (TAILQ_NEXT(pv, pv_next) != NULL) {
5315 TAILQ_REMOVE(&m->md.pv_list, pv, pv_next);
5316 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_next);
5318 } while ((pv = TAILQ_FIRST(&m->md.pv_list)) != pvf && rtval <
5319 PMAP_TS_REFERENCED_MAX);
5322 rw_wunlock(&pvh_global_lock);
5327 * Clear the wired attribute from the mappings for the specified range of
5328 * addresses in the given pmap. Every valid mapping within that range
5329 * must have the wired attribute set. In contrast, invalid mappings
5330 * cannot have the wired attribute set, so they are ignored.
5332 * The wired attribute of the page table entry is not a hardware feature,
5333 * so there is no need to invalidate any TLB entries.
5336 pmap_unwire(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
5339 pt1_entry_t *pte1p, pte1;
5340 pt2_entry_t *pte2p, pte2;
5341 boolean_t pv_lists_locked;
5343 if (pmap_is_current(pmap))
5344 pv_lists_locked = FALSE;
5346 pv_lists_locked = TRUE;
5348 rw_wlock(&pvh_global_lock);
5352 for (; sva < eva; sva = nextva) {
5353 nextva = pte1_trunc(sva + PTE1_SIZE);
5357 pte1p = pmap_pte1(pmap, sva);
5358 pte1 = pte1_load(pte1p);
5361 * Weed out invalid mappings. Note: we assume that L1 page
5362 * page table is always allocated, and in kernel virtual.
5367 if (pte1_is_section(pte1)) {
5368 if (!pte1_is_wired(pte1))
5369 panic("%s: pte1 %#x not wired", __func__, pte1);
5372 * Are we unwiring the entire large page? If not,
5373 * demote the mapping and fall through.
5375 if (sva + PTE1_SIZE == nextva && eva >= nextva) {
5376 pte1_clear_bit(pte1p, PTE1_W);
5377 pmap->pm_stats.wired_count -= PTE1_SIZE /
5381 if (!pv_lists_locked) {
5382 pv_lists_locked = TRUE;
5383 if (!rw_try_wlock(&pvh_global_lock)) {
5390 if (!pmap_demote_pte1(pmap, pte1p, sva))
5391 panic("%s: demotion failed", __func__);
5394 /* Update pte1 after demotion */
5395 pte1 = pte1_load(pte1p);
5401 KASSERT(pte1_is_link(pte1), ("%s: pmap %p va %#x pte1 %#x at %p"
5402 " is not link", __func__, pmap, sva, pte1, pte1p));
5405 * Limit our scan to either the end of the va represented
5406 * by the current L2 page table page, or to the end of the
5407 * range being protected.
5412 for (pte2p = pmap_pte2_quick(pmap, sva); sva != nextva; pte2p++,
5414 pte2 = pte2_load(pte2p);
5415 if (!pte2_is_valid(pte2))
5417 if (!pte2_is_wired(pte2))
5418 panic("%s: pte2 %#x is missing PTE2_W",
5422 * PTE2_W must be cleared atomically. Although the pmap
5423 * lock synchronizes access to PTE2_W, another processor
5424 * could be changing PTE2_NM and/or PTE2_A concurrently.
5426 pte2_clear_bit(pte2p, PTE2_W);
5427 pmap->pm_stats.wired_count--;
5430 if (pv_lists_locked) {
5432 rw_wunlock(&pvh_global_lock);
5438 * Clear the write and modified bits in each of the given page's mappings.
5441 pmap_remove_write(vm_page_t m)
5443 struct md_page *pvh;
5444 pv_entry_t next_pv, pv;
5447 pt2_entry_t *pte2p, opte2;
5450 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
5451 ("%s: page %p is not managed", __func__, m));
5454 * If the page is not exclusive busied, then PGA_WRITEABLE cannot be
5455 * set by another thread while the object is locked. Thus,
5456 * if PGA_WRITEABLE is clear, no page table entries need updating.
5458 VM_OBJECT_ASSERT_WLOCKED(m->object);
5459 if (!vm_page_xbusied(m) && (m->aflags & PGA_WRITEABLE) == 0)
5461 rw_wlock(&pvh_global_lock);
5463 if ((m->flags & PG_FICTITIOUS) != 0)
5464 goto small_mappings;
5465 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
5466 TAILQ_FOREACH_SAFE(pv, &pvh->pv_list, pv_next, next_pv) {
5470 pte1p = pmap_pte1(pmap, va);
5471 if (!(pte1_load(pte1p) & PTE1_RO))
5472 (void)pmap_demote_pte1(pmap, pte1p, va);
5476 TAILQ_FOREACH(pv, &m->md.pv_list, pv_next) {
5479 pte1p = pmap_pte1(pmap, pv->pv_va);
5480 KASSERT(!pte1_is_section(pte1_load(pte1p)), ("%s: found"
5481 " a section in page %p's pv list", __func__, m));
5482 pte2p = pmap_pte2_quick(pmap, pv->pv_va);
5483 opte2 = pte2_load(pte2p);
5484 if (!(opte2 & PTE2_RO)) {
5485 pte2_store(pte2p, opte2 | PTE2_RO | PTE2_NM);
5486 if (pte2_is_dirty(opte2))
5488 pmap_tlb_flush(pmap, pv->pv_va);
5492 vm_page_aflag_clear(m, PGA_WRITEABLE);
5494 rw_wunlock(&pvh_global_lock);
5498 * Apply the given advice to the specified range of addresses within the
5499 * given pmap. Depending on the advice, clear the referenced and/or
5500 * modified flags in each mapping and set the mapped page's dirty field.
5503 pmap_advise(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, int advice)
5505 pt1_entry_t *pte1p, opte1;
5506 pt2_entry_t *pte2p, pte2;
5509 boolean_t pv_lists_locked;
5511 if (advice != MADV_DONTNEED && advice != MADV_FREE)
5513 if (pmap_is_current(pmap))
5514 pv_lists_locked = FALSE;
5516 pv_lists_locked = TRUE;
5518 rw_wlock(&pvh_global_lock);
5522 for (; sva < eva; sva = pdnxt) {
5523 pdnxt = pte1_trunc(sva + PTE1_SIZE);
5526 pte1p = pmap_pte1(pmap, sva);
5527 opte1 = pte1_load(pte1p);
5528 if (!pte1_is_valid(opte1)) /* XXX */
5530 else if (pte1_is_section(opte1)) {
5531 if (!pte1_is_managed(opte1))
5533 if (!pv_lists_locked) {
5534 pv_lists_locked = TRUE;
5535 if (!rw_try_wlock(&pvh_global_lock)) {
5541 if (!pmap_demote_pte1(pmap, pte1p, sva)) {
5543 * The large page mapping was destroyed.
5549 * Unless the page mappings are wired, remove the
5550 * mapping to a single page so that a subsequent
5551 * access may repromote. Since the underlying L2 page
5552 * table is fully populated, this removal never
5553 * frees a L2 page table page.
5555 if (!pte1_is_wired(opte1)) {
5556 pte2p = pmap_pte2_quick(pmap, sva);
5557 KASSERT(pte2_is_valid(pte2_load(pte2p)),
5558 ("%s: invalid PTE2", __func__));
5559 pmap_remove_pte2(pmap, pte2p, sva, NULL);
5564 for (pte2p = pmap_pte2_quick(pmap, sva); sva != pdnxt; pte2p++,
5566 pte2 = pte2_load(pte2p);
5567 if (!pte2_is_valid(pte2) || !pte2_is_managed(pte2))
5569 else if (pte2_is_dirty(pte2)) {
5570 if (advice == MADV_DONTNEED) {
5572 * Future calls to pmap_is_modified()
5573 * can be avoided by making the page
5576 m = PHYS_TO_VM_PAGE(pte2_pa(pte2));
5579 pte2_set_bit(pte2p, PTE2_NM);
5580 pte2_clear_bit(pte2p, PTE2_A);
5581 } else if ((pte2 & PTE2_A) != 0)
5582 pte2_clear_bit(pte2p, PTE2_A);
5585 pmap_tlb_flush(pmap, sva);
5588 if (pv_lists_locked) {
5590 rw_wunlock(&pvh_global_lock);
5596 * Clear the modify bits on the specified physical page.
5599 pmap_clear_modify(vm_page_t m)
5601 struct md_page *pvh;
5602 pv_entry_t next_pv, pv;
5604 pt1_entry_t *pte1p, opte1;
5605 pt2_entry_t *pte2p, opte2;
5608 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
5609 ("%s: page %p is not managed", __func__, m));
5610 VM_OBJECT_ASSERT_WLOCKED(m->object);
5611 KASSERT(!vm_page_xbusied(m),
5612 ("%s: page %p is exclusive busy", __func__, m));
5615 * If the page is not PGA_WRITEABLE, then no PTE2s can have PTE2_NM
5616 * cleared. If the object containing the page is locked and the page
5617 * is not exclusive busied, then PGA_WRITEABLE cannot be concurrently
5620 if ((m->flags & PGA_WRITEABLE) == 0)
5622 rw_wlock(&pvh_global_lock);
5624 if ((m->flags & PG_FICTITIOUS) != 0)
5625 goto small_mappings;
5626 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
5627 TAILQ_FOREACH_SAFE(pv, &pvh->pv_list, pv_next, next_pv) {
5631 pte1p = pmap_pte1(pmap, va);
5632 opte1 = pte1_load(pte1p);
5633 if (!(opte1 & PTE1_RO)) {
5634 if (pmap_demote_pte1(pmap, pte1p, va) &&
5635 !pte1_is_wired(opte1)) {
5637 * Write protect the mapping to a
5638 * single page so that a subsequent
5639 * write access may repromote.
5641 va += VM_PAGE_TO_PHYS(m) - pte1_pa(opte1);
5642 pte2p = pmap_pte2_quick(pmap, va);
5643 opte2 = pte2_load(pte2p);
5644 if ((opte2 & PTE2_V)) {
5645 pte2_set_bit(pte2p, PTE2_NM | PTE2_RO);
5647 pmap_tlb_flush(pmap, va);
5654 TAILQ_FOREACH(pv, &m->md.pv_list, pv_next) {
5657 pte1p = pmap_pte1(pmap, pv->pv_va);
5658 KASSERT(!pte1_is_section(pte1_load(pte1p)), ("%s: found"
5659 " a section in page %p's pv list", __func__, m));
5660 pte2p = pmap_pte2_quick(pmap, pv->pv_va);
5661 if (pte2_is_dirty(pte2_load(pte2p))) {
5662 pte2_set_bit(pte2p, PTE2_NM);
5663 pmap_tlb_flush(pmap, pv->pv_va);
5668 rw_wunlock(&pvh_global_lock);
5673 * Sets the memory attribute for the specified page.
5676 pmap_page_set_memattr(vm_page_t m, vm_memattr_t ma)
5678 pt2_entry_t *cmap2_pte2p;
5683 oma = m->md.pat_mode;
5684 m->md.pat_mode = ma;
5686 CTR5(KTR_PMAP, "%s: page %p - 0x%08X oma: %d, ma: %d", __func__, m,
5687 VM_PAGE_TO_PHYS(m), oma, ma);
5688 if ((m->flags & PG_FICTITIOUS) != 0)
5692 * If "m" is a normal page, flush it from the cache.
5694 * First, try to find an existing mapping of the page by sf
5695 * buffer. sf_buf_invalidate_cache() modifies mapping and
5696 * flushes the cache.
5698 if (sf_buf_invalidate_cache(m, oma))
5702 * If page is not mapped by sf buffer, map the page
5703 * transient and do invalidation.
5706 pa = VM_PAGE_TO_PHYS(m);
5709 cmap2_pte2p = pc->pc_cmap2_pte2p;
5710 mtx_lock(&pc->pc_cmap_lock);
5711 if (pte2_load(cmap2_pte2p) != 0)
5712 panic("%s: CMAP2 busy", __func__);
5713 pte2_store(cmap2_pte2p, PTE2_KERN_NG(pa, PTE2_AP_KRW,
5714 vm_memattr_to_pte2(ma)));
5715 dcache_wbinv_poc((vm_offset_t)pc->pc_cmap2_addr, pa, PAGE_SIZE);
5716 pte2_clear(cmap2_pte2p);
5717 tlb_flush((vm_offset_t)pc->pc_cmap2_addr);
5719 mtx_unlock(&pc->pc_cmap_lock);
5724 * Miscellaneous support routines follow
5728 * Returns TRUE if the given page is mapped individually or as part of
5729 * a 1mpage. Otherwise, returns FALSE.
5732 pmap_page_is_mapped(vm_page_t m)
5736 if ((m->oflags & VPO_UNMANAGED) != 0)
5738 rw_wlock(&pvh_global_lock);
5739 rv = !TAILQ_EMPTY(&m->md.pv_list) ||
5740 ((m->flags & PG_FICTITIOUS) == 0 &&
5741 !TAILQ_EMPTY(&pa_to_pvh(VM_PAGE_TO_PHYS(m))->pv_list));
5742 rw_wunlock(&pvh_global_lock);
5747 * Returns true if the pmap's pv is one of the first
5748 * 16 pvs linked to from this page. This count may
5749 * be changed upwards or downwards in the future; it
5750 * is only necessary that true be returned for a small
5751 * subset of pmaps for proper page aging.
5754 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
5756 struct md_page *pvh;
5761 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
5762 ("%s: page %p is not managed", __func__, m));
5764 rw_wlock(&pvh_global_lock);
5765 TAILQ_FOREACH(pv, &m->md.pv_list, pv_next) {
5766 if (PV_PMAP(pv) == pmap) {
5774 if (!rv && loops < 16 && (m->flags & PG_FICTITIOUS) == 0) {
5775 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
5776 TAILQ_FOREACH(pv, &pvh->pv_list, pv_next) {
5777 if (PV_PMAP(pv) == pmap) {
5786 rw_wunlock(&pvh_global_lock);
5791 * pmap_zero_page zeros the specified hardware page by mapping
5792 * the page into KVM and using bzero to clear its contents.
5795 pmap_zero_page(vm_page_t m)
5797 pt2_entry_t *cmap2_pte2p;
5802 cmap2_pte2p = pc->pc_cmap2_pte2p;
5803 mtx_lock(&pc->pc_cmap_lock);
5804 if (pte2_load(cmap2_pte2p) != 0)
5805 panic("%s: CMAP2 busy", __func__);
5806 pte2_store(cmap2_pte2p, PTE2_KERN_NG(VM_PAGE_TO_PHYS(m), PTE2_AP_KRW,
5807 vm_page_pte2_attr(m)));
5808 pagezero(pc->pc_cmap2_addr);
5809 pte2_clear(cmap2_pte2p);
5810 tlb_flush((vm_offset_t)pc->pc_cmap2_addr);
5812 mtx_unlock(&pc->pc_cmap_lock);
5816 * pmap_zero_page_area zeros the specified hardware page by mapping
5817 * the page into KVM and using bzero to clear its contents.
5819 * off and size may not cover an area beyond a single hardware page.
5822 pmap_zero_page_area(vm_page_t m, int off, int size)
5824 pt2_entry_t *cmap2_pte2p;
5829 cmap2_pte2p = pc->pc_cmap2_pte2p;
5830 mtx_lock(&pc->pc_cmap_lock);
5831 if (pte2_load(cmap2_pte2p) != 0)
5832 panic("%s: CMAP2 busy", __func__);
5833 pte2_store(cmap2_pte2p, PTE2_KERN_NG(VM_PAGE_TO_PHYS(m), PTE2_AP_KRW,
5834 vm_page_pte2_attr(m)));
5835 if (off == 0 && size == PAGE_SIZE)
5836 pagezero(pc->pc_cmap2_addr);
5838 bzero(pc->pc_cmap2_addr + off, size);
5839 pte2_clear(cmap2_pte2p);
5840 tlb_flush((vm_offset_t)pc->pc_cmap2_addr);
5842 mtx_unlock(&pc->pc_cmap_lock);
5846 * pmap_copy_page copies the specified (machine independent)
5847 * page by mapping the page into virtual memory and using
5848 * bcopy to copy the page, one machine dependent page at a
5852 pmap_copy_page(vm_page_t src, vm_page_t dst)
5854 pt2_entry_t *cmap1_pte2p, *cmap2_pte2p;
5859 cmap1_pte2p = pc->pc_cmap1_pte2p;
5860 cmap2_pte2p = pc->pc_cmap2_pte2p;
5861 mtx_lock(&pc->pc_cmap_lock);
5862 if (pte2_load(cmap1_pte2p) != 0)
5863 panic("%s: CMAP1 busy", __func__);
5864 if (pte2_load(cmap2_pte2p) != 0)
5865 panic("%s: CMAP2 busy", __func__);
5866 pte2_store(cmap1_pte2p, PTE2_KERN_NG(VM_PAGE_TO_PHYS(src),
5867 PTE2_AP_KR | PTE2_NM, vm_page_pte2_attr(src)));
5868 pte2_store(cmap2_pte2p, PTE2_KERN_NG(VM_PAGE_TO_PHYS(dst),
5869 PTE2_AP_KRW, vm_page_pte2_attr(dst)));
5870 bcopy(pc->pc_cmap1_addr, pc->pc_cmap2_addr, PAGE_SIZE);
5871 pte2_clear(cmap1_pte2p);
5872 tlb_flush((vm_offset_t)pc->pc_cmap1_addr);
5873 pte2_clear(cmap2_pte2p);
5874 tlb_flush((vm_offset_t)pc->pc_cmap2_addr);
5876 mtx_unlock(&pc->pc_cmap_lock);
5879 int unmapped_buf_allowed = 1;
5882 pmap_copy_pages(vm_page_t ma[], vm_offset_t a_offset, vm_page_t mb[],
5883 vm_offset_t b_offset, int xfersize)
5885 pt2_entry_t *cmap1_pte2p, *cmap2_pte2p;
5886 vm_page_t a_pg, b_pg;
5888 vm_offset_t a_pg_offset, b_pg_offset;
5894 cmap1_pte2p = pc->pc_cmap1_pte2p;
5895 cmap2_pte2p = pc->pc_cmap2_pte2p;
5896 mtx_lock(&pc->pc_cmap_lock);
5897 if (pte2_load(cmap1_pte2p) != 0)
5898 panic("pmap_copy_pages: CMAP1 busy");
5899 if (pte2_load(cmap2_pte2p) != 0)
5900 panic("pmap_copy_pages: CMAP2 busy");
5901 while (xfersize > 0) {
5902 a_pg = ma[a_offset >> PAGE_SHIFT];
5903 a_pg_offset = a_offset & PAGE_MASK;
5904 cnt = min(xfersize, PAGE_SIZE - a_pg_offset);
5905 b_pg = mb[b_offset >> PAGE_SHIFT];
5906 b_pg_offset = b_offset & PAGE_MASK;
5907 cnt = min(cnt, PAGE_SIZE - b_pg_offset);
5908 pte2_store(cmap1_pte2p, PTE2_KERN_NG(VM_PAGE_TO_PHYS(a_pg),
5909 PTE2_AP_KR | PTE2_NM, vm_page_pte2_attr(a_pg)));
5910 tlb_flush_local((vm_offset_t)pc->pc_cmap1_addr);
5911 pte2_store(cmap2_pte2p, PTE2_KERN_NG(VM_PAGE_TO_PHYS(b_pg),
5912 PTE2_AP_KRW, vm_page_pte2_attr(b_pg)));
5913 tlb_flush_local((vm_offset_t)pc->pc_cmap2_addr);
5914 a_cp = pc->pc_cmap1_addr + a_pg_offset;
5915 b_cp = pc->pc_cmap2_addr + b_pg_offset;
5916 bcopy(a_cp, b_cp, cnt);
5921 pte2_clear(cmap1_pte2p);
5922 tlb_flush((vm_offset_t)pc->pc_cmap1_addr);
5923 pte2_clear(cmap2_pte2p);
5924 tlb_flush((vm_offset_t)pc->pc_cmap2_addr);
5926 mtx_unlock(&pc->pc_cmap_lock);
5930 pmap_quick_enter_page(vm_page_t m)
5937 pte2p = pc->pc_qmap_pte2p;
5939 KASSERT(pte2_load(pte2p) == 0, ("%s: PTE2 busy", __func__));
5941 pte2_store(pte2p, PTE2_KERN_NG(VM_PAGE_TO_PHYS(m), PTE2_AP_KRW,
5942 vm_page_pte2_attr(m)));
5943 return (pc->pc_qmap_addr);
5947 pmap_quick_remove_page(vm_offset_t addr)
5953 pte2p = pc->pc_qmap_pte2p;
5955 KASSERT(addr == pc->pc_qmap_addr, ("%s: invalid address", __func__));
5956 KASSERT(pte2_load(pte2p) != 0, ("%s: PTE2 not in use", __func__));
5959 tlb_flush(pc->pc_qmap_addr);
5964 * Copy the range specified by src_addr/len
5965 * from the source map to the range dst_addr/len
5966 * in the destination map.
5968 * This routine is only advisory and need not do anything.
5971 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr, vm_size_t len,
5972 vm_offset_t src_addr)
5974 struct spglist free;
5976 vm_offset_t end_addr = src_addr + len;
5979 if (dst_addr != src_addr)
5982 if (!pmap_is_current(src_pmap))
5985 rw_wlock(&pvh_global_lock);
5986 if (dst_pmap < src_pmap) {
5987 PMAP_LOCK(dst_pmap);
5988 PMAP_LOCK(src_pmap);
5990 PMAP_LOCK(src_pmap);
5991 PMAP_LOCK(dst_pmap);
5994 for (addr = src_addr; addr < end_addr; addr = nextva) {
5995 pt2_entry_t *src_pte2p, *dst_pte2p;
5996 vm_page_t dst_mpt2pg, src_mpt2pg;
5997 pt1_entry_t src_pte1;
6000 KASSERT(addr < VM_MAXUSER_ADDRESS,
6001 ("%s: invalid to pmap_copy page tables", __func__));
6003 nextva = pte1_trunc(addr + PTE1_SIZE);
6007 pte1_idx = pte1_index(addr);
6008 src_pte1 = src_pmap->pm_pt1[pte1_idx];
6009 if (pte1_is_section(src_pte1)) {
6010 if ((addr & PTE1_OFFSET) != 0 ||
6011 (addr + PTE1_SIZE) > end_addr)
6013 if (dst_pmap->pm_pt1[pte1_idx] == 0 &&
6014 (!pte1_is_managed(src_pte1) ||
6015 pmap_pv_insert_pte1(dst_pmap, addr,
6016 pte1_pa(src_pte1)))) {
6017 dst_pmap->pm_pt1[pte1_idx] = src_pte1 &
6019 dst_pmap->pm_stats.resident_count +=
6020 PTE1_SIZE / PAGE_SIZE;
6021 pmap_pte1_mappings++;
6024 } else if (!pte1_is_link(src_pte1))
6027 src_mpt2pg = PHYS_TO_VM_PAGE(pte1_link_pa(src_pte1));
6030 * We leave PT2s to be linked from PT1 even if they are not
6031 * referenced until all PT2s in a page are without reference.
6033 * QQQ: It could be changed ...
6035 #if 0 /* single_pt2_link_is_cleared */
6036 KASSERT(pt2_wirecount_get(src_mpt2pg, pte1_idx) > 0,
6037 ("%s: source page table page is unused", __func__));
6039 if (pt2_wirecount_get(src_mpt2pg, pte1_idx) == 0)
6042 if (nextva > end_addr)
6045 src_pte2p = pt2map_entry(addr);
6046 while (addr < nextva) {
6047 pt2_entry_t temp_pte2;
6048 temp_pte2 = pte2_load(src_pte2p);
6050 * we only virtual copy managed pages
6052 if (pte2_is_managed(temp_pte2)) {
6053 dst_mpt2pg = pmap_allocpte2(dst_pmap, addr,
6054 PMAP_ENTER_NOSLEEP);
6055 if (dst_mpt2pg == NULL)
6057 dst_pte2p = pmap_pte2_quick(dst_pmap, addr);
6058 if (!pte2_is_valid(pte2_load(dst_pte2p)) &&
6059 pmap_try_insert_pv_entry(dst_pmap, addr,
6060 PHYS_TO_VM_PAGE(pte2_pa(temp_pte2)))) {
6062 * Clear the wired, modified, and
6063 * accessed (referenced) bits
6066 temp_pte2 &= ~(PTE2_W | PTE2_A);
6067 temp_pte2 |= PTE2_NM;
6068 pte2_store(dst_pte2p, temp_pte2);
6069 dst_pmap->pm_stats.resident_count++;
6072 if (pmap_unwire_pt2(dst_pmap, addr,
6073 dst_mpt2pg, &free)) {
6074 pmap_tlb_flush(dst_pmap, addr);
6075 pmap_free_zero_pages(&free);
6079 if (pt2_wirecount_get(dst_mpt2pg, pte1_idx) >=
6080 pt2_wirecount_get(src_mpt2pg, pte1_idx))
6089 rw_wunlock(&pvh_global_lock);
6090 PMAP_UNLOCK(src_pmap);
6091 PMAP_UNLOCK(dst_pmap);
6095 * Increase the starting virtual address of the given mapping if a
6096 * different alignment might result in more section mappings.
6099 pmap_align_superpage(vm_object_t object, vm_ooffset_t offset,
6100 vm_offset_t *addr, vm_size_t size)
6102 vm_offset_t pte1_offset;
6104 if (size < PTE1_SIZE)
6106 if (object != NULL && (object->flags & OBJ_COLORED) != 0)
6107 offset += ptoa(object->pg_color);
6108 pte1_offset = offset & PTE1_OFFSET;
6109 if (size - ((PTE1_SIZE - pte1_offset) & PTE1_OFFSET) < PTE1_SIZE ||
6110 (*addr & PTE1_OFFSET) == pte1_offset)
6112 if ((*addr & PTE1_OFFSET) < pte1_offset)
6113 *addr = pte1_trunc(*addr) + pte1_offset;
6115 *addr = pte1_roundup(*addr) + pte1_offset;
6119 pmap_activate(struct thread *td)
6121 pmap_t pmap, oldpmap;
6124 PDEBUG(9, printf("%s: td = %08x\n", __func__, (uint32_t)td));
6127 pmap = vmspace_pmap(td->td_proc->p_vmspace);
6128 oldpmap = PCPU_GET(curpmap);
6129 cpuid = PCPU_GET(cpuid);
6132 CPU_CLR_ATOMIC(cpuid, &oldpmap->pm_active);
6133 CPU_SET_ATOMIC(cpuid, &pmap->pm_active);
6135 CPU_CLR(cpuid, &oldpmap->pm_active);
6136 CPU_SET(cpuid, &pmap->pm_active);
6139 ttb = pmap_ttb_get(pmap);
6142 * pmap_activate is for the current thread on the current cpu
6144 td->td_pcb->pcb_pagedir = ttb;
6146 PCPU_SET(curpmap, pmap);
6151 * Perform the pmap work for mincore.
6154 pmap_mincore(pmap_t pmap, vm_offset_t addr, vm_paddr_t *locked_pa)
6156 pt1_entry_t *pte1p, pte1;
6157 pt2_entry_t *pte2p, pte2;
6164 pte1p = pmap_pte1(pmap, addr);
6165 pte1 = pte1_load(pte1p);
6166 if (pte1_is_section(pte1)) {
6167 pa = trunc_page(pte1_pa(pte1) | (addr & PTE1_OFFSET));
6168 managed = pte1_is_managed(pte1);
6169 val = MINCORE_SUPER | MINCORE_INCORE;
6170 if (pte1_is_dirty(pte1))
6171 val |= MINCORE_MODIFIED | MINCORE_MODIFIED_OTHER;
6173 val |= MINCORE_REFERENCED | MINCORE_REFERENCED_OTHER;
6174 } else if (pte1_is_link(pte1)) {
6175 pte2p = pmap_pte2(pmap, addr);
6176 pte2 = pte2_load(pte2p);
6177 pmap_pte2_release(pte2p);
6179 managed = pte2_is_managed(pte2);
6180 val = MINCORE_INCORE;
6181 if (pte2_is_dirty(pte2))
6182 val |= MINCORE_MODIFIED | MINCORE_MODIFIED_OTHER;
6184 val |= MINCORE_REFERENCED | MINCORE_REFERENCED_OTHER;
6189 if ((val & (MINCORE_MODIFIED_OTHER | MINCORE_REFERENCED_OTHER)) !=
6190 (MINCORE_MODIFIED_OTHER | MINCORE_REFERENCED_OTHER) && managed) {
6191 /* Ensure that "PHYS_TO_VM_PAGE(pa)->object" doesn't change. */
6192 if (vm_page_pa_tryrelock(pmap, pa, locked_pa))
6195 PA_UNLOCK_COND(*locked_pa);
6201 pmap_kenter_device(vm_offset_t va, vm_size_t size, vm_paddr_t pa)
6206 KASSERT((size & PAGE_MASK) == 0,
6207 ("%s: device mapping not page-sized", __func__));
6210 l2attr = vm_memattr_to_pte2(VM_MEMATTR_DEVICE);
6212 pmap_kenter_prot_attr(va, pa, PTE2_AP_KRW, l2attr);
6217 tlb_flush_range(sva, va - sva);
6221 pmap_kremove_device(vm_offset_t va, vm_size_t size)
6225 KASSERT((size & PAGE_MASK) == 0,
6226 ("%s: device mapping not page-sized", __func__));
6234 tlb_flush_range(sva, va - sva);
6238 pmap_set_pcb_pagedir(pmap_t pmap, struct pcb *pcb)
6241 pcb->pcb_pagedir = pmap_ttb_get(pmap);
6246 * Clean L1 data cache range by physical address.
6247 * The range must be within a single page.
6250 pmap_dcache_wb_pou(vm_paddr_t pa, vm_size_t size, uint32_t attr)
6252 pt2_entry_t *cmap2_pte2p;
6255 KASSERT(((pa & PAGE_MASK) + size) <= PAGE_SIZE,
6256 ("%s: not on single page", __func__));
6260 cmap2_pte2p = pc->pc_cmap2_pte2p;
6261 mtx_lock(&pc->pc_cmap_lock);
6262 if (pte2_load(cmap2_pte2p) != 0)
6263 panic("%s: CMAP2 busy", __func__);
6264 pte2_store(cmap2_pte2p, PTE2_KERN_NG(pa, PTE2_AP_KRW, attr));
6265 dcache_wb_pou((vm_offset_t)pc->pc_cmap2_addr + (pa & PAGE_MASK), size);
6266 pte2_clear(cmap2_pte2p);
6267 tlb_flush((vm_offset_t)pc->pc_cmap2_addr);
6269 mtx_unlock(&pc->pc_cmap_lock);
6273 * Sync instruction cache range which is not mapped yet.
6276 cache_icache_sync_fresh(vm_offset_t va, vm_paddr_t pa, vm_size_t size)
6278 uint32_t len, offset;
6281 /* Write back d-cache on given address range. */
6282 offset = pa & PAGE_MASK;
6283 for ( ; size != 0; size -= len, pa += len, offset = 0) {
6284 len = min(PAGE_SIZE - offset, size);
6285 m = PHYS_TO_VM_PAGE(pa);
6286 KASSERT(m != NULL, ("%s: vm_page_t is null for %#x",
6288 pmap_dcache_wb_pou(pa, len, vm_page_pte2_attr(m));
6291 * I-cache is VIPT. Only way how to flush all virtual mappings
6292 * on given physical address is to invalidate all i-cache.
6298 pmap_sync_icache(pmap_t pmap, vm_offset_t va, vm_size_t size)
6301 /* Write back d-cache on given address range. */
6302 if (va >= VM_MIN_KERNEL_ADDRESS) {
6303 dcache_wb_pou(va, size);
6305 uint32_t len, offset;
6309 offset = va & PAGE_MASK;
6310 for ( ; size != 0; size -= len, va += len, offset = 0) {
6311 pa = pmap_extract(pmap, va); /* offset is preserved */
6312 len = min(PAGE_SIZE - offset, size);
6313 m = PHYS_TO_VM_PAGE(pa);
6314 KASSERT(m != NULL, ("%s: vm_page_t is null for %#x",
6316 pmap_dcache_wb_pou(pa, len, vm_page_pte2_attr(m));
6320 * I-cache is VIPT. Only way how to flush all virtual mappings
6321 * on given physical address is to invalidate all i-cache.
6327 * The implementation of pmap_fault() uses IN_RANGE2() macro which
6328 * depends on the fact that given range size is a power of 2.
6330 CTASSERT(powerof2(NB_IN_PT1));
6331 CTASSERT(powerof2(PT2MAP_SIZE));
6333 #define IN_RANGE2(addr, start, size) \
6334 ((vm_offset_t)(start) == ((vm_offset_t)(addr) & ~((size) - 1)))
6337 * Handle access and R/W emulation faults.
6340 pmap_fault(pmap_t pmap, vm_offset_t far, uint32_t fsr, int idx, bool usermode)
6342 pt1_entry_t *pte1p, pte1;
6343 pt2_entry_t *pte2p, pte2;
6349 * In kernel, we should never get abort with FAR which is in range of
6350 * pmap->pm_pt1 or PT2MAP address spaces. If it happens, stop here
6351 * and print out a useful abort message and even get to the debugger
6352 * otherwise it likely ends with never ending loop of aborts.
6354 if (__predict_false(IN_RANGE2(far, pmap->pm_pt1, NB_IN_PT1))) {
6356 * All L1 tables should always be mapped and present.
6357 * However, we check only current one herein. For user mode,
6358 * only permission abort from malicious user is not fatal.
6359 * And alignment abort as it may have higher priority.
6361 if (!usermode || (idx != FAULT_ALIGN && idx != FAULT_PERM_L2)) {
6362 CTR4(KTR_PMAP, "%s: pmap %#x pm_pt1 %#x far %#x",
6363 __func__, pmap, pmap->pm_pt1, far);
6364 panic("%s: pm_pt1 abort", __func__);
6366 return (KERN_INVALID_ADDRESS);
6368 if (__predict_false(IN_RANGE2(far, PT2MAP, PT2MAP_SIZE))) {
6370 * PT2MAP should be always mapped and present in current
6371 * L1 table. However, only existing L2 tables are mapped
6372 * in PT2MAP. For user mode, only L2 translation abort and
6373 * permission abort from malicious user is not fatal.
6374 * And alignment abort as it may have higher priority.
6376 if (!usermode || (idx != FAULT_ALIGN &&
6377 idx != FAULT_TRAN_L2 && idx != FAULT_PERM_L2)) {
6378 CTR4(KTR_PMAP, "%s: pmap %#x PT2MAP %#x far %#x",
6379 __func__, pmap, PT2MAP, far);
6380 panic("%s: PT2MAP abort", __func__);
6382 return (KERN_INVALID_ADDRESS);
6386 * A pmap lock is used below for handling of access and R/W emulation
6387 * aborts. They were handled by atomic operations before so some
6388 * analysis of new situation is needed to answer the following question:
6389 * Is it safe to use the lock even for these aborts?
6391 * There may happen two cases in general:
6393 * (1) Aborts while the pmap lock is locked already - this should not
6394 * happen as pmap lock is not recursive. However, under pmap lock only
6395 * internal kernel data should be accessed and such data should be
6396 * mapped with A bit set and NM bit cleared. If double abort happens,
6397 * then a mapping of data which has caused it must be fixed. Further,
6398 * all new mappings are always made with A bit set and the bit can be
6399 * cleared only on managed mappings.
6401 * (2) Aborts while another lock(s) is/are locked - this already can
6402 * happen. However, there is no difference here if it's either access or
6403 * R/W emulation abort, or if it's some other abort.
6408 pte1 = pte1_load(pmap_pte1(pmap, far));
6409 if (pte1_is_link(pte1)) {
6411 * Check in advance that associated L2 page table is mapped into
6412 * PT2MAP space. Note that faulty access to not mapped L2 page
6413 * table is caught in more general check above where "far" is
6414 * checked that it does not lay in PT2MAP space. Note also that
6415 * L1 page table and PT2TAB always exist and are mapped.
6417 pte2 = pt2tab_load(pmap_pt2tab_entry(pmap, far));
6418 if (!pte2_is_valid(pte2))
6419 panic("%s: missing L2 page table (%p, %#x)",
6420 __func__, pmap, far);
6425 * Special treatment is due to break-before-make approach done when
6426 * pte1 is updated for userland mapping during section promotion or
6427 * demotion. If not caught here, pmap_enter() can find a section
6428 * mapping on faulting address. That is not allowed.
6430 if (idx == FAULT_TRAN_L1 && usermode && cp15_ats1cur_check(far) == 0) {
6432 return (KERN_SUCCESS);
6436 * Accesss bits for page and section. Note that the entry
6437 * is not in TLB yet, so TLB flush is not necessary.
6439 * QQQ: This is hardware emulation, we do not call userret()
6440 * for aborts from user mode.
6442 if (idx == FAULT_ACCESS_L2) {
6443 pte1 = pte1_load(pmap_pte1(pmap, far));
6444 if (pte1_is_link(pte1)) {
6445 /* L2 page table should exist and be mapped. */
6446 pte2p = pt2map_entry(far);
6447 pte2 = pte2_load(pte2p);
6448 if (pte2_is_valid(pte2)) {
6449 pte2_store(pte2p, pte2 | PTE2_A);
6451 return (KERN_SUCCESS);
6455 * We got L2 access fault but PTE1 is not a link.
6456 * Probably some race happened, do nothing.
6458 CTR3(KTR_PMAP, "%s: FAULT_ACCESS_L2 - pmap %#x far %#x",
6459 __func__, pmap, far);
6461 return (KERN_SUCCESS);
6464 if (idx == FAULT_ACCESS_L1) {
6465 pte1p = pmap_pte1(pmap, far);
6466 pte1 = pte1_load(pte1p);
6467 if (pte1_is_section(pte1)) {
6468 pte1_store(pte1p, pte1 | PTE1_A);
6470 return (KERN_SUCCESS);
6473 * We got L1 access fault but PTE1 is not section
6474 * mapping. Probably some race happened, do nothing.
6476 CTR3(KTR_PMAP, "%s: FAULT_ACCESS_L1 - pmap %#x far %#x",
6477 __func__, pmap, far);
6479 return (KERN_SUCCESS);
6484 * Handle modify bits for page and section. Note that the modify
6485 * bit is emulated by software. So PTEx_RO is software read only
6486 * bit and PTEx_NM flag is real hardware read only bit.
6488 * QQQ: This is hardware emulation, we do not call userret()
6489 * for aborts from user mode.
6491 if ((fsr & FSR_WNR) && (idx == FAULT_PERM_L2)) {
6492 pte1 = pte1_load(pmap_pte1(pmap, far));
6493 if (pte1_is_link(pte1)) {
6494 /* L2 page table should exist and be mapped. */
6495 pte2p = pt2map_entry(far);
6496 pte2 = pte2_load(pte2p);
6497 if (pte2_is_valid(pte2) && !(pte2 & PTE2_RO) &&
6499 pte2_store(pte2p, pte2 & ~PTE2_NM);
6500 tlb_flush(trunc_page(far));
6502 return (KERN_SUCCESS);
6506 * We got L2 permission fault but PTE1 is not a link.
6507 * Probably some race happened, do nothing.
6509 CTR3(KTR_PMAP, "%s: FAULT_PERM_L2 - pmap %#x far %#x",
6510 __func__, pmap, far);
6512 return (KERN_SUCCESS);
6515 if ((fsr & FSR_WNR) && (idx == FAULT_PERM_L1)) {
6516 pte1p = pmap_pte1(pmap, far);
6517 pte1 = pte1_load(pte1p);
6518 if (pte1_is_section(pte1)) {
6519 if (!(pte1 & PTE1_RO) && (pte1 & PTE1_NM)) {
6520 pte1_store(pte1p, pte1 & ~PTE1_NM);
6521 tlb_flush(pte1_trunc(far));
6523 return (KERN_SUCCESS);
6527 * We got L1 permission fault but PTE1 is not section
6528 * mapping. Probably some race happened, do nothing.
6530 CTR3(KTR_PMAP, "%s: FAULT_PERM_L1 - pmap %#x far %#x",
6531 __func__, pmap, far);
6533 return (KERN_SUCCESS);
6538 * QQQ: The previous code, mainly fast handling of access and
6539 * modify bits aborts, could be moved to ASM. Now we are
6540 * starting to deal with not fast aborts.
6543 return (KERN_FAILURE);
6546 #if defined(PMAP_DEBUG)
6548 * Reusing of KVA used in pmap_zero_page function !!!
6551 pmap_zero_page_check(vm_page_t m)
6553 pt2_entry_t *cmap2_pte2p;
6559 cmap2_pte2p = pc->pc_cmap2_pte2p;
6560 mtx_lock(&pc->pc_cmap_lock);
6561 if (pte2_load(cmap2_pte2p) != 0)
6562 panic("%s: CMAP2 busy", __func__);
6563 pte2_store(cmap2_pte2p, PTE2_KERN_NG(VM_PAGE_TO_PHYS(m), PTE2_AP_KRW,
6564 vm_page_pte2_attr(m)));
6565 end = (uint32_t*)(pc->pc_cmap2_addr + PAGE_SIZE);
6566 for (p = (uint32_t*)pc->pc_cmap2_addr; p < end; p++)
6568 panic("%s: page %p not zero, va: %p", __func__, m,
6570 pte2_clear(cmap2_pte2p);
6571 tlb_flush((vm_offset_t)pc->pc_cmap2_addr);
6573 mtx_unlock(&pc->pc_cmap_lock);
6577 pmap_pid_dump(int pid)
6584 sx_slock(&allproc_lock);
6585 FOREACH_PROC_IN_SYSTEM(p) {
6586 if (p->p_pid != pid || p->p_vmspace == NULL)
6589 pmap = vmspace_pmap(p->p_vmspace);
6590 for (i = 0; i < NPTE1_IN_PT1; i++) {
6592 pt2_entry_t *pte2p, pte2;
6593 vm_offset_t base, va;
6597 base = i << PTE1_SHIFT;
6598 pte1 = pte1_load(&pmap->pm_pt1[i]);
6600 if (pte1_is_section(pte1)) {
6602 * QQQ: Do something here!
6604 } else if (pte1_is_link(pte1)) {
6605 for (j = 0; j < NPTE2_IN_PT2; j++) {
6606 va = base + (j << PAGE_SHIFT);
6607 if (va >= VM_MIN_KERNEL_ADDRESS) {
6612 sx_sunlock(&allproc_lock);
6615 pte2p = pmap_pte2(pmap, va);
6616 pte2 = pte2_load(pte2p);
6617 pmap_pte2_release(pte2p);
6618 if (!pte2_is_valid(pte2))
6622 m = PHYS_TO_VM_PAGE(pa);
6623 printf("va: 0x%x, pa: 0x%x, h: %d, w:"
6624 " %d, f: 0x%x", va, pa,
6625 m->hold_count, m->wire_count,
6639 sx_sunlock(&allproc_lock);
6646 static pt2_entry_t *
6647 pmap_pte2_ddb(pmap_t pmap, vm_offset_t va)
6650 vm_paddr_t pt2pg_pa;
6652 pte1 = pte1_load(pmap_pte1(pmap, va));
6653 if (!pte1_is_link(pte1))
6656 if (pmap_is_current(pmap))
6657 return (pt2map_entry(va));
6659 /* Note that L2 page table size is not equal to PAGE_SIZE. */
6660 pt2pg_pa = trunc_page(pte1_link_pa(pte1));
6661 if (pte2_pa(pte2_load(PMAP3)) != pt2pg_pa) {
6662 pte2_store(PMAP3, PTE2_KPT(pt2pg_pa));
6664 PMAP3cpu = PCPU_GET(cpuid);
6666 tlb_flush_local((vm_offset_t)PADDR3);
6669 else if (PMAP3cpu != PCPU_GET(cpuid)) {
6670 PMAP3cpu = PCPU_GET(cpuid);
6671 tlb_flush_local((vm_offset_t)PADDR3);
6674 return (PADDR3 + (arm32_btop(va) & (NPTE2_IN_PG - 1)));
6678 dump_pmap(pmap_t pmap)
6681 printf("pmap %p\n", pmap);
6682 printf(" pm_pt1: %p\n", pmap->pm_pt1);
6683 printf(" pm_pt2tab: %p\n", pmap->pm_pt2tab);
6684 printf(" pm_active: 0x%08lX\n", pmap->pm_active.__bits[0]);
6687 DB_SHOW_COMMAND(pmaps, pmap_list_pmaps)
6691 LIST_FOREACH(pmap, &allpmaps, pm_list) {
6697 pte2_class(pt2_entry_t pte2)
6701 cls = (pte2 >> 2) & 0x03;
6702 cls |= (pte2 >> 4) & 0x04;
6707 dump_section(pmap_t pmap, uint32_t pte1_idx)
6712 dump_link(pmap_t pmap, uint32_t pte1_idx, boolean_t invalid_ok)
6716 pt2_entry_t *pte2p, pte2;
6719 va = pte1_idx << PTE1_SHIFT;
6720 pte2p = pmap_pte2_ddb(pmap, va);
6721 for (i = 0; i < NPTE2_IN_PT2; i++, pte2p++, va += PAGE_SIZE) {
6722 pte2 = pte2_load(pte2p);
6725 if (!pte2_is_valid(pte2)) {
6726 printf(" 0x%08X: 0x%08X", va, pte2);
6728 printf(" - not valid !!!");
6732 m = PHYS_TO_VM_PAGE(pte2_pa(pte2));
6733 printf(" 0x%08X: 0x%08X, TEX%d, s:%d, g:%d, m:%p", va , pte2,
6734 pte2_class(pte2), !!(pte2 & PTE2_S), !(pte2 & PTE2_NG), m);
6736 printf(" v:%d h:%d w:%d f:0x%04X\n", m->valid,
6737 m->hold_count, m->wire_count, m->flags);
6744 static __inline boolean_t
6745 is_pv_chunk_space(vm_offset_t va)
6748 if ((((vm_offset_t)pv_chunkbase) <= va) &&
6749 (va < ((vm_offset_t)pv_chunkbase + PAGE_SIZE * pv_maxchunks)))
6754 DB_SHOW_COMMAND(pmap, pmap_pmap_print)
6756 /* XXX convert args. */
6757 pmap_t pmap = (pmap_t)addr;
6760 vm_offset_t va, eva;
6763 boolean_t invalid_ok, dump_link_ok, dump_pv_chunk;
6768 LIST_FOREACH(pm, &allpmaps, pm_list)
6769 if (pm == pmap) break;
6771 printf("given pmap %p is not in allpmaps list\n", pmap);
6775 pmap = PCPU_GET(curpmap);
6777 eva = (modif[0] == 'u') ? VM_MAXUSER_ADDRESS : 0xFFFFFFFF;
6778 dump_pv_chunk = FALSE; /* XXX evaluate from modif[] */
6780 printf("pmap: 0x%08X\n", (uint32_t)pmap);
6781 printf("PT2MAP: 0x%08X\n", (uint32_t)PT2MAP);
6782 printf("pt2tab: 0x%08X\n", (uint32_t)pmap->pm_pt2tab);
6784 for(i = 0; i < NPTE1_IN_PT1; i++) {
6785 pte1 = pte1_load(&pmap->pm_pt1[i]);
6788 va = i << PTE1_SHIFT;
6792 if (pte1_is_section(pte1)) {
6793 printf("0x%08X: Section 0x%08X, s:%d g:%d\n", va, pte1,
6794 !!(pte1 & PTE1_S), !(pte1 & PTE1_NG));
6795 dump_section(pmap, i);
6796 } else if (pte1_is_link(pte1)) {
6797 dump_link_ok = TRUE;
6799 pte2 = pte2_load(pmap_pt2tab_entry(pmap, va));
6800 m = PHYS_TO_VM_PAGE(pte1_link_pa(pte1));
6801 printf("0x%08X: Link 0x%08X, pt2tab: 0x%08X m: %p",
6803 if (is_pv_chunk_space(va)) {
6804 printf(" - pv_chunk space");
6808 dump_link_ok = FALSE;
6811 printf(" w:%d w2:%u", m->wire_count,
6812 pt2_wirecount_get(m, pte1_index(va)));
6814 printf(" !!! pt2tab entry is ZERO");
6815 else if (pte2_pa(pte1) != pte2_pa(pte2))
6816 printf(" !!! pt2tab entry is DIFFERENT - m: %p",
6817 PHYS_TO_VM_PAGE(pte2_pa(pte2)));
6820 dump_link(pmap, i, invalid_ok);
6822 printf("0x%08X: Invalid entry 0x%08X\n", va, pte1);
6827 dump_pt2tab(pmap_t pmap)
6835 printf("PT2TAB:\n");
6836 for (i = 0; i < PT2TAB_ENTRIES; i++) {
6837 pte2 = pte2_load(&pmap->pm_pt2tab[i]);
6838 if (!pte2_is_valid(pte2))
6840 va = i << PT2TAB_SHIFT;
6842 m = PHYS_TO_VM_PAGE(pa);
6843 printf(" 0x%08X: 0x%08X, TEX%d, s:%d, m:%p", va, pte2,
6844 pte2_class(pte2), !!(pte2 & PTE2_S), m);
6846 printf(" , h: %d, w: %d, f: 0x%04X pidx: %lld",
6847 m->hold_count, m->wire_count, m->flags, m->pindex);
6852 DB_SHOW_COMMAND(pmap_pt2tab, pmap_pt2tab_print)
6854 /* XXX convert args. */
6855 pmap_t pmap = (pmap_t)addr;
6862 printf("supported only on current pmap\n");
6866 pmap = PCPU_GET(curpmap);
6867 printf("curpmap: 0x%08X\n", (uint32_t)pmap);
6868 printf("PT2MAP: 0x%08X\n", (uint32_t)PT2MAP);
6869 printf("pt2tab: 0x%08X\n", (uint32_t)pmap->pm_pt2tab);
6871 start = pte1_index((vm_offset_t)PT2MAP);
6872 for (i = start; i < (start + NPT2_IN_PT2TAB); i++) {
6873 pte1 = pte1_load(&pmap->pm_pt1[i]);
6876 va = i << PTE1_SHIFT;
6877 if (pte1_is_section(pte1)) {
6878 printf("0x%08X: Section 0x%08X, s:%d\n", va, pte1,
6880 dump_section(pmap, i);
6881 } else if (pte1_is_link(pte1)) {
6882 pte2 = pte2_load(pmap_pt2tab_entry(pmap, va));
6883 printf("0x%08X: Link 0x%08X, pt2tab: 0x%08X\n", va,
6886 printf(" !!! pt2tab entry is ZERO\n");
6888 printf("0x%08X: Invalid entry 0x%08X\n", va, pte1);