1 /* From: $NetBSD: pmap.c,v 1.148 2004/04/03 04:35:48 bsh Exp $ */
3 * Copyright 2004 Olivier Houchard.
4 * Copyright 2003 Wasabi Systems, Inc.
7 * Written by Steve C. Woodford for Wasabi Systems, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed for the NetBSD Project by
20 * Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 * or promote products derived from this software without specific prior
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
39 * Copyright (c) 2002-2003 Wasabi Systems, Inc.
40 * Copyright (c) 2001 Richard Earnshaw
41 * Copyright (c) 2001-2002 Christopher Gilbert
42 * All rights reserved.
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 * 3. The name of the company nor the name of the author may be used to
50 * endorse or promote products derived from this software without specific
51 * prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
54 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
55 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
56 * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
57 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
58 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
59 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * Copyright (c) 1999 The NetBSD Foundation, Inc.
67 * All rights reserved.
69 * This code is derived from software contributed to The NetBSD Foundation
70 * by Charles M. Hannum.
72 * Redistribution and use in source and binary forms, with or without
73 * modification, are permitted provided that the following conditions
75 * 1. Redistributions of source code must retain the above copyright
76 * notice, this list of conditions and the following disclaimer.
77 * 2. Redistributions in binary form must reproduce the above copyright
78 * notice, this list of conditions and the following disclaimer in the
79 * documentation and/or other materials provided with the distribution.
81 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
82 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
83 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
84 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
85 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
86 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
87 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
88 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
89 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
90 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
91 * POSSIBILITY OF SUCH DAMAGE.
95 * Copyright (c) 1994-1998 Mark Brinicombe.
96 * Copyright (c) 1994 Brini.
97 * All rights reserved.
99 * This code is derived from software written for Brini by Mark Brinicombe
101 * Redistribution and use in source and binary forms, with or without
102 * modification, are permitted provided that the following conditions
104 * 1. Redistributions of source code must retain the above copyright
105 * notice, this list of conditions and the following disclaimer.
106 * 2. Redistributions in binary form must reproduce the above copyright
107 * notice, this list of conditions and the following disclaimer in the
108 * documentation and/or other materials provided with the distribution.
109 * 3. All advertising materials mentioning features or use of this software
110 * must display the following acknowledgement:
111 * This product includes software developed by Mark Brinicombe.
112 * 4. The name of the author may not be used to endorse or promote products
113 * derived from this software without specific prior written permission.
115 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
116 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
117 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
118 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
119 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
120 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
121 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
122 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
123 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
125 * RiscBSD kernel project
129 * Machine dependent vm stuff
135 * Special compilation symbols
136 * PMAP_DEBUG - Build in pmap_debug_level code
138 * Note that pmap_mapdev() and pmap_unmapdev() are implemented in arm/devmap.c
140 /* Include header files */
144 #include <sys/cdefs.h>
145 __FBSDID("$FreeBSD$");
146 #include <sys/param.h>
147 #include <sys/systm.h>
148 #include <sys/kernel.h>
150 #include <sys/lock.h>
151 #include <sys/proc.h>
152 #include <sys/malloc.h>
153 #include <sys/msgbuf.h>
154 #include <sys/mutex.h>
155 #include <sys/vmmeter.h>
156 #include <sys/mman.h>
157 #include <sys/rwlock.h>
159 #include <sys/sched.h>
162 #include <vm/vm_param.h>
165 #include <vm/vm_kern.h>
166 #include <vm/vm_object.h>
167 #include <vm/vm_map.h>
168 #include <vm/vm_page.h>
169 #include <vm/vm_pageout.h>
170 #include <vm/vm_phys.h>
171 #include <vm/vm_extern.h>
173 #include <machine/md_var.h>
174 #include <machine/cpu.h>
175 #include <machine/cpufunc.h>
176 #include <machine/pcb.h>
179 #define PDEBUG(_lev_,_stat_) \
180 if (pmap_debug_level >= (_lev_)) \
182 #define dprintf printf
184 int pmap_debug_level = 0;
186 #else /* PMAP_DEBUG */
187 #define PDEBUG(_lev_,_stat_) /* Nothing */
188 #define dprintf(x, arg...)
189 #define PMAP_INLINE __inline
190 #endif /* PMAP_DEBUG */
192 extern struct pv_addr systempage;
194 extern int last_fault_code;
197 * Internal function prototypes
199 static void pmap_free_pv_entry (pv_entry_t);
200 static pv_entry_t pmap_get_pv_entry(void);
202 static int pmap_enter_locked(pmap_t, vm_offset_t, vm_page_t,
204 static vm_paddr_t pmap_extract_locked(pmap_t pmap, vm_offset_t va);
205 static void pmap_fix_cache(struct vm_page *, pmap_t, vm_offset_t);
206 static void pmap_alloc_l1(pmap_t);
207 static void pmap_free_l1(pmap_t);
209 static int pmap_clearbit(struct vm_page *, u_int);
211 static struct l2_bucket *pmap_get_l2_bucket(pmap_t, vm_offset_t);
212 static struct l2_bucket *pmap_alloc_l2_bucket(pmap_t, vm_offset_t);
213 static void pmap_free_l2_bucket(pmap_t, struct l2_bucket *, u_int);
214 static vm_offset_t kernel_pt_lookup(vm_paddr_t);
216 static MALLOC_DEFINE(M_VMPMAP, "pmap", "PMAP L1");
218 vm_offset_t virtual_avail; /* VA of first avail page (after kernel bss) */
219 vm_offset_t virtual_end; /* VA of last avail page (end of kernel AS) */
220 vm_offset_t pmap_curmaxkvaddr;
221 vm_paddr_t kernel_l1pa;
223 vm_offset_t kernel_vm_end = 0;
225 vm_offset_t vm_max_kernel_address;
227 struct pmap kernel_pmap_store;
229 static pt_entry_t *csrc_pte, *cdst_pte;
230 static vm_offset_t csrcp, cdstp, qmap_addr;
231 static struct mtx cmtx, qmap_mtx;
233 static void pmap_init_l1(struct l1_ttable *, pd_entry_t *);
235 * These routines are called when the CPU type is identified to set up
236 * the PTE prototypes, cache modes, etc.
238 * The variables are always here, just in case LKMs need to reference
239 * them (though, they shouldn't).
242 pt_entry_t pte_l1_s_cache_mode;
243 pt_entry_t pte_l1_s_cache_mode_pt;
244 pt_entry_t pte_l1_s_cache_mask;
246 pt_entry_t pte_l2_l_cache_mode;
247 pt_entry_t pte_l2_l_cache_mode_pt;
248 pt_entry_t pte_l2_l_cache_mask;
250 pt_entry_t pte_l2_s_cache_mode;
251 pt_entry_t pte_l2_s_cache_mode_pt;
252 pt_entry_t pte_l2_s_cache_mask;
254 pt_entry_t pte_l2_s_prot_u;
255 pt_entry_t pte_l2_s_prot_w;
256 pt_entry_t pte_l2_s_prot_mask;
258 pt_entry_t pte_l1_s_proto;
259 pt_entry_t pte_l1_c_proto;
260 pt_entry_t pte_l2_s_proto;
262 void (*pmap_copy_page_func)(vm_paddr_t, vm_paddr_t);
263 void (*pmap_copy_page_offs_func)(vm_paddr_t a_phys,
264 vm_offset_t a_offs, vm_paddr_t b_phys, vm_offset_t b_offs,
266 void (*pmap_zero_page_func)(vm_paddr_t, int, int);
271 static caddr_t crashdumpmap;
273 extern void bcopy_page(vm_offset_t, vm_offset_t);
274 extern void bzero_page(vm_offset_t);
276 extern vm_offset_t alloc_firstaddr;
281 * Metadata for L1 translation tables.
284 /* Entry on the L1 Table list */
285 SLIST_ENTRY(l1_ttable) l1_link;
287 /* Entry on the L1 Least Recently Used list */
288 TAILQ_ENTRY(l1_ttable) l1_lru;
290 /* Track how many domains are allocated from this L1 */
291 volatile u_int l1_domain_use_count;
294 * A free-list of domain numbers for this L1.
295 * We avoid using ffs() and a bitmap to track domains since ffs()
298 u_int8_t l1_domain_first;
299 u_int8_t l1_domain_free[PMAP_DOMAINS];
301 /* Physical address of this L1 page table */
302 vm_paddr_t l1_physaddr;
304 /* KVA of this L1 page table */
309 * Convert a virtual address into its L1 table index. That is, the
310 * index used to locate the L2 descriptor table pointer in an L1 table.
311 * This is basically used to index l1->l1_kva[].
313 * Each L2 descriptor table represents 1MB of VA space.
315 #define L1_IDX(va) (((vm_offset_t)(va)) >> L1_S_SHIFT)
318 * L1 Page Tables are tracked using a Least Recently Used list.
319 * - New L1s are allocated from the HEAD.
320 * - Freed L1s are added to the TAIl.
321 * - Recently accessed L1s (where an 'access' is some change to one of
322 * the userland pmaps which owns this L1) are moved to the TAIL.
324 static TAILQ_HEAD(, l1_ttable) l1_lru_list;
326 * A list of all L1 tables
328 static SLIST_HEAD(, l1_ttable) l1_list;
329 static struct mtx l1_lru_lock;
332 * The l2_dtable tracks L2_BUCKET_SIZE worth of L1 slots.
334 * This is normally 16MB worth L2 page descriptors for any given pmap.
335 * Reference counts are maintained for L2 descriptors so they can be
339 /* The number of L2 page descriptors allocated to this l2_dtable */
342 /* List of L2 page descriptors */
344 pt_entry_t *l2b_kva; /* KVA of L2 Descriptor Table */
345 vm_paddr_t l2b_phys; /* Physical address of same */
346 u_short l2b_l1idx; /* This L2 table's L1 index */
347 u_short l2b_occupancy; /* How many active descriptors */
348 } l2_bucket[L2_BUCKET_SIZE];
351 /* pmap_kenter_internal flags */
352 #define KENTER_CACHE 0x1
353 #define KENTER_USER 0x2
356 * Given an L1 table index, calculate the corresponding l2_dtable index
357 * and bucket index within the l2_dtable.
359 #define L2_IDX(l1idx) (((l1idx) >> L2_BUCKET_LOG2) & \
361 #define L2_BUCKET(l1idx) ((l1idx) & (L2_BUCKET_SIZE - 1))
364 * Given a virtual address, this macro returns the
365 * virtual address required to drop into the next L2 bucket.
367 #define L2_NEXT_BUCKET(va) (((va) & L1_S_FRAME) + L1_S_SIZE)
370 * We try to map the page tables write-through, if possible. However, not
371 * all CPUs have a write-through cache mode, so on those we have to sync
372 * the cache when we frob page tables.
374 * We try to evaluate this at compile time, if possible. However, it's
375 * not always possible to do that, hence this run-time var.
377 int pmap_needs_pte_sync;
380 * Macro to determine if a mapping might be resident in the
381 * instruction cache and/or TLB
383 #define PV_BEEN_EXECD(f) (((f) & (PVF_REF | PVF_EXEC)) == (PVF_REF | PVF_EXEC))
386 * Macro to determine if a mapping might be resident in the
387 * data cache and/or TLB
389 #define PV_BEEN_REFD(f) (((f) & PVF_REF) != 0)
391 #ifndef PMAP_SHPGPERPROC
392 #define PMAP_SHPGPERPROC 200
395 #define pmap_is_current(pm) ((pm) == kernel_pmap || \
396 curproc->p_vmspace->vm_map.pmap == (pm))
397 static uma_zone_t pvzone = NULL;
399 static uma_zone_t l2table_zone;
400 static vm_offset_t pmap_kernel_l2dtable_kva;
401 static vm_offset_t pmap_kernel_l2ptp_kva;
402 static vm_paddr_t pmap_kernel_l2ptp_phys;
403 static int pv_entry_count=0, pv_entry_max=0, pv_entry_high_water=0;
404 static struct rwlock pvh_global_lock;
406 void pmap_copy_page_offs_generic(vm_paddr_t a_phys, vm_offset_t a_offs,
407 vm_paddr_t b_phys, vm_offset_t b_offs, int cnt);
408 #if ARM_MMU_XSCALE == 1
409 void pmap_copy_page_offs_xscale(vm_paddr_t a_phys, vm_offset_t a_offs,
410 vm_paddr_t b_phys, vm_offset_t b_offs, int cnt);
414 * This list exists for the benefit of pmap_map_chunk(). It keeps track
415 * of the kernel L2 tables during bootstrap, so that pmap_map_chunk() can
416 * find them as necessary.
418 * Note that the data on this list MUST remain valid after initarm() returns,
419 * as pmap_bootstrap() uses it to contruct L2 table metadata.
421 SLIST_HEAD(, pv_addr) kernel_pt_list = SLIST_HEAD_INITIALIZER(kernel_pt_list);
424 pmap_init_l1(struct l1_ttable *l1, pd_entry_t *l1pt)
429 l1->l1_domain_use_count = 0;
430 l1->l1_domain_first = 0;
432 for (i = 0; i < PMAP_DOMAINS; i++)
433 l1->l1_domain_free[i] = i + 1;
436 * Copy the kernel's L1 entries to each new L1.
438 if (l1pt != kernel_pmap->pm_l1->l1_kva)
439 memcpy(l1pt, kernel_pmap->pm_l1->l1_kva, L1_TABLE_SIZE);
441 if ((l1->l1_physaddr = pmap_extract(kernel_pmap, (vm_offset_t)l1pt)) == 0)
442 panic("pmap_init_l1: can't get PA of L1 at %p", l1pt);
443 SLIST_INSERT_HEAD(&l1_list, l1, l1_link);
444 TAILQ_INSERT_TAIL(&l1_lru_list, l1, l1_lru);
448 kernel_pt_lookup(vm_paddr_t pa)
452 SLIST_FOREACH(pv, &kernel_pt_list, pv_list) {
459 #if ARM_MMU_GENERIC != 0
461 pmap_pte_init_generic(void)
464 pte_l1_s_cache_mode = L1_S_B|L1_S_C;
465 pte_l1_s_cache_mask = L1_S_CACHE_MASK_generic;
467 pte_l2_l_cache_mode = L2_B|L2_C;
468 pte_l2_l_cache_mask = L2_L_CACHE_MASK_generic;
470 pte_l2_s_cache_mode = L2_B|L2_C;
471 pte_l2_s_cache_mask = L2_S_CACHE_MASK_generic;
474 * If we have a write-through cache, set B and C. If
475 * we have a write-back cache, then we assume setting
476 * only C will make those pages write-through.
478 if (cpufuncs.cf_dcache_wb_range == (void *) cpufunc_nullop) {
479 pte_l1_s_cache_mode_pt = L1_S_B|L1_S_C;
480 pte_l2_l_cache_mode_pt = L2_B|L2_C;
481 pte_l2_s_cache_mode_pt = L2_B|L2_C;
483 pte_l1_s_cache_mode_pt = L1_S_C;
484 pte_l2_l_cache_mode_pt = L2_C;
485 pte_l2_s_cache_mode_pt = L2_C;
488 pte_l2_s_prot_u = L2_S_PROT_U_generic;
489 pte_l2_s_prot_w = L2_S_PROT_W_generic;
490 pte_l2_s_prot_mask = L2_S_PROT_MASK_generic;
492 pte_l1_s_proto = L1_S_PROTO_generic;
493 pte_l1_c_proto = L1_C_PROTO_generic;
494 pte_l2_s_proto = L2_S_PROTO_generic;
496 pmap_copy_page_func = pmap_copy_page_generic;
497 pmap_copy_page_offs_func = pmap_copy_page_offs_generic;
498 pmap_zero_page_func = pmap_zero_page_generic;
501 #endif /* ARM_MMU_GENERIC != 0 */
503 #if ARM_MMU_XSCALE == 1
504 #if (ARM_NMMUS > 1) || defined (CPU_XSCALE_CORE3)
505 static u_int xscale_use_minidata;
509 pmap_pte_init_xscale(void)
512 int write_through = 0;
514 pte_l1_s_cache_mode = L1_S_B|L1_S_C|L1_S_XSCALE_P;
515 pte_l1_s_cache_mask = L1_S_CACHE_MASK_xscale;
517 pte_l2_l_cache_mode = L2_B|L2_C;
518 pte_l2_l_cache_mask = L2_L_CACHE_MASK_xscale;
520 pte_l2_s_cache_mode = L2_B|L2_C;
521 pte_l2_s_cache_mask = L2_S_CACHE_MASK_xscale;
523 pte_l1_s_cache_mode_pt = L1_S_C;
524 pte_l2_l_cache_mode_pt = L2_C;
525 pte_l2_s_cache_mode_pt = L2_C;
526 #ifdef XSCALE_CACHE_READ_WRITE_ALLOCATE
528 * The XScale core has an enhanced mode where writes that
529 * miss the cache cause a cache line to be allocated. This
530 * is significantly faster than the traditional, write-through
531 * behavior of this case.
533 pte_l1_s_cache_mode |= L1_S_XSCALE_TEX(TEX_XSCALE_X);
534 pte_l2_l_cache_mode |= L2_XSCALE_L_TEX(TEX_XSCALE_X);
535 pte_l2_s_cache_mode |= L2_XSCALE_T_TEX(TEX_XSCALE_X);
536 #endif /* XSCALE_CACHE_READ_WRITE_ALLOCATE */
537 #ifdef XSCALE_CACHE_WRITE_THROUGH
539 * Some versions of the XScale core have various bugs in
540 * their cache units, the work-around for which is to run
541 * the cache in write-through mode. Unfortunately, this
542 * has a major (negative) impact on performance. So, we
543 * go ahead and run fast-and-loose, in the hopes that we
544 * don't line up the planets in a way that will trip the
547 * However, we give you the option to be slow-but-correct.
550 #elif defined(XSCALE_CACHE_WRITE_BACK)
551 /* force write back cache mode */
553 #elif defined(CPU_XSCALE_PXA2X0)
555 * Intel PXA2[15]0 processors are known to have a bug in
556 * write-back cache on revision 4 and earlier (stepping
557 * A[01] and B[012]). Fixed for C0 and later.
563 type = id & ~(CPU_ID_XSCALE_COREREV_MASK|CPU_ID_REVISION_MASK);
565 if (type == CPU_ID_PXA250 || type == CPU_ID_PXA210) {
566 if ((id & CPU_ID_REVISION_MASK) < 5) {
567 /* write through for stepping A0-1 and B0-2 */
572 #endif /* XSCALE_CACHE_WRITE_THROUGH */
575 pte_l1_s_cache_mode = L1_S_C;
576 pte_l2_l_cache_mode = L2_C;
577 pte_l2_s_cache_mode = L2_C;
581 xscale_use_minidata = 1;
584 pte_l2_s_prot_u = L2_S_PROT_U_xscale;
585 pte_l2_s_prot_w = L2_S_PROT_W_xscale;
586 pte_l2_s_prot_mask = L2_S_PROT_MASK_xscale;
588 pte_l1_s_proto = L1_S_PROTO_xscale;
589 pte_l1_c_proto = L1_C_PROTO_xscale;
590 pte_l2_s_proto = L2_S_PROTO_xscale;
592 #ifdef CPU_XSCALE_CORE3
593 pmap_copy_page_func = pmap_copy_page_generic;
594 pmap_copy_page_offs_func = pmap_copy_page_offs_generic;
595 pmap_zero_page_func = pmap_zero_page_generic;
596 xscale_use_minidata = 0;
597 /* Make sure it is L2-cachable */
598 pte_l1_s_cache_mode |= L1_S_XSCALE_TEX(TEX_XSCALE_T);
599 pte_l1_s_cache_mode_pt = pte_l1_s_cache_mode &~ L1_S_XSCALE_P;
600 pte_l2_l_cache_mode |= L2_XSCALE_L_TEX(TEX_XSCALE_T) ;
601 pte_l2_l_cache_mode_pt = pte_l1_s_cache_mode;
602 pte_l2_s_cache_mode |= L2_XSCALE_T_TEX(TEX_XSCALE_T);
603 pte_l2_s_cache_mode_pt = pte_l2_s_cache_mode;
606 pmap_copy_page_func = pmap_copy_page_xscale;
607 pmap_copy_page_offs_func = pmap_copy_page_offs_xscale;
608 pmap_zero_page_func = pmap_zero_page_xscale;
612 * Disable ECC protection of page table access, for now.
614 __asm __volatile("mrc p15, 0, %0, c1, c0, 1" : "=r" (auxctl));
615 auxctl &= ~XSCALE_AUXCTL_P;
616 __asm __volatile("mcr p15, 0, %0, c1, c0, 1" : : "r" (auxctl));
620 * xscale_setup_minidata:
622 * Set up the mini-data cache clean area. We require the
623 * caller to allocate the right amount of physically and
624 * virtually contiguous space.
626 extern vm_offset_t xscale_minidata_clean_addr;
627 extern vm_size_t xscale_minidata_clean_size; /* already initialized */
629 xscale_setup_minidata(vm_offset_t l1pt, vm_offset_t va, vm_paddr_t pa)
631 pd_entry_t *pde = (pd_entry_t *) l1pt;
636 xscale_minidata_clean_addr = va;
638 /* Round it to page size. */
639 size = (xscale_minidata_clean_size + L2_S_OFFSET) & L2_S_FRAME;
642 va += L2_S_SIZE, pa += L2_S_SIZE, size -= L2_S_SIZE) {
643 pte = (pt_entry_t *) kernel_pt_lookup(
644 pde[L1_IDX(va)] & L1_C_ADDR_MASK);
646 panic("xscale_setup_minidata: can't find L2 table for "
647 "VA 0x%08x", (u_int32_t) va);
648 pte[l2pte_index(va)] =
649 L2_S_PROTO | pa | L2_S_PROT(PTE_KERNEL, VM_PROT_READ) |
650 L2_C | L2_XSCALE_T_TEX(TEX_XSCALE_X);
654 * Configure the mini-data cache for write-back with
655 * read/write-allocate.
657 * NOTE: In order to reconfigure the mini-data cache, we must
658 * make sure it contains no valid data! In order to do that,
659 * we must issue a global data cache invalidate command!
661 * WE ASSUME WE ARE RUNNING UN-CACHED WHEN THIS ROUTINE IS CALLED!
662 * THIS IS VERY IMPORTANT!
665 /* Invalidate data and mini-data. */
666 __asm __volatile("mcr p15, 0, %0, c7, c6, 0" : : "r" (0));
667 __asm __volatile("mrc p15, 0, %0, c1, c0, 1" : "=r" (auxctl));
668 auxctl = (auxctl & ~XSCALE_AUXCTL_MD_MASK) | XSCALE_AUXCTL_MD_WB_RWA;
669 __asm __volatile("mcr p15, 0, %0, c1, c0, 1" : : "r" (auxctl));
674 * Allocate an L1 translation table for the specified pmap.
675 * This is called at pmap creation time.
678 pmap_alloc_l1(pmap_t pm)
680 struct l1_ttable *l1;
684 * Remove the L1 at the head of the LRU list
686 mtx_lock(&l1_lru_lock);
687 l1 = TAILQ_FIRST(&l1_lru_list);
688 TAILQ_REMOVE(&l1_lru_list, l1, l1_lru);
691 * Pick the first available domain number, and update
692 * the link to the next number.
694 domain = l1->l1_domain_first;
695 l1->l1_domain_first = l1->l1_domain_free[domain];
698 * If there are still free domain numbers in this L1,
699 * put it back on the TAIL of the LRU list.
701 if (++l1->l1_domain_use_count < PMAP_DOMAINS)
702 TAILQ_INSERT_TAIL(&l1_lru_list, l1, l1_lru);
704 mtx_unlock(&l1_lru_lock);
707 * Fix up the relevant bits in the pmap structure
710 pm->pm_domain = domain + 1;
714 * Free an L1 translation table.
715 * This is called at pmap destruction time.
718 pmap_free_l1(pmap_t pm)
720 struct l1_ttable *l1 = pm->pm_l1;
722 mtx_lock(&l1_lru_lock);
725 * If this L1 is currently on the LRU list, remove it.
727 if (l1->l1_domain_use_count < PMAP_DOMAINS)
728 TAILQ_REMOVE(&l1_lru_list, l1, l1_lru);
731 * Free up the domain number which was allocated to the pmap
733 l1->l1_domain_free[pm->pm_domain - 1] = l1->l1_domain_first;
734 l1->l1_domain_first = pm->pm_domain - 1;
735 l1->l1_domain_use_count--;
738 * The L1 now must have at least 1 free domain, so add
739 * it back to the LRU list. If the use count is zero,
740 * put it at the head of the list, otherwise it goes
743 if (l1->l1_domain_use_count == 0) {
744 TAILQ_INSERT_HEAD(&l1_lru_list, l1, l1_lru);
746 TAILQ_INSERT_TAIL(&l1_lru_list, l1, l1_lru);
748 mtx_unlock(&l1_lru_lock);
752 * Returns a pointer to the L2 bucket associated with the specified pmap
753 * and VA, or NULL if no L2 bucket exists for the address.
755 static PMAP_INLINE struct l2_bucket *
756 pmap_get_l2_bucket(pmap_t pm, vm_offset_t va)
758 struct l2_dtable *l2;
759 struct l2_bucket *l2b;
764 if ((l2 = pm->pm_l2[L2_IDX(l1idx)]) == NULL ||
765 (l2b = &l2->l2_bucket[L2_BUCKET(l1idx)])->l2b_kva == NULL)
772 * Returns a pointer to the L2 bucket associated with the specified pmap
775 * If no L2 bucket exists, perform the necessary allocations to put an L2
776 * bucket/page table in place.
778 * Note that if a new L2 bucket/page was allocated, the caller *must*
779 * increment the bucket occupancy counter appropriately *before*
780 * releasing the pmap's lock to ensure no other thread or cpu deallocates
781 * the bucket/page in the meantime.
783 static struct l2_bucket *
784 pmap_alloc_l2_bucket(pmap_t pm, vm_offset_t va)
786 struct l2_dtable *l2;
787 struct l2_bucket *l2b;
792 PMAP_ASSERT_LOCKED(pm);
793 rw_assert(&pvh_global_lock, RA_WLOCKED);
794 if ((l2 = pm->pm_l2[L2_IDX(l1idx)]) == NULL) {
796 * No mapping at this address, as there is
797 * no entry in the L1 table.
798 * Need to allocate a new l2_dtable.
801 rw_wunlock(&pvh_global_lock);
802 if ((l2 = uma_zalloc(l2table_zone, M_NOWAIT)) == NULL) {
803 rw_wlock(&pvh_global_lock);
807 rw_wlock(&pvh_global_lock);
809 if (pm->pm_l2[L2_IDX(l1idx)] != NULL) {
811 * Someone already allocated the l2_dtable while
812 * we were doing the same.
814 uma_zfree(l2table_zone, l2);
815 l2 = pm->pm_l2[L2_IDX(l1idx)];
817 bzero(l2, sizeof(*l2));
819 * Link it into the parent pmap
821 pm->pm_l2[L2_IDX(l1idx)] = l2;
825 l2b = &l2->l2_bucket[L2_BUCKET(l1idx)];
828 * Fetch pointer to the L2 page table associated with the address.
830 if (l2b->l2b_kva == NULL) {
834 * No L2 page table has been allocated. Chances are, this
835 * is because we just allocated the l2_dtable, above.
839 rw_wunlock(&pvh_global_lock);
840 ptep = uma_zalloc(l2zone, M_NOWAIT);
841 rw_wlock(&pvh_global_lock);
843 if (l2b->l2b_kva != NULL) {
844 /* We lost the race. */
846 uma_zfree(l2zone, ptep);
849 l2b->l2b_phys = vtophys(ptep);
852 * Oops, no more L2 page tables available at this
853 * time. We may need to deallocate the l2_dtable
854 * if we allocated a new one above.
857 if (l2->l2_occupancy == 0) {
858 pm->pm_l2[L2_IDX(l1idx)] = NULL;
859 uma_zfree(l2table_zone, l2);
865 l2b->l2b_l1idx = l1idx;
871 static PMAP_INLINE void
872 #ifndef PMAP_INCLUDE_PTE_SYNC
873 pmap_free_l2_ptp(pt_entry_t *l2)
875 pmap_free_l2_ptp(boolean_t need_sync, pt_entry_t *l2)
878 #ifdef PMAP_INCLUDE_PTE_SYNC
880 * Note: With a write-back cache, we may need to sync this
881 * L2 table before re-using it.
882 * This is because it may have belonged to a non-current
883 * pmap, in which case the cache syncs would have been
884 * skipped when the pages were being unmapped. If the
885 * L2 table were then to be immediately re-allocated to
886 * the *current* pmap, it may well contain stale mappings
887 * which have not yet been cleared by a cache write-back
888 * and so would still be visible to the mmu.
891 PTE_SYNC_RANGE(l2, L2_TABLE_SIZE_REAL / sizeof(pt_entry_t));
893 uma_zfree(l2zone, l2);
896 * One or more mappings in the specified L2 descriptor table have just been
899 * Garbage collect the metadata and descriptor table itself if necessary.
901 * The pmap lock must be acquired when this is called (not necessary
902 * for the kernel pmap).
905 pmap_free_l2_bucket(pmap_t pm, struct l2_bucket *l2b, u_int count)
907 struct l2_dtable *l2;
908 pd_entry_t *pl1pd, l1pd;
914 * Update the bucket's reference count according to how many
915 * PTEs the caller has just invalidated.
917 l2b->l2b_occupancy -= count;
922 * Level 2 page tables allocated to the kernel pmap are never freed
923 * as that would require checking all Level 1 page tables and
924 * removing any references to the Level 2 page table. See also the
925 * comment elsewhere about never freeing bootstrap L2 descriptors.
927 * We make do with just invalidating the mapping in the L2 table.
929 * This isn't really a big deal in practice and, in fact, leads
930 * to a performance win over time as we don't need to continually
933 if (l2b->l2b_occupancy > 0 || pm == kernel_pmap)
937 * There are no more valid mappings in this level 2 page table.
938 * Go ahead and NULL-out the pointer in the bucket, then
939 * free the page table.
941 l1idx = l2b->l2b_l1idx;
945 pl1pd = &pm->pm_l1->l1_kva[l1idx];
948 * If the L1 slot matches the pmap's domain
949 * number, then invalidate it.
951 l1pd = *pl1pd & (L1_TYPE_MASK | L1_C_DOM_MASK);
952 if (l1pd == (L1_C_DOM(pm->pm_domain) | L1_TYPE_C)) {
958 * Release the L2 descriptor table back to the pool cache.
960 #ifndef PMAP_INCLUDE_PTE_SYNC
961 pmap_free_l2_ptp(ptep);
963 pmap_free_l2_ptp(!pmap_is_current(pm), ptep);
967 * Update the reference count in the associated l2_dtable
969 l2 = pm->pm_l2[L2_IDX(l1idx)];
970 if (--l2->l2_occupancy > 0)
974 * There are no more valid mappings in any of the Level 1
975 * slots managed by this l2_dtable. Go ahead and NULL-out
976 * the pointer in the parent pmap and free the l2_dtable.
978 pm->pm_l2[L2_IDX(l1idx)] = NULL;
979 uma_zfree(l2table_zone, l2);
983 * Pool cache constructors for L2 descriptor tables, metadata and pmap
987 pmap_l2ptp_ctor(void *mem, int size, void *arg, int flags)
989 #ifndef PMAP_INCLUDE_PTE_SYNC
990 struct l2_bucket *l2b;
991 pt_entry_t *ptep, pte;
993 vm_offset_t va = (vm_offset_t)mem & ~PAGE_MASK;
996 * The mappings for these page tables were initially made using
997 * pmap_kenter() by the pool subsystem. Therefore, the cache-
998 * mode will not be right for page table mappings. To avoid
999 * polluting the pmap_kenter() code with a special case for
1000 * page tables, we simply fix up the cache-mode here if it's not
1003 l2b = pmap_get_l2_bucket(kernel_pmap, va);
1004 ptep = &l2b->l2b_kva[l2pte_index(va)];
1007 if ((pte & L2_S_CACHE_MASK) != pte_l2_s_cache_mode_pt) {
1009 * Page tables must have the cache-mode set to
1012 *ptep = (pte & ~L2_S_CACHE_MASK) | pte_l2_s_cache_mode_pt;
1014 cpu_tlb_flushD_SE(va);
1018 memset(mem, 0, L2_TABLE_SIZE_REAL);
1019 PTE_SYNC_RANGE(mem, L2_TABLE_SIZE_REAL / sizeof(pt_entry_t));
1024 * A bunch of routines to conditionally flush the caches/TLB depending
1025 * on whether the specified pmap actually needs to be flushed at any
1028 static PMAP_INLINE void
1029 pmap_tlb_flushID_SE(pmap_t pm, vm_offset_t va)
1032 if (pmap_is_current(pm))
1033 cpu_tlb_flushID_SE(va);
1036 static PMAP_INLINE void
1037 pmap_tlb_flushD_SE(pmap_t pm, vm_offset_t va)
1040 if (pmap_is_current(pm))
1041 cpu_tlb_flushD_SE(va);
1044 static PMAP_INLINE void
1045 pmap_tlb_flushID(pmap_t pm)
1048 if (pmap_is_current(pm))
1051 static PMAP_INLINE void
1052 pmap_tlb_flushD(pmap_t pm)
1055 if (pmap_is_current(pm))
1060 pmap_has_valid_mapping(pmap_t pm, vm_offset_t va)
1065 if (pmap_get_pde_pte(pm, va, &pde, &ptep) &&
1066 ptep && ((*ptep & L2_TYPE_MASK) != L2_TYPE_INV))
1072 static PMAP_INLINE void
1073 pmap_idcache_wbinv_range(pmap_t pm, vm_offset_t va, vm_size_t len)
1077 CTR4(KTR_PMAP, "pmap_dcache_wbinv_range: pmap %p is_kernel %d va 0x%08x"
1078 " len 0x%x ", pm, pm == kernel_pmap, va, len);
1080 if (pmap_is_current(pm) || pm == kernel_pmap) {
1081 rest = MIN(PAGE_SIZE - (va & PAGE_MASK), len);
1083 if (pmap_has_valid_mapping(pm, va)) {
1084 cpu_idcache_wbinv_range(va, rest);
1085 cpu_l2cache_wbinv_range(va, rest);
1089 rest = MIN(PAGE_SIZE, len);
1094 static PMAP_INLINE void
1095 pmap_dcache_wb_range(pmap_t pm, vm_offset_t va, vm_size_t len, boolean_t do_inv,
1100 CTR4(KTR_PMAP, "pmap_dcache_wb_range: pmap %p is_kernel %d va 0x%08x "
1101 "len 0x%x ", pm, pm == kernel_pmap, va, len);
1102 CTR2(KTR_PMAP, " do_inv %d rd_only %d", do_inv, rd_only);
1104 if (pmap_is_current(pm)) {
1105 rest = MIN(PAGE_SIZE - (va & PAGE_MASK), len);
1107 if (pmap_has_valid_mapping(pm, va)) {
1108 if (do_inv && rd_only) {
1109 cpu_dcache_inv_range(va, rest);
1110 cpu_l2cache_inv_range(va, rest);
1111 } else if (do_inv) {
1112 cpu_dcache_wbinv_range(va, rest);
1113 cpu_l2cache_wbinv_range(va, rest);
1114 } else if (!rd_only) {
1115 cpu_dcache_wb_range(va, rest);
1116 cpu_l2cache_wb_range(va, rest);
1122 rest = MIN(PAGE_SIZE, len);
1127 static PMAP_INLINE void
1128 pmap_idcache_wbinv_all(pmap_t pm)
1131 if (pmap_is_current(pm)) {
1132 cpu_idcache_wbinv_all();
1133 cpu_l2cache_wbinv_all();
1138 static PMAP_INLINE void
1139 pmap_dcache_wbinv_all(pmap_t pm)
1142 if (pmap_is_current(pm)) {
1143 cpu_dcache_wbinv_all();
1144 cpu_l2cache_wbinv_all();
1152 * Make sure the pte is written out to RAM.
1153 * We need to do this for one of two cases:
1154 * - We're dealing with the kernel pmap
1155 * - There is no pmap active in the cache/tlb.
1156 * - The specified pmap is 'active' in the cache/tlb.
1158 #ifdef PMAP_INCLUDE_PTE_SYNC
1159 #define PTE_SYNC_CURRENT(pm, ptep) \
1161 if (PMAP_NEEDS_PTE_SYNC && \
1162 pmap_is_current(pm)) \
1164 } while (/*CONSTCOND*/0)
1166 #define PTE_SYNC_CURRENT(pm, ptep) /* nothing */
1170 * cacheable == -1 means we must make the entry uncacheable, 1 means
1173 static __inline void
1174 pmap_set_cache_entry(pv_entry_t pv, pmap_t pm, vm_offset_t va, int cacheable)
1176 struct l2_bucket *l2b;
1177 pt_entry_t *ptep, pte;
1179 l2b = pmap_get_l2_bucket(pv->pv_pmap, pv->pv_va);
1180 ptep = &l2b->l2b_kva[l2pte_index(pv->pv_va)];
1182 if (cacheable == 1) {
1183 pte = (*ptep & ~L2_S_CACHE_MASK) | pte_l2_s_cache_mode;
1184 if (l2pte_valid(pte)) {
1185 if (PV_BEEN_EXECD(pv->pv_flags)) {
1186 pmap_tlb_flushID_SE(pv->pv_pmap, pv->pv_va);
1187 } else if (PV_BEEN_REFD(pv->pv_flags)) {
1188 pmap_tlb_flushD_SE(pv->pv_pmap, pv->pv_va);
1192 pte = *ptep &~ L2_S_CACHE_MASK;
1193 if ((va != pv->pv_va || pm != pv->pv_pmap) &&
1195 if (PV_BEEN_EXECD(pv->pv_flags)) {
1196 pmap_idcache_wbinv_range(pv->pv_pmap,
1197 pv->pv_va, PAGE_SIZE);
1198 pmap_tlb_flushID_SE(pv->pv_pmap, pv->pv_va);
1199 } else if (PV_BEEN_REFD(pv->pv_flags)) {
1200 pmap_dcache_wb_range(pv->pv_pmap,
1201 pv->pv_va, PAGE_SIZE, TRUE,
1202 (pv->pv_flags & PVF_WRITE) == 0);
1203 pmap_tlb_flushD_SE(pv->pv_pmap,
1209 PTE_SYNC_CURRENT(pv->pv_pmap, ptep);
1213 pmap_fix_cache(struct vm_page *pg, pmap_t pm, vm_offset_t va)
1216 int writable = 0, kwritable = 0, uwritable = 0;
1217 int entries = 0, kentries = 0, uentries = 0;
1218 struct pv_entry *pv;
1220 rw_assert(&pvh_global_lock, RA_WLOCKED);
1222 /* the cache gets written back/invalidated on context switch.
1223 * therefore, if a user page shares an entry in the same page or
1224 * with the kernel map and at least one is writable, then the
1225 * cache entry must be set write-through.
1228 TAILQ_FOREACH(pv, &pg->md.pv_list, pv_list) {
1229 /* generate a count of the pv_entry uses */
1230 if (pv->pv_flags & PVF_WRITE) {
1231 if (pv->pv_pmap == kernel_pmap)
1233 else if (pv->pv_pmap == pm)
1237 if (pv->pv_pmap == kernel_pmap)
1240 if (pv->pv_pmap == pm)
1246 * check if the user duplicate mapping has
1249 if ((pm != kernel_pmap) && (((uentries > 1) && uwritable) ||
1253 TAILQ_FOREACH(pv, &pg->md.pv_list, pv_list) {
1254 /* check for user uncachable conditions - order is important */
1255 if (pm != kernel_pmap &&
1256 (pv->pv_pmap == pm || pv->pv_pmap == kernel_pmap)) {
1258 if ((uentries > 1 && uwritable) || uwritable > 1) {
1260 /* user duplicate mapping */
1261 if (pv->pv_pmap != kernel_pmap)
1262 pv->pv_flags |= PVF_MWC;
1264 if (!(pv->pv_flags & PVF_NC)) {
1265 pv->pv_flags |= PVF_NC;
1266 pmap_set_cache_entry(pv, pm, va, -1);
1269 } else /* no longer a duplicate user */
1270 pv->pv_flags &= ~PVF_MWC;
1274 * check for kernel uncachable conditions
1275 * kernel writable or kernel readable with writable user entry
1277 if ((kwritable && (entries || kentries > 1)) ||
1279 ((kwritable != writable) && kentries &&
1280 (pv->pv_pmap == kernel_pmap ||
1281 (pv->pv_flags & PVF_WRITE) ||
1282 (pv->pv_flags & PVF_MWC)))) {
1284 if (!(pv->pv_flags & PVF_NC)) {
1285 pv->pv_flags |= PVF_NC;
1286 pmap_set_cache_entry(pv, pm, va, -1);
1291 /* kernel and user are cachable */
1292 if ((pm == kernel_pmap) && !(pv->pv_flags & PVF_MWC) &&
1293 (pv->pv_flags & PVF_NC)) {
1295 pv->pv_flags &= ~PVF_NC;
1296 if (pg->md.pv_memattr != VM_MEMATTR_UNCACHEABLE)
1297 pmap_set_cache_entry(pv, pm, va, 1);
1300 /* user is no longer sharable and writable */
1301 if (pm != kernel_pmap &&
1302 (pv->pv_pmap == pm || pv->pv_pmap == kernel_pmap) &&
1303 !pmwc && (pv->pv_flags & PVF_NC)) {
1305 pv->pv_flags &= ~(PVF_NC | PVF_MWC);
1306 if (pg->md.pv_memattr != VM_MEMATTR_UNCACHEABLE)
1307 pmap_set_cache_entry(pv, pm, va, 1);
1311 if ((kwritable == 0) && (writable == 0)) {
1312 pg->md.pvh_attrs &= ~PVF_MOD;
1313 vm_page_aflag_clear(pg, PGA_WRITEABLE);
1319 * Modify pte bits for all ptes corresponding to the given physical address.
1320 * We use `maskbits' rather than `clearbits' because we're always passing
1321 * constants and the latter would require an extra inversion at run-time.
1324 pmap_clearbit(struct vm_page *pg, u_int maskbits)
1326 struct l2_bucket *l2b;
1327 struct pv_entry *pv;
1328 pt_entry_t *ptep, npte, opte;
1334 rw_wlock(&pvh_global_lock);
1336 if (maskbits & PVF_WRITE)
1337 maskbits |= PVF_MOD;
1339 * Clear saved attributes (modify, reference)
1341 pg->md.pvh_attrs &= ~(maskbits & (PVF_MOD | PVF_REF));
1343 if (TAILQ_EMPTY(&pg->md.pv_list)) {
1344 rw_wunlock(&pvh_global_lock);
1349 * Loop over all current mappings setting/clearing as appropos
1351 TAILQ_FOREACH(pv, &pg->md.pv_list, pv_list) {
1354 oflags = pv->pv_flags;
1356 if (!(oflags & maskbits)) {
1357 if ((maskbits & PVF_WRITE) && (pv->pv_flags & PVF_NC)) {
1358 if (pg->md.pv_memattr !=
1359 VM_MEMATTR_UNCACHEABLE) {
1361 l2b = pmap_get_l2_bucket(pm, va);
1362 ptep = &l2b->l2b_kva[l2pte_index(va)];
1363 *ptep |= pte_l2_s_cache_mode;
1367 pv->pv_flags &= ~(PVF_NC | PVF_MWC);
1371 pv->pv_flags &= ~maskbits;
1375 l2b = pmap_get_l2_bucket(pm, va);
1377 ptep = &l2b->l2b_kva[l2pte_index(va)];
1378 npte = opte = *ptep;
1380 if (maskbits & (PVF_WRITE|PVF_MOD)) {
1381 if ((pv->pv_flags & PVF_NC)) {
1383 * Entry is not cacheable:
1385 * Don't turn caching on again if this is a
1386 * modified emulation. This would be
1387 * inconsistent with the settings created by
1388 * pmap_fix_cache(). Otherwise, it's safe
1389 * to re-enable caching.
1391 * There's no need to call pmap_fix_cache()
1392 * here: all pages are losing their write
1395 if (maskbits & PVF_WRITE) {
1396 if (pg->md.pv_memattr !=
1397 VM_MEMATTR_UNCACHEABLE)
1398 npte |= pte_l2_s_cache_mode;
1399 pv->pv_flags &= ~(PVF_NC | PVF_MWC);
1402 if (opte & L2_S_PROT_W) {
1405 * Entry is writable/cacheable: check if pmap
1406 * is current if it is flush it, otherwise it
1407 * won't be in the cache
1409 if (PV_BEEN_EXECD(oflags))
1410 pmap_idcache_wbinv_range(pm, pv->pv_va,
1413 if (PV_BEEN_REFD(oflags))
1414 pmap_dcache_wb_range(pm, pv->pv_va,
1416 (maskbits & PVF_REF) ? TRUE : FALSE,
1420 /* make the pte read only */
1421 npte &= ~L2_S_PROT_W;
1424 if (maskbits & PVF_REF) {
1425 if ((pv->pv_flags & PVF_NC) == 0 &&
1426 (maskbits & (PVF_WRITE|PVF_MOD)) == 0) {
1428 * Check npte here; we may have already
1429 * done the wbinv above, and the validity
1430 * of the PTE is the same for opte and
1433 if (npte & L2_S_PROT_W) {
1434 if (PV_BEEN_EXECD(oflags))
1435 pmap_idcache_wbinv_range(pm,
1436 pv->pv_va, PAGE_SIZE);
1438 if (PV_BEEN_REFD(oflags))
1439 pmap_dcache_wb_range(pm,
1440 pv->pv_va, PAGE_SIZE,
1443 if ((npte & L2_TYPE_MASK) != L2_TYPE_INV) {
1444 /* XXXJRT need idcache_inv_range */
1445 if (PV_BEEN_EXECD(oflags))
1446 pmap_idcache_wbinv_range(pm,
1447 pv->pv_va, PAGE_SIZE);
1449 if (PV_BEEN_REFD(oflags))
1450 pmap_dcache_wb_range(pm,
1451 pv->pv_va, PAGE_SIZE,
1457 * Make the PTE invalid so that we will take a
1458 * page fault the next time the mapping is
1461 npte &= ~L2_TYPE_MASK;
1462 npte |= L2_TYPE_INV;
1469 /* Flush the TLB entry if a current pmap. */
1470 if (PV_BEEN_EXECD(oflags))
1471 pmap_tlb_flushID_SE(pm, pv->pv_va);
1473 if (PV_BEEN_REFD(oflags))
1474 pmap_tlb_flushD_SE(pm, pv->pv_va);
1481 if (maskbits & PVF_WRITE)
1482 vm_page_aflag_clear(pg, PGA_WRITEABLE);
1483 rw_wunlock(&pvh_global_lock);
1488 * main pv_entry manipulation functions:
1489 * pmap_enter_pv: enter a mapping onto a vm_page list
1490 * pmap_remove_pv: remove a mappiing from a vm_page list
1492 * NOTE: pmap_enter_pv expects to lock the pvh itself
1493 * pmap_remove_pv expects the caller to lock the pvh before calling
1497 * pmap_enter_pv: enter a mapping onto a vm_page's PV list
1499 * => caller should hold the proper lock on pvh_global_lock
1500 * => caller should have pmap locked
1501 * => we will (someday) gain the lock on the vm_page's PV list
1502 * => caller should adjust ptp's wire_count before calling
1503 * => caller should not adjust pmap's wire_count
1506 pmap_enter_pv(struct vm_page *pg, struct pv_entry *pve, pmap_t pm,
1507 vm_offset_t va, u_int flags)
1510 rw_assert(&pvh_global_lock, RA_WLOCKED);
1511 PMAP_ASSERT_LOCKED(pm);
1512 if (pg->md.pv_kva != 0) {
1513 pve->pv_pmap = kernel_pmap;
1514 pve->pv_va = pg->md.pv_kva;
1515 pve->pv_flags = PVF_WRITE | PVF_UNMAN;
1516 if (pm != kernel_pmap)
1517 PMAP_LOCK(kernel_pmap);
1518 TAILQ_INSERT_HEAD(&pg->md.pv_list, pve, pv_list);
1519 TAILQ_INSERT_HEAD(&kernel_pmap->pm_pvlist, pve, pv_plist);
1520 if (pm != kernel_pmap)
1521 PMAP_UNLOCK(kernel_pmap);
1523 if ((pve = pmap_get_pv_entry()) == NULL)
1524 panic("pmap_kenter_pv: no pv entries");
1528 pve->pv_flags = flags;
1529 TAILQ_INSERT_HEAD(&pg->md.pv_list, pve, pv_list);
1530 TAILQ_INSERT_HEAD(&pm->pm_pvlist, pve, pv_plist);
1531 pg->md.pvh_attrs |= flags & (PVF_REF | PVF_MOD);
1532 if (pve->pv_flags & PVF_WIRED)
1533 ++pm->pm_stats.wired_count;
1534 vm_page_aflag_set(pg, PGA_REFERENCED);
1539 * pmap_find_pv: Find a pv entry
1541 * => caller should hold lock on vm_page
1543 static PMAP_INLINE struct pv_entry *
1544 pmap_find_pv(struct vm_page *pg, pmap_t pm, vm_offset_t va)
1546 struct pv_entry *pv;
1548 rw_assert(&pvh_global_lock, RA_WLOCKED);
1549 TAILQ_FOREACH(pv, &pg->md.pv_list, pv_list)
1550 if (pm == pv->pv_pmap && va == pv->pv_va)
1556 * vector_page_setprot:
1558 * Manipulate the protection of the vector page.
1561 vector_page_setprot(int prot)
1563 struct l2_bucket *l2b;
1566 l2b = pmap_get_l2_bucket(kernel_pmap, vector_page);
1568 ptep = &l2b->l2b_kva[l2pte_index(vector_page)];
1570 *ptep = (*ptep & ~L1_S_PROT_MASK) | L2_S_PROT(PTE_KERNEL, prot);
1572 cpu_tlb_flushD_SE(vector_page);
1577 * pmap_remove_pv: try to remove a mapping from a pv_list
1579 * => caller should hold proper lock on pmap_main_lock
1580 * => pmap should be locked
1581 * => caller should hold lock on vm_page [so that attrs can be adjusted]
1582 * => caller should adjust ptp's wire_count and free PTP if needed
1583 * => caller should NOT adjust pmap's wire_count
1584 * => we return the removed pve
1588 pmap_nuke_pv(struct vm_page *pg, pmap_t pm, struct pv_entry *pve)
1591 struct pv_entry *pv;
1592 rw_assert(&pvh_global_lock, RA_WLOCKED);
1593 PMAP_ASSERT_LOCKED(pm);
1594 TAILQ_REMOVE(&pg->md.pv_list, pve, pv_list);
1595 TAILQ_REMOVE(&pm->pm_pvlist, pve, pv_plist);
1596 if (pve->pv_flags & PVF_WIRED)
1597 --pm->pm_stats.wired_count;
1598 if (pg->md.pvh_attrs & PVF_MOD)
1600 if (TAILQ_FIRST(&pg->md.pv_list) == NULL)
1601 pg->md.pvh_attrs &= ~PVF_REF;
1603 vm_page_aflag_set(pg, PGA_REFERENCED);
1604 if ((pve->pv_flags & PVF_NC) && ((pm == kernel_pmap) ||
1605 (pve->pv_flags & PVF_WRITE) || !(pve->pv_flags & PVF_MWC)))
1606 pmap_fix_cache(pg, pm, 0);
1607 else if (pve->pv_flags & PVF_WRITE) {
1608 TAILQ_FOREACH(pve, &pg->md.pv_list, pv_list)
1609 if (pve->pv_flags & PVF_WRITE)
1612 pg->md.pvh_attrs &= ~PVF_MOD;
1613 vm_page_aflag_clear(pg, PGA_WRITEABLE);
1616 pv = TAILQ_FIRST(&pg->md.pv_list);
1617 if (pv != NULL && (pv->pv_flags & PVF_UNMAN) &&
1618 TAILQ_NEXT(pv, pv_list) == NULL) {
1620 pg->md.pv_kva = pv->pv_va;
1621 /* a recursive pmap_nuke_pv */
1622 TAILQ_REMOVE(&pg->md.pv_list, pv, pv_list);
1623 TAILQ_REMOVE(&pm->pm_pvlist, pv, pv_plist);
1624 if (pv->pv_flags & PVF_WIRED)
1625 --pm->pm_stats.wired_count;
1626 pg->md.pvh_attrs &= ~PVF_REF;
1627 pg->md.pvh_attrs &= ~PVF_MOD;
1628 vm_page_aflag_clear(pg, PGA_WRITEABLE);
1629 pmap_free_pv_entry(pv);
1633 static struct pv_entry *
1634 pmap_remove_pv(struct vm_page *pg, pmap_t pm, vm_offset_t va)
1636 struct pv_entry *pve;
1638 rw_assert(&pvh_global_lock, RA_WLOCKED);
1639 pve = TAILQ_FIRST(&pg->md.pv_list);
1642 if (pve->pv_pmap == pm && pve->pv_va == va) { /* match? */
1643 pmap_nuke_pv(pg, pm, pve);
1646 pve = TAILQ_NEXT(pve, pv_list);
1649 if (pve == NULL && pg->md.pv_kva == va)
1652 return(pve); /* return removed pve */
1656 * pmap_modify_pv: Update pv flags
1658 * => caller should hold lock on vm_page [so that attrs can be adjusted]
1659 * => caller should NOT adjust pmap's wire_count
1660 * => we return the old flags
1662 * Modify a physical-virtual mapping in the pv table
1665 pmap_modify_pv(struct vm_page *pg, pmap_t pm, vm_offset_t va,
1666 u_int clr_mask, u_int set_mask)
1668 struct pv_entry *npv;
1669 u_int flags, oflags;
1671 PMAP_ASSERT_LOCKED(pm);
1672 rw_assert(&pvh_global_lock, RA_WLOCKED);
1673 if ((npv = pmap_find_pv(pg, pm, va)) == NULL)
1677 * There is at least one VA mapping this page.
1680 if (clr_mask & (PVF_REF | PVF_MOD))
1681 pg->md.pvh_attrs |= set_mask & (PVF_REF | PVF_MOD);
1683 oflags = npv->pv_flags;
1684 npv->pv_flags = flags = (oflags & ~clr_mask) | set_mask;
1686 if ((flags ^ oflags) & PVF_WIRED) {
1687 if (flags & PVF_WIRED)
1688 ++pm->pm_stats.wired_count;
1690 --pm->pm_stats.wired_count;
1693 if ((flags ^ oflags) & PVF_WRITE)
1694 pmap_fix_cache(pg, pm, 0);
1699 /* Function to set the debug level of the pmap code */
1702 pmap_debug(int level)
1704 pmap_debug_level = level;
1705 dprintf("pmap_debug: level=%d\n", pmap_debug_level);
1707 #endif /* PMAP_DEBUG */
1710 pmap_pinit0(struct pmap *pmap)
1712 PDEBUG(1, printf("pmap_pinit0: pmap = %08x\n", (u_int32_t) pmap));
1714 bcopy(kernel_pmap, pmap, sizeof(*pmap));
1715 bzero(&pmap->pm_mtx, sizeof(pmap->pm_mtx));
1716 PMAP_LOCK_INIT(pmap);
1720 * Initialize a vm_page's machine-dependent fields.
1723 pmap_page_init(vm_page_t m)
1726 TAILQ_INIT(&m->md.pv_list);
1727 m->md.pv_memattr = VM_MEMATTR_DEFAULT;
1728 m->md.pvh_attrs = 0;
1733 * Initialize the pmap module.
1734 * Called by vm_init, to initialize any structures that the pmap
1735 * system needs to map virtual memory.
1740 int shpgperproc = PMAP_SHPGPERPROC;
1742 l2zone = uma_zcreate("L2 Table", L2_TABLE_SIZE_REAL, pmap_l2ptp_ctor,
1743 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_VM | UMA_ZONE_NOFREE);
1744 l2table_zone = uma_zcreate("L2 Table", sizeof(struct l2_dtable), NULL,
1745 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_VM | UMA_ZONE_NOFREE);
1748 * Initialize the PV entry allocator.
1750 pvzone = uma_zcreate("PV ENTRY", sizeof (struct pv_entry), NULL, NULL,
1751 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_VM | UMA_ZONE_NOFREE);
1752 TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
1753 pv_entry_max = shpgperproc * maxproc + vm_cnt.v_page_count;
1754 uma_zone_reserve_kva(pvzone, pv_entry_max);
1755 pv_entry_high_water = 9 * (pv_entry_max / 10);
1758 * Now it is safe to enable pv_table recording.
1760 PDEBUG(1, printf("pmap_init: done!\n"));
1764 pmap_fault_fixup(pmap_t pm, vm_offset_t va, vm_prot_t ftype, int user)
1766 struct l2_dtable *l2;
1767 struct l2_bucket *l2b;
1768 pd_entry_t *pl1pd, l1pd;
1769 pt_entry_t *ptep, pte;
1775 rw_wlock(&pvh_global_lock);
1779 * If there is no l2_dtable for this address, then the process
1780 * has no business accessing it.
1782 * Note: This will catch userland processes trying to access
1785 l2 = pm->pm_l2[L2_IDX(l1idx)];
1790 * Likewise if there is no L2 descriptor table
1792 l2b = &l2->l2_bucket[L2_BUCKET(l1idx)];
1793 if (l2b->l2b_kva == NULL)
1797 * Check the PTE itself.
1799 ptep = &l2b->l2b_kva[l2pte_index(va)];
1805 * Catch a userland access to the vector page mapped at 0x0
1807 if (user && (pte & L2_S_PROT_U) == 0)
1809 if (va == vector_page)
1814 if ((ftype & VM_PROT_WRITE) && (pte & L2_S_PROT_W) == 0) {
1816 * This looks like a good candidate for "page modified"
1819 struct pv_entry *pv;
1822 /* Extract the physical address of the page */
1823 if ((pg = PHYS_TO_VM_PAGE(pa)) == NULL) {
1826 /* Get the current flags for this page. */
1828 pv = pmap_find_pv(pg, pm, va);
1834 * Do the flags say this page is writable? If not then it
1835 * is a genuine write fault. If yes then the write fault is
1836 * our fault as we did not reflect the write access in the
1837 * PTE. Now we know a write has occurred we can correct this
1838 * and also set the modified bit
1840 if ((pv->pv_flags & PVF_WRITE) == 0) {
1844 pg->md.pvh_attrs |= PVF_REF | PVF_MOD;
1846 pv->pv_flags |= PVF_REF | PVF_MOD;
1849 * Re-enable write permissions for the page. No need to call
1850 * pmap_fix_cache(), since this is just a
1851 * modified-emulation fault, and the PVF_WRITE bit isn't
1852 * changing. We've already set the cacheable bits based on
1853 * the assumption that we can write to this page.
1855 *ptep = (pte & ~L2_TYPE_MASK) | L2_S_PROTO | L2_S_PROT_W;
1859 if ((pte & L2_TYPE_MASK) == L2_TYPE_INV) {
1861 * This looks like a good candidate for "page referenced"
1864 struct pv_entry *pv;
1867 /* Extract the physical address of the page */
1868 if ((pg = PHYS_TO_VM_PAGE(pa)) == NULL)
1870 /* Get the current flags for this page. */
1872 pv = pmap_find_pv(pg, pm, va);
1876 pg->md.pvh_attrs |= PVF_REF;
1877 pv->pv_flags |= PVF_REF;
1880 *ptep = (pte & ~L2_TYPE_MASK) | L2_S_PROTO;
1886 * We know there is a valid mapping here, so simply
1887 * fix up the L1 if necessary.
1889 pl1pd = &pm->pm_l1->l1_kva[l1idx];
1890 l1pd = l2b->l2b_phys | L1_C_DOM(pm->pm_domain) | L1_C_PROTO;
1891 if (*pl1pd != l1pd) {
1899 * If 'rv == 0' at this point, it generally indicates that there is a
1900 * stale TLB entry for the faulting address. This happens when two or
1901 * more processes are sharing an L1. Since we don't flush the TLB on
1902 * a context switch between such processes, we can take domain faults
1903 * for mappings which exist at the same VA in both processes. EVEN IF
1904 * WE'VE RECENTLY FIXED UP THE CORRESPONDING L1 in pmap_enter(), for
1907 * This is extremely likely to happen if pmap_enter() updated the L1
1908 * entry for a recently entered mapping. In this case, the TLB is
1909 * flushed for the new mapping, but there may still be TLB entries for
1910 * other mappings belonging to other processes in the 1MB range
1911 * covered by the L1 entry.
1913 * Since 'rv == 0', we know that the L1 already contains the correct
1914 * value, so the fault must be due to a stale TLB entry.
1916 * Since we always need to flush the TLB anyway in the case where we
1917 * fixed up the L1, or frobbed the L2 PTE, we effectively deal with
1918 * stale TLB entries dynamically.
1920 * However, the above condition can ONLY happen if the current L1 is
1921 * being shared. If it happens when the L1 is unshared, it indicates
1922 * that other parts of the pmap are not doing their job WRT managing
1925 if (rv == 0 && pm->pm_l1->l1_domain_use_count == 1) {
1926 printf("fixup: pm %p, va 0x%lx, ftype %d - nothing to do!\n",
1927 pm, (u_long)va, ftype);
1928 printf("fixup: l2 %p, l2b %p, ptep %p, pl1pd %p\n",
1929 l2, l2b, ptep, pl1pd);
1930 printf("fixup: pte 0x%x, l1pd 0x%x, last code 0x%x\n",
1931 pte, l1pd, last_fault_code);
1938 cpu_tlb_flushID_SE(va);
1944 rw_wunlock(&pvh_global_lock);
1952 struct l2_bucket *l2b;
1953 struct l1_ttable *l1;
1955 pt_entry_t *ptep, pte;
1956 vm_offset_t va, eva;
1959 needed = (maxproc / PMAP_DOMAINS) + ((maxproc % PMAP_DOMAINS) ? 1 : 0);
1961 l1 = malloc(sizeof(*l1) * needed, M_VMPMAP, M_WAITOK);
1963 for (loop = 0; loop < needed; loop++, l1++) {
1964 /* Allocate a L1 page table */
1965 va = (vm_offset_t)contigmalloc(L1_TABLE_SIZE, M_VMPMAP, 0, 0x0,
1966 0xffffffff, L1_TABLE_SIZE, 0);
1969 panic("Cannot allocate L1 KVM");
1971 eva = va + L1_TABLE_SIZE;
1972 pl1pt = (pd_entry_t *)va;
1975 l2b = pmap_get_l2_bucket(kernel_pmap, va);
1976 ptep = &l2b->l2b_kva[l2pte_index(va)];
1978 pte = (pte & ~L2_S_CACHE_MASK) | pte_l2_s_cache_mode_pt;
1981 cpu_tlb_flushD_SE(va);
1985 pmap_init_l1(l1, pl1pt);
1990 printf("pmap_postinit: Allocated %d static L1 descriptor tables\n",
1996 * This is used to stuff certain critical values into the PCB where they
1997 * can be accessed quickly from cpu_switch() et al.
2000 pmap_set_pcb_pagedir(pmap_t pm, struct pcb *pcb)
2002 struct l2_bucket *l2b;
2004 pcb->pcb_pagedir = pm->pm_l1->l1_physaddr;
2005 pcb->pcb_dacr = (DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL * 2)) |
2006 (DOMAIN_CLIENT << (pm->pm_domain * 2));
2008 if (vector_page < KERNBASE) {
2009 pcb->pcb_pl1vec = &pm->pm_l1->l1_kva[L1_IDX(vector_page)];
2010 l2b = pmap_get_l2_bucket(pm, vector_page);
2011 pcb->pcb_l1vec = l2b->l2b_phys | L1_C_PROTO |
2012 L1_C_DOM(pm->pm_domain) | L1_C_DOM(PMAP_DOMAIN_KERNEL);
2014 pcb->pcb_pl1vec = NULL;
2018 pmap_activate(struct thread *td)
2023 pm = vmspace_pmap(td->td_proc->p_vmspace);
2027 pmap_set_pcb_pagedir(pm, pcb);
2029 if (td == curthread) {
2030 u_int cur_dacr, cur_ttb;
2032 __asm __volatile("mrc p15, 0, %0, c2, c0, 0" : "=r"(cur_ttb));
2033 __asm __volatile("mrc p15, 0, %0, c3, c0, 0" : "=r"(cur_dacr));
2035 cur_ttb &= ~(L1_TABLE_SIZE - 1);
2037 if (cur_ttb == (u_int)pcb->pcb_pagedir &&
2038 cur_dacr == pcb->pcb_dacr) {
2040 * No need to switch address spaces.
2048 * We MUST, I repeat, MUST fix up the L1 entry corresponding
2049 * to 'vector_page' in the incoming L1 table before switching
2050 * to it otherwise subsequent interrupts/exceptions (including
2051 * domain faults!) will jump into hyperspace.
2053 if (pcb->pcb_pl1vec) {
2055 *pcb->pcb_pl1vec = pcb->pcb_l1vec;
2057 * Don't need to PTE_SYNC() at this point since
2058 * cpu_setttb() is about to flush both the cache
2063 cpu_domains(pcb->pcb_dacr);
2064 cpu_setttb(pcb->pcb_pagedir);
2070 pmap_set_pt_cache_mode(pd_entry_t *kl1, vm_offset_t va)
2072 pd_entry_t *pdep, pde;
2073 pt_entry_t *ptep, pte;
2078 * Make sure the descriptor itself has the correct cache mode
2080 pdep = &kl1[L1_IDX(va)];
2083 if (l1pte_section_p(pde)) {
2084 if ((pde & L1_S_CACHE_MASK) != pte_l1_s_cache_mode_pt) {
2085 *pdep = (pde & ~L1_S_CACHE_MASK) |
2086 pte_l1_s_cache_mode_pt;
2088 cpu_dcache_wbinv_range((vm_offset_t)pdep,
2090 cpu_l2cache_wbinv_range((vm_offset_t)pdep,
2095 pa = (vm_paddr_t)(pde & L1_C_ADDR_MASK);
2096 ptep = (pt_entry_t *)kernel_pt_lookup(pa);
2098 panic("pmap_bootstrap: No L2 for L2 @ va %p\n", ptep);
2100 ptep = &ptep[l2pte_index(va)];
2102 if ((pte & L2_S_CACHE_MASK) != pte_l2_s_cache_mode_pt) {
2103 *ptep = (pte & ~L2_S_CACHE_MASK) |
2104 pte_l2_s_cache_mode_pt;
2106 cpu_dcache_wbinv_range((vm_offset_t)ptep,
2108 cpu_l2cache_wbinv_range((vm_offset_t)ptep,
2118 pmap_alloc_specials(vm_offset_t *availp, int pages, vm_offset_t *vap,
2121 vm_offset_t va = *availp;
2122 struct l2_bucket *l2b;
2125 l2b = pmap_get_l2_bucket(kernel_pmap, va);
2127 panic("pmap_alloc_specials: no l2b for 0x%x", va);
2129 *ptep = &l2b->l2b_kva[l2pte_index(va)];
2133 *availp = va + (PAGE_SIZE * pages);
2137 * Bootstrap the system enough to run with virtual memory.
2139 * On the arm this is called after mapping has already been enabled
2140 * and just syncs the pmap module with what has already been done.
2141 * [We can't call it easily with mapping off since the kernel is not
2142 * mapped with PA == VA, hence we would have to relocate every address
2143 * from the linked base (virtual) address "KERNBASE" to the actual
2144 * (physical) address starting relative to 0]
2146 #define PMAP_STATIC_L2_SIZE 16
2148 pmap_bootstrap(vm_offset_t firstaddr, struct pv_addr *l1pt)
2150 static struct l1_ttable static_l1;
2151 static struct l2_dtable static_l2[PMAP_STATIC_L2_SIZE];
2152 struct l1_ttable *l1 = &static_l1;
2153 struct l2_dtable *l2;
2154 struct l2_bucket *l2b;
2156 pd_entry_t *kernel_l1pt = (pd_entry_t *)l1pt->pv_va;
2158 pt_entry_t *qmap_pte;
2162 int l1idx, l2idx, l2next = 0;
2164 PDEBUG(1, printf("firstaddr = %08x, lastaddr = %08x\n",
2165 firstaddr, vm_max_kernel_address));
2167 virtual_avail = firstaddr;
2168 kernel_pmap->pm_l1 = l1;
2169 kernel_l1pa = l1pt->pv_pa;
2172 * Scan the L1 translation table created by initarm() and create
2173 * the required metadata for all valid mappings found in it.
2175 for (l1idx = 0; l1idx < (L1_TABLE_SIZE / sizeof(pd_entry_t)); l1idx++) {
2176 pde = kernel_l1pt[l1idx];
2179 * We're only interested in Coarse mappings.
2180 * pmap_extract() can deal with section mappings without
2181 * recourse to checking L2 metadata.
2183 if ((pde & L1_TYPE_MASK) != L1_TYPE_C)
2187 * Lookup the KVA of this L2 descriptor table
2189 pa = (vm_paddr_t)(pde & L1_C_ADDR_MASK);
2190 ptep = (pt_entry_t *)kernel_pt_lookup(pa);
2193 panic("pmap_bootstrap: No L2 for va 0x%x, pa 0x%lx",
2194 (u_int)l1idx << L1_S_SHIFT, (long unsigned int)pa);
2198 * Fetch the associated L2 metadata structure.
2199 * Allocate a new one if necessary.
2201 if ((l2 = kernel_pmap->pm_l2[L2_IDX(l1idx)]) == NULL) {
2202 if (l2next == PMAP_STATIC_L2_SIZE)
2203 panic("pmap_bootstrap: out of static L2s");
2204 kernel_pmap->pm_l2[L2_IDX(l1idx)] = l2 =
2205 &static_l2[l2next++];
2209 * One more L1 slot tracked...
2214 * Fill in the details of the L2 descriptor in the
2215 * appropriate bucket.
2217 l2b = &l2->l2_bucket[L2_BUCKET(l1idx)];
2218 l2b->l2b_kva = ptep;
2220 l2b->l2b_l1idx = l1idx;
2223 * Establish an initial occupancy count for this descriptor
2226 l2idx < (L2_TABLE_SIZE_REAL / sizeof(pt_entry_t));
2228 if ((ptep[l2idx] & L2_TYPE_MASK) != L2_TYPE_INV) {
2229 l2b->l2b_occupancy++;
2234 * Make sure the descriptor itself has the correct cache mode.
2235 * If not, fix it, but whine about the problem. Port-meisters
2236 * should consider this a clue to fix up their initarm()
2239 if (pmap_set_pt_cache_mode(kernel_l1pt, (vm_offset_t)ptep)) {
2240 printf("pmap_bootstrap: WARNING! wrong cache mode for "
2241 "L2 pte @ %p\n", ptep);
2247 * Ensure the primary (kernel) L1 has the correct cache mode for
2248 * a page table. Bitch if it is not correctly set.
2250 for (va = (vm_offset_t)kernel_l1pt;
2251 va < ((vm_offset_t)kernel_l1pt + L1_TABLE_SIZE); va += PAGE_SIZE) {
2252 if (pmap_set_pt_cache_mode(kernel_l1pt, va))
2253 printf("pmap_bootstrap: WARNING! wrong cache mode for "
2254 "primary L1 @ 0x%x\n", va);
2257 cpu_dcache_wbinv_all();
2258 cpu_l2cache_wbinv_all();
2262 PMAP_LOCK_INIT(kernel_pmap);
2263 CPU_FILL(&kernel_pmap->pm_active);
2264 kernel_pmap->pm_domain = PMAP_DOMAIN_KERNEL;
2265 TAILQ_INIT(&kernel_pmap->pm_pvlist);
2268 * Initialize the global pv list lock.
2270 rw_init_flags(&pvh_global_lock, "pmap pv global", RW_RECURSE);
2273 * Reserve some special page table entries/VA space for temporary
2276 pmap_alloc_specials(&virtual_avail, 1, &csrcp, &csrc_pte);
2277 pmap_set_pt_cache_mode(kernel_l1pt, (vm_offset_t)csrc_pte);
2278 pmap_alloc_specials(&virtual_avail, 1, &cdstp, &cdst_pte);
2279 pmap_set_pt_cache_mode(kernel_l1pt, (vm_offset_t)cdst_pte);
2280 pmap_alloc_specials(&virtual_avail, 1, &qmap_addr, &qmap_pte);
2281 pmap_set_pt_cache_mode(kernel_l1pt, (vm_offset_t)qmap_pte);
2282 size = ((vm_max_kernel_address - pmap_curmaxkvaddr) + L1_S_OFFSET) /
2284 pmap_alloc_specials(&virtual_avail,
2285 round_page(size * L2_TABLE_SIZE_REAL) / PAGE_SIZE,
2286 &pmap_kernel_l2ptp_kva, NULL);
2288 size = howmany(size, L2_BUCKET_SIZE);
2289 pmap_alloc_specials(&virtual_avail,
2290 round_page(size * sizeof(struct l2_dtable)) / PAGE_SIZE,
2291 &pmap_kernel_l2dtable_kva, NULL);
2293 pmap_alloc_specials(&virtual_avail,
2294 1, (vm_offset_t*)&_tmppt, NULL);
2295 pmap_alloc_specials(&virtual_avail,
2296 MAXDUMPPGS, (vm_offset_t *)&crashdumpmap, NULL);
2297 SLIST_INIT(&l1_list);
2298 TAILQ_INIT(&l1_lru_list);
2299 mtx_init(&l1_lru_lock, "l1 list lock", NULL, MTX_DEF);
2300 pmap_init_l1(l1, kernel_l1pt);
2301 cpu_dcache_wbinv_all();
2302 cpu_l2cache_wbinv_all();
2304 virtual_avail = round_page(virtual_avail);
2305 virtual_end = vm_max_kernel_address;
2306 kernel_vm_end = pmap_curmaxkvaddr;
2307 mtx_init(&cmtx, "TMP mappings mtx", NULL, MTX_DEF);
2308 mtx_init(&qmap_mtx, "quick mapping mtx", NULL, MTX_DEF);
2310 pmap_set_pcb_pagedir(kernel_pmap, thread0.td_pcb);
2313 /***************************************************
2314 * Pmap allocation/deallocation routines.
2315 ***************************************************/
2318 * Release any resources held by the given physical map.
2319 * Called when a pmap initialized by pmap_pinit is being released.
2320 * Should only be called if the map contains no valid mappings.
2323 pmap_release(pmap_t pmap)
2327 pmap_idcache_wbinv_all(pmap);
2328 cpu_l2cache_wbinv_all();
2329 pmap_tlb_flushID(pmap);
2331 if (vector_page < KERNBASE) {
2332 struct pcb *curpcb = PCPU_GET(curpcb);
2333 pcb = thread0.td_pcb;
2334 if (pmap_is_current(pmap)) {
2336 * Frob the L1 entry corresponding to the vector
2337 * page so that it contains the kernel pmap's domain
2338 * number. This will ensure pmap_remove() does not
2339 * pull the current vector page out from under us.
2342 *pcb->pcb_pl1vec = pcb->pcb_l1vec;
2343 cpu_domains(pcb->pcb_dacr);
2344 cpu_setttb(pcb->pcb_pagedir);
2347 pmap_remove(pmap, vector_page, vector_page + PAGE_SIZE);
2349 * Make sure cpu_switch(), et al, DTRT. This is safe to do
2350 * since this process has no remaining mappings of its own.
2352 curpcb->pcb_pl1vec = pcb->pcb_pl1vec;
2353 curpcb->pcb_l1vec = pcb->pcb_l1vec;
2354 curpcb->pcb_dacr = pcb->pcb_dacr;
2355 curpcb->pcb_pagedir = pcb->pcb_pagedir;
2360 dprintf("pmap_release()\n");
2366 * Helper function for pmap_grow_l2_bucket()
2369 pmap_grow_map(vm_offset_t va, pt_entry_t cache_mode, vm_paddr_t *pap)
2371 struct l2_bucket *l2b;
2376 pg = vm_page_alloc(NULL, 0, VM_ALLOC_NOOBJ | VM_ALLOC_WIRED);
2379 pa = VM_PAGE_TO_PHYS(pg);
2384 l2b = pmap_get_l2_bucket(kernel_pmap, va);
2386 ptep = &l2b->l2b_kva[l2pte_index(va)];
2387 *ptep = L2_S_PROTO | pa | cache_mode |
2388 L2_S_PROT(PTE_KERNEL, VM_PROT_READ | VM_PROT_WRITE);
2394 * This is the same as pmap_alloc_l2_bucket(), except that it is only
2395 * used by pmap_growkernel().
2397 static __inline struct l2_bucket *
2398 pmap_grow_l2_bucket(pmap_t pm, vm_offset_t va)
2400 struct l2_dtable *l2;
2401 struct l2_bucket *l2b;
2402 struct l1_ttable *l1;
2409 if ((l2 = pm->pm_l2[L2_IDX(l1idx)]) == NULL) {
2411 * No mapping at this address, as there is
2412 * no entry in the L1 table.
2413 * Need to allocate a new l2_dtable.
2415 nva = pmap_kernel_l2dtable_kva;
2416 if ((nva & PAGE_MASK) == 0) {
2418 * Need to allocate a backing page
2420 if (pmap_grow_map(nva, pte_l2_s_cache_mode, NULL))
2424 l2 = (struct l2_dtable *)nva;
2425 nva += sizeof(struct l2_dtable);
2427 if ((nva & PAGE_MASK) < (pmap_kernel_l2dtable_kva &
2430 * The new l2_dtable straddles a page boundary.
2431 * Map in another page to cover it.
2433 if (pmap_grow_map(nva, pte_l2_s_cache_mode, NULL))
2437 pmap_kernel_l2dtable_kva = nva;
2440 * Link it into the parent pmap
2442 pm->pm_l2[L2_IDX(l1idx)] = l2;
2443 memset(l2, 0, sizeof(*l2));
2446 l2b = &l2->l2_bucket[L2_BUCKET(l1idx)];
2449 * Fetch pointer to the L2 page table associated with the address.
2451 if (l2b->l2b_kva == NULL) {
2455 * No L2 page table has been allocated. Chances are, this
2456 * is because we just allocated the l2_dtable, above.
2458 nva = pmap_kernel_l2ptp_kva;
2459 ptep = (pt_entry_t *)nva;
2460 if ((nva & PAGE_MASK) == 0) {
2462 * Need to allocate a backing page
2464 if (pmap_grow_map(nva, pte_l2_s_cache_mode_pt,
2465 &pmap_kernel_l2ptp_phys))
2467 PTE_SYNC_RANGE(ptep, PAGE_SIZE / sizeof(pt_entry_t));
2469 memset(ptep, 0, L2_TABLE_SIZE_REAL);
2471 l2b->l2b_kva = ptep;
2472 l2b->l2b_l1idx = l1idx;
2473 l2b->l2b_phys = pmap_kernel_l2ptp_phys;
2475 pmap_kernel_l2ptp_kva += L2_TABLE_SIZE_REAL;
2476 pmap_kernel_l2ptp_phys += L2_TABLE_SIZE_REAL;
2479 /* Distribute new L1 entry to all other L1s */
2480 SLIST_FOREACH(l1, &l1_list, l1_link) {
2481 pl1pd = &l1->l1_kva[L1_IDX(va)];
2482 *pl1pd = l2b->l2b_phys | L1_C_DOM(PMAP_DOMAIN_KERNEL) |
2492 * grow the number of kernel page table entries, if needed
2495 pmap_growkernel(vm_offset_t addr)
2497 pmap_t kpm = kernel_pmap;
2499 if (addr <= pmap_curmaxkvaddr)
2500 return; /* we are OK */
2503 * whoops! we need to add kernel PTPs
2506 /* Map 1MB at a time */
2507 for (; pmap_curmaxkvaddr < addr; pmap_curmaxkvaddr += L1_S_SIZE)
2508 pmap_grow_l2_bucket(kpm, pmap_curmaxkvaddr);
2511 * flush out the cache, expensive but growkernel will happen so
2514 cpu_dcache_wbinv_all();
2515 cpu_l2cache_wbinv_all();
2518 kernel_vm_end = pmap_curmaxkvaddr;
2523 * Remove all pages from specified address space
2524 * this aids process exit speeds. Also, this code
2525 * is special cased for current process only, but
2526 * can have the more generic (and slightly slower)
2527 * mode enabled. This is much faster than pmap_remove
2528 * in the case of running down an entire address space.
2531 pmap_remove_pages(pmap_t pmap)
2533 struct pv_entry *pv, *npv;
2534 struct l2_bucket *l2b = NULL;
2538 rw_wlock(&pvh_global_lock);
2540 cpu_idcache_wbinv_all();
2541 cpu_l2cache_wbinv_all();
2542 for (pv = TAILQ_FIRST(&pmap->pm_pvlist); pv; pv = npv) {
2543 if (pv->pv_flags & PVF_WIRED || pv->pv_flags & PVF_UNMAN) {
2544 /* Cannot remove wired or unmanaged pages now. */
2545 npv = TAILQ_NEXT(pv, pv_plist);
2548 pmap->pm_stats.resident_count--;
2549 l2b = pmap_get_l2_bucket(pmap, pv->pv_va);
2550 KASSERT(l2b != NULL, ("No L2 bucket in pmap_remove_pages"));
2551 pt = &l2b->l2b_kva[l2pte_index(pv->pv_va)];
2552 m = PHYS_TO_VM_PAGE(*pt & L2_S_FRAME);
2553 KASSERT((vm_offset_t)m >= KERNBASE, ("Trying to access non-existent page va %x pte %x", pv->pv_va, *pt));
2556 npv = TAILQ_NEXT(pv, pv_plist);
2557 pmap_nuke_pv(m, pmap, pv);
2558 if (TAILQ_EMPTY(&m->md.pv_list))
2559 vm_page_aflag_clear(m, PGA_WRITEABLE);
2560 pmap_free_pv_entry(pv);
2561 pmap_free_l2_bucket(pmap, l2b, 1);
2563 rw_wunlock(&pvh_global_lock);
2570 /***************************************************
2571 * Low level mapping routines.....
2572 ***************************************************/
2574 #ifdef ARM_HAVE_SUPERSECTIONS
2575 /* Map a super section into the KVA. */
2578 pmap_kenter_supersection(vm_offset_t va, uint64_t pa, int flags)
2580 pd_entry_t pd = L1_S_PROTO | L1_S_SUPERSEC | (pa & L1_SUP_FRAME) |
2581 (((pa >> 32) & 0xf) << 20) | L1_S_PROT(PTE_KERNEL,
2582 VM_PROT_READ|VM_PROT_WRITE) | L1_S_DOM(PMAP_DOMAIN_KERNEL);
2583 struct l1_ttable *l1;
2584 vm_offset_t va0, va_end;
2586 KASSERT(((va | pa) & L1_SUP_OFFSET) == 0,
2587 ("Not a valid super section mapping"));
2588 if (flags & SECTION_CACHE)
2589 pd |= pte_l1_s_cache_mode;
2590 else if (flags & SECTION_PT)
2591 pd |= pte_l1_s_cache_mode_pt;
2592 va0 = va & L1_SUP_FRAME;
2593 va_end = va + L1_SUP_SIZE;
2594 SLIST_FOREACH(l1, &l1_list, l1_link) {
2596 for (; va < va_end; va += L1_S_SIZE) {
2597 l1->l1_kva[L1_IDX(va)] = pd;
2598 PTE_SYNC(&l1->l1_kva[L1_IDX(va)]);
2604 /* Map a section into the KVA. */
2607 pmap_kenter_section(vm_offset_t va, vm_offset_t pa, int flags)
2609 pd_entry_t pd = L1_S_PROTO | pa | L1_S_PROT(PTE_KERNEL,
2610 VM_PROT_READ|VM_PROT_WRITE) | L1_S_DOM(PMAP_DOMAIN_KERNEL);
2611 struct l1_ttable *l1;
2613 KASSERT(((va | pa) & L1_S_OFFSET) == 0,
2614 ("Not a valid section mapping"));
2615 if (flags & SECTION_CACHE)
2616 pd |= pte_l1_s_cache_mode;
2617 else if (flags & SECTION_PT)
2618 pd |= pte_l1_s_cache_mode_pt;
2619 SLIST_FOREACH(l1, &l1_list, l1_link) {
2620 l1->l1_kva[L1_IDX(va)] = pd;
2621 PTE_SYNC(&l1->l1_kva[L1_IDX(va)]);
2626 * Make a temporary mapping for a physical address. This is only intended
2627 * to be used for panic dumps.
2630 pmap_kenter_temporary(vm_paddr_t pa, int i)
2634 va = (vm_offset_t)crashdumpmap + (i * PAGE_SIZE);
2635 pmap_kenter(va, pa);
2636 return ((void *)crashdumpmap);
2640 * add a wired page to the kva
2641 * note that in order for the mapping to take effect -- you
2642 * should do a invltlb after doing the pmap_kenter...
2644 static PMAP_INLINE void
2645 pmap_kenter_internal(vm_offset_t va, vm_offset_t pa, int flags)
2647 struct l2_bucket *l2b;
2650 struct pv_entry *pve;
2653 PDEBUG(1, printf("pmap_kenter: va = %08x, pa = %08x\n",
2654 (uint32_t) va, (uint32_t) pa));
2657 l2b = pmap_get_l2_bucket(kernel_pmap, va);
2659 l2b = pmap_grow_l2_bucket(kernel_pmap, va);
2660 KASSERT(l2b != NULL, ("No L2 Bucket"));
2661 pte = &l2b->l2b_kva[l2pte_index(va)];
2663 PDEBUG(1, printf("pmap_kenter: pte = %08x, opte = %08x, npte = %08x\n",
2664 (uint32_t) pte, opte, *pte));
2665 if (l2pte_valid(opte)) {
2669 l2b->l2b_occupancy++;
2671 *pte = L2_S_PROTO | pa | L2_S_PROT(PTE_KERNEL,
2672 VM_PROT_READ | VM_PROT_WRITE);
2673 if (flags & KENTER_CACHE)
2674 *pte |= pte_l2_s_cache_mode;
2675 if (flags & KENTER_USER)
2676 *pte |= L2_S_PROT_U;
2680 * A kernel mapping may not be the page's only mapping, so create a PV
2681 * entry to ensure proper caching.
2683 * The existence test for the pvzone is used to delay the recording of
2684 * kernel mappings until the VM system is fully initialized.
2686 * This expects the physical memory to have a vm_page_array entry.
2688 if (pvzone != NULL && (m = vm_phys_paddr_to_vm_page(pa)) != NULL) {
2689 rw_wlock(&pvh_global_lock);
2690 if (!TAILQ_EMPTY(&m->md.pv_list) || m->md.pv_kva != 0) {
2691 if ((pve = pmap_get_pv_entry()) == NULL)
2692 panic("pmap_kenter_internal: no pv entries");
2693 PMAP_LOCK(kernel_pmap);
2694 pmap_enter_pv(m, pve, kernel_pmap, va,
2695 PVF_WRITE | PVF_UNMAN);
2696 pmap_fix_cache(m, kernel_pmap, va);
2697 PMAP_UNLOCK(kernel_pmap);
2701 rw_wunlock(&pvh_global_lock);
2706 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
2708 pmap_kenter_internal(va, pa, KENTER_CACHE);
2712 pmap_kenter_nocache(vm_offset_t va, vm_paddr_t pa)
2715 pmap_kenter_internal(va, pa, 0);
2719 pmap_kenter_device(vm_offset_t va, vm_size_t size, vm_paddr_t pa)
2723 KASSERT((size & PAGE_MASK) == 0,
2724 ("%s: device mapping not page-sized", __func__));
2728 pmap_kenter_internal(va, pa, 0);
2736 pmap_kremove_device(vm_offset_t va, vm_size_t size)
2740 KASSERT((size & PAGE_MASK) == 0,
2741 ("%s: device mapping not page-sized", __func__));
2752 pmap_kenter_user(vm_offset_t va, vm_paddr_t pa)
2755 pmap_kenter_internal(va, pa, KENTER_CACHE|KENTER_USER);
2757 * Call pmap_fault_fixup now, to make sure we'll have no exception
2758 * at the first use of the new address, or bad things will happen,
2759 * as we use one of these addresses in the exception handlers.
2761 pmap_fault_fixup(kernel_pmap, va, VM_PROT_READ|VM_PROT_WRITE, 1);
2765 pmap_kextract(vm_offset_t va)
2768 return (pmap_extract_locked(kernel_pmap, va));
2772 * remove a page from the kernel pagetables
2775 pmap_kremove(vm_offset_t va)
2777 struct l2_bucket *l2b;
2778 pt_entry_t *pte, opte;
2779 struct pv_entry *pve;
2783 l2b = pmap_get_l2_bucket(kernel_pmap, va);
2786 KASSERT(l2b != NULL, ("No L2 Bucket"));
2787 pte = &l2b->l2b_kva[l2pte_index(va)];
2789 if (l2pte_valid(opte)) {
2790 /* pa = vtophs(va) taken from pmap_extract() */
2791 if ((opte & L2_TYPE_MASK) == L2_TYPE_L)
2792 pa = (opte & L2_L_FRAME) | (va & L2_L_OFFSET);
2794 pa = (opte & L2_S_FRAME) | (va & L2_S_OFFSET);
2795 /* note: should never have to remove an allocation
2796 * before the pvzone is initialized.
2798 rw_wlock(&pvh_global_lock);
2799 PMAP_LOCK(kernel_pmap);
2800 if (pvzone != NULL && (m = vm_phys_paddr_to_vm_page(pa)) &&
2801 (pve = pmap_remove_pv(m, kernel_pmap, va)))
2802 pmap_free_pv_entry(pve);
2803 PMAP_UNLOCK(kernel_pmap);
2804 rw_wunlock(&pvh_global_lock);
2805 va = va & ~PAGE_MASK;
2806 cpu_dcache_wbinv_range(va, PAGE_SIZE);
2807 cpu_l2cache_wbinv_range(va, PAGE_SIZE);
2808 cpu_tlb_flushD_SE(va);
2816 * Used to map a range of physical addresses into kernel
2817 * virtual address space.
2819 * The value passed in '*virt' is a suggested virtual address for
2820 * the mapping. Architectures which can support a direct-mapped
2821 * physical to virtual region can return the appropriate address
2822 * within that region, leaving '*virt' unchanged. Other
2823 * architectures should map the pages starting at '*virt' and
2824 * update '*virt' with the first usable address after the mapped
2828 pmap_map(vm_offset_t *virt, vm_offset_t start, vm_offset_t end, int prot)
2830 vm_offset_t sva = *virt;
2831 vm_offset_t va = sva;
2833 PDEBUG(1, printf("pmap_map: virt = %08x, start = %08x, end = %08x, "
2834 "prot = %d\n", (uint32_t) *virt, (uint32_t) start, (uint32_t) end,
2837 while (start < end) {
2838 pmap_kenter(va, start);
2847 pmap_wb_page(vm_page_t m)
2849 struct pv_entry *pv;
2851 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list)
2852 pmap_dcache_wb_range(pv->pv_pmap, pv->pv_va, PAGE_SIZE, FALSE,
2853 (pv->pv_flags & PVF_WRITE) == 0);
2857 pmap_inv_page(vm_page_t m)
2859 struct pv_entry *pv;
2861 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list)
2862 pmap_dcache_wb_range(pv->pv_pmap, pv->pv_va, PAGE_SIZE, TRUE, TRUE);
2865 * Add a list of wired pages to the kva
2866 * this routine is only used for temporary
2867 * kernel mappings that do not need to have
2868 * page modification or references recorded.
2869 * Note that old mappings are simply written
2870 * over. The page *must* be wired.
2873 pmap_qenter(vm_offset_t va, vm_page_t *m, int count)
2877 for (i = 0; i < count; i++) {
2879 pmap_kenter_internal(va, VM_PAGE_TO_PHYS(m[i]),
2887 * this routine jerks page mappings from the
2888 * kernel -- it is meant only for temporary mappings.
2891 pmap_qremove(vm_offset_t va, int count)
2896 for (i = 0; i < count; i++) {
2899 pmap_inv_page(PHYS_TO_VM_PAGE(pa));
2908 * pmap_object_init_pt preloads the ptes for a given object
2909 * into the specified pmap. This eliminates the blast of soft
2910 * faults on process startup and immediately after an mmap.
2913 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr, vm_object_t object,
2914 vm_pindex_t pindex, vm_size_t size)
2917 VM_OBJECT_ASSERT_WLOCKED(object);
2918 KASSERT(object->type == OBJT_DEVICE || object->type == OBJT_SG,
2919 ("pmap_object_init_pt: non-device object"));
2924 * pmap_is_prefaultable:
2926 * Return whether or not the specified virtual address is elgible
2930 pmap_is_prefaultable(pmap_t pmap, vm_offset_t addr)
2935 if (!pmap_get_pde_pte(pmap, addr, &pde, &pte))
2937 KASSERT(pte != NULL, ("Valid mapping but no pte ?"));
2944 * Fetch pointers to the PDE/PTE for the given pmap/VA pair.
2945 * Returns TRUE if the mapping exists, else FALSE.
2947 * NOTE: This function is only used by a couple of arm-specific modules.
2948 * It is not safe to take any pmap locks here, since we could be right
2949 * in the middle of debugging the pmap anyway...
2951 * It is possible for this routine to return FALSE even though a valid
2952 * mapping does exist. This is because we don't lock, so the metadata
2953 * state may be inconsistent.
2955 * NOTE: We can return a NULL *ptp in the case where the L1 pde is
2956 * a "section" mapping.
2959 pmap_get_pde_pte(pmap_t pm, vm_offset_t va, pd_entry_t **pdp, pt_entry_t **ptp)
2961 struct l2_dtable *l2;
2962 pd_entry_t *pl1pd, l1pd;
2966 if (pm->pm_l1 == NULL)
2970 *pdp = pl1pd = &pm->pm_l1->l1_kva[l1idx];
2973 if (l1pte_section_p(l1pd)) {
2978 if (pm->pm_l2 == NULL)
2981 l2 = pm->pm_l2[L2_IDX(l1idx)];
2984 (ptep = l2->l2_bucket[L2_BUCKET(l1idx)].l2b_kva) == NULL) {
2988 *ptp = &ptep[l2pte_index(va)];
2993 * Routine: pmap_remove_all
2995 * Removes this physical page from
2996 * all physical maps in which it resides.
2997 * Reflects back modify bits to the pager.
3000 * Original versions of this routine were very
3001 * inefficient because they iteratively called
3002 * pmap_remove (slow...)
3005 pmap_remove_all(vm_page_t m)
3009 struct l2_bucket *l2b;
3010 boolean_t flush = FALSE;
3014 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
3015 ("pmap_remove_all: page %p is not managed", m));
3016 if (TAILQ_EMPTY(&m->md.pv_list))
3018 rw_wlock(&pvh_global_lock);
3021 * XXX This call shouldn't exist. Iterating over the PV list twice,
3022 * once in pmap_clearbit() and again below, is both unnecessary and
3023 * inefficient. The below code should itself write back the cache
3024 * entry before it destroys the mapping.
3026 pmap_clearbit(m, PVF_WRITE);
3027 curpm = vmspace_pmap(curproc->p_vmspace);
3028 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
3029 if (flush == FALSE && (pv->pv_pmap == curpm ||
3030 pv->pv_pmap == kernel_pmap))
3033 PMAP_LOCK(pv->pv_pmap);
3035 * Cached contents were written-back in pmap_clearbit(),
3036 * but we still have to invalidate the cache entry to make
3037 * sure stale data are not retrieved when another page will be
3038 * mapped under this virtual address.
3040 if (pmap_is_current(pv->pv_pmap)) {
3041 cpu_dcache_inv_range(pv->pv_va, PAGE_SIZE);
3042 if (pmap_has_valid_mapping(pv->pv_pmap, pv->pv_va))
3043 cpu_l2cache_inv_range(pv->pv_va, PAGE_SIZE);
3046 if (pv->pv_flags & PVF_UNMAN) {
3047 /* remove the pv entry, but do not remove the mapping
3048 * and remember this is a kernel mapped page
3050 m->md.pv_kva = pv->pv_va;
3052 /* remove the mapping and pv entry */
3053 l2b = pmap_get_l2_bucket(pv->pv_pmap, pv->pv_va);
3054 KASSERT(l2b != NULL, ("No l2 bucket"));
3055 ptep = &l2b->l2b_kva[l2pte_index(pv->pv_va)];
3057 PTE_SYNC_CURRENT(pv->pv_pmap, ptep);
3058 pmap_free_l2_bucket(pv->pv_pmap, l2b, 1);
3059 pv->pv_pmap->pm_stats.resident_count--;
3060 flags |= pv->pv_flags;
3062 pmap_nuke_pv(m, pv->pv_pmap, pv);
3063 PMAP_UNLOCK(pv->pv_pmap);
3064 pmap_free_pv_entry(pv);
3068 if (PV_BEEN_EXECD(flags))
3069 pmap_tlb_flushID(curpm);
3071 pmap_tlb_flushD(curpm);
3073 vm_page_aflag_clear(m, PGA_WRITEABLE);
3074 rw_wunlock(&pvh_global_lock);
3079 * Set the physical protection on the
3080 * specified range of this map as requested.
3083 pmap_protect(pmap_t pm, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
3085 struct l2_bucket *l2b;
3086 pt_entry_t *ptep, pte;
3087 vm_offset_t next_bucket;
3091 CTR4(KTR_PMAP, "pmap_protect: pmap %p sva 0x%08x eva 0x%08x prot %x",
3092 pm, sva, eva, prot);
3094 if ((prot & VM_PROT_READ) == 0) {
3095 pmap_remove(pm, sva, eva);
3099 if (prot & VM_PROT_WRITE) {
3101 * If this is a read->write transition, just ignore it and let
3102 * vm_fault() take care of it later.
3107 rw_wlock(&pvh_global_lock);
3111 * OK, at this point, we know we're doing write-protect operation.
3112 * If the pmap is active, write-back the range.
3114 pmap_dcache_wb_range(pm, sva, eva - sva, FALSE, FALSE);
3116 flush = ((eva - sva) >= (PAGE_SIZE * 4)) ? 0 : -1;
3120 next_bucket = L2_NEXT_BUCKET(sva);
3121 if (next_bucket > eva)
3124 l2b = pmap_get_l2_bucket(pm, sva);
3130 ptep = &l2b->l2b_kva[l2pte_index(sva)];
3132 while (sva < next_bucket) {
3133 if ((pte = *ptep) != 0 && (pte & L2_S_PROT_W) != 0) {
3137 pg = PHYS_TO_VM_PAGE(l2pte_pa(pte));
3138 pte &= ~L2_S_PROT_W;
3142 if (!(pg->oflags & VPO_UNMANAGED)) {
3143 f = pmap_modify_pv(pg, pm, sva,
3154 if (PV_BEEN_EXECD(f))
3155 pmap_tlb_flushID_SE(pm, sva);
3157 if (PV_BEEN_REFD(f))
3158 pmap_tlb_flushD_SE(pm, sva);
3168 if (PV_BEEN_EXECD(flags))
3169 pmap_tlb_flushID(pm);
3171 if (PV_BEEN_REFD(flags))
3172 pmap_tlb_flushD(pm);
3174 rw_wunlock(&pvh_global_lock);
3181 * Insert the given physical page (p) at
3182 * the specified virtual address (v) in the
3183 * target physical map with the protection requested.
3185 * If specified, the page will be wired down, meaning
3186 * that the related pte can not be reclaimed.
3188 * NB: This is the only routine which MAY NOT lazy-evaluate
3189 * or lose information. That is, this routine must actually
3190 * insert this page into the given map NOW.
3194 pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
3195 u_int flags, int8_t psind __unused)
3199 rw_wlock(&pvh_global_lock);
3201 rv = pmap_enter_locked(pmap, va, m, prot, flags);
3202 rw_wunlock(&pvh_global_lock);
3208 * The pvh global and pmap locks must be held.
3211 pmap_enter_locked(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
3214 struct l2_bucket *l2b = NULL;
3215 struct vm_page *opg;
3216 struct pv_entry *pve = NULL;
3217 pt_entry_t *ptep, npte, opte;
3222 PMAP_ASSERT_LOCKED(pmap);
3223 rw_assert(&pvh_global_lock, RA_WLOCKED);
3224 if (va == vector_page) {
3225 pa = systempage.pv_pa;
3228 if ((m->oflags & VPO_UNMANAGED) == 0 && !vm_page_xbusied(m))
3229 VM_OBJECT_ASSERT_LOCKED(m->object);
3230 pa = VM_PAGE_TO_PHYS(m);
3233 if (prot & VM_PROT_WRITE)
3234 nflags |= PVF_WRITE;
3235 if (prot & VM_PROT_EXECUTE)
3237 if ((flags & PMAP_ENTER_WIRED) != 0)
3238 nflags |= PVF_WIRED;
3239 PDEBUG(1, printf("pmap_enter: pmap = %08x, va = %08x, m = %08x, prot = %x, "
3240 "flags = %x\n", (uint32_t) pmap, va, (uint32_t) m, prot, flags));
3242 if (pmap == kernel_pmap) {
3243 l2b = pmap_get_l2_bucket(pmap, va);
3245 l2b = pmap_grow_l2_bucket(pmap, va);
3248 l2b = pmap_alloc_l2_bucket(pmap, va);
3250 if ((flags & PMAP_ENTER_NOSLEEP) == 0) {
3252 rw_wunlock(&pvh_global_lock);
3254 rw_wlock(&pvh_global_lock);
3258 return (KERN_RESOURCE_SHORTAGE);
3262 ptep = &l2b->l2b_kva[l2pte_index(va)];
3269 * There is already a mapping at this address.
3270 * If the physical address is different, lookup the
3273 if (l2pte_pa(opte) != pa)
3274 opg = PHYS_TO_VM_PAGE(l2pte_pa(opte));
3280 if ((prot & (VM_PROT_ALL)) ||
3281 (!m || m->md.pvh_attrs & PVF_REF)) {
3283 * - The access type indicates that we don't need
3284 * to do referenced emulation.
3286 * - The physical page has already been referenced
3287 * so no need to re-do referenced emulation here.
3293 if (m && ((prot & VM_PROT_WRITE) != 0 ||
3294 (m->md.pvh_attrs & PVF_MOD))) {
3296 * This is a writable mapping, and the
3297 * page's mod state indicates it has
3298 * already been modified. Make it
3299 * writable from the outset.
3302 if (!(m->md.pvh_attrs & PVF_MOD))
3306 vm_page_aflag_set(m, PGA_REFERENCED);
3309 * Need to do page referenced emulation.
3311 npte |= L2_TYPE_INV;
3314 if (prot & VM_PROT_WRITE) {
3315 npte |= L2_S_PROT_W;
3317 (m->oflags & VPO_UNMANAGED) == 0)
3318 vm_page_aflag_set(m, PGA_WRITEABLE);
3320 if (m->md.pv_memattr != VM_MEMATTR_UNCACHEABLE)
3321 npte |= pte_l2_s_cache_mode;
3322 if (m && m == opg) {
3324 * We're changing the attrs of an existing mapping.
3326 oflags = pmap_modify_pv(m, pmap, va,
3327 PVF_WRITE | PVF_EXEC | PVF_WIRED |
3328 PVF_MOD | PVF_REF, nflags);
3331 * We may need to flush the cache if we're
3334 if (pmap_is_current(pmap) &&
3335 (oflags & PVF_NC) == 0 &&
3336 (opte & L2_S_PROT_W) != 0 &&
3337 (prot & VM_PROT_WRITE) == 0 &&
3338 (opte & L2_TYPE_MASK) != L2_TYPE_INV) {
3339 cpu_dcache_wb_range(va, PAGE_SIZE);
3340 cpu_l2cache_wb_range(va, PAGE_SIZE);
3344 * New mapping, or changing the backing page
3345 * of an existing mapping.
3349 * Replacing an existing mapping with a new one.
3350 * It is part of our managed memory so we
3351 * must remove it from the PV list
3353 if ((pve = pmap_remove_pv(opg, pmap, va))) {
3355 /* note for patch: the oflags/invalidation was moved
3356 * because PG_FICTITIOUS pages could free the pve
3358 oflags = pve->pv_flags;
3360 * If the old mapping was valid (ref/mod
3361 * emulation creates 'invalid' mappings
3362 * initially) then make sure to frob
3365 if ((oflags & PVF_NC) == 0 && l2pte_valid(opte)) {
3366 if (PV_BEEN_EXECD(oflags)) {
3367 pmap_idcache_wbinv_range(pmap, va,
3370 if (PV_BEEN_REFD(oflags)) {
3371 pmap_dcache_wb_range(pmap, va,
3373 (oflags & PVF_WRITE) == 0);
3377 /* free/allocate a pv_entry for UNMANAGED pages if
3378 * this physical page is not/is already mapped.
3381 if (m && (m->oflags & VPO_UNMANAGED) &&
3383 TAILQ_EMPTY(&m->md.pv_list)) {
3384 pmap_free_pv_entry(pve);
3388 (!(m->oflags & VPO_UNMANAGED) || m->md.pv_kva ||
3389 !TAILQ_EMPTY(&m->md.pv_list)))
3390 pve = pmap_get_pv_entry();
3392 (!(m->oflags & VPO_UNMANAGED) || m->md.pv_kva ||
3393 !TAILQ_EMPTY(&m->md.pv_list)))
3394 pve = pmap_get_pv_entry();
3397 if ((m->oflags & VPO_UNMANAGED)) {
3398 if (!TAILQ_EMPTY(&m->md.pv_list) ||
3400 KASSERT(pve != NULL, ("No pv"));
3401 nflags |= PVF_UNMAN;
3402 pmap_enter_pv(m, pve, pmap, va, nflags);
3406 KASSERT(va < kmi.clean_sva ||
3407 va >= kmi.clean_eva,
3408 ("pmap_enter: managed mapping within the clean submap"));
3409 KASSERT(pve != NULL, ("No pv"));
3410 pmap_enter_pv(m, pve, pmap, va, nflags);
3415 * Make sure userland mappings get the right permissions
3417 if (pmap != kernel_pmap && va != vector_page) {
3418 npte |= L2_S_PROT_U;
3422 * Keep the stats up to date
3425 l2b->l2b_occupancy++;
3426 pmap->pm_stats.resident_count++;
3430 * If this is just a wiring change, the two PTEs will be
3431 * identical, so there's no need to update the page table.
3434 boolean_t is_cached = pmap_is_current(pmap);
3439 * We only need to frob the cache/tlb if this pmap
3443 if (L1_IDX(va) != L1_IDX(vector_page) &&
3444 l2pte_valid(npte)) {
3446 * This mapping is likely to be accessed as
3447 * soon as we return to userland. Fix up the
3448 * L1 entry to avoid taking another
3449 * page/domain fault.
3451 pd_entry_t *pl1pd, l1pd;
3453 pl1pd = &pmap->pm_l1->l1_kva[L1_IDX(va)];
3454 l1pd = l2b->l2b_phys | L1_C_DOM(pmap->pm_domain) |
3456 if (*pl1pd != l1pd) {
3463 if (PV_BEEN_EXECD(oflags))
3464 pmap_tlb_flushID_SE(pmap, va);
3465 else if (PV_BEEN_REFD(oflags))
3466 pmap_tlb_flushD_SE(pmap, va);
3470 pmap_fix_cache(m, pmap, va);
3472 return (KERN_SUCCESS);
3476 * Maps a sequence of resident pages belonging to the same object.
3477 * The sequence begins with the given page m_start. This page is
3478 * mapped at the given virtual address start. Each subsequent page is
3479 * mapped at a virtual address that is offset from start by the same
3480 * amount as the page is offset from m_start within the object. The
3481 * last page in the sequence is the page with the largest offset from
3482 * m_start that can be mapped at a virtual address less than the given
3483 * virtual address end. Not every virtual page between start and end
3484 * is mapped; only those for which a resident page exists with the
3485 * corresponding offset from m_start are mapped.
3488 pmap_enter_object(pmap_t pmap, vm_offset_t start, vm_offset_t end,
3489 vm_page_t m_start, vm_prot_t prot)
3492 vm_pindex_t diff, psize;
3494 VM_OBJECT_ASSERT_LOCKED(m_start->object);
3496 psize = atop(end - start);
3498 rw_wlock(&pvh_global_lock);
3500 while (m != NULL && (diff = m->pindex - m_start->pindex) < psize) {
3501 pmap_enter_locked(pmap, start + ptoa(diff), m, prot &
3502 (VM_PROT_READ | VM_PROT_EXECUTE), PMAP_ENTER_NOSLEEP);
3503 m = TAILQ_NEXT(m, listq);
3505 rw_wunlock(&pvh_global_lock);
3510 * this code makes some *MAJOR* assumptions:
3511 * 1. Current pmap & pmap exists.
3514 * 4. No page table pages.
3515 * but is *MUCH* faster than pmap_enter...
3519 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot)
3522 rw_wlock(&pvh_global_lock);
3524 pmap_enter_locked(pmap, va, m, prot & (VM_PROT_READ | VM_PROT_EXECUTE),
3525 PMAP_ENTER_NOSLEEP);
3526 rw_wunlock(&pvh_global_lock);
3531 * Clear the wired attribute from the mappings for the specified range of
3532 * addresses in the given pmap. Every valid mapping within that range
3533 * must have the wired attribute set. In contrast, invalid mappings
3534 * cannot have the wired attribute set, so they are ignored.
3536 * XXX Wired mappings of unmanaged pages cannot be counted by this pmap
3540 pmap_unwire(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
3542 struct l2_bucket *l2b;
3543 pt_entry_t *ptep, pte;
3545 vm_offset_t next_bucket;
3548 rw_wlock(&pvh_global_lock);
3551 next_bucket = L2_NEXT_BUCKET(sva);
3552 if (next_bucket > eva)
3554 l2b = pmap_get_l2_bucket(pmap, sva);
3559 for (ptep = &l2b->l2b_kva[l2pte_index(sva)]; sva < next_bucket;
3560 sva += PAGE_SIZE, ptep++) {
3561 if ((pte = *ptep) == 0 ||
3562 (m = PHYS_TO_VM_PAGE(l2pte_pa(pte))) == NULL ||
3563 (m->oflags & VPO_UNMANAGED) != 0)
3565 pv = pmap_find_pv(m, pmap, sva);
3566 if ((pv->pv_flags & PVF_WIRED) == 0)
3567 panic("pmap_unwire: pv %p isn't wired", pv);
3568 pv->pv_flags &= ~PVF_WIRED;
3569 pmap->pm_stats.wired_count--;
3572 rw_wunlock(&pvh_global_lock);
3578 * Copy the range specified by src_addr/len
3579 * from the source map to the range dst_addr/len
3580 * in the destination map.
3582 * This routine is only advisory and need not do anything.
3585 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr,
3586 vm_size_t len, vm_offset_t src_addr)
3592 * Routine: pmap_extract
3594 * Extract the physical page address associated
3595 * with the given map/virtual_address pair.
3598 pmap_extract(pmap_t pmap, vm_offset_t va)
3603 pa = pmap_extract_locked(pmap, va);
3609 pmap_extract_locked(pmap_t pmap, vm_offset_t va)
3611 struct l2_dtable *l2;
3613 pt_entry_t *ptep, pte;
3617 if (pmap != kernel_pmap)
3618 PMAP_ASSERT_LOCKED(pmap);
3620 l1pd = pmap->pm_l1->l1_kva[l1idx];
3621 if (l1pte_section_p(l1pd)) {
3623 * These should only happen for the kernel pmap.
3625 KASSERT(pmap == kernel_pmap, ("unexpected section"));
3626 /* XXX: what to do about the bits > 32 ? */
3627 if (l1pd & L1_S_SUPERSEC)
3628 pa = (l1pd & L1_SUP_FRAME) | (va & L1_SUP_OFFSET);
3630 pa = (l1pd & L1_S_FRAME) | (va & L1_S_OFFSET);
3633 * Note that we can't rely on the validity of the L1
3634 * descriptor as an indication that a mapping exists.
3635 * We have to look it up in the L2 dtable.
3637 l2 = pmap->pm_l2[L2_IDX(l1idx)];
3639 (ptep = l2->l2_bucket[L2_BUCKET(l1idx)].l2b_kva) == NULL)
3641 pte = ptep[l2pte_index(va)];
3644 if ((pte & L2_TYPE_MASK) == L2_TYPE_L)
3645 pa = (pte & L2_L_FRAME) | (va & L2_L_OFFSET);
3647 pa = (pte & L2_S_FRAME) | (va & L2_S_OFFSET);
3653 * Atomically extract and hold the physical page with the given
3654 * pmap and virtual address pair if that mapping permits the given
3659 pmap_extract_and_hold(pmap_t pmap, vm_offset_t va, vm_prot_t prot)
3661 struct l2_dtable *l2;
3663 pt_entry_t *ptep, pte;
3664 vm_paddr_t pa, paddr;
3672 l1pd = pmap->pm_l1->l1_kva[l1idx];
3673 if (l1pte_section_p(l1pd)) {
3675 * These should only happen for kernel_pmap
3677 KASSERT(pmap == kernel_pmap, ("huh"));
3678 /* XXX: what to do about the bits > 32 ? */
3679 if (l1pd & L1_S_SUPERSEC)
3680 pa = (l1pd & L1_SUP_FRAME) | (va & L1_SUP_OFFSET);
3682 pa = (l1pd & L1_S_FRAME) | (va & L1_S_OFFSET);
3683 if (vm_page_pa_tryrelock(pmap, pa & PG_FRAME, &paddr))
3685 if (l1pd & L1_S_PROT_W || (prot & VM_PROT_WRITE) == 0) {
3686 m = PHYS_TO_VM_PAGE(pa);
3692 * Note that we can't rely on the validity of the L1
3693 * descriptor as an indication that a mapping exists.
3694 * We have to look it up in the L2 dtable.
3696 l2 = pmap->pm_l2[L2_IDX(l1idx)];
3699 (ptep = l2->l2_bucket[L2_BUCKET(l1idx)].l2b_kva) == NULL) {
3704 ptep = &ptep[l2pte_index(va)];
3711 if (pte & L2_S_PROT_W || (prot & VM_PROT_WRITE) == 0) {
3712 if ((pte & L2_TYPE_MASK) == L2_TYPE_L)
3713 pa = (pte & L2_L_FRAME) | (va & L2_L_OFFSET);
3715 pa = (pte & L2_S_FRAME) | (va & L2_S_OFFSET);
3716 if (vm_page_pa_tryrelock(pmap, pa & PG_FRAME, &paddr))
3718 m = PHYS_TO_VM_PAGE(pa);
3724 PA_UNLOCK_COND(paddr);
3729 pmap_dump_kextract(vm_offset_t va, pt2_entry_t *pte2p)
3731 struct l2_dtable *l2;
3733 pt_entry_t *ptep, pte;
3738 l1pd = kernel_pmap->pm_l1->l1_kva[l1idx];
3739 if (l1pte_section_p(l1pd)) {
3740 if (l1pd & L1_S_SUPERSEC)
3741 pa = (l1pd & L1_SUP_FRAME) | (va & L1_SUP_OFFSET);
3743 pa = (l1pd & L1_S_FRAME) | (va & L1_S_OFFSET);
3744 pte = L2_S_PROTO | pa |
3745 L2_S_PROT(PTE_KERNEL, VM_PROT_READ | VM_PROT_WRITE);
3747 l2 = kernel_pmap->pm_l2[L2_IDX(l1idx)];
3749 (ptep = l2->l2_bucket[L2_BUCKET(l1idx)].l2b_kva) == NULL) {
3754 pte = ptep[l2pte_index(va)];
3759 if ((pte & L2_TYPE_MASK) == L2_TYPE_L)
3760 pa = (pte & L2_L_FRAME) | (va & L2_L_OFFSET);
3762 pa = (pte & L2_S_FRAME) | (va & L2_S_OFFSET);
3771 * Initialize a preallocated and zeroed pmap structure,
3772 * such as one in a vmspace structure.
3776 pmap_pinit(pmap_t pmap)
3778 PDEBUG(1, printf("pmap_pinit: pmap = %08x\n", (uint32_t) pmap));
3780 pmap_alloc_l1(pmap);
3781 bzero(pmap->pm_l2, sizeof(pmap->pm_l2));
3783 CPU_ZERO(&pmap->pm_active);
3785 TAILQ_INIT(&pmap->pm_pvlist);
3786 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
3787 pmap->pm_stats.resident_count = 1;
3788 if (vector_page < KERNBASE) {
3789 pmap_enter(pmap, vector_page, PHYS_TO_VM_PAGE(systempage.pv_pa),
3790 VM_PROT_READ, PMAP_ENTER_WIRED | VM_PROT_READ, 0);
3796 /***************************************************
3797 * page management routines.
3798 ***************************************************/
3802 pmap_free_pv_entry(pv_entry_t pv)
3805 uma_zfree(pvzone, pv);
3810 * get a new pv_entry, allocating a block from the system
3812 * the memory allocation is performed bypassing the malloc code
3813 * because of the possibility of allocations at interrupt time.
3816 pmap_get_pv_entry(void)
3818 pv_entry_t ret_value;
3821 if (pv_entry_count > pv_entry_high_water)
3822 pagedaemon_wakeup(0); /* XXX ARM NUMA */
3823 ret_value = uma_zalloc(pvzone, M_NOWAIT);
3828 * Remove the given range of addresses from the specified map.
3830 * It is assumed that the start and end are properly
3831 * rounded to the page size.
3833 #define PMAP_REMOVE_CLEAN_LIST_SIZE 3
3835 pmap_remove(pmap_t pm, vm_offset_t sva, vm_offset_t eva)
3837 struct l2_bucket *l2b;
3838 vm_offset_t next_bucket;
3841 u_int mappings, is_exec, is_refd;
3846 * we lock in the pmap => pv_head direction
3849 rw_wlock(&pvh_global_lock);
3854 * Do one L2 bucket's worth at a time.
3856 next_bucket = L2_NEXT_BUCKET(sva);
3857 if (next_bucket > eva)
3860 l2b = pmap_get_l2_bucket(pm, sva);
3866 ptep = &l2b->l2b_kva[l2pte_index(sva)];
3869 while (sva < next_bucket) {
3878 * Nothing here, move along
3885 pm->pm_stats.resident_count--;
3891 * Update flags. In a number of circumstances,
3892 * we could cluster a lot of these and do a
3893 * number of sequential pages in one go.
3895 if ((pg = PHYS_TO_VM_PAGE(pa)) != NULL) {
3896 struct pv_entry *pve;
3898 pve = pmap_remove_pv(pg, pm, sva);
3900 is_exec = PV_BEEN_EXECD(pve->pv_flags);
3901 is_refd = PV_BEEN_REFD(pve->pv_flags);
3902 pmap_free_pv_entry(pve);
3906 if (l2pte_valid(pte) && pmap_is_current(pm)) {
3907 if (total < PMAP_REMOVE_CLEAN_LIST_SIZE) {
3910 cpu_idcache_wbinv_range(sva,
3912 cpu_l2cache_wbinv_range(sva,
3914 cpu_tlb_flushID_SE(sva);
3915 } else if (is_refd) {
3916 cpu_dcache_wbinv_range(sva,
3918 cpu_l2cache_wbinv_range(sva,
3920 cpu_tlb_flushD_SE(sva);
3922 } else if (total == PMAP_REMOVE_CLEAN_LIST_SIZE) {
3923 /* flushall will also only get set for
3924 * for a current pmap
3926 cpu_idcache_wbinv_all();
3927 cpu_l2cache_wbinv_all();
3940 pmap_free_l2_bucket(pm, l2b, mappings);
3943 rw_wunlock(&pvh_global_lock);
3952 * Zero a given physical page by mapping it at a page hook point.
3953 * In doing the zero page op, the page we zero is mapped cachable, as with
3954 * StrongARM accesses to non-cached pages are non-burst making writing
3955 * _any_ bulk data very slow.
3957 #if ARM_MMU_GENERIC != 0 || defined(CPU_XSCALE_CORE3)
3959 pmap_zero_page_generic(vm_paddr_t phys, int off, int size)
3962 if (_arm_bzero && size >= _min_bzero_size &&
3963 _arm_bzero((void *)(phys + off), size, IS_PHYSICAL) == 0)
3968 * Hook in the page, zero it, invalidate the TLB as needed.
3970 * Note the temporary zero-page mapping must be a non-cached page in
3971 * order to work without corruption when write-allocate is enabled.
3973 *cdst_pte = L2_S_PROTO | phys | L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE);
3975 cpu_tlb_flushD_SE(cdstp);
3977 if (off || size != PAGE_SIZE)
3978 bzero((void *)(cdstp + off), size);
3984 #endif /* ARM_MMU_GENERIC != 0 */
3986 #if ARM_MMU_XSCALE == 1
3988 pmap_zero_page_xscale(vm_paddr_t phys, int off, int size)
3991 if (_arm_bzero && size >= _min_bzero_size &&
3992 _arm_bzero((void *)(phys + off), size, IS_PHYSICAL) == 0)
3997 * Hook in the page, zero it, and purge the cache for that
3998 * zeroed page. Invalidate the TLB as needed.
4000 *cdst_pte = L2_S_PROTO | phys |
4001 L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE) |
4002 L2_C | L2_XSCALE_T_TEX(TEX_XSCALE_X); /* mini-data */
4004 cpu_tlb_flushD_SE(cdstp);
4006 if (off || size != PAGE_SIZE)
4007 bzero((void *)(cdstp + off), size);
4011 xscale_cache_clean_minidata();
4015 * Change the PTEs for the specified kernel mappings such that they
4016 * will use the mini data cache instead of the main data cache.
4019 pmap_use_minicache(vm_offset_t va, vm_size_t size)
4021 struct l2_bucket *l2b;
4022 pt_entry_t *ptep, *sptep, pte;
4023 vm_offset_t next_bucket, eva;
4025 #if (ARM_NMMUS > 1) || defined(CPU_XSCALE_CORE3)
4026 if (xscale_use_minidata == 0)
4033 next_bucket = L2_NEXT_BUCKET(va);
4034 if (next_bucket > eva)
4037 l2b = pmap_get_l2_bucket(kernel_pmap, va);
4039 sptep = ptep = &l2b->l2b_kva[l2pte_index(va)];
4041 while (va < next_bucket) {
4043 if (!l2pte_minidata(pte)) {
4044 cpu_dcache_wbinv_range(va, PAGE_SIZE);
4045 cpu_tlb_flushD_SE(va);
4046 *ptep = pte & ~L2_B;
4051 PTE_SYNC_RANGE(sptep, (u_int)(ptep - sptep));
4055 #endif /* ARM_MMU_XSCALE == 1 */
4058 * pmap_zero_page zeros the specified hardware page by mapping
4059 * the page into KVM and using bzero to clear its contents.
4062 pmap_zero_page(vm_page_t m)
4064 pmap_zero_page_func(VM_PAGE_TO_PHYS(m), 0, PAGE_SIZE);
4069 * pmap_zero_page_area zeros the specified hardware page by mapping
4070 * the page into KVM and using bzero to clear its contents.
4072 * off and size may not cover an area beyond a single hardware page.
4075 pmap_zero_page_area(vm_page_t m, int off, int size)
4078 pmap_zero_page_func(VM_PAGE_TO_PHYS(m), off, size);
4086 * This is a local function used to work out the best strategy to clean
4087 * a single page referenced by its entry in the PV table. It should be used by
4088 * pmap_copy_page, pmap_zero page and maybe some others later on.
4090 * Its policy is effectively:
4091 * o If there are no mappings, we don't bother doing anything with the cache.
4092 * o If there is one mapping, we clean just that page.
4093 * o If there are multiple mappings, we clean the entire cache.
4095 * So that some functions can be further optimised, it returns 0 if it didn't
4096 * clean the entire cache, or 1 if it did.
4098 * XXX One bug in this routine is that if the pv_entry has a single page
4099 * mapped at 0x00000000 a whole cache clean will be performed rather than
4100 * just the 1 page. Since this should not occur in everyday use and if it does
4101 * it will just result in not the most efficient clean for the page.
4103 * We don't yet use this function but may want to.
4106 pmap_clean_page(struct pv_entry *pv, boolean_t is_src)
4108 pmap_t pm, pm_to_clean = NULL;
4109 struct pv_entry *npv;
4110 u_int cache_needs_cleaning = 0;
4112 vm_offset_t page_to_clean = 0;
4115 /* nothing mapped in so nothing to flush */
4120 * Since we flush the cache each time we change to a different
4121 * user vmspace, we only need to flush the page if it is in the
4125 pm = vmspace_pmap(curproc->p_vmspace);
4129 for (npv = pv; npv; npv = TAILQ_NEXT(npv, pv_list)) {
4130 if (npv->pv_pmap == kernel_pmap || npv->pv_pmap == pm) {
4131 flags |= npv->pv_flags;
4133 * The page is mapped non-cacheable in
4134 * this map. No need to flush the cache.
4136 if (npv->pv_flags & PVF_NC) {
4138 if (cache_needs_cleaning)
4139 panic("pmap_clean_page: "
4140 "cache inconsistency");
4143 } else if (is_src && (npv->pv_flags & PVF_WRITE) == 0)
4145 if (cache_needs_cleaning) {
4149 page_to_clean = npv->pv_va;
4150 pm_to_clean = npv->pv_pmap;
4152 cache_needs_cleaning = 1;
4155 if (page_to_clean) {
4156 if (PV_BEEN_EXECD(flags))
4157 pmap_idcache_wbinv_range(pm_to_clean, page_to_clean,
4160 pmap_dcache_wb_range(pm_to_clean, page_to_clean,
4161 PAGE_SIZE, !is_src, (flags & PVF_WRITE) == 0);
4162 } else if (cache_needs_cleaning) {
4163 if (PV_BEEN_EXECD(flags))
4164 pmap_idcache_wbinv_all(pm);
4166 pmap_dcache_wbinv_all(pm);
4174 * pmap_copy_page copies the specified (machine independent)
4175 * page by mapping the page into virtual memory and using
4176 * bcopy to copy the page, one machine dependent page at a
4183 * Copy one physical page into another, by mapping the pages into
4184 * hook points. The same comment regarding cachability as in
4185 * pmap_zero_page also applies here.
4187 #if ARM_MMU_GENERIC != 0 || defined (CPU_XSCALE_CORE3)
4189 pmap_copy_page_generic(vm_paddr_t src, vm_paddr_t dst)
4192 struct vm_page *src_pg = PHYS_TO_VM_PAGE(src);
4196 * Clean the source page. Hold the source page's lock for
4197 * the duration of the copy so that no other mappings can
4198 * be created while we have a potentially aliased mapping.
4202 * XXX: Not needed while we call cpu_dcache_wbinv_all() in
4205 (void) pmap_clean_page(TAILQ_FIRST(&src_pg->md.pv_list), TRUE);
4208 * Map the pages into the page hook points, copy them, and purge
4209 * the cache for the appropriate page. Invalidate the TLB
4213 *csrc_pte = L2_S_PROTO | src |
4214 L2_S_PROT(PTE_KERNEL, VM_PROT_READ) | pte_l2_s_cache_mode;
4216 *cdst_pte = L2_S_PROTO | dst |
4217 L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE) | pte_l2_s_cache_mode;
4219 cpu_tlb_flushD_SE(csrcp);
4220 cpu_tlb_flushD_SE(cdstp);
4222 bcopy_page(csrcp, cdstp);
4224 cpu_dcache_inv_range(csrcp, PAGE_SIZE);
4225 cpu_dcache_wbinv_range(cdstp, PAGE_SIZE);
4226 cpu_l2cache_inv_range(csrcp, PAGE_SIZE);
4227 cpu_l2cache_wbinv_range(cdstp, PAGE_SIZE);
4231 pmap_copy_page_offs_generic(vm_paddr_t a_phys, vm_offset_t a_offs,
4232 vm_paddr_t b_phys, vm_offset_t b_offs, int cnt)
4236 *csrc_pte = L2_S_PROTO | a_phys |
4237 L2_S_PROT(PTE_KERNEL, VM_PROT_READ) | pte_l2_s_cache_mode;
4239 *cdst_pte = L2_S_PROTO | b_phys |
4240 L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE) | pte_l2_s_cache_mode;
4242 cpu_tlb_flushD_SE(csrcp);
4243 cpu_tlb_flushD_SE(cdstp);
4245 bcopy((char *)csrcp + a_offs, (char *)cdstp + b_offs, cnt);
4247 cpu_dcache_inv_range(csrcp + a_offs, cnt);
4248 cpu_dcache_wbinv_range(cdstp + b_offs, cnt);
4249 cpu_l2cache_inv_range(csrcp + a_offs, cnt);
4250 cpu_l2cache_wbinv_range(cdstp + b_offs, cnt);
4252 #endif /* ARM_MMU_GENERIC != 0 */
4254 #if ARM_MMU_XSCALE == 1
4256 pmap_copy_page_xscale(vm_paddr_t src, vm_paddr_t dst)
4259 /* XXX: Only needed for pmap_clean_page(), which is commented out. */
4260 struct vm_page *src_pg = PHYS_TO_VM_PAGE(src);
4264 * Clean the source page. Hold the source page's lock for
4265 * the duration of the copy so that no other mappings can
4266 * be created while we have a potentially aliased mapping.
4270 * XXX: Not needed while we call cpu_dcache_wbinv_all() in
4273 (void) pmap_clean_page(TAILQ_FIRST(&src_pg->md.pv_list), TRUE);
4276 * Map the pages into the page hook points, copy them, and purge
4277 * the cache for the appropriate page. Invalidate the TLB
4281 *csrc_pte = L2_S_PROTO | src |
4282 L2_S_PROT(PTE_KERNEL, VM_PROT_READ) |
4283 L2_C | L2_XSCALE_T_TEX(TEX_XSCALE_X); /* mini-data */
4285 *cdst_pte = L2_S_PROTO | dst |
4286 L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE) |
4287 L2_C | L2_XSCALE_T_TEX(TEX_XSCALE_X); /* mini-data */
4289 cpu_tlb_flushD_SE(csrcp);
4290 cpu_tlb_flushD_SE(cdstp);
4292 bcopy_page(csrcp, cdstp);
4294 xscale_cache_clean_minidata();
4298 pmap_copy_page_offs_xscale(vm_paddr_t a_phys, vm_offset_t a_offs,
4299 vm_paddr_t b_phys, vm_offset_t b_offs, int cnt)
4303 *csrc_pte = L2_S_PROTO | a_phys |
4304 L2_S_PROT(PTE_KERNEL, VM_PROT_READ) |
4305 L2_C | L2_XSCALE_T_TEX(TEX_XSCALE_X);
4307 *cdst_pte = L2_S_PROTO | b_phys |
4308 L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE) |
4309 L2_C | L2_XSCALE_T_TEX(TEX_XSCALE_X);
4311 cpu_tlb_flushD_SE(csrcp);
4312 cpu_tlb_flushD_SE(cdstp);
4314 bcopy((char *)csrcp + a_offs, (char *)cdstp + b_offs, cnt);
4316 xscale_cache_clean_minidata();
4318 #endif /* ARM_MMU_XSCALE == 1 */
4321 pmap_copy_page(vm_page_t src, vm_page_t dst)
4324 cpu_dcache_wbinv_all();
4325 cpu_l2cache_wbinv_all();
4326 if (_arm_memcpy && PAGE_SIZE >= _min_memcpy_size &&
4327 _arm_memcpy((void *)VM_PAGE_TO_PHYS(dst),
4328 (void *)VM_PAGE_TO_PHYS(src), PAGE_SIZE, IS_PHYSICAL) == 0)
4330 pmap_copy_page_func(VM_PAGE_TO_PHYS(src), VM_PAGE_TO_PHYS(dst));
4334 * We have code to do unmapped I/O. However, it isn't quite right and
4335 * causes un-page-aligned I/O to devices to fail (most notably newfs
4336 * or fsck). We give up a little performance to not allow unmapped I/O
4337 * to gain stability.
4339 int unmapped_buf_allowed = 0;
4342 pmap_copy_pages(vm_page_t ma[], vm_offset_t a_offset, vm_page_t mb[],
4343 vm_offset_t b_offset, int xfersize)
4345 vm_page_t a_pg, b_pg;
4346 vm_offset_t a_pg_offset, b_pg_offset;
4349 cpu_dcache_wbinv_all();
4350 cpu_l2cache_wbinv_all();
4351 while (xfersize > 0) {
4352 a_pg = ma[a_offset >> PAGE_SHIFT];
4353 a_pg_offset = a_offset & PAGE_MASK;
4354 cnt = min(xfersize, PAGE_SIZE - a_pg_offset);
4355 b_pg = mb[b_offset >> PAGE_SHIFT];
4356 b_pg_offset = b_offset & PAGE_MASK;
4357 cnt = min(cnt, PAGE_SIZE - b_pg_offset);
4358 pmap_copy_page_offs_func(VM_PAGE_TO_PHYS(a_pg), a_pg_offset,
4359 VM_PAGE_TO_PHYS(b_pg), b_pg_offset, cnt);
4367 pmap_quick_enter_page(vm_page_t m)
4370 * Don't bother with a PCPU pageframe, since we don't support
4371 * SMP for anything pre-armv7. Use pmap_kenter() to ensure
4372 * caching is handled correctly for multiple mappings of the
4373 * same physical page.
4376 mtx_assert(&qmap_mtx, MA_NOTOWNED);
4377 mtx_lock(&qmap_mtx);
4379 pmap_kenter(qmap_addr, VM_PAGE_TO_PHYS(m));
4385 pmap_quick_remove_page(vm_offset_t addr)
4387 KASSERT(addr == qmap_addr,
4388 ("pmap_quick_remove_page: invalid address"));
4389 mtx_assert(&qmap_mtx, MA_OWNED);
4391 mtx_unlock(&qmap_mtx);
4395 * this routine returns true if a physical page resides
4396 * in the given pmap.
4399 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
4405 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4406 ("pmap_page_exists_quick: page %p is not managed", m));
4408 rw_wlock(&pvh_global_lock);
4409 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
4410 if (pv->pv_pmap == pmap) {
4418 rw_wunlock(&pvh_global_lock);
4423 * pmap_page_wired_mappings:
4425 * Return the number of managed mappings to the given physical page
4429 pmap_page_wired_mappings(vm_page_t m)
4435 if ((m->oflags & VPO_UNMANAGED) != 0)
4437 rw_wlock(&pvh_global_lock);
4438 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list)
4439 if ((pv->pv_flags & PVF_WIRED) != 0)
4441 rw_wunlock(&pvh_global_lock);
4446 * This function is advisory.
4449 pmap_advise(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, int advice)
4454 * pmap_ts_referenced:
4456 * Return the count of reference bits for a page, clearing all of them.
4459 pmap_ts_referenced(vm_page_t m)
4462 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4463 ("pmap_ts_referenced: page %p is not managed", m));
4464 return (pmap_clearbit(m, PVF_REF));
4469 pmap_is_modified(vm_page_t m)
4472 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4473 ("pmap_is_modified: page %p is not managed", m));
4474 if (m->md.pvh_attrs & PVF_MOD)
4482 * Clear the modify bits on the specified physical page.
4485 pmap_clear_modify(vm_page_t m)
4488 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4489 ("pmap_clear_modify: page %p is not managed", m));
4490 VM_OBJECT_ASSERT_WLOCKED(m->object);
4491 KASSERT(!vm_page_xbusied(m),
4492 ("pmap_clear_modify: page %p is exclusive busied", m));
4495 * If the page is not PGA_WRITEABLE, then no mappings can be modified.
4496 * If the object containing the page is locked and the page is not
4497 * exclusive busied, then PGA_WRITEABLE cannot be concurrently set.
4499 if ((m->aflags & PGA_WRITEABLE) == 0)
4501 if (m->md.pvh_attrs & PVF_MOD)
4502 pmap_clearbit(m, PVF_MOD);
4507 * pmap_is_referenced:
4509 * Return whether or not the specified physical page was referenced
4510 * in any physical maps.
4513 pmap_is_referenced(vm_page_t m)
4516 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4517 ("pmap_is_referenced: page %p is not managed", m));
4518 return ((m->md.pvh_attrs & PVF_REF) != 0);
4523 * Clear the write and modified bits in each of the given page's mappings.
4526 pmap_remove_write(vm_page_t m)
4529 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4530 ("pmap_remove_write: page %p is not managed", m));
4533 * If the page is not exclusive busied, then PGA_WRITEABLE cannot be
4534 * set by another thread while the object is locked. Thus,
4535 * if PGA_WRITEABLE is clear, no page table entries need updating.
4537 VM_OBJECT_ASSERT_WLOCKED(m->object);
4538 if (vm_page_xbusied(m) || (m->aflags & PGA_WRITEABLE) != 0)
4539 pmap_clearbit(m, PVF_WRITE);
4544 * perform the pmap work for mincore
4547 pmap_mincore(pmap_t pmap, vm_offset_t addr, vm_paddr_t *locked_pa)
4549 struct l2_bucket *l2b;
4550 pt_entry_t *ptep, pte;
4558 l2b = pmap_get_l2_bucket(pmap, addr);
4563 ptep = &l2b->l2b_kva[l2pte_index(addr)];
4565 if (!l2pte_valid(pte)) {
4569 val = MINCORE_INCORE;
4570 if (pte & L2_S_PROT_W)
4571 val |= MINCORE_MODIFIED | MINCORE_MODIFIED_OTHER;
4574 m = PHYS_TO_VM_PAGE(pa);
4575 if (m != NULL && !(m->oflags & VPO_UNMANAGED))
4579 * The ARM pmap tries to maintain a per-mapping
4580 * reference bit. The trouble is that it's kept in
4581 * the PV entry, not the PTE, so it's costly to access
4582 * here. You would need to acquire the pvh global
4583 * lock, call pmap_find_pv(), and introduce a custom
4584 * version of vm_page_pa_tryrelock() that releases and
4585 * reacquires the pvh global lock. In the end, I
4586 * doubt it's worthwhile. This may falsely report
4587 * the given address as referenced.
4589 if ((m->md.pvh_attrs & PVF_REF) != 0)
4590 val |= MINCORE_REFERENCED | MINCORE_REFERENCED_OTHER;
4592 if ((val & (MINCORE_MODIFIED_OTHER | MINCORE_REFERENCED_OTHER)) !=
4593 (MINCORE_MODIFIED_OTHER | MINCORE_REFERENCED_OTHER) && managed) {
4594 /* Ensure that "PHYS_TO_VM_PAGE(pa)->object" doesn't change. */
4595 if (vm_page_pa_tryrelock(pmap, pa, locked_pa))
4599 PA_UNLOCK_COND(*locked_pa);
4606 pmap_sync_icache(pmap_t pm, vm_offset_t va, vm_size_t sz)
4612 * Increase the starting virtual address of the given mapping if a
4613 * different alignment might result in more superpage mappings.
4616 pmap_align_superpage(vm_object_t object, vm_ooffset_t offset,
4617 vm_offset_t *addr, vm_size_t size)
4621 #define BOOTSTRAP_DEBUG
4626 * Create a single section mapping.
4629 pmap_map_section(vm_offset_t l1pt, vm_offset_t va, vm_offset_t pa,
4630 int prot, int cache)
4632 pd_entry_t *pde = (pd_entry_t *) l1pt;
4635 KASSERT(((va | pa) & L1_S_OFFSET) == 0, ("ouin2"));
4644 fl = pte_l1_s_cache_mode;
4648 fl = pte_l1_s_cache_mode_pt;
4652 pde[va >> L1_S_SHIFT] = L1_S_PROTO | pa |
4653 L1_S_PROT(PTE_KERNEL, prot) | fl | L1_S_DOM(PMAP_DOMAIN_KERNEL);
4654 PTE_SYNC(&pde[va >> L1_S_SHIFT]);
4661 * Link the L2 page table specified by l2pv.pv_pa into the L1
4662 * page table at the slot for "va".
4665 pmap_link_l2pt(vm_offset_t l1pt, vm_offset_t va, struct pv_addr *l2pv)
4667 pd_entry_t *pde = (pd_entry_t *) l1pt, proto;
4668 u_int slot = va >> L1_S_SHIFT;
4670 proto = L1_S_DOM(PMAP_DOMAIN_KERNEL) | L1_C_PROTO;
4672 #ifdef VERBOSE_INIT_ARM
4673 printf("pmap_link_l2pt: pa=0x%x va=0x%x\n", l2pv->pv_pa, l2pv->pv_va);
4676 pde[slot + 0] = proto | (l2pv->pv_pa + 0x000);
4678 PTE_SYNC(&pde[slot]);
4680 SLIST_INSERT_HEAD(&kernel_pt_list, l2pv, pv_list);
4688 * Create a single page mapping.
4691 pmap_map_entry(vm_offset_t l1pt, vm_offset_t va, vm_offset_t pa, int prot,
4694 pd_entry_t *pde = (pd_entry_t *) l1pt;
4698 KASSERT(((va | pa) & PAGE_MASK) == 0, ("ouin"));
4707 fl = pte_l2_s_cache_mode;
4711 fl = pte_l2_s_cache_mode_pt;
4715 if ((pde[va >> L1_S_SHIFT] & L1_TYPE_MASK) != L1_TYPE_C)
4716 panic("pmap_map_entry: no L2 table for VA 0x%08x", va);
4718 pte = (pt_entry_t *) kernel_pt_lookup(pde[L1_IDX(va)] & L1_C_ADDR_MASK);
4721 panic("pmap_map_entry: can't find L2 table for VA 0x%08x", va);
4723 pte[l2pte_index(va)] =
4724 L2_S_PROTO | pa | L2_S_PROT(PTE_KERNEL, prot) | fl;
4725 PTE_SYNC(&pte[l2pte_index(va)]);
4731 * Map a chunk of memory using the most efficient mappings
4732 * possible (section. large page, small page) into the
4733 * provided L1 and L2 tables at the specified virtual address.
4736 pmap_map_chunk(vm_offset_t l1pt, vm_offset_t va, vm_offset_t pa,
4737 vm_size_t size, int prot, int cache)
4739 pd_entry_t *pde = (pd_entry_t *) l1pt;
4740 pt_entry_t *pte, f1, f2s, f2l;
4744 resid = roundup2(size, PAGE_SIZE);
4747 panic("pmap_map_chunk: no L1 table provided");
4749 #ifdef VERBOSE_INIT_ARM
4750 printf("pmap_map_chunk: pa=0x%x va=0x%x size=0x%x resid=0x%x "
4751 "prot=0x%x cache=%d\n", pa, va, size, resid, prot, cache);
4763 f1 = pte_l1_s_cache_mode;
4764 f2l = pte_l2_l_cache_mode;
4765 f2s = pte_l2_s_cache_mode;
4769 f1 = pte_l1_s_cache_mode_pt;
4770 f2l = pte_l2_l_cache_mode_pt;
4771 f2s = pte_l2_s_cache_mode_pt;
4778 /* See if we can use a section mapping. */
4779 if (L1_S_MAPPABLE_P(va, pa, resid)) {
4780 #ifdef VERBOSE_INIT_ARM
4783 pde[va >> L1_S_SHIFT] = L1_S_PROTO | pa |
4784 L1_S_PROT(PTE_KERNEL, prot) | f1 |
4785 L1_S_DOM(PMAP_DOMAIN_KERNEL);
4786 PTE_SYNC(&pde[va >> L1_S_SHIFT]);
4794 * Ok, we're going to use an L2 table. Make sure
4795 * one is actually in the corresponding L1 slot
4796 * for the current VA.
4798 if ((pde[va >> L1_S_SHIFT] & L1_TYPE_MASK) != L1_TYPE_C)
4799 panic("pmap_map_chunk: no L2 table for VA 0x%08x", va);
4801 pte = (pt_entry_t *) kernel_pt_lookup(
4802 pde[L1_IDX(va)] & L1_C_ADDR_MASK);
4804 panic("pmap_map_chunk: can't find L2 table for VA"
4806 /* See if we can use a L2 large page mapping. */
4807 if (L2_L_MAPPABLE_P(va, pa, resid)) {
4808 #ifdef VERBOSE_INIT_ARM
4811 for (i = 0; i < 16; i++) {
4812 pte[l2pte_index(va) + i] =
4814 L2_L_PROT(PTE_KERNEL, prot) | f2l;
4815 PTE_SYNC(&pte[l2pte_index(va) + i]);
4823 /* Use a small page mapping. */
4824 #ifdef VERBOSE_INIT_ARM
4827 pte[l2pte_index(va)] =
4828 L2_S_PROTO | pa | L2_S_PROT(PTE_KERNEL, prot) | f2s;
4829 PTE_SYNC(&pte[l2pte_index(va)]);
4834 #ifdef VERBOSE_INIT_ARM
4842 pmap_page_set_memattr(vm_page_t m, vm_memattr_t ma)
4845 * Remember the memattr in a field that gets used to set the appropriate
4846 * bits in the PTEs as mappings are established.
4848 m->md.pv_memattr = ma;
4851 * It appears that this function can only be called before any mappings
4852 * for the page are established on ARM. If this ever changes, this code
4853 * will need to walk the pv_list and make each of the existing mappings
4854 * uncacheable, being careful to sync caches and PTEs (and maybe
4855 * invalidate TLB?) for any current mapping it modifies.
4857 if (m->md.pv_kva != 0 || TAILQ_FIRST(&m->md.pv_list) != NULL)
4858 panic("Can't change memattr on page with existing mappings");