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.
80 * 3. All advertising materials mentioning features or use of this software
81 * must display the following acknowledgement:
82 * This product includes software developed by the NetBSD
83 * Foundation, Inc. and its contributors.
84 * 4. Neither the name of The NetBSD Foundation nor the names of its
85 * contributors may be used to endorse or promote products derived
86 * from this software without specific prior written permission.
88 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
89 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
90 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
91 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
92 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
93 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
94 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
95 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
96 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
97 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
98 * POSSIBILITY OF SUCH DAMAGE.
102 * Copyright (c) 1994-1998 Mark Brinicombe.
103 * Copyright (c) 1994 Brini.
104 * All rights reserved.
106 * This code is derived from software written for Brini by Mark Brinicombe
108 * Redistribution and use in source and binary forms, with or without
109 * modification, are permitted provided that the following conditions
111 * 1. Redistributions of source code must retain the above copyright
112 * notice, this list of conditions and the following disclaimer.
113 * 2. Redistributions in binary form must reproduce the above copyright
114 * notice, this list of conditions and the following disclaimer in the
115 * documentation and/or other materials provided with the distribution.
116 * 3. All advertising materials mentioning features or use of this software
117 * must display the following acknowledgement:
118 * This product includes software developed by Mark Brinicombe.
119 * 4. The name of the author may not be used to endorse or promote products
120 * derived from this software without specific prior written permission.
122 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
123 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
124 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
125 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
126 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
127 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
128 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
129 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
130 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
132 * RiscBSD kernel project
136 * Machine dependant vm stuff
142 * Special compilation symbols
143 * PMAP_DEBUG - Build in pmap_debug_level code
145 /* Include header files */
149 #include <sys/cdefs.h>
150 __FBSDID("$FreeBSD$");
151 #include <sys/param.h>
152 #include <sys/systm.h>
153 #include <sys/kernel.h>
154 #include <sys/proc.h>
155 #include <sys/malloc.h>
156 #include <sys/msgbuf.h>
157 #include <sys/vmmeter.h>
158 #include <sys/mman.h>
160 #include <sys/sched.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_extern.h>
171 #include <sys/lock.h>
172 #include <sys/mutex.h>
173 #include <machine/md_var.h>
174 #include <machine/vmparam.h>
175 #include <machine/cpu.h>
176 #include <machine/cpufunc.h>
177 #include <machine/pcb.h>
180 #define PDEBUG(_lev_,_stat_) \
181 if (pmap_debug_level >= (_lev_)) \
183 #define dprintf printf
185 int pmap_debug_level = 0;
187 #else /* PMAP_DEBUG */
188 #define PDEBUG(_lev_,_stat_) /* Nothing */
189 #define dprintf(x, arg...)
190 #define PMAP_INLINE __inline
191 #endif /* PMAP_DEBUG */
193 extern struct pv_addr systempage;
195 * Internal function prototypes
197 static void pmap_free_pv_entry (pv_entry_t);
198 static pv_entry_t pmap_get_pv_entry(void);
200 static void pmap_enter_locked(pmap_t, vm_offset_t, vm_page_t,
201 vm_prot_t, boolean_t, int);
202 static void pmap_vac_me_harder(struct vm_page *, pmap_t,
204 static void pmap_vac_me_kpmap(struct vm_page *, pmap_t,
206 static void pmap_vac_me_user(struct vm_page *, pmap_t, vm_offset_t);
207 static void pmap_alloc_l1(pmap_t);
208 static void pmap_free_l1(pmap_t);
209 static void pmap_use_l1(pmap_t);
211 static int pmap_clearbit(struct vm_page *, u_int);
213 static struct l2_bucket *pmap_get_l2_bucket(pmap_t, vm_offset_t);
214 static struct l2_bucket *pmap_alloc_l2_bucket(pmap_t, vm_offset_t);
215 static void pmap_free_l2_bucket(pmap_t, struct l2_bucket *, u_int);
216 static vm_offset_t kernel_pt_lookup(vm_paddr_t);
218 static MALLOC_DEFINE(M_VMPMAP, "pmap", "PMAP L1");
220 vm_offset_t virtual_avail; /* VA of first avail page (after kernel bss) */
221 vm_offset_t virtual_end; /* VA of last avail page (end of kernel AS) */
222 vm_offset_t pmap_curmaxkvaddr;
223 vm_paddr_t kernel_l1pa;
226 vm_offset_t kernel_vm_end = 0;
228 struct pmap kernel_pmap_store;
231 static pt_entry_t *csrc_pte, *cdst_pte;
232 static vm_offset_t csrcp, cdstp;
233 static struct mtx cmtx;
235 static void pmap_init_l1(struct l1_ttable *, pd_entry_t *);
237 * These routines are called when the CPU type is identified to set up
238 * the PTE prototypes, cache modes, etc.
240 * The variables are always here, just in case LKMs need to reference
241 * them (though, they shouldn't).
244 pt_entry_t pte_l1_s_cache_mode;
245 pt_entry_t pte_l1_s_cache_mode_pt;
246 pt_entry_t pte_l1_s_cache_mask;
248 pt_entry_t pte_l2_l_cache_mode;
249 pt_entry_t pte_l2_l_cache_mode_pt;
250 pt_entry_t pte_l2_l_cache_mask;
252 pt_entry_t pte_l2_s_cache_mode;
253 pt_entry_t pte_l2_s_cache_mode_pt;
254 pt_entry_t pte_l2_s_cache_mask;
256 pt_entry_t pte_l2_s_prot_u;
257 pt_entry_t pte_l2_s_prot_w;
258 pt_entry_t pte_l2_s_prot_mask;
260 pt_entry_t pte_l1_s_proto;
261 pt_entry_t pte_l1_c_proto;
262 pt_entry_t pte_l2_s_proto;
264 void (*pmap_copy_page_func)(vm_paddr_t, vm_paddr_t);
265 void (*pmap_zero_page_func)(vm_paddr_t, int, int);
267 * Which pmap is currently 'live' in the cache
269 * XXXSCW: Fix for SMP ...
271 union pmap_cache_state *pmap_cache_state;
273 struct msgbuf *msgbufp = 0;
275 extern void bcopy_page(vm_offset_t, vm_offset_t);
276 extern void bzero_page(vm_offset_t);
278 extern vm_offset_t alloc_firstaddr;
283 * Metadata for L1 translation tables.
286 /* Entry on the L1 Table list */
287 SLIST_ENTRY(l1_ttable) l1_link;
289 /* Entry on the L1 Least Recently Used list */
290 TAILQ_ENTRY(l1_ttable) l1_lru;
292 /* Track how many domains are allocated from this L1 */
293 volatile u_int l1_domain_use_count;
296 * A free-list of domain numbers for this L1.
297 * We avoid using ffs() and a bitmap to track domains since ffs()
300 u_int8_t l1_domain_first;
301 u_int8_t l1_domain_free[PMAP_DOMAINS];
303 /* Physical address of this L1 page table */
304 vm_paddr_t l1_physaddr;
306 /* KVA of this L1 page table */
311 * Convert a virtual address into its L1 table index. That is, the
312 * index used to locate the L2 descriptor table pointer in an L1 table.
313 * This is basically used to index l1->l1_kva[].
315 * Each L2 descriptor table represents 1MB of VA space.
317 #define L1_IDX(va) (((vm_offset_t)(va)) >> L1_S_SHIFT)
320 * L1 Page Tables are tracked using a Least Recently Used list.
321 * - New L1s are allocated from the HEAD.
322 * - Freed L1s are added to the TAIl.
323 * - Recently accessed L1s (where an 'access' is some change to one of
324 * the userland pmaps which owns this L1) are moved to the TAIL.
326 static TAILQ_HEAD(, l1_ttable) l1_lru_list;
328 * A list of all L1 tables
330 static SLIST_HEAD(, l1_ttable) l1_list;
331 static struct mtx l1_lru_lock;
334 * The l2_dtable tracks L2_BUCKET_SIZE worth of L1 slots.
336 * This is normally 16MB worth L2 page descriptors for any given pmap.
337 * Reference counts are maintained for L2 descriptors so they can be
341 /* The number of L2 page descriptors allocated to this l2_dtable */
344 /* List of L2 page descriptors */
346 pt_entry_t *l2b_kva; /* KVA of L2 Descriptor Table */
347 vm_paddr_t l2b_phys; /* Physical address of same */
348 u_short l2b_l1idx; /* This L2 table's L1 index */
349 u_short l2b_occupancy; /* How many active descriptors */
350 } l2_bucket[L2_BUCKET_SIZE];
353 /* pmap_kenter_internal flags */
354 #define KENTER_CACHE 0x1
355 #define KENTER_USER 0x2
358 * Given an L1 table index, calculate the corresponding l2_dtable index
359 * and bucket index within the l2_dtable.
361 #define L2_IDX(l1idx) (((l1idx) >> L2_BUCKET_LOG2) & \
363 #define L2_BUCKET(l1idx) ((l1idx) & (L2_BUCKET_SIZE - 1))
366 * Given a virtual address, this macro returns the
367 * virtual address required to drop into the next L2 bucket.
369 #define L2_NEXT_BUCKET(va) (((va) & L1_S_FRAME) + L1_S_SIZE)
374 #define pmap_alloc_l2_dtable() \
375 (void*)uma_zalloc(l2table_zone, M_NOWAIT|M_USE_RESERVE)
376 #define pmap_free_l2_dtable(l2) \
377 uma_zfree(l2table_zone, l2)
380 * We try to map the page tables write-through, if possible. However, not
381 * all CPUs have a write-through cache mode, so on those we have to sync
382 * the cache when we frob page tables.
384 * We try to evaluate this at compile time, if possible. However, it's
385 * not always possible to do that, hence this run-time var.
387 int pmap_needs_pte_sync;
390 * Macro to determine if a mapping might be resident in the
391 * instruction cache and/or TLB
393 #define PV_BEEN_EXECD(f) (((f) & (PVF_REF | PVF_EXEC)) == (PVF_REF | PVF_EXEC))
396 * Macro to determine if a mapping might be resident in the
397 * data cache and/or TLB
399 #define PV_BEEN_REFD(f) (((f) & PVF_REF) != 0)
401 #ifndef PMAP_SHPGPERPROC
402 #define PMAP_SHPGPERPROC 200
405 #define pmap_is_current(pm) ((pm) == pmap_kernel() || \
406 curproc->p_vmspace->vm_map.pmap == (pm))
407 static uma_zone_t pvzone;
409 static uma_zone_t l2table_zone;
410 static vm_offset_t pmap_kernel_l2dtable_kva;
411 static vm_offset_t pmap_kernel_l2ptp_kva;
412 static vm_paddr_t pmap_kernel_l2ptp_phys;
413 static struct vm_object pvzone_obj;
414 static int pv_entry_count=0, pv_entry_max=0, pv_entry_high_water=0;
417 * This list exists for the benefit of pmap_map_chunk(). It keeps track
418 * of the kernel L2 tables during bootstrap, so that pmap_map_chunk() can
419 * find them as necessary.
421 * Note that the data on this list MUST remain valid after initarm() returns,
422 * as pmap_bootstrap() uses it to contruct L2 table metadata.
424 SLIST_HEAD(, pv_addr) kernel_pt_list = SLIST_HEAD_INITIALIZER(kernel_pt_list);
427 pmap_init_l1(struct l1_ttable *l1, pd_entry_t *l1pt)
432 l1->l1_domain_use_count = 0;
433 l1->l1_domain_first = 0;
435 for (i = 0; i < PMAP_DOMAINS; i++)
436 l1->l1_domain_free[i] = i + 1;
439 * Copy the kernel's L1 entries to each new L1.
441 if (l1pt != pmap_kernel()->pm_l1->l1_kva)
442 memcpy(l1pt, pmap_kernel()->pm_l1->l1_kva, L1_TABLE_SIZE);
444 if ((l1->l1_physaddr = pmap_extract(pmap_kernel(), (vm_offset_t)l1pt)) == 0)
445 panic("pmap_init_l1: can't get PA of L1 at %p", l1pt);
446 SLIST_INSERT_HEAD(&l1_list, l1, l1_link);
447 TAILQ_INSERT_TAIL(&l1_lru_list, l1, l1_lru);
451 kernel_pt_lookup(vm_paddr_t pa)
455 SLIST_FOREACH(pv, &kernel_pt_list, pv_list) {
462 #if (ARM_MMU_GENERIC + ARM_MMU_SA1) != 0
464 pmap_pte_init_generic(void)
467 pte_l1_s_cache_mode = L1_S_B|L1_S_C;
468 pte_l1_s_cache_mask = L1_S_CACHE_MASK_generic;
470 pte_l2_l_cache_mode = L2_B|L2_C;
471 pte_l2_l_cache_mask = L2_L_CACHE_MASK_generic;
473 pte_l2_s_cache_mode = L2_B|L2_C;
474 pte_l2_s_cache_mask = L2_S_CACHE_MASK_generic;
477 * If we have a write-through cache, set B and C. If
478 * we have a write-back cache, then we assume setting
479 * only C will make those pages write-through.
481 if (cpufuncs.cf_dcache_wb_range == (void *) cpufunc_nullop) {
482 pte_l1_s_cache_mode_pt = L1_S_B|L1_S_C;
483 pte_l2_l_cache_mode_pt = L2_B|L2_C;
484 pte_l2_s_cache_mode_pt = L2_B|L2_C;
486 pte_l1_s_cache_mode_pt = L1_S_C;
487 pte_l2_l_cache_mode_pt = L2_C;
488 pte_l2_s_cache_mode_pt = L2_C;
491 pte_l2_s_prot_u = L2_S_PROT_U_generic;
492 pte_l2_s_prot_w = L2_S_PROT_W_generic;
493 pte_l2_s_prot_mask = L2_S_PROT_MASK_generic;
495 pte_l1_s_proto = L1_S_PROTO_generic;
496 pte_l1_c_proto = L1_C_PROTO_generic;
497 pte_l2_s_proto = L2_S_PROTO_generic;
499 pmap_copy_page_func = pmap_copy_page_generic;
500 pmap_zero_page_func = pmap_zero_page_generic;
503 #if defined(CPU_ARM8)
505 pmap_pte_init_arm8(void)
509 * ARM8 is compatible with generic, but we need to use
510 * the page tables uncached.
512 pmap_pte_init_generic();
514 pte_l1_s_cache_mode_pt = 0;
515 pte_l2_l_cache_mode_pt = 0;
516 pte_l2_s_cache_mode_pt = 0;
518 #endif /* CPU_ARM8 */
520 #if defined(CPU_ARM9) && defined(ARM9_CACHE_WRITE_THROUGH)
522 pmap_pte_init_arm9(void)
526 * ARM9 is compatible with generic, but we want to use
527 * write-through caching for now.
529 pmap_pte_init_generic();
531 pte_l1_s_cache_mode = L1_S_C;
532 pte_l2_l_cache_mode = L2_C;
533 pte_l2_s_cache_mode = L2_C;
535 pte_l1_s_cache_mode_pt = L1_S_C;
536 pte_l2_l_cache_mode_pt = L2_C;
537 pte_l2_s_cache_mode_pt = L2_C;
539 #endif /* CPU_ARM9 */
540 #endif /* (ARM_MMU_GENERIC + ARM_MMU_SA1) != 0 */
542 #if defined(CPU_ARM10)
544 pmap_pte_init_arm10(void)
548 * ARM10 is compatible with generic, but we want to use
549 * write-through caching for now.
551 pmap_pte_init_generic();
553 pte_l1_s_cache_mode = L1_S_B | L1_S_C;
554 pte_l2_l_cache_mode = L2_B | L2_C;
555 pte_l2_s_cache_mode = L2_B | L2_C;
557 pte_l1_s_cache_mode_pt = L1_S_C;
558 pte_l2_l_cache_mode_pt = L2_C;
559 pte_l2_s_cache_mode_pt = L2_C;
562 #endif /* CPU_ARM10 */
566 pmap_pte_init_sa1(void)
570 * The StrongARM SA-1 cache does not have a write-through
571 * mode. So, do the generic initialization, then reset
572 * the page table cache mode to B=1,C=1, and note that
573 * the PTEs need to be sync'd.
575 pmap_pte_init_generic();
577 pte_l1_s_cache_mode_pt = L1_S_B|L1_S_C;
578 pte_l2_l_cache_mode_pt = L2_B|L2_C;
579 pte_l2_s_cache_mode_pt = L2_B|L2_C;
581 pmap_needs_pte_sync = 1;
583 #endif /* ARM_MMU_SA1 == 1*/
585 #if ARM_MMU_XSCALE == 1
586 #if (ARM_NMMUS > 1) || defined (CPU_XSCALE_CORE3)
587 static u_int xscale_use_minidata;
591 pmap_pte_init_xscale(void)
594 int write_through = 0;
596 pte_l1_s_cache_mode = L1_S_B|L1_S_C|L1_S_XSCALE_P;
597 pte_l1_s_cache_mask = L1_S_CACHE_MASK_xscale;
599 pte_l2_l_cache_mode = L2_B|L2_C;
600 pte_l2_l_cache_mask = L2_L_CACHE_MASK_xscale;
602 pte_l2_s_cache_mode = L2_B|L2_C;
603 pte_l2_s_cache_mask = L2_S_CACHE_MASK_xscale;
605 pte_l1_s_cache_mode_pt = L1_S_C;
606 pte_l2_l_cache_mode_pt = L2_C;
607 pte_l2_s_cache_mode_pt = L2_C;
608 #ifdef XSCALE_CACHE_READ_WRITE_ALLOCATE
610 * The XScale core has an enhanced mode where writes that
611 * miss the cache cause a cache line to be allocated. This
612 * is significantly faster than the traditional, write-through
613 * behavior of this case.
615 pte_l1_s_cache_mode |= L1_S_XSCALE_TEX(TEX_XSCALE_X);
616 pte_l2_l_cache_mode |= L2_XSCALE_L_TEX(TEX_XSCALE_X);
617 pte_l2_s_cache_mode |= L2_XSCALE_T_TEX(TEX_XSCALE_X);
618 #endif /* XSCALE_CACHE_READ_WRITE_ALLOCATE */
619 #ifdef XSCALE_CACHE_WRITE_THROUGH
621 * Some versions of the XScale core have various bugs in
622 * their cache units, the work-around for which is to run
623 * the cache in write-through mode. Unfortunately, this
624 * has a major (negative) impact on performance. So, we
625 * go ahead and run fast-and-loose, in the hopes that we
626 * don't line up the planets in a way that will trip the
629 * However, we give you the option to be slow-but-correct.
632 #elif defined(XSCALE_CACHE_WRITE_BACK)
633 /* force write back cache mode */
635 #elif defined(CPU_XSCALE_PXA2X0)
637 * Intel PXA2[15]0 processors are known to have a bug in
638 * write-back cache on revision 4 and earlier (stepping
639 * A[01] and B[012]). Fixed for C0 and later.
645 type = id & ~(CPU_ID_XSCALE_COREREV_MASK|CPU_ID_REVISION_MASK);
647 if (type == CPU_ID_PXA250 || type == CPU_ID_PXA210) {
648 if ((id & CPU_ID_REVISION_MASK) < 5) {
649 /* write through for stepping A0-1 and B0-2 */
654 #endif /* XSCALE_CACHE_WRITE_THROUGH */
657 pte_l1_s_cache_mode = L1_S_C;
658 pte_l2_l_cache_mode = L2_C;
659 pte_l2_s_cache_mode = L2_C;
663 xscale_use_minidata = 1;
666 pte_l2_s_prot_u = L2_S_PROT_U_xscale;
667 pte_l2_s_prot_w = L2_S_PROT_W_xscale;
668 pte_l2_s_prot_mask = L2_S_PROT_MASK_xscale;
670 pte_l1_s_proto = L1_S_PROTO_xscale;
671 pte_l1_c_proto = L1_C_PROTO_xscale;
672 pte_l2_s_proto = L2_S_PROTO_xscale;
674 #ifdef CPU_XSCALE_CORE3
675 pmap_copy_page_func = pmap_copy_page_generic;
676 pmap_zero_page_func = pmap_zero_page_generic;
677 xscale_use_minidata = 0;
678 /* Make sure it is L2-cachable */
679 pte_l1_s_cache_mode |= L1_S_XSCALE_TEX(TEX_XSCALE_T);
680 pte_l1_s_cache_mode_pt = pte_l1_s_cache_mode &~ L1_S_XSCALE_P;
681 pte_l2_l_cache_mode |= L2_XSCALE_L_TEX(TEX_XSCALE_T) ;
682 pte_l2_l_cache_mode_pt = pte_l1_s_cache_mode;
683 pte_l2_s_cache_mode |= L2_XSCALE_T_TEX(TEX_XSCALE_T);
684 pte_l2_s_cache_mode_pt = pte_l2_s_cache_mode;
687 pmap_copy_page_func = pmap_copy_page_xscale;
688 pmap_zero_page_func = pmap_zero_page_xscale;
692 * Disable ECC protection of page table access, for now.
694 __asm __volatile("mrc p15, 0, %0, c1, c0, 1" : "=r" (auxctl));
695 auxctl &= ~XSCALE_AUXCTL_P;
696 __asm __volatile("mcr p15, 0, %0, c1, c0, 1" : : "r" (auxctl));
700 * xscale_setup_minidata:
702 * Set up the mini-data cache clean area. We require the
703 * caller to allocate the right amount of physically and
704 * virtually contiguous space.
706 extern vm_offset_t xscale_minidata_clean_addr;
707 extern vm_size_t xscale_minidata_clean_size; /* already initialized */
709 xscale_setup_minidata(vm_offset_t l1pt, vm_offset_t va, vm_paddr_t pa)
711 pd_entry_t *pde = (pd_entry_t *) l1pt;
716 xscale_minidata_clean_addr = va;
718 /* Round it to page size. */
719 size = (xscale_minidata_clean_size + L2_S_OFFSET) & L2_S_FRAME;
722 va += L2_S_SIZE, pa += L2_S_SIZE, size -= L2_S_SIZE) {
723 pte = (pt_entry_t *) kernel_pt_lookup(
724 pde[L1_IDX(va)] & L1_C_ADDR_MASK);
726 panic("xscale_setup_minidata: can't find L2 table for "
727 "VA 0x%08x", (u_int32_t) va);
728 pte[l2pte_index(va)] =
729 L2_S_PROTO | pa | L2_S_PROT(PTE_KERNEL, VM_PROT_READ) |
730 L2_C | L2_XSCALE_T_TEX(TEX_XSCALE_X);
734 * Configure the mini-data cache for write-back with
735 * read/write-allocate.
737 * NOTE: In order to reconfigure the mini-data cache, we must
738 * make sure it contains no valid data! In order to do that,
739 * we must issue a global data cache invalidate command!
741 * WE ASSUME WE ARE RUNNING UN-CACHED WHEN THIS ROUTINE IS CALLED!
742 * THIS IS VERY IMPORTANT!
745 /* Invalidate data and mini-data. */
746 __asm __volatile("mcr p15, 0, %0, c7, c6, 0" : : "r" (0));
747 __asm __volatile("mrc p15, 0, %0, c1, c0, 1" : "=r" (auxctl));
748 auxctl = (auxctl & ~XSCALE_AUXCTL_MD_MASK) | XSCALE_AUXCTL_MD_WB_RWA;
749 __asm __volatile("mcr p15, 0, %0, c1, c0, 1" : : "r" (auxctl));
754 * Allocate an L1 translation table for the specified pmap.
755 * This is called at pmap creation time.
758 pmap_alloc_l1(pmap_t pm)
760 struct l1_ttable *l1;
764 * Remove the L1 at the head of the LRU list
766 mtx_lock(&l1_lru_lock);
767 l1 = TAILQ_FIRST(&l1_lru_list);
768 TAILQ_REMOVE(&l1_lru_list, l1, l1_lru);
771 * Pick the first available domain number, and update
772 * the link to the next number.
774 domain = l1->l1_domain_first;
775 l1->l1_domain_first = l1->l1_domain_free[domain];
778 * If there are still free domain numbers in this L1,
779 * put it back on the TAIL of the LRU list.
781 if (++l1->l1_domain_use_count < PMAP_DOMAINS)
782 TAILQ_INSERT_TAIL(&l1_lru_list, l1, l1_lru);
784 mtx_unlock(&l1_lru_lock);
787 * Fix up the relevant bits in the pmap structure
790 pm->pm_domain = domain + 1;
794 * Free an L1 translation table.
795 * This is called at pmap destruction time.
798 pmap_free_l1(pmap_t pm)
800 struct l1_ttable *l1 = pm->pm_l1;
802 mtx_lock(&l1_lru_lock);
805 * If this L1 is currently on the LRU list, remove it.
807 if (l1->l1_domain_use_count < PMAP_DOMAINS)
808 TAILQ_REMOVE(&l1_lru_list, l1, l1_lru);
811 * Free up the domain number which was allocated to the pmap
813 l1->l1_domain_free[pm->pm_domain - 1] = l1->l1_domain_first;
814 l1->l1_domain_first = pm->pm_domain - 1;
815 l1->l1_domain_use_count--;
818 * The L1 now must have at least 1 free domain, so add
819 * it back to the LRU list. If the use count is zero,
820 * put it at the head of the list, otherwise it goes
823 if (l1->l1_domain_use_count == 0) {
824 TAILQ_INSERT_HEAD(&l1_lru_list, l1, l1_lru);
826 TAILQ_INSERT_TAIL(&l1_lru_list, l1, l1_lru);
828 mtx_unlock(&l1_lru_lock);
831 static PMAP_INLINE void
832 pmap_use_l1(pmap_t pm)
834 struct l1_ttable *l1;
837 * Do nothing if we're in interrupt context.
838 * Access to an L1 by the kernel pmap must not affect
841 if (pm == pmap_kernel())
847 * If the L1 is not currently on the LRU list, just return
849 if (l1->l1_domain_use_count == PMAP_DOMAINS)
852 mtx_lock(&l1_lru_lock);
855 * Check the use count again, now that we've acquired the lock
857 if (l1->l1_domain_use_count == PMAP_DOMAINS) {
858 mtx_unlock(&l1_lru_lock);
863 * Move the L1 to the back of the LRU list
865 TAILQ_REMOVE(&l1_lru_list, l1, l1_lru);
866 TAILQ_INSERT_TAIL(&l1_lru_list, l1, l1_lru);
868 mtx_unlock(&l1_lru_lock);
873 * Returns a pointer to the L2 bucket associated with the specified pmap
874 * and VA, or NULL if no L2 bucket exists for the address.
876 static PMAP_INLINE struct l2_bucket *
877 pmap_get_l2_bucket(pmap_t pm, vm_offset_t va)
879 struct l2_dtable *l2;
880 struct l2_bucket *l2b;
885 if ((l2 = pm->pm_l2[L2_IDX(l1idx)]) == NULL ||
886 (l2b = &l2->l2_bucket[L2_BUCKET(l1idx)])->l2b_kva == NULL)
893 * Returns a pointer to the L2 bucket associated with the specified pmap
896 * If no L2 bucket exists, perform the necessary allocations to put an L2
897 * bucket/page table in place.
899 * Note that if a new L2 bucket/page was allocated, the caller *must*
900 * increment the bucket occupancy counter appropriately *before*
901 * releasing the pmap's lock to ensure no other thread or cpu deallocates
902 * the bucket/page in the meantime.
904 static struct l2_bucket *
905 pmap_alloc_l2_bucket(pmap_t pm, vm_offset_t va)
907 struct l2_dtable *l2;
908 struct l2_bucket *l2b;
913 PMAP_ASSERT_LOCKED(pm);
914 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
915 if ((l2 = pm->pm_l2[L2_IDX(l1idx)]) == NULL) {
917 * No mapping at this address, as there is
918 * no entry in the L1 table.
919 * Need to allocate a new l2_dtable.
923 vm_page_unlock_queues();
924 if ((l2 = pmap_alloc_l2_dtable()) == NULL) {
925 vm_page_lock_queues();
929 vm_page_lock_queues();
931 if (pm->pm_l2[L2_IDX(l1idx)] != NULL) {
933 vm_page_unlock_queues();
934 uma_zfree(l2table_zone, l2);
935 vm_page_lock_queues();
937 l2 = pm->pm_l2[L2_IDX(l1idx)];
941 * Someone already allocated the l2_dtable while
942 * we were doing the same.
945 bzero(l2, sizeof(*l2));
947 * Link it into the parent pmap
949 pm->pm_l2[L2_IDX(l1idx)] = l2;
953 l2b = &l2->l2_bucket[L2_BUCKET(l1idx)];
956 * Fetch pointer to the L2 page table associated with the address.
958 if (l2b->l2b_kva == NULL) {
962 * No L2 page table has been allocated. Chances are, this
963 * is because we just allocated the l2_dtable, above.
967 vm_page_unlock_queues();
968 ptep = (void*)uma_zalloc(l2zone, M_NOWAIT|M_USE_RESERVE);
969 vm_page_lock_queues();
971 if (l2b->l2b_kva != 0) {
972 /* We lost the race. */
974 vm_page_unlock_queues();
975 uma_zfree(l2zone, ptep);
976 vm_page_lock_queues();
978 if (l2b->l2b_kva == 0)
982 l2b->l2b_phys = vtophys(ptep);
985 * Oops, no more L2 page tables available at this
986 * time. We may need to deallocate the l2_dtable
987 * if we allocated a new one above.
989 if (l2->l2_occupancy == 0) {
990 pm->pm_l2[L2_IDX(l1idx)] = NULL;
991 pmap_free_l2_dtable(l2);
998 l2b->l2b_l1idx = l1idx;
1004 static PMAP_INLINE void
1005 #ifndef PMAP_INCLUDE_PTE_SYNC
1006 pmap_free_l2_ptp(pt_entry_t *l2)
1008 pmap_free_l2_ptp(boolean_t need_sync, pt_entry_t *l2)
1011 #ifdef PMAP_INCLUDE_PTE_SYNC
1013 * Note: With a write-back cache, we may need to sync this
1014 * L2 table before re-using it.
1015 * This is because it may have belonged to a non-current
1016 * pmap, in which case the cache syncs would have been
1017 * skipped when the pages were being unmapped. If the
1018 * L2 table were then to be immediately re-allocated to
1019 * the *current* pmap, it may well contain stale mappings
1020 * which have not yet been cleared by a cache write-back
1021 * and so would still be visible to the mmu.
1024 PTE_SYNC_RANGE(l2, L2_TABLE_SIZE_REAL / sizeof(pt_entry_t));
1026 uma_zfree(l2zone, l2);
1029 * One or more mappings in the specified L2 descriptor table have just been
1032 * Garbage collect the metadata and descriptor table itself if necessary.
1034 * The pmap lock must be acquired when this is called (not necessary
1035 * for the kernel pmap).
1038 pmap_free_l2_bucket(pmap_t pm, struct l2_bucket *l2b, u_int count)
1040 struct l2_dtable *l2;
1041 pd_entry_t *pl1pd, l1pd;
1047 * Update the bucket's reference count according to how many
1048 * PTEs the caller has just invalidated.
1050 l2b->l2b_occupancy -= count;
1055 * Level 2 page tables allocated to the kernel pmap are never freed
1056 * as that would require checking all Level 1 page tables and
1057 * removing any references to the Level 2 page table. See also the
1058 * comment elsewhere about never freeing bootstrap L2 descriptors.
1060 * We make do with just invalidating the mapping in the L2 table.
1062 * This isn't really a big deal in practice and, in fact, leads
1063 * to a performance win over time as we don't need to continually
1066 if (l2b->l2b_occupancy > 0 || pm == pmap_kernel())
1070 * There are no more valid mappings in this level 2 page table.
1071 * Go ahead and NULL-out the pointer in the bucket, then
1072 * free the page table.
1074 l1idx = l2b->l2b_l1idx;
1075 ptep = l2b->l2b_kva;
1076 l2b->l2b_kva = NULL;
1078 pl1pd = &pm->pm_l1->l1_kva[l1idx];
1081 * If the L1 slot matches the pmap's domain
1082 * number, then invalidate it.
1084 l1pd = *pl1pd & (L1_TYPE_MASK | L1_C_DOM_MASK);
1085 if (l1pd == (L1_C_DOM(pm->pm_domain) | L1_TYPE_C)) {
1091 * Release the L2 descriptor table back to the pool cache.
1093 #ifndef PMAP_INCLUDE_PTE_SYNC
1094 pmap_free_l2_ptp(ptep);
1096 pmap_free_l2_ptp(!pmap_is_current(pm), ptep);
1100 * Update the reference count in the associated l2_dtable
1102 l2 = pm->pm_l2[L2_IDX(l1idx)];
1103 if (--l2->l2_occupancy > 0)
1107 * There are no more valid mappings in any of the Level 1
1108 * slots managed by this l2_dtable. Go ahead and NULL-out
1109 * the pointer in the parent pmap and free the l2_dtable.
1111 pm->pm_l2[L2_IDX(l1idx)] = NULL;
1112 pmap_free_l2_dtable(l2);
1116 * Pool cache constructors for L2 descriptor tables, metadata and pmap
1120 pmap_l2ptp_ctor(void *mem, int size, void *arg, int flags)
1122 #ifndef PMAP_INCLUDE_PTE_SYNC
1123 struct l2_bucket *l2b;
1124 pt_entry_t *ptep, pte;
1125 #ifdef ARM_USE_SMALL_ALLOC
1128 vm_offset_t va = (vm_offset_t)mem & ~PAGE_MASK;
1131 * The mappings for these page tables were initially made using
1132 * pmap_kenter() by the pool subsystem. Therefore, the cache-
1133 * mode will not be right for page table mappings. To avoid
1134 * polluting the pmap_kenter() code with a special case for
1135 * page tables, we simply fix up the cache-mode here if it's not
1138 #ifdef ARM_USE_SMALL_ALLOC
1139 pde = &kernel_pmap->pm_l1->l1_kva[L1_IDX(va)];
1140 if (!l1pte_section_p(*pde)) {
1142 l2b = pmap_get_l2_bucket(pmap_kernel(), va);
1143 ptep = &l2b->l2b_kva[l2pte_index(va)];
1146 if ((pte & L2_S_CACHE_MASK) != pte_l2_s_cache_mode_pt) {
1148 * Page tables must have the cache-mode set to
1151 *ptep = (pte & ~L2_S_CACHE_MASK) | pte_l2_s_cache_mode_pt;
1153 cpu_tlb_flushD_SE(va);
1156 #ifdef ARM_USE_SMALL_ALLOC
1160 memset(mem, 0, L2_TABLE_SIZE_REAL);
1161 PTE_SYNC_RANGE(mem, L2_TABLE_SIZE_REAL / sizeof(pt_entry_t));
1166 * A bunch of routines to conditionally flush the caches/TLB depending
1167 * on whether the specified pmap actually needs to be flushed at any
1170 static PMAP_INLINE void
1171 pmap_tlb_flushID_SE(pmap_t pm, vm_offset_t va)
1174 if (pmap_is_current(pm))
1175 cpu_tlb_flushID_SE(va);
1178 static PMAP_INLINE void
1179 pmap_tlb_flushD_SE(pmap_t pm, vm_offset_t va)
1182 if (pmap_is_current(pm))
1183 cpu_tlb_flushD_SE(va);
1186 static PMAP_INLINE void
1187 pmap_tlb_flushID(pmap_t pm)
1190 if (pmap_is_current(pm))
1193 static PMAP_INLINE void
1194 pmap_tlb_flushD(pmap_t pm)
1197 if (pmap_is_current(pm))
1201 static PMAP_INLINE void
1202 pmap_idcache_wbinv_range(pmap_t pm, vm_offset_t va, vm_size_t len)
1205 if (pmap_is_current(pm))
1206 cpu_idcache_wbinv_range(va, len);
1209 static PMAP_INLINE void
1210 pmap_dcache_wb_range(pmap_t pm, vm_offset_t va, vm_size_t len,
1211 boolean_t do_inv, boolean_t rd_only)
1214 if (pmap_is_current(pm)) {
1217 cpu_dcache_inv_range(va, len);
1219 cpu_dcache_wbinv_range(va, len);
1222 cpu_dcache_wb_range(va, len);
1226 static PMAP_INLINE void
1227 pmap_idcache_wbinv_all(pmap_t pm)
1230 if (pmap_is_current(pm))
1231 cpu_idcache_wbinv_all();
1234 static PMAP_INLINE void
1235 pmap_dcache_wbinv_all(pmap_t pm)
1238 if (pmap_is_current(pm))
1239 cpu_dcache_wbinv_all();
1245 * Make sure the pte is written out to RAM.
1246 * We need to do this for one of two cases:
1247 * - We're dealing with the kernel pmap
1248 * - There is no pmap active in the cache/tlb.
1249 * - The specified pmap is 'active' in the cache/tlb.
1251 #ifdef PMAP_INCLUDE_PTE_SYNC
1252 #define PTE_SYNC_CURRENT(pm, ptep) \
1254 if (PMAP_NEEDS_PTE_SYNC && \
1255 pmap_is_current(pm)) \
1257 } while (/*CONSTCOND*/0)
1259 #define PTE_SYNC_CURRENT(pm, ptep) /* nothing */
1263 * Since we have a virtually indexed cache, we may need to inhibit caching if
1264 * there is more than one mapping and at least one of them is writable.
1265 * Since we purge the cache on every context switch, we only need to check for
1266 * other mappings within the same pmap, or kernel_pmap.
1267 * This function is also called when a page is unmapped, to possibly reenable
1268 * caching on any remaining mappings.
1270 * The code implements the following logic, where:
1272 * KW = # of kernel read/write pages
1273 * KR = # of kernel read only pages
1274 * UW = # of user read/write pages
1275 * UR = # of user read only pages
1277 * KC = kernel mapping is cacheable
1278 * UC = user mapping is cacheable
1280 * KW=0,KR=0 KW=0,KR>0 KW=1,KR=0 KW>1,KR>=0
1281 * +---------------------------------------------
1282 * UW=0,UR=0 | --- KC=1 KC=1 KC=0
1283 * UW=0,UR>0 | UC=1 KC=1,UC=1 KC=0,UC=0 KC=0,UC=0
1284 * UW=1,UR=0 | UC=1 KC=0,UC=0 KC=0,UC=0 KC=0,UC=0
1285 * UW>1,UR>=0 | UC=0 KC=0,UC=0 KC=0,UC=0 KC=0,UC=0
1288 static const int pmap_vac_flags[4][4] = {
1289 {-1, 0, 0, PVF_KNC},
1290 {0, 0, PVF_NC, PVF_NC},
1291 {0, PVF_NC, PVF_NC, PVF_NC},
1292 {PVF_UNC, PVF_NC, PVF_NC, PVF_NC}
1295 static PMAP_INLINE int
1296 pmap_get_vac_flags(const struct vm_page *pg)
1301 if (pg->md.kro_mappings || pg->md.krw_mappings > 1)
1303 if (pg->md.krw_mappings)
1307 if (pg->md.uro_mappings || pg->md.urw_mappings > 1)
1309 if (pg->md.urw_mappings)
1312 return (pmap_vac_flags[uidx][kidx]);
1315 static __inline void
1316 pmap_vac_me_harder(struct vm_page *pg, pmap_t pm, vm_offset_t va)
1320 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1321 nattr = pmap_get_vac_flags(pg);
1324 pg->md.pvh_attrs &= ~PVF_NC;
1328 if (nattr == 0 && (pg->md.pvh_attrs & PVF_NC) == 0) {
1332 if (pm == pmap_kernel())
1333 pmap_vac_me_kpmap(pg, pm, va);
1335 pmap_vac_me_user(pg, pm, va);
1337 pg->md.pvh_attrs = (pg->md.pvh_attrs & ~PVF_NC) | nattr;
1341 pmap_vac_me_kpmap(struct vm_page *pg, pmap_t pm, vm_offset_t va)
1343 u_int u_cacheable, u_entries;
1344 struct pv_entry *pv;
1345 pmap_t last_pmap = pm;
1348 * Pass one, see if there are both kernel and user pmaps for
1349 * this page. Calculate whether there are user-writable or
1350 * kernel-writable pages.
1353 TAILQ_FOREACH(pv, &pg->md.pv_list, pv_list) {
1354 if (pv->pv_pmap != pm && (pv->pv_flags & PVF_NC) == 0)
1358 u_entries = pg->md.urw_mappings + pg->md.uro_mappings;
1361 * We know we have just been updating a kernel entry, so if
1362 * all user pages are already cacheable, then there is nothing
1365 if (pg->md.k_mappings == 0 && u_cacheable == u_entries)
1370 * Scan over the list again, for each entry, if it
1371 * might not be set correctly, call pmap_vac_me_user
1372 * to recalculate the settings.
1374 TAILQ_FOREACH(pv, &pg->md.pv_list, pv_list) {
1376 * We know kernel mappings will get set
1377 * correctly in other calls. We also know
1378 * that if the pmap is the same as last_pmap
1379 * then we've just handled this entry.
1381 if (pv->pv_pmap == pm || pv->pv_pmap == last_pmap)
1385 * If there are kernel entries and this page
1386 * is writable but non-cacheable, then we can
1387 * skip this entry also.
1389 if (pg->md.k_mappings &&
1390 (pv->pv_flags & (PVF_NC | PVF_WRITE)) ==
1391 (PVF_NC | PVF_WRITE))
1395 * Similarly if there are no kernel-writable
1396 * entries and the page is already
1397 * read-only/cacheable.
1399 if (pg->md.krw_mappings == 0 &&
1400 (pv->pv_flags & (PVF_NC | PVF_WRITE)) == 0)
1404 * For some of the remaining cases, we know
1405 * that we must recalculate, but for others we
1406 * can't tell if they are correct or not, so
1407 * we recalculate anyway.
1409 pmap_vac_me_user(pg, (last_pmap = pv->pv_pmap), 0);
1412 if (pg->md.k_mappings == 0)
1416 pmap_vac_me_user(pg, pm, va);
1420 pmap_vac_me_user(struct vm_page *pg, pmap_t pm, vm_offset_t va)
1422 pmap_t kpmap = pmap_kernel();
1423 struct pv_entry *pv, *npv;
1424 struct l2_bucket *l2b;
1425 pt_entry_t *ptep, pte;
1428 u_int cacheable_entries = 0;
1429 u_int kern_cacheable = 0;
1430 u_int other_writable = 0;
1433 * Count mappings and writable mappings in this pmap.
1434 * Include kernel mappings as part of our own.
1435 * Keep a pointer to the first one.
1437 npv = TAILQ_FIRST(&pg->md.pv_list);
1438 TAILQ_FOREACH(pv, &pg->md.pv_list, pv_list) {
1439 /* Count mappings in the same pmap */
1440 if (pm == pv->pv_pmap || kpmap == pv->pv_pmap) {
1444 /* Cacheable mappings */
1445 if ((pv->pv_flags & PVF_NC) == 0) {
1446 cacheable_entries++;
1447 if (kpmap == pv->pv_pmap)
1451 /* Writable mappings */
1452 if (pv->pv_flags & PVF_WRITE)
1455 if (pv->pv_flags & PVF_WRITE)
1460 * Enable or disable caching as necessary.
1461 * Note: the first entry might be part of the kernel pmap,
1462 * so we can't assume this is indicative of the state of the
1463 * other (maybe non-kpmap) entries.
1465 if ((entries > 1 && writable) ||
1466 (entries > 0 && pm == kpmap && other_writable)) {
1467 if (cacheable_entries == 0)
1470 for (pv = npv; pv; pv = TAILQ_NEXT(pv, pv_list)) {
1471 if ((pm != pv->pv_pmap && kpmap != pv->pv_pmap) ||
1472 (pv->pv_flags & PVF_NC))
1475 pv->pv_flags |= PVF_NC;
1477 l2b = pmap_get_l2_bucket(pv->pv_pmap, pv->pv_va);
1478 ptep = &l2b->l2b_kva[l2pte_index(pv->pv_va)];
1479 pte = *ptep & ~L2_S_CACHE_MASK;
1481 if ((va != pv->pv_va || pm != pv->pv_pmap) &&
1483 if (PV_BEEN_EXECD(pv->pv_flags)) {
1484 pmap_idcache_wbinv_range(pv->pv_pmap,
1485 pv->pv_va, PAGE_SIZE);
1486 pmap_tlb_flushID_SE(pv->pv_pmap,
1489 if (PV_BEEN_REFD(pv->pv_flags)) {
1490 pmap_dcache_wb_range(pv->pv_pmap,
1491 pv->pv_va, PAGE_SIZE, TRUE,
1492 (pv->pv_flags & PVF_WRITE) == 0);
1493 pmap_tlb_flushD_SE(pv->pv_pmap,
1499 PTE_SYNC_CURRENT(pv->pv_pmap, ptep);
1503 if (entries > cacheable_entries) {
1505 * Turn cacheing back on for some pages. If it is a kernel
1506 * page, only do so if there are no other writable pages.
1508 for (pv = npv; pv; pv = TAILQ_NEXT(pv, pv_list)) {
1509 if (!(pv->pv_flags & PVF_NC) || (pm != pv->pv_pmap &&
1510 (kpmap != pv->pv_pmap || other_writable)))
1513 pv->pv_flags &= ~PVF_NC;
1515 l2b = pmap_get_l2_bucket(pv->pv_pmap, pv->pv_va);
1516 ptep = &l2b->l2b_kva[l2pte_index(pv->pv_va)];
1517 pte = (*ptep & ~L2_S_CACHE_MASK) | pte_l2_s_cache_mode;
1519 if (l2pte_valid(pte)) {
1520 if (PV_BEEN_EXECD(pv->pv_flags)) {
1521 pmap_tlb_flushID_SE(pv->pv_pmap,
1524 if (PV_BEEN_REFD(pv->pv_flags)) {
1525 pmap_tlb_flushD_SE(pv->pv_pmap,
1531 PTE_SYNC_CURRENT(pv->pv_pmap, ptep);
1537 * Modify pte bits for all ptes corresponding to the given physical address.
1538 * We use `maskbits' rather than `clearbits' because we're always passing
1539 * constants and the latter would require an extra inversion at run-time.
1542 pmap_clearbit(struct vm_page *pg, u_int maskbits)
1544 struct l2_bucket *l2b;
1545 struct pv_entry *pv;
1546 pt_entry_t *ptep, npte, opte;
1552 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1555 * Clear saved attributes (modify, reference)
1557 pg->md.pvh_attrs &= ~(maskbits & (PVF_MOD | PVF_REF));
1559 if (TAILQ_EMPTY(&pg->md.pv_list)) {
1564 * Loop over all current mappings setting/clearing as appropos
1566 TAILQ_FOREACH(pv, &pg->md.pv_list, pv_list) {
1569 oflags = pv->pv_flags;
1570 pv->pv_flags &= ~maskbits;
1574 l2b = pmap_get_l2_bucket(pm, va);
1576 ptep = &l2b->l2b_kva[l2pte_index(va)];
1577 npte = opte = *ptep;
1579 if (maskbits & (PVF_WRITE|PVF_MOD)) {
1580 if ((pv->pv_flags & PVF_NC)) {
1582 * Entry is not cacheable:
1584 * Don't turn caching on again if this is a
1585 * modified emulation. This would be
1586 * inconsitent with the settings created by
1587 * pmap_vac_me_harder(). Otherwise, it's safe
1588 * to re-enable cacheing.
1590 * There's no need to call pmap_vac_me_harder()
1591 * here: all pages are losing their write
1594 if (maskbits & PVF_WRITE) {
1595 npte |= pte_l2_s_cache_mode;
1596 pv->pv_flags &= ~PVF_NC;
1599 if (opte & L2_S_PROT_W) {
1602 * Entry is writable/cacheable: check if pmap
1603 * is current if it is flush it, otherwise it
1604 * won't be in the cache
1606 if (PV_BEEN_EXECD(oflags))
1607 pmap_idcache_wbinv_range(pm, pv->pv_va,
1610 if (PV_BEEN_REFD(oflags))
1611 pmap_dcache_wb_range(pm, pv->pv_va,
1613 (maskbits & PVF_REF) ? TRUE : FALSE,
1617 /* make the pte read only */
1618 npte &= ~L2_S_PROT_W;
1620 if (maskbits & PVF_WRITE) {
1622 * Keep alias accounting up to date
1624 if (pv->pv_pmap == pmap_kernel()) {
1625 if (oflags & PVF_WRITE) {
1626 pg->md.krw_mappings--;
1627 pg->md.kro_mappings++;
1630 if (oflags & PVF_WRITE) {
1631 pg->md.urw_mappings--;
1632 pg->md.uro_mappings++;
1637 if (maskbits & PVF_REF) {
1638 if ((pv->pv_flags & PVF_NC) == 0 &&
1639 (maskbits & (PVF_WRITE|PVF_MOD)) == 0) {
1641 * Check npte here; we may have already
1642 * done the wbinv above, and the validity
1643 * of the PTE is the same for opte and
1646 if (npte & L2_S_PROT_W) {
1647 if (PV_BEEN_EXECD(oflags))
1648 pmap_idcache_wbinv_range(pm,
1649 pv->pv_va, PAGE_SIZE);
1651 if (PV_BEEN_REFD(oflags))
1652 pmap_dcache_wb_range(pm,
1653 pv->pv_va, PAGE_SIZE,
1656 if ((npte & L2_TYPE_MASK) != L2_TYPE_INV) {
1657 /* XXXJRT need idcache_inv_range */
1658 if (PV_BEEN_EXECD(oflags))
1659 pmap_idcache_wbinv_range(pm,
1660 pv->pv_va, PAGE_SIZE);
1662 if (PV_BEEN_REFD(oflags))
1663 pmap_dcache_wb_range(pm,
1664 pv->pv_va, PAGE_SIZE,
1670 * Make the PTE invalid so that we will take a
1671 * page fault the next time the mapping is
1674 npte &= ~L2_TYPE_MASK;
1675 npte |= L2_TYPE_INV;
1682 /* Flush the TLB entry if a current pmap. */
1683 if (PV_BEEN_EXECD(oflags))
1684 pmap_tlb_flushID_SE(pm, pv->pv_va);
1686 if (PV_BEEN_REFD(oflags))
1687 pmap_tlb_flushD_SE(pm, pv->pv_va);
1694 if (maskbits & PVF_WRITE)
1695 vm_page_flag_clear(pg, PG_WRITEABLE);
1700 * main pv_entry manipulation functions:
1701 * pmap_enter_pv: enter a mapping onto a vm_page list
1702 * pmap_remove_pv: remove a mappiing from a vm_page list
1704 * NOTE: pmap_enter_pv expects to lock the pvh itself
1705 * pmap_remove_pv expects te caller to lock the pvh before calling
1709 * pmap_enter_pv: enter a mapping onto a vm_page lst
1711 * => caller should hold the proper lock on pmap_main_lock
1712 * => caller should have pmap locked
1713 * => we will gain the lock on the vm_page and allocate the new pv_entry
1714 * => caller should adjust ptp's wire_count before calling
1715 * => caller should not adjust pmap's wire_count
1718 pmap_enter_pv(struct vm_page *pg, struct pv_entry *pve, pmap_t pm,
1719 vm_offset_t va, u_int flags)
1722 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1723 PMAP_ASSERT_LOCKED(pm);
1726 pve->pv_flags = flags;
1728 TAILQ_INSERT_HEAD(&pg->md.pv_list, pve, pv_list);
1729 TAILQ_INSERT_HEAD(&pm->pm_pvlist, pve, pv_plist);
1730 pg->md.pvh_attrs |= flags & (PVF_REF | PVF_MOD);
1731 if (pm == pmap_kernel()) {
1732 if (flags & PVF_WRITE)
1733 pg->md.krw_mappings++;
1735 pg->md.kro_mappings++;
1737 if (flags & PVF_WRITE)
1738 pg->md.urw_mappings++;
1740 pg->md.uro_mappings++;
1741 pg->md.pv_list_count++;
1742 if (pve->pv_flags & PVF_WIRED)
1743 ++pm->pm_stats.wired_count;
1744 vm_page_flag_set(pg, PG_REFERENCED);
1749 * pmap_find_pv: Find a pv entry
1751 * => caller should hold lock on vm_page
1753 static PMAP_INLINE struct pv_entry *
1754 pmap_find_pv(struct vm_page *pg, pmap_t pm, vm_offset_t va)
1756 struct pv_entry *pv;
1758 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1759 TAILQ_FOREACH(pv, &pg->md.pv_list, pv_list)
1760 if (pm == pv->pv_pmap && va == pv->pv_va)
1766 * vector_page_setprot:
1768 * Manipulate the protection of the vector page.
1771 vector_page_setprot(int prot)
1773 struct l2_bucket *l2b;
1776 l2b = pmap_get_l2_bucket(pmap_kernel(), vector_page);
1778 ptep = &l2b->l2b_kva[l2pte_index(vector_page)];
1780 *ptep = (*ptep & ~L1_S_PROT_MASK) | L2_S_PROT(PTE_KERNEL, prot);
1782 cpu_tlb_flushD_SE(vector_page);
1787 * pmap_remove_pv: try to remove a mapping from a pv_list
1789 * => caller should hold proper lock on pmap_main_lock
1790 * => pmap should be locked
1791 * => caller should hold lock on vm_page [so that attrs can be adjusted]
1792 * => caller should adjust ptp's wire_count and free PTP if needed
1793 * => caller should NOT adjust pmap's wire_count
1794 * => we return the removed pve
1798 pmap_nuke_pv(struct vm_page *pg, pmap_t pm, struct pv_entry *pve)
1801 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1802 PMAP_ASSERT_LOCKED(pm);
1803 TAILQ_REMOVE(&pg->md.pv_list, pve, pv_list);
1804 TAILQ_REMOVE(&pm->pm_pvlist, pve, pv_plist);
1805 if (pve->pv_flags & PVF_WIRED)
1806 --pm->pm_stats.wired_count;
1807 pg->md.pv_list_count--;
1808 if (pg->md.pvh_attrs & PVF_MOD)
1810 if (pm == pmap_kernel()) {
1811 if (pve->pv_flags & PVF_WRITE)
1812 pg->md.krw_mappings--;
1814 pg->md.kro_mappings--;
1816 if (pve->pv_flags & PVF_WRITE)
1817 pg->md.urw_mappings--;
1819 pg->md.uro_mappings--;
1820 if (TAILQ_FIRST(&pg->md.pv_list) == NULL ||
1821 (pg->md.krw_mappings == 0 && pg->md.urw_mappings == 0)) {
1822 pg->md.pvh_attrs &= ~PVF_MOD;
1823 if (TAILQ_FIRST(&pg->md.pv_list) == NULL)
1824 pg->md.pvh_attrs &= ~PVF_REF;
1825 vm_page_flag_clear(pg, PG_WRITEABLE);
1827 if (TAILQ_FIRST(&pg->md.pv_list))
1828 vm_page_flag_set(pg, PG_REFERENCED);
1829 if (pve->pv_flags & PVF_WRITE)
1830 pmap_vac_me_harder(pg, pm, 0);
1833 static struct pv_entry *
1834 pmap_remove_pv(struct vm_page *pg, pmap_t pm, vm_offset_t va)
1836 struct pv_entry *pve;
1838 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1839 pve = TAILQ_FIRST(&pg->md.pv_list);
1842 if (pve->pv_pmap == pm && pve->pv_va == va) { /* match? */
1843 pmap_nuke_pv(pg, pm, pve);
1846 pve = TAILQ_NEXT(pve, pv_list);
1849 return(pve); /* return removed pve */
1853 * pmap_modify_pv: Update pv flags
1855 * => caller should hold lock on vm_page [so that attrs can be adjusted]
1856 * => caller should NOT adjust pmap's wire_count
1857 * => caller must call pmap_vac_me_harder() if writable status of a page
1859 * => we return the old flags
1861 * Modify a physical-virtual mapping in the pv table
1864 pmap_modify_pv(struct vm_page *pg, pmap_t pm, vm_offset_t va,
1865 u_int clr_mask, u_int set_mask)
1867 struct pv_entry *npv;
1868 u_int flags, oflags;
1870 PMAP_ASSERT_LOCKED(pm);
1871 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1872 if ((npv = pmap_find_pv(pg, pm, va)) == NULL)
1876 * There is at least one VA mapping this page.
1879 if (clr_mask & (PVF_REF | PVF_MOD))
1880 pg->md.pvh_attrs |= set_mask & (PVF_REF | PVF_MOD);
1882 oflags = npv->pv_flags;
1883 npv->pv_flags = flags = (oflags & ~clr_mask) | set_mask;
1885 if ((flags ^ oflags) & PVF_WIRED) {
1886 if (flags & PVF_WIRED)
1887 ++pm->pm_stats.wired_count;
1889 --pm->pm_stats.wired_count;
1892 if ((flags ^ oflags) & PVF_WRITE) {
1893 if (pm == pmap_kernel()) {
1894 if (flags & PVF_WRITE) {
1895 pg->md.krw_mappings++;
1896 pg->md.kro_mappings--;
1898 pg->md.kro_mappings++;
1899 pg->md.krw_mappings--;
1902 if (flags & PVF_WRITE) {
1903 pg->md.urw_mappings++;
1904 pg->md.uro_mappings--;
1906 pg->md.uro_mappings++;
1907 pg->md.urw_mappings--;
1909 if (pg->md.krw_mappings == 0 && pg->md.urw_mappings == 0) {
1910 pg->md.pvh_attrs &= ~PVF_MOD;
1911 vm_page_flag_clear(pg, PG_WRITEABLE);
1913 pmap_vac_me_harder(pg, pm, 0);
1919 /* Function to set the debug level of the pmap code */
1922 pmap_debug(int level)
1924 pmap_debug_level = level;
1925 dprintf("pmap_debug: level=%d\n", pmap_debug_level);
1927 #endif /* PMAP_DEBUG */
1930 pmap_pinit0(struct pmap *pmap)
1932 PDEBUG(1, printf("pmap_pinit0: pmap = %08x\n", (u_int32_t) pmap));
1934 dprintf("pmap_pinit0: pmap = %08x, pm_pdir = %08x\n",
1935 (u_int32_t) pmap, (u_int32_t) pmap->pm_pdir);
1936 bcopy(kernel_pmap, pmap, sizeof(*pmap));
1937 bzero(&pmap->pm_mtx, sizeof(pmap->pm_mtx));
1938 PMAP_LOCK_INIT(pmap);
1942 * Initialize a vm_page's machine-dependent fields.
1945 pmap_page_init(vm_page_t m)
1948 TAILQ_INIT(&m->md.pv_list);
1949 m->md.pv_list_count = 0;
1953 * Initialize the pmap module.
1954 * Called by vm_init, to initialize any structures that the pmap
1955 * system needs to map virtual memory.
1960 int shpgperproc = PMAP_SHPGPERPROC;
1962 PDEBUG(1, printf("pmap_init: phys_start = %08x\n"));
1965 * init the pv free list
1967 pvzone = uma_zcreate("PV ENTRY", sizeof (struct pv_entry), NULL, NULL,
1968 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_VM | UMA_ZONE_NOFREE);
1970 * Now it is safe to enable pv_table recording.
1972 PDEBUG(1, printf("pmap_init: done!\n"));
1974 TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
1976 pv_entry_max = shpgperproc * maxproc + cnt.v_page_count;
1977 pv_entry_high_water = 9 * (pv_entry_max / 10);
1978 l2zone = uma_zcreate("L2 Table", L2_TABLE_SIZE_REAL, pmap_l2ptp_ctor,
1979 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_VM | UMA_ZONE_NOFREE);
1980 l2table_zone = uma_zcreate("L2 Table", sizeof(struct l2_dtable),
1981 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
1982 UMA_ZONE_VM | UMA_ZONE_NOFREE);
1984 uma_zone_set_obj(pvzone, &pvzone_obj, pv_entry_max);
1989 pmap_fault_fixup(pmap_t pm, vm_offset_t va, vm_prot_t ftype, int user)
1991 struct l2_dtable *l2;
1992 struct l2_bucket *l2b;
1993 pd_entry_t *pl1pd, l1pd;
1994 pt_entry_t *ptep, pte;
2000 vm_page_lock_queues();
2004 * If there is no l2_dtable for this address, then the process
2005 * has no business accessing it.
2007 * Note: This will catch userland processes trying to access
2010 l2 = pm->pm_l2[L2_IDX(l1idx)];
2015 * Likewise if there is no L2 descriptor table
2017 l2b = &l2->l2_bucket[L2_BUCKET(l1idx)];
2018 if (l2b->l2b_kva == NULL)
2022 * Check the PTE itself.
2024 ptep = &l2b->l2b_kva[l2pte_index(va)];
2030 * Catch a userland access to the vector page mapped at 0x0
2032 if (user && (pte & L2_S_PROT_U) == 0)
2034 if (va == vector_page)
2039 if ((ftype & VM_PROT_WRITE) && (pte & L2_S_PROT_W) == 0) {
2041 * This looks like a good candidate for "page modified"
2044 struct pv_entry *pv;
2047 /* Extract the physical address of the page */
2048 if ((pg = PHYS_TO_VM_PAGE(pa)) == NULL) {
2051 /* Get the current flags for this page. */
2053 pv = pmap_find_pv(pg, pm, va);
2059 * Do the flags say this page is writable? If not then it
2060 * is a genuine write fault. If yes then the write fault is
2061 * our fault as we did not reflect the write access in the
2062 * PTE. Now we know a write has occurred we can correct this
2063 * and also set the modified bit
2065 if ((pv->pv_flags & PVF_WRITE) == 0) {
2069 pg->md.pvh_attrs |= PVF_REF | PVF_MOD;
2071 pv->pv_flags |= PVF_REF | PVF_MOD;
2074 * Re-enable write permissions for the page. No need to call
2075 * pmap_vac_me_harder(), since this is just a
2076 * modified-emulation fault, and the PVF_WRITE bit isn't
2077 * changing. We've already set the cacheable bits based on
2078 * the assumption that we can write to this page.
2080 *ptep = (pte & ~L2_TYPE_MASK) | L2_S_PROTO | L2_S_PROT_W;
2084 if ((pte & L2_TYPE_MASK) == L2_TYPE_INV) {
2086 * This looks like a good candidate for "page referenced"
2089 struct pv_entry *pv;
2092 /* Extract the physical address of the page */
2093 if ((pg = PHYS_TO_VM_PAGE(pa)) == NULL)
2095 /* Get the current flags for this page. */
2097 pv = pmap_find_pv(pg, pm, va);
2101 pg->md.pvh_attrs |= PVF_REF;
2102 pv->pv_flags |= PVF_REF;
2105 *ptep = (pte & ~L2_TYPE_MASK) | L2_S_PROTO;
2111 * We know there is a valid mapping here, so simply
2112 * fix up the L1 if necessary.
2114 pl1pd = &pm->pm_l1->l1_kva[l1idx];
2115 l1pd = l2b->l2b_phys | L1_C_DOM(pm->pm_domain) | L1_C_PROTO;
2116 if (*pl1pd != l1pd) {
2124 * There are bugs in the rev K SA110. This is a check for one
2127 if (rv == 0 && curcpu()->ci_arm_cputype == CPU_ID_SA110 &&
2128 curcpu()->ci_arm_cpurev < 3) {
2129 /* Always current pmap */
2130 if (l2pte_valid(pte)) {
2131 extern int kernel_debug;
2132 if (kernel_debug & 1) {
2133 struct proc *p = curlwp->l_proc;
2134 printf("prefetch_abort: page is already "
2135 "mapped - pte=%p *pte=%08x\n", ptep, pte);
2136 printf("prefetch_abort: pc=%08lx proc=%p "
2137 "process=%s\n", va, p, p->p_comm);
2138 printf("prefetch_abort: far=%08x fs=%x\n",
2139 cpu_faultaddress(), cpu_faultstatus());
2142 if (kernel_debug & 2)
2148 #endif /* CPU_SA110 */
2152 * If 'rv == 0' at this point, it generally indicates that there is a
2153 * stale TLB entry for the faulting address. This happens when two or
2154 * more processes are sharing an L1. Since we don't flush the TLB on
2155 * a context switch between such processes, we can take domain faults
2156 * for mappings which exist at the same VA in both processes. EVEN IF
2157 * WE'VE RECENTLY FIXED UP THE CORRESPONDING L1 in pmap_enter(), for
2160 * This is extremely likely to happen if pmap_enter() updated the L1
2161 * entry for a recently entered mapping. In this case, the TLB is
2162 * flushed for the new mapping, but there may still be TLB entries for
2163 * other mappings belonging to other processes in the 1MB range
2164 * covered by the L1 entry.
2166 * Since 'rv == 0', we know that the L1 already contains the correct
2167 * value, so the fault must be due to a stale TLB entry.
2169 * Since we always need to flush the TLB anyway in the case where we
2170 * fixed up the L1, or frobbed the L2 PTE, we effectively deal with
2171 * stale TLB entries dynamically.
2173 * However, the above condition can ONLY happen if the current L1 is
2174 * being shared. If it happens when the L1 is unshared, it indicates
2175 * that other parts of the pmap are not doing their job WRT managing
2178 if (rv == 0 && pm->pm_l1->l1_domain_use_count == 1) {
2179 extern int last_fault_code;
2180 printf("fixup: pm %p, va 0x%lx, ftype %d - nothing to do!\n",
2182 printf("fixup: l2 %p, l2b %p, ptep %p, pl1pd %p\n",
2183 l2, l2b, ptep, pl1pd);
2184 printf("fixup: pte 0x%x, l1pd 0x%x, last code 0x%x\n",
2185 pte, l1pd, last_fault_code);
2192 cpu_tlb_flushID_SE(va);
2198 vm_page_unlock_queues();
2206 struct l2_bucket *l2b;
2207 struct l1_ttable *l1;
2209 pt_entry_t *ptep, pte;
2210 vm_offset_t va, eva;
2213 needed = (maxproc / PMAP_DOMAINS) + ((maxproc % PMAP_DOMAINS) ? 1 : 0);
2215 l1 = malloc(sizeof(*l1) * needed, M_VMPMAP, M_WAITOK);
2217 for (loop = 0; loop < needed; loop++, l1++) {
2218 /* Allocate a L1 page table */
2219 va = (vm_offset_t)contigmalloc(L1_TABLE_SIZE, M_VMPMAP, 0, 0x0,
2220 0xffffffff, L1_TABLE_SIZE, 0);
2223 panic("Cannot allocate L1 KVM");
2225 eva = va + L1_TABLE_SIZE;
2226 pl1pt = (pd_entry_t *)va;
2229 l2b = pmap_get_l2_bucket(pmap_kernel(), va);
2230 ptep = &l2b->l2b_kva[l2pte_index(va)];
2232 pte = (pte & ~L2_S_CACHE_MASK) | pte_l2_s_cache_mode_pt;
2235 cpu_tlb_flushD_SE(va);
2239 pmap_init_l1(l1, pl1pt);
2244 printf("pmap_postinit: Allocated %d static L1 descriptor tables\n",
2250 * This is used to stuff certain critical values into the PCB where they
2251 * can be accessed quickly from cpu_switch() et al.
2254 pmap_set_pcb_pagedir(pmap_t pm, struct pcb *pcb)
2256 struct l2_bucket *l2b;
2258 pcb->pcb_pagedir = pm->pm_l1->l1_physaddr;
2259 pcb->pcb_dacr = (DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL * 2)) |
2260 (DOMAIN_CLIENT << (pm->pm_domain * 2));
2262 if (vector_page < KERNBASE) {
2263 pcb->pcb_pl1vec = &pm->pm_l1->l1_kva[L1_IDX(vector_page)];
2264 l2b = pmap_get_l2_bucket(pm, vector_page);
2265 pcb->pcb_l1vec = l2b->l2b_phys | L1_C_PROTO |
2266 L1_C_DOM(pm->pm_domain) | L1_C_DOM(PMAP_DOMAIN_KERNEL);
2268 pcb->pcb_pl1vec = NULL;
2272 pmap_activate(struct thread *td)
2277 pm = vmspace_pmap(td->td_proc->p_vmspace);
2281 pmap_set_pcb_pagedir(pm, pcb);
2283 if (td == curthread) {
2284 u_int cur_dacr, cur_ttb;
2286 __asm __volatile("mrc p15, 0, %0, c2, c0, 0" : "=r"(cur_ttb));
2287 __asm __volatile("mrc p15, 0, %0, c3, c0, 0" : "=r"(cur_dacr));
2289 cur_ttb &= ~(L1_TABLE_SIZE - 1);
2291 if (cur_ttb == (u_int)pcb->pcb_pagedir &&
2292 cur_dacr == pcb->pcb_dacr) {
2294 * No need to switch address spaces.
2302 * We MUST, I repeat, MUST fix up the L1 entry corresponding
2303 * to 'vector_page' in the incoming L1 table before switching
2304 * to it otherwise subsequent interrupts/exceptions (including
2305 * domain faults!) will jump into hyperspace.
2307 if (pcb->pcb_pl1vec) {
2309 *pcb->pcb_pl1vec = pcb->pcb_l1vec;
2311 * Don't need to PTE_SYNC() at this point since
2312 * cpu_setttb() is about to flush both the cache
2317 cpu_domains(pcb->pcb_dacr);
2318 cpu_setttb(pcb->pcb_pagedir);
2324 pmap_set_pt_cache_mode(pd_entry_t *kl1, vm_offset_t va)
2326 pd_entry_t *pdep, pde;
2327 pt_entry_t *ptep, pte;
2332 * Make sure the descriptor itself has the correct cache mode
2334 pdep = &kl1[L1_IDX(va)];
2337 if (l1pte_section_p(pde)) {
2338 if ((pde & L1_S_CACHE_MASK) != pte_l1_s_cache_mode_pt) {
2339 *pdep = (pde & ~L1_S_CACHE_MASK) |
2340 pte_l1_s_cache_mode_pt;
2342 cpu_dcache_wbinv_range((vm_offset_t)pdep,
2347 pa = (vm_paddr_t)(pde & L1_C_ADDR_MASK);
2348 ptep = (pt_entry_t *)kernel_pt_lookup(pa);
2350 panic("pmap_bootstrap: No L2 for L2 @ va %p\n", ptep);
2352 ptep = &ptep[l2pte_index(va)];
2354 if ((pte & L2_S_CACHE_MASK) != pte_l2_s_cache_mode_pt) {
2355 *ptep = (pte & ~L2_S_CACHE_MASK) |
2356 pte_l2_s_cache_mode_pt;
2358 cpu_dcache_wbinv_range((vm_offset_t)ptep,
2368 pmap_alloc_specials(vm_offset_t *availp, int pages, vm_offset_t *vap,
2371 vm_offset_t va = *availp;
2372 struct l2_bucket *l2b;
2375 l2b = pmap_get_l2_bucket(pmap_kernel(), va);
2377 panic("pmap_alloc_specials: no l2b for 0x%x", va);
2379 *ptep = &l2b->l2b_kva[l2pte_index(va)];
2383 *availp = va + (PAGE_SIZE * pages);
2387 * Bootstrap the system enough to run with virtual memory.
2389 * On the arm this is called after mapping has already been enabled
2390 * and just syncs the pmap module with what has already been done.
2391 * [We can't call it easily with mapping off since the kernel is not
2392 * mapped with PA == VA, hence we would have to relocate every address
2393 * from the linked base (virtual) address "KERNBASE" to the actual
2394 * (physical) address starting relative to 0]
2396 #define PMAP_STATIC_L2_SIZE 16
2397 #ifdef ARM_USE_SMALL_ALLOC
2398 extern struct mtx smallalloc_mtx;
2402 pmap_bootstrap(vm_offset_t firstaddr, vm_offset_t lastaddr, struct pv_addr *l1pt)
2404 static struct l1_ttable static_l1;
2405 static struct l2_dtable static_l2[PMAP_STATIC_L2_SIZE];
2406 struct l1_ttable *l1 = &static_l1;
2407 struct l2_dtable *l2;
2408 struct l2_bucket *l2b;
2410 pd_entry_t *kernel_l1pt = (pd_entry_t *)l1pt->pv_va;
2415 int l1idx, l2idx, l2next = 0;
2417 PDEBUG(1, printf("firstaddr = %08x, loadaddr = %08x\n",
2418 firstaddr, loadaddr));
2420 virtual_avail = firstaddr;
2421 kernel_pmap = &kernel_pmap_store;
2422 kernel_pmap->pm_l1 = l1;
2423 kernel_l1pa = l1pt->pv_pa;
2426 * Scan the L1 translation table created by initarm() and create
2427 * the required metadata for all valid mappings found in it.
2429 for (l1idx = 0; l1idx < (L1_TABLE_SIZE / sizeof(pd_entry_t)); l1idx++) {
2430 pde = kernel_l1pt[l1idx];
2433 * We're only interested in Coarse mappings.
2434 * pmap_extract() can deal with section mappings without
2435 * recourse to checking L2 metadata.
2437 if ((pde & L1_TYPE_MASK) != L1_TYPE_C)
2441 * Lookup the KVA of this L2 descriptor table
2443 pa = (vm_paddr_t)(pde & L1_C_ADDR_MASK);
2444 ptep = (pt_entry_t *)kernel_pt_lookup(pa);
2447 panic("pmap_bootstrap: No L2 for va 0x%x, pa 0x%lx",
2448 (u_int)l1idx << L1_S_SHIFT, (long unsigned int)pa);
2452 * Fetch the associated L2 metadata structure.
2453 * Allocate a new one if necessary.
2455 if ((l2 = kernel_pmap->pm_l2[L2_IDX(l1idx)]) == NULL) {
2456 if (l2next == PMAP_STATIC_L2_SIZE)
2457 panic("pmap_bootstrap: out of static L2s");
2458 kernel_pmap->pm_l2[L2_IDX(l1idx)] = l2 =
2459 &static_l2[l2next++];
2463 * One more L1 slot tracked...
2468 * Fill in the details of the L2 descriptor in the
2469 * appropriate bucket.
2471 l2b = &l2->l2_bucket[L2_BUCKET(l1idx)];
2472 l2b->l2b_kva = ptep;
2474 l2b->l2b_l1idx = l1idx;
2477 * Establish an initial occupancy count for this descriptor
2480 l2idx < (L2_TABLE_SIZE_REAL / sizeof(pt_entry_t));
2482 if ((ptep[l2idx] & L2_TYPE_MASK) != L2_TYPE_INV) {
2483 l2b->l2b_occupancy++;
2488 * Make sure the descriptor itself has the correct cache mode.
2489 * If not, fix it, but whine about the problem. Port-meisters
2490 * should consider this a clue to fix up their initarm()
2493 if (pmap_set_pt_cache_mode(kernel_l1pt, (vm_offset_t)ptep)) {
2494 printf("pmap_bootstrap: WARNING! wrong cache mode for "
2495 "L2 pte @ %p\n", ptep);
2501 * Ensure the primary (kernel) L1 has the correct cache mode for
2502 * a page table. Bitch if it is not correctly set.
2504 for (va = (vm_offset_t)kernel_l1pt;
2505 va < ((vm_offset_t)kernel_l1pt + L1_TABLE_SIZE); va += PAGE_SIZE) {
2506 if (pmap_set_pt_cache_mode(kernel_l1pt, va))
2507 printf("pmap_bootstrap: WARNING! wrong cache mode for "
2508 "primary L1 @ 0x%x\n", va);
2511 cpu_dcache_wbinv_all();
2515 PMAP_LOCK_INIT(kernel_pmap);
2516 kernel_pmap->pm_active = -1;
2517 kernel_pmap->pm_domain = PMAP_DOMAIN_KERNEL;
2518 TAILQ_INIT(&kernel_pmap->pm_pvlist);
2521 * Reserve some special page table entries/VA space for temporary
2524 #define SYSMAP(c, p, v, n) \
2525 v = (c)va; va += ((n)*PAGE_SIZE); p = pte; pte += (n);
2527 pmap_alloc_specials(&virtual_avail, 1, &csrcp, &csrc_pte);
2528 pmap_set_pt_cache_mode(kernel_l1pt, (vm_offset_t)csrc_pte);
2529 pmap_alloc_specials(&virtual_avail, 1, &cdstp, &cdst_pte);
2530 pmap_set_pt_cache_mode(kernel_l1pt, (vm_offset_t)cdst_pte);
2531 size = ((lastaddr - pmap_curmaxkvaddr) + L1_S_OFFSET) / L1_S_SIZE;
2532 pmap_alloc_specials(&virtual_avail,
2533 round_page(size * L2_TABLE_SIZE_REAL) / PAGE_SIZE,
2534 &pmap_kernel_l2ptp_kva, NULL);
2536 size = (size + (L2_BUCKET_SIZE - 1)) / L2_BUCKET_SIZE;
2537 pmap_alloc_specials(&virtual_avail,
2538 round_page(size * sizeof(struct l2_dtable)) / PAGE_SIZE,
2539 &pmap_kernel_l2dtable_kva, NULL);
2541 pmap_alloc_specials(&virtual_avail,
2542 1, (vm_offset_t*)&_tmppt, NULL);
2543 SLIST_INIT(&l1_list);
2544 TAILQ_INIT(&l1_lru_list);
2545 mtx_init(&l1_lru_lock, "l1 list lock", NULL, MTX_DEF);
2546 pmap_init_l1(l1, kernel_l1pt);
2547 cpu_dcache_wbinv_all();
2549 virtual_avail = round_page(virtual_avail);
2550 virtual_end = lastaddr;
2551 kernel_vm_end = pmap_curmaxkvaddr;
2552 arm_nocache_startaddr = lastaddr;
2553 mtx_init(&cmtx, "TMP mappings mtx", NULL, MTX_DEF);
2555 #ifdef ARM_USE_SMALL_ALLOC
2556 mtx_init(&smallalloc_mtx, "Small alloc page list", NULL, MTX_DEF);
2557 arm_init_smallalloc();
2559 pmap_set_pcb_pagedir(kernel_pmap, thread0.td_pcb);
2562 /***************************************************
2563 * Pmap allocation/deallocation routines.
2564 ***************************************************/
2567 * Release any resources held by the given physical map.
2568 * Called when a pmap initialized by pmap_pinit is being released.
2569 * Should only be called if the map contains no valid mappings.
2572 pmap_release(pmap_t pmap)
2576 pmap_idcache_wbinv_all(pmap);
2577 pmap_tlb_flushID(pmap);
2579 if (vector_page < KERNBASE) {
2580 struct pcb *curpcb = PCPU_GET(curpcb);
2581 pcb = thread0.td_pcb;
2582 if (pmap_is_current(pmap)) {
2584 * Frob the L1 entry corresponding to the vector
2585 * page so that it contains the kernel pmap's domain
2586 * number. This will ensure pmap_remove() does not
2587 * pull the current vector page out from under us.
2590 *pcb->pcb_pl1vec = pcb->pcb_l1vec;
2591 cpu_domains(pcb->pcb_dacr);
2592 cpu_setttb(pcb->pcb_pagedir);
2595 pmap_remove(pmap, vector_page, vector_page + PAGE_SIZE);
2597 * Make sure cpu_switch(), et al, DTRT. This is safe to do
2598 * since this process has no remaining mappings of its own.
2600 curpcb->pcb_pl1vec = pcb->pcb_pl1vec;
2601 curpcb->pcb_l1vec = pcb->pcb_l1vec;
2602 curpcb->pcb_dacr = pcb->pcb_dacr;
2603 curpcb->pcb_pagedir = pcb->pcb_pagedir;
2607 PMAP_LOCK_DESTROY(pmap);
2609 dprintf("pmap_release()\n");
2615 * Helper function for pmap_grow_l2_bucket()
2618 pmap_grow_map(vm_offset_t va, pt_entry_t cache_mode, vm_paddr_t *pap)
2620 struct l2_bucket *l2b;
2625 pg = vm_page_alloc(NULL, 0, VM_ALLOC_NOOBJ | VM_ALLOC_WIRED);
2628 pa = VM_PAGE_TO_PHYS(pg);
2633 l2b = pmap_get_l2_bucket(pmap_kernel(), va);
2635 ptep = &l2b->l2b_kva[l2pte_index(va)];
2636 *ptep = L2_S_PROTO | pa | cache_mode |
2637 L2_S_PROT(PTE_KERNEL, VM_PROT_READ | VM_PROT_WRITE);
2643 * This is the same as pmap_alloc_l2_bucket(), except that it is only
2644 * used by pmap_growkernel().
2646 static __inline struct l2_bucket *
2647 pmap_grow_l2_bucket(pmap_t pm, vm_offset_t va)
2649 struct l2_dtable *l2;
2650 struct l2_bucket *l2b;
2651 struct l1_ttable *l1;
2658 if ((l2 = pm->pm_l2[L2_IDX(l1idx)]) == NULL) {
2660 * No mapping at this address, as there is
2661 * no entry in the L1 table.
2662 * Need to allocate a new l2_dtable.
2664 nva = pmap_kernel_l2dtable_kva;
2665 if ((nva & PAGE_MASK) == 0) {
2667 * Need to allocate a backing page
2669 if (pmap_grow_map(nva, pte_l2_s_cache_mode, NULL))
2673 l2 = (struct l2_dtable *)nva;
2674 nva += sizeof(struct l2_dtable);
2676 if ((nva & PAGE_MASK) < (pmap_kernel_l2dtable_kva &
2679 * The new l2_dtable straddles a page boundary.
2680 * Map in another page to cover it.
2682 if (pmap_grow_map(nva, pte_l2_s_cache_mode, NULL))
2686 pmap_kernel_l2dtable_kva = nva;
2689 * Link it into the parent pmap
2691 pm->pm_l2[L2_IDX(l1idx)] = l2;
2692 memset(l2, 0, sizeof(*l2));
2695 l2b = &l2->l2_bucket[L2_BUCKET(l1idx)];
2698 * Fetch pointer to the L2 page table associated with the address.
2700 if (l2b->l2b_kva == NULL) {
2704 * No L2 page table has been allocated. Chances are, this
2705 * is because we just allocated the l2_dtable, above.
2707 nva = pmap_kernel_l2ptp_kva;
2708 ptep = (pt_entry_t *)nva;
2709 if ((nva & PAGE_MASK) == 0) {
2711 * Need to allocate a backing page
2713 if (pmap_grow_map(nva, pte_l2_s_cache_mode_pt,
2714 &pmap_kernel_l2ptp_phys))
2716 PTE_SYNC_RANGE(ptep, PAGE_SIZE / sizeof(pt_entry_t));
2718 memset(ptep, 0, L2_TABLE_SIZE_REAL);
2720 l2b->l2b_kva = ptep;
2721 l2b->l2b_l1idx = l1idx;
2722 l2b->l2b_phys = pmap_kernel_l2ptp_phys;
2724 pmap_kernel_l2ptp_kva += L2_TABLE_SIZE_REAL;
2725 pmap_kernel_l2ptp_phys += L2_TABLE_SIZE_REAL;
2728 /* Distribute new L1 entry to all other L1s */
2729 SLIST_FOREACH(l1, &l1_list, l1_link) {
2730 pl1pd = &l1->l1_kva[L1_IDX(va)];
2731 *pl1pd = l2b->l2b_phys | L1_C_DOM(PMAP_DOMAIN_KERNEL) |
2741 * grow the number of kernel page table entries, if needed
2744 pmap_growkernel(vm_offset_t addr)
2746 pmap_t kpm = pmap_kernel();
2748 if (addr <= pmap_curmaxkvaddr)
2749 return; /* we are OK */
2752 * whoops! we need to add kernel PTPs
2755 /* Map 1MB at a time */
2756 for (; pmap_curmaxkvaddr < addr; pmap_curmaxkvaddr += L1_S_SIZE)
2757 pmap_grow_l2_bucket(kpm, pmap_curmaxkvaddr);
2760 * flush out the cache, expensive but growkernel will happen so
2763 cpu_dcache_wbinv_all();
2766 kernel_vm_end = pmap_curmaxkvaddr;
2772 * Remove all pages from specified address space
2773 * this aids process exit speeds. Also, this code
2774 * is special cased for current process only, but
2775 * can have the more generic (and slightly slower)
2776 * mode enabled. This is much faster than pmap_remove
2777 * in the case of running down an entire address space.
2780 pmap_remove_pages(pmap_t pmap)
2782 struct pv_entry *pv, *npv;
2783 struct l2_bucket *l2b = NULL;
2787 vm_page_lock_queues();
2789 for (pv = TAILQ_FIRST(&pmap->pm_pvlist); pv; pv = npv) {
2790 if (pv->pv_flags & PVF_WIRED) {
2791 /* The page is wired, cannot remove it now. */
2792 npv = TAILQ_NEXT(pv, pv_plist);
2795 pmap->pm_stats.resident_count--;
2796 l2b = pmap_get_l2_bucket(pmap, pv->pv_va);
2797 KASSERT(l2b != NULL, ("No L2 bucket in pmap_remove_pages"));
2798 pt = &l2b->l2b_kva[l2pte_index(pv->pv_va)];
2799 m = PHYS_TO_VM_PAGE(*pt & L2_ADDR_MASK);
2800 #ifdef ARM_USE_SMALL_ALLOC
2801 KASSERT((vm_offset_t)m >= alloc_firstaddr, ("Trying to access non-existent page va %x pte %x", pv->pv_va, *pt));
2803 KASSERT((vm_offset_t)m >= KERNBASE, ("Trying to access non-existent page va %x pte %x", pv->pv_va, *pt));
2807 npv = TAILQ_NEXT(pv, pv_plist);
2808 pmap_nuke_pv(m, pmap, pv);
2809 if (TAILQ_EMPTY(&m->md.pv_list))
2810 vm_page_flag_clear(m, PG_WRITEABLE);
2811 pmap_free_pv_entry(pv);
2812 pmap_free_l2_bucket(pmap, l2b, 1);
2814 vm_page_unlock_queues();
2815 cpu_idcache_wbinv_all();
2822 /***************************************************
2823 * Low level mapping routines.....
2824 ***************************************************/
2826 #ifdef ARM_HAVE_SUPERSECTIONS
2827 /* Map a super section into the KVA. */
2830 pmap_kenter_supersection(vm_offset_t va, uint64_t pa, int flags)
2832 pd_entry_t pd = L1_S_PROTO | L1_S_SUPERSEC | (pa & L1_SUP_FRAME) |
2833 (((pa >> 32) & 0xf) << 20) | L1_S_PROT(PTE_KERNEL,
2834 VM_PROT_READ|VM_PROT_WRITE) | L1_S_DOM(PMAP_DOMAIN_KERNEL);
2835 struct l1_ttable *l1;
2836 vm_offset_t va0, va_end;
2838 KASSERT(((va | pa) & L1_SUP_OFFSET) == 0,
2839 ("Not a valid super section mapping"));
2840 if (flags & SECTION_CACHE)
2841 pd |= pte_l1_s_cache_mode;
2842 else if (flags & SECTION_PT)
2843 pd |= pte_l1_s_cache_mode_pt;
2844 va0 = va & L1_SUP_FRAME;
2845 va_end = va + L1_SUP_SIZE;
2846 SLIST_FOREACH(l1, &l1_list, l1_link) {
2848 for (; va < va_end; va += L1_S_SIZE) {
2849 l1->l1_kva[L1_IDX(va)] = pd;
2850 PTE_SYNC(&l1->l1_kva[L1_IDX(va)]);
2856 /* Map a section into the KVA. */
2859 pmap_kenter_section(vm_offset_t va, vm_offset_t pa, int flags)
2861 pd_entry_t pd = L1_S_PROTO | pa | L1_S_PROT(PTE_KERNEL,
2862 VM_PROT_READ|VM_PROT_WRITE) | L1_S_DOM(PMAP_DOMAIN_KERNEL);
2863 struct l1_ttable *l1;
2865 KASSERT(((va | pa) & L1_S_OFFSET) == 0,
2866 ("Not a valid section mapping"));
2867 if (flags & SECTION_CACHE)
2868 pd |= pte_l1_s_cache_mode;
2869 else if (flags & SECTION_PT)
2870 pd |= pte_l1_s_cache_mode_pt;
2871 SLIST_FOREACH(l1, &l1_list, l1_link) {
2872 l1->l1_kva[L1_IDX(va)] = pd;
2873 PTE_SYNC(&l1->l1_kva[L1_IDX(va)]);
2878 * add a wired page to the kva
2879 * note that in order for the mapping to take effect -- you
2880 * should do a invltlb after doing the pmap_kenter...
2882 static PMAP_INLINE void
2883 pmap_kenter_internal(vm_offset_t va, vm_offset_t pa, int flags)
2885 struct l2_bucket *l2b;
2888 PDEBUG(1, printf("pmap_kenter: va = %08x, pa = %08x\n",
2889 (uint32_t) va, (uint32_t) pa));
2892 l2b = pmap_get_l2_bucket(pmap_kernel(), va);
2894 l2b = pmap_grow_l2_bucket(pmap_kernel(), va);
2895 KASSERT(l2b != NULL, ("No L2 Bucket"));
2896 pte = &l2b->l2b_kva[l2pte_index(va)];
2898 PDEBUG(1, printf("pmap_kenter: pte = %08x, opte = %08x, npte = %08x\n",
2899 (uint32_t) pte, opte, *pte));
2900 if (l2pte_valid(opte)) {
2901 cpu_dcache_wbinv_range(va, PAGE_SIZE);
2902 cpu_tlb_flushD_SE(va);
2906 l2b->l2b_occupancy++;
2908 *pte = L2_S_PROTO | pa | L2_S_PROT(PTE_KERNEL,
2909 VM_PROT_READ | VM_PROT_WRITE);
2910 if (flags & KENTER_CACHE)
2911 *pte |= pte_l2_s_cache_mode;
2912 if (flags & KENTER_USER)
2913 *pte |= L2_S_PROT_U;
2918 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
2920 pmap_kenter_internal(va, pa, KENTER_CACHE);
2924 pmap_kenter_nocache(vm_offset_t va, vm_paddr_t pa)
2927 pmap_kenter_internal(va, pa, 0);
2931 pmap_kenter_user(vm_offset_t va, vm_paddr_t pa)
2934 pmap_kenter_internal(va, pa, KENTER_CACHE|KENTER_USER);
2936 * Call pmap_fault_fixup now, to make sure we'll have no exception
2937 * at the first use of the new address, or bad things will happen,
2938 * as we use one of these addresses in the exception handlers.
2940 pmap_fault_fixup(pmap_kernel(), va, VM_PROT_READ|VM_PROT_WRITE, 1);
2944 * remove a page rom the kernel pagetables
2947 pmap_kremove(vm_offset_t va)
2949 struct l2_bucket *l2b;
2950 pt_entry_t *pte, opte;
2952 l2b = pmap_get_l2_bucket(pmap_kernel(), va);
2955 KASSERT(l2b != NULL, ("No L2 Bucket"));
2956 pte = &l2b->l2b_kva[l2pte_index(va)];
2958 if (l2pte_valid(opte)) {
2959 cpu_dcache_wbinv_range(va, PAGE_SIZE);
2960 cpu_tlb_flushD_SE(va);
2968 * Used to map a range of physical addresses into kernel
2969 * virtual address space.
2971 * The value passed in '*virt' is a suggested virtual address for
2972 * the mapping. Architectures which can support a direct-mapped
2973 * physical to virtual region can return the appropriate address
2974 * within that region, leaving '*virt' unchanged. Other
2975 * architectures should map the pages starting at '*virt' and
2976 * update '*virt' with the first usable address after the mapped
2980 pmap_map(vm_offset_t *virt, vm_offset_t start, vm_offset_t end, int prot)
2982 #ifdef ARM_USE_SMALL_ALLOC
2983 return (arm_ptovirt(start));
2985 vm_offset_t sva = *virt;
2986 vm_offset_t va = sva;
2988 PDEBUG(1, printf("pmap_map: virt = %08x, start = %08x, end = %08x, "
2989 "prot = %d\n", (uint32_t) *virt, (uint32_t) start, (uint32_t) end,
2992 while (start < end) {
2993 pmap_kenter(va, start);
3003 pmap_wb_page(vm_page_t m)
3005 struct pv_entry *pv;
3007 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list)
3008 pmap_dcache_wb_range(pv->pv_pmap, pv->pv_va, PAGE_SIZE, FALSE,
3009 (pv->pv_flags & PVF_WRITE) == 0);
3013 pmap_inv_page(vm_page_t m)
3015 struct pv_entry *pv;
3017 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list)
3018 pmap_dcache_wb_range(pv->pv_pmap, pv->pv_va, PAGE_SIZE, TRUE, TRUE);
3021 * Add a list of wired pages to the kva
3022 * this routine is only used for temporary
3023 * kernel mappings that do not need to have
3024 * page modification or references recorded.
3025 * Note that old mappings are simply written
3026 * over. The page *must* be wired.
3029 pmap_qenter(vm_offset_t va, vm_page_t *m, int count)
3033 for (i = 0; i < count; i++) {
3035 pmap_kenter_internal(va, VM_PAGE_TO_PHYS(m[i]),
3043 * this routine jerks page mappings from the
3044 * kernel -- it is meant only for temporary mappings.
3047 pmap_qremove(vm_offset_t va, int count)
3052 for (i = 0; i < count; i++) {
3055 pmap_inv_page(PHYS_TO_VM_PAGE(pa));
3064 * pmap_object_init_pt preloads the ptes for a given object
3065 * into the specified pmap. This eliminates the blast of soft
3066 * faults on process startup and immediately after an mmap.
3069 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr, vm_object_t object,
3070 vm_pindex_t pindex, vm_size_t size)
3073 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
3074 KASSERT(object->type == OBJT_DEVICE,
3075 ("pmap_object_init_pt: non-device object"));
3080 * pmap_is_prefaultable:
3082 * Return whether or not the specified virtual address is elgible
3086 pmap_is_prefaultable(pmap_t pmap, vm_offset_t addr)
3091 if (!pmap_get_pde_pte(pmap, addr, &pde, &pte))
3093 KASSERT(pte != NULL, ("Valid mapping but no pte ?"));
3100 * Fetch pointers to the PDE/PTE for the given pmap/VA pair.
3101 * Returns TRUE if the mapping exists, else FALSE.
3103 * NOTE: This function is only used by a couple of arm-specific modules.
3104 * It is not safe to take any pmap locks here, since we could be right
3105 * in the middle of debugging the pmap anyway...
3107 * It is possible for this routine to return FALSE even though a valid
3108 * mapping does exist. This is because we don't lock, so the metadata
3109 * state may be inconsistent.
3111 * NOTE: We can return a NULL *ptp in the case where the L1 pde is
3112 * a "section" mapping.
3115 pmap_get_pde_pte(pmap_t pm, vm_offset_t va, pd_entry_t **pdp, pt_entry_t **ptp)
3117 struct l2_dtable *l2;
3118 pd_entry_t *pl1pd, l1pd;
3122 if (pm->pm_l1 == NULL)
3126 *pdp = pl1pd = &pm->pm_l1->l1_kva[l1idx];
3129 if (l1pte_section_p(l1pd)) {
3134 if (pm->pm_l2 == NULL)
3137 l2 = pm->pm_l2[L2_IDX(l1idx)];
3140 (ptep = l2->l2_bucket[L2_BUCKET(l1idx)].l2b_kva) == NULL) {
3144 *ptp = &ptep[l2pte_index(va)];
3149 * Routine: pmap_remove_all
3151 * Removes this physical page from
3152 * all physical maps in which it resides.
3153 * Reflects back modify bits to the pager.
3156 * Original versions of this routine were very
3157 * inefficient because they iteratively called
3158 * pmap_remove (slow...)
3161 pmap_remove_all(vm_page_t m)
3164 pt_entry_t *ptep, pte;
3165 struct l2_bucket *l2b;
3166 boolean_t flush = FALSE;
3170 #if defined(PMAP_DEBUG)
3172 * XXX this makes pmap_page_protect(NONE) illegal for non-managed
3175 if (m->flags & PG_FICTITIOUS) {
3176 panic("pmap_page_protect: illegal for unmanaged page, va: 0x%x", VM_PAGE_TO_PHYS(m));
3180 if (TAILQ_EMPTY(&m->md.pv_list))
3182 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
3183 curpm = vmspace_pmap(curproc->p_vmspace);
3184 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
3185 if (flush == FALSE && (pv->pv_pmap == curpm ||
3186 pv->pv_pmap == pmap_kernel()))
3188 PMAP_LOCK(pv->pv_pmap);
3189 l2b = pmap_get_l2_bucket(pv->pv_pmap, pv->pv_va);
3190 KASSERT(l2b != NULL, ("No l2 bucket"));
3191 ptep = &l2b->l2b_kva[l2pte_index(pv->pv_va)];
3194 PTE_SYNC_CURRENT(pv->pv_pmap, ptep);
3195 pmap_free_l2_bucket(pv->pv_pmap, l2b, 1);
3196 if (pv->pv_flags & PVF_WIRED)
3197 pv->pv_pmap->pm_stats.wired_count--;
3198 pv->pv_pmap->pm_stats.resident_count--;
3199 flags |= pv->pv_flags;
3200 pmap_nuke_pv(m, pv->pv_pmap, pv);
3201 PMAP_UNLOCK(pv->pv_pmap);
3202 pmap_free_pv_entry(pv);
3206 if (PV_BEEN_EXECD(flags))
3207 pmap_tlb_flushID(curpm);
3209 pmap_tlb_flushD(curpm);
3211 vm_page_flag_clear(m, PG_WRITEABLE);
3216 * Set the physical protection on the
3217 * specified range of this map as requested.
3220 pmap_protect(pmap_t pm, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
3222 struct l2_bucket *l2b;
3223 pt_entry_t *ptep, pte;
3224 vm_offset_t next_bucket;
3228 if ((prot & VM_PROT_READ) == 0) {
3229 pmap_remove(pm, sva, eva);
3233 if (prot & VM_PROT_WRITE) {
3235 * If this is a read->write transition, just ignore it and let
3236 * vm_fault() take care of it later.
3241 vm_page_lock_queues();
3245 * OK, at this point, we know we're doing write-protect operation.
3246 * If the pmap is active, write-back the range.
3248 pmap_dcache_wb_range(pm, sva, eva - sva, FALSE, FALSE);
3250 flush = ((eva - sva) >= (PAGE_SIZE * 4)) ? 0 : -1;
3254 next_bucket = L2_NEXT_BUCKET(sva);
3255 if (next_bucket > eva)
3258 l2b = pmap_get_l2_bucket(pm, sva);
3264 ptep = &l2b->l2b_kva[l2pte_index(sva)];
3266 while (sva < next_bucket) {
3267 if ((pte = *ptep) != 0 && (pte & L2_S_PROT_W) != 0) {
3271 pg = PHYS_TO_VM_PAGE(l2pte_pa(pte));
3272 pte &= ~L2_S_PROT_W;
3277 f = pmap_modify_pv(pg, pm, sva,
3279 pmap_vac_me_harder(pg, pm, sva);
3282 f = PVF_REF | PVF_EXEC;
3288 if (PV_BEEN_EXECD(f))
3289 pmap_tlb_flushID_SE(pm, sva);
3291 if (PV_BEEN_REFD(f))
3292 pmap_tlb_flushD_SE(pm, sva);
3302 if (PV_BEEN_EXECD(flags))
3303 pmap_tlb_flushID(pm);
3305 if (PV_BEEN_REFD(flags))
3306 pmap_tlb_flushD(pm);
3308 vm_page_unlock_queues();
3315 * Insert the given physical page (p) at
3316 * the specified virtual address (v) in the
3317 * target physical map with the protection requested.
3319 * If specified, the page will be wired down, meaning
3320 * that the related pte can not be reclaimed.
3322 * NB: This is the only routine which MAY NOT lazy-evaluate
3323 * or lose information. That is, this routine must actually
3324 * insert this page into the given map NOW.
3328 pmap_enter(pmap_t pmap, vm_offset_t va, vm_prot_t access, vm_page_t m,
3329 vm_prot_t prot, boolean_t wired)
3332 vm_page_lock_queues();
3334 pmap_enter_locked(pmap, va, m, prot, wired, M_WAITOK);
3335 vm_page_unlock_queues();
3340 * The page queues and pmap must be locked.
3343 pmap_enter_locked(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
3344 boolean_t wired, int flags)
3346 struct l2_bucket *l2b = NULL;
3347 struct vm_page *opg;
3348 struct pv_entry *pve = NULL;
3349 pt_entry_t *ptep, npte, opte;
3354 PMAP_ASSERT_LOCKED(pmap);
3355 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
3356 if (va == vector_page) {
3357 pa = systempage.pv_pa;
3360 pa = VM_PAGE_TO_PHYS(m);
3362 if (prot & VM_PROT_WRITE)
3363 nflags |= PVF_WRITE;
3364 if (prot & VM_PROT_EXECUTE)
3367 nflags |= PVF_WIRED;
3368 PDEBUG(1, printf("pmap_enter: pmap = %08x, va = %08x, m = %08x, prot = %x, "
3369 "wired = %x\n", (uint32_t) pmap, va, (uint32_t) m, prot, wired));
3371 if (pmap == pmap_kernel()) {
3372 l2b = pmap_get_l2_bucket(pmap, va);
3374 l2b = pmap_grow_l2_bucket(pmap, va);
3377 l2b = pmap_alloc_l2_bucket(pmap, va);
3379 if (flags & M_WAITOK) {
3381 vm_page_unlock_queues();
3383 vm_page_lock_queues();
3391 ptep = &l2b->l2b_kva[l2pte_index(va)];
3398 * There is already a mapping at this address.
3399 * If the physical address is different, lookup the
3402 if (l2pte_pa(opte) != pa)
3403 opg = PHYS_TO_VM_PAGE(l2pte_pa(opte));
3409 if ((prot & (VM_PROT_ALL)) ||
3410 (!m || m->md.pvh_attrs & PVF_REF)) {
3412 * - The access type indicates that we don't need
3413 * to do referenced emulation.
3415 * - The physical page has already been referenced
3416 * so no need to re-do referenced emulation here.
3422 if (m && ((prot & VM_PROT_WRITE) != 0 ||
3423 (m->md.pvh_attrs & PVF_MOD))) {
3425 * This is a writable mapping, and the
3426 * page's mod state indicates it has
3427 * already been modified. Make it
3428 * writable from the outset.
3431 if (!(m->md.pvh_attrs & PVF_MOD))
3435 vm_page_flag_set(m, PG_REFERENCED);
3438 * Need to do page referenced emulation.
3440 npte |= L2_TYPE_INV;
3443 if (prot & VM_PROT_WRITE) {
3444 npte |= L2_S_PROT_W;
3446 vm_page_flag_set(m, PG_WRITEABLE);
3448 npte |= pte_l2_s_cache_mode;
3449 if (m && m == opg) {
3451 * We're changing the attrs of an existing mapping.
3453 oflags = pmap_modify_pv(m, pmap, va,
3454 PVF_WRITE | PVF_EXEC | PVF_WIRED |
3455 PVF_MOD | PVF_REF, nflags);
3458 * We may need to flush the cache if we're
3461 if (pmap_is_current(pmap) &&
3462 (oflags & PVF_NC) == 0 &&
3463 (opte & L2_S_PROT_W) != 0 &&
3464 (prot & VM_PROT_WRITE) == 0)
3465 cpu_dcache_wb_range(va, PAGE_SIZE);
3468 * New mapping, or changing the backing page
3469 * of an existing mapping.
3473 * Replacing an existing mapping with a new one.
3474 * It is part of our managed memory so we
3475 * must remove it from the PV list
3477 pve = pmap_remove_pv(opg, pmap, va);
3478 if (m && (m->flags & (PG_UNMANAGED | PG_FICTITIOUS)) &&
3480 pmap_free_pv_entry(pve);
3482 !(m->flags & (PG_UNMANAGED | PG_FICTITIOUS)))
3483 pve = pmap_get_pv_entry();
3484 KASSERT(pve != NULL || m->flags & (PG_UNMANAGED |
3485 PG_FICTITIOUS), ("No pv"));
3486 oflags = pve->pv_flags;
3489 * If the old mapping was valid (ref/mod
3490 * emulation creates 'invalid' mappings
3491 * initially) then make sure to frob
3494 if ((oflags & PVF_NC) == 0 &&
3495 l2pte_valid(opte)) {
3496 if (PV_BEEN_EXECD(oflags)) {
3497 pmap_idcache_wbinv_range(pmap, va,
3500 if (PV_BEEN_REFD(oflags)) {
3501 pmap_dcache_wb_range(pmap, va,
3503 (oflags & PVF_WRITE) == 0);
3506 } else if (m && !(m->flags & (PG_UNMANAGED | PG_FICTITIOUS)))
3507 if ((pve = pmap_get_pv_entry()) == NULL) {
3508 panic("pmap_enter: no pv entries");
3510 if (m && !(m->flags & (PG_UNMANAGED | PG_FICTITIOUS))) {
3511 KASSERT(va < kmi.clean_sva || va >= kmi.clean_eva,
3512 ("pmap_enter: managed mapping within the clean submap"));
3513 pmap_enter_pv(m, pve, pmap, va, nflags);
3517 * Make sure userland mappings get the right permissions
3519 if (pmap != pmap_kernel() && va != vector_page) {
3520 npte |= L2_S_PROT_U;
3524 * Keep the stats up to date
3527 l2b->l2b_occupancy++;
3528 pmap->pm_stats.resident_count++;
3533 * If this is just a wiring change, the two PTEs will be
3534 * identical, so there's no need to update the page table.
3537 boolean_t is_cached = pmap_is_current(pmap);
3542 * We only need to frob the cache/tlb if this pmap
3546 if (L1_IDX(va) != L1_IDX(vector_page) &&
3547 l2pte_valid(npte)) {
3549 * This mapping is likely to be accessed as
3550 * soon as we return to userland. Fix up the
3551 * L1 entry to avoid taking another
3552 * page/domain fault.
3554 pd_entry_t *pl1pd, l1pd;
3556 pl1pd = &pmap->pm_l1->l1_kva[L1_IDX(va)];
3557 l1pd = l2b->l2b_phys | L1_C_DOM(pmap->pm_domain) |
3559 if (*pl1pd != l1pd) {
3566 if (PV_BEEN_EXECD(oflags))
3567 pmap_tlb_flushID_SE(pmap, va);
3568 else if (PV_BEEN_REFD(oflags))
3569 pmap_tlb_flushD_SE(pmap, va);
3573 pmap_vac_me_harder(m, pmap, va);
3578 * Maps a sequence of resident pages belonging to the same object.
3579 * The sequence begins with the given page m_start. This page is
3580 * mapped at the given virtual address start. Each subsequent page is
3581 * mapped at a virtual address that is offset from start by the same
3582 * amount as the page is offset from m_start within the object. The
3583 * last page in the sequence is the page with the largest offset from
3584 * m_start that can be mapped at a virtual address less than the given
3585 * virtual address end. Not every virtual page between start and end
3586 * is mapped; only those for which a resident page exists with the
3587 * corresponding offset from m_start are mapped.
3590 pmap_enter_object(pmap_t pmap, vm_offset_t start, vm_offset_t end,
3591 vm_page_t m_start, vm_prot_t prot)
3594 vm_pindex_t diff, psize;
3596 psize = atop(end - start);
3599 while (m != NULL && (diff = m->pindex - m_start->pindex) < psize) {
3600 pmap_enter_locked(pmap, start + ptoa(diff), m, prot &
3601 (VM_PROT_READ | VM_PROT_EXECUTE), FALSE, M_NOWAIT);
3602 m = TAILQ_NEXT(m, listq);
3608 * this code makes some *MAJOR* assumptions:
3609 * 1. Current pmap & pmap exists.
3612 * 4. No page table pages.
3613 * but is *MUCH* faster than pmap_enter...
3617 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot)
3621 pmap_enter_locked(pmap, va, m, prot & (VM_PROT_READ | VM_PROT_EXECUTE),
3627 * Routine: pmap_change_wiring
3628 * Function: Change the wiring attribute for a map/virtual-address
3630 * In/out conditions:
3631 * The mapping must already exist in the pmap.
3634 pmap_change_wiring(pmap_t pmap, vm_offset_t va, boolean_t wired)
3636 struct l2_bucket *l2b;
3637 pt_entry_t *ptep, pte;
3640 vm_page_lock_queues();
3642 l2b = pmap_get_l2_bucket(pmap, va);
3643 KASSERT(l2b, ("No l2b bucket in pmap_change_wiring"));
3644 ptep = &l2b->l2b_kva[l2pte_index(va)];
3646 pg = PHYS_TO_VM_PAGE(l2pte_pa(pte));
3648 pmap_modify_pv(pg, pmap, va, PVF_WIRED, wired);
3649 vm_page_unlock_queues();
3655 * Copy the range specified by src_addr/len
3656 * from the source map to the range dst_addr/len
3657 * in the destination map.
3659 * This routine is only advisory and need not do anything.
3662 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr,
3663 vm_size_t len, vm_offset_t src_addr)
3669 * Routine: pmap_extract
3671 * Extract the physical page address associated
3672 * with the given map/virtual_address pair.
3675 pmap_extract(pmap_t pm, vm_offset_t va)
3677 struct l2_dtable *l2;
3679 pt_entry_t *ptep, pte;
3685 l1pd = pm->pm_l1->l1_kva[l1idx];
3686 if (l1pte_section_p(l1pd)) {
3688 * These should only happen for pmap_kernel()
3690 KASSERT(pm == pmap_kernel(), ("huh"));
3691 /* XXX: what to do about the bits > 32 ? */
3692 if (l1pd & L1_S_SUPERSEC)
3693 pa = (l1pd & L1_SUP_FRAME) | (va & L1_SUP_OFFSET);
3695 pa = (l1pd & L1_S_FRAME) | (va & L1_S_OFFSET);
3698 * Note that we can't rely on the validity of the L1
3699 * descriptor as an indication that a mapping exists.
3700 * We have to look it up in the L2 dtable.
3702 l2 = pm->pm_l2[L2_IDX(l1idx)];
3705 (ptep = l2->l2_bucket[L2_BUCKET(l1idx)].l2b_kva) == NULL) {
3710 ptep = &ptep[l2pte_index(va)];
3718 switch (pte & L2_TYPE_MASK) {
3720 pa = (pte & L2_L_FRAME) | (va & L2_L_OFFSET);
3724 pa = (pte & L2_S_FRAME) | (va & L2_S_OFFSET);
3734 * Atomically extract and hold the physical page with the given
3735 * pmap and virtual address pair if that mapping permits the given
3740 pmap_extract_and_hold(pmap_t pmap, vm_offset_t va, vm_prot_t prot)
3742 struct l2_dtable *l2;
3744 pt_entry_t *ptep, pte;
3750 vm_page_lock_queues();
3752 l1pd = pmap->pm_l1->l1_kva[l1idx];
3753 if (l1pte_section_p(l1pd)) {
3755 * These should only happen for pmap_kernel()
3757 KASSERT(pmap == pmap_kernel(), ("huh"));
3758 /* XXX: what to do about the bits > 32 ? */
3759 if (l1pd & L1_S_SUPERSEC)
3760 pa = (l1pd & L1_SUP_FRAME) | (va & L1_SUP_OFFSET);
3762 pa = (l1pd & L1_S_FRAME) | (va & L1_S_OFFSET);
3763 if (l1pd & L1_S_PROT_W || (prot & VM_PROT_WRITE) == 0) {
3764 m = PHYS_TO_VM_PAGE(pa);
3770 * Note that we can't rely on the validity of the L1
3771 * descriptor as an indication that a mapping exists.
3772 * We have to look it up in the L2 dtable.
3774 l2 = pmap->pm_l2[L2_IDX(l1idx)];
3777 (ptep = l2->l2_bucket[L2_BUCKET(l1idx)].l2b_kva) == NULL) {
3779 vm_page_unlock_queues();
3783 ptep = &ptep[l2pte_index(va)];
3788 vm_page_unlock_queues();
3791 if (pte & L2_S_PROT_W || (prot & VM_PROT_WRITE) == 0) {
3792 switch (pte & L2_TYPE_MASK) {
3794 pa = (pte & L2_L_FRAME) | (va & L2_L_OFFSET);
3798 pa = (pte & L2_S_FRAME) | (va & L2_S_OFFSET);
3801 m = PHYS_TO_VM_PAGE(pa);
3807 vm_page_unlock_queues();
3812 * Initialize a preallocated and zeroed pmap structure,
3813 * such as one in a vmspace structure.
3817 pmap_pinit(pmap_t pmap)
3819 PDEBUG(1, printf("pmap_pinit: pmap = %08x\n", (uint32_t) pmap));
3821 PMAP_LOCK_INIT(pmap);
3822 pmap_alloc_l1(pmap);
3823 bzero(pmap->pm_l2, sizeof(pmap->pm_l2));
3826 pmap->pm_active = 0;
3828 TAILQ_INIT(&pmap->pm_pvlist);
3829 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
3830 pmap->pm_stats.resident_count = 1;
3831 if (vector_page < KERNBASE) {
3832 pmap_enter(pmap, vector_page,
3833 VM_PROT_READ, PHYS_TO_VM_PAGE(systempage.pv_pa),
3840 /***************************************************
3841 * page management routines.
3842 ***************************************************/
3846 pmap_free_pv_entry(pv_entry_t pv)
3849 uma_zfree(pvzone, pv);
3854 * get a new pv_entry, allocating a block from the system
3856 * the memory allocation is performed bypassing the malloc code
3857 * because of the possibility of allocations at interrupt time.
3860 pmap_get_pv_entry(void)
3862 pv_entry_t ret_value;
3865 if (pv_entry_count > pv_entry_high_water)
3866 pagedaemon_wakeup();
3867 ret_value = uma_zalloc(pvzone, M_NOWAIT);
3873 * Remove the given range of addresses from the specified map.
3875 * It is assumed that the start and end are properly
3876 * rounded to the page size.
3878 #define PMAP_REMOVE_CLEAN_LIST_SIZE 3
3880 pmap_remove(pmap_t pm, vm_offset_t sva, vm_offset_t eva)
3882 struct l2_bucket *l2b;
3883 vm_offset_t next_bucket;
3885 u_int cleanlist_idx, total, cnt;
3889 } cleanlist[PMAP_REMOVE_CLEAN_LIST_SIZE];
3890 u_int mappings, is_exec, is_refd;
3895 * we lock in the pmap => pv_head direction
3898 vm_page_lock_queues();
3900 if (!pmap_is_current(pm)) {
3901 cleanlist_idx = PMAP_REMOVE_CLEAN_LIST_SIZE + 1;
3908 * Do one L2 bucket's worth at a time.
3910 next_bucket = L2_NEXT_BUCKET(sva);
3911 if (next_bucket > eva)
3914 l2b = pmap_get_l2_bucket(pm, sva);
3920 ptep = &l2b->l2b_kva[l2pte_index(sva)];
3923 while (sva < next_bucket) {
3932 * Nothing here, move along
3939 pm->pm_stats.resident_count--;
3945 * Update flags. In a number of circumstances,
3946 * we could cluster a lot of these and do a
3947 * number of sequential pages in one go.
3949 if ((pg = PHYS_TO_VM_PAGE(pa)) != NULL) {
3950 struct pv_entry *pve;
3952 pve = pmap_remove_pv(pg, pm, sva);
3954 is_exec = PV_BEEN_EXECD(pve->pv_flags);
3955 is_refd = PV_BEEN_REFD(pve->pv_flags);
3956 pmap_free_pv_entry(pve);
3960 if (!l2pte_valid(pte)) {
3962 PTE_SYNC_CURRENT(pm, ptep);
3969 if (cleanlist_idx < PMAP_REMOVE_CLEAN_LIST_SIZE) {
3970 /* Add to the clean list. */
3971 cleanlist[cleanlist_idx].pte = ptep;
3972 cleanlist[cleanlist_idx].va =
3973 sva | (is_exec & 1);
3976 if (cleanlist_idx == PMAP_REMOVE_CLEAN_LIST_SIZE) {
3977 /* Nuke everything if needed. */
3978 pmap_idcache_wbinv_all(pm);
3979 pmap_tlb_flushID(pm);
3982 * Roll back the previous PTE list,
3983 * and zero out the current PTE.
3986 cnt < PMAP_REMOVE_CLEAN_LIST_SIZE; cnt++) {
3987 *cleanlist[cnt].pte = 0;
3997 pmap_tlb_flushID_SE(pm, sva);
4000 pmap_tlb_flushD_SE(pm, sva);
4009 * Deal with any left overs
4011 if (cleanlist_idx <= PMAP_REMOVE_CLEAN_LIST_SIZE) {
4012 total += cleanlist_idx;
4013 for (cnt = 0; cnt < cleanlist_idx; cnt++) {
4015 cleanlist[cnt].va & ~1;
4016 if (cleanlist[cnt].va & 1) {
4017 pmap_idcache_wbinv_range(pm,
4019 pmap_tlb_flushID_SE(pm, clva);
4021 pmap_dcache_wb_range(pm,
4022 clva, PAGE_SIZE, TRUE,
4024 pmap_tlb_flushD_SE(pm, clva);
4026 *cleanlist[cnt].pte = 0;
4027 PTE_SYNC_CURRENT(pm, cleanlist[cnt].pte);
4030 if (total <= PMAP_REMOVE_CLEAN_LIST_SIZE)
4034 * We are removing so much entries it's just
4035 * easier to flush the whole cache.
4037 cleanlist_idx = PMAP_REMOVE_CLEAN_LIST_SIZE + 1;
4038 pmap_idcache_wbinv_all(pm);
4043 pmap_free_l2_bucket(pm, l2b, mappings);
4046 vm_page_unlock_queues();
4058 * Zero a given physical page by mapping it at a page hook point.
4059 * In doing the zero page op, the page we zero is mapped cachable, as with
4060 * StrongARM accesses to non-cached pages are non-burst making writing
4061 * _any_ bulk data very slow.
4063 #if (ARM_MMU_GENERIC + ARM_MMU_SA1) != 0 || defined(CPU_XSCALE_CORE3)
4065 pmap_zero_page_generic(vm_paddr_t phys, int off, int size)
4067 #ifdef ARM_USE_SMALL_ALLOC
4072 struct vm_page *pg = PHYS_TO_VM_PAGE(phys);
4074 if (pg->md.pvh_list != NULL)
4075 panic("pmap_zero_page: page has mappings");
4078 if (_arm_bzero && size >= _min_bzero_size &&
4079 _arm_bzero((void *)(phys + off), size, IS_PHYSICAL) == 0)
4082 #ifdef ARM_USE_SMALL_ALLOC
4083 dstpg = (char *)arm_ptovirt(phys);
4084 if (off || size != PAGE_SIZE) {
4085 bzero(dstpg + off, size);
4086 cpu_dcache_wbinv_range((vm_offset_t)(dstpg + off), size);
4088 bzero_page((vm_offset_t)dstpg);
4089 cpu_dcache_wbinv_range((vm_offset_t)dstpg, PAGE_SIZE);
4095 * Hook in the page, zero it, and purge the cache for that
4096 * zeroed page. Invalidate the TLB as needed.
4098 *cdst_pte = L2_S_PROTO | phys |
4099 L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE) | pte_l2_s_cache_mode;
4101 cpu_tlb_flushD_SE(cdstp);
4103 if (off || size != PAGE_SIZE) {
4104 bzero((void *)(cdstp + off), size);
4105 cpu_dcache_wbinv_range(cdstp + off, size);
4108 cpu_dcache_wbinv_range(cdstp, PAGE_SIZE);
4113 #endif /* (ARM_MMU_GENERIC + ARM_MMU_SA1) != 0 */
4115 #if ARM_MMU_XSCALE == 1
4117 pmap_zero_page_xscale(vm_paddr_t phys, int off, int size)
4119 if (_arm_bzero && size >= _min_bzero_size &&
4120 _arm_bzero((void *)(phys + off), size, IS_PHYSICAL) == 0)
4124 * Hook in the page, zero it, and purge the cache for that
4125 * zeroed page. Invalidate the TLB as needed.
4127 *cdst_pte = L2_S_PROTO | phys |
4128 L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE) |
4129 L2_C | L2_XSCALE_T_TEX(TEX_XSCALE_X); /* mini-data */
4131 cpu_tlb_flushD_SE(cdstp);
4133 if (off || size != PAGE_SIZE)
4134 bzero((void *)(cdstp + off), size);
4138 xscale_cache_clean_minidata();
4142 * Change the PTEs for the specified kernel mappings such that they
4143 * will use the mini data cache instead of the main data cache.
4146 pmap_use_minicache(vm_offset_t va, vm_size_t size)
4148 struct l2_bucket *l2b;
4149 pt_entry_t *ptep, *sptep, pte;
4150 vm_offset_t next_bucket, eva;
4152 #if (ARM_NMMUS > 1) || defined(CPU_XSCALE_CORE3)
4153 if (xscale_use_minidata == 0)
4160 next_bucket = L2_NEXT_BUCKET(va);
4161 if (next_bucket > eva)
4164 l2b = pmap_get_l2_bucket(pmap_kernel(), va);
4166 sptep = ptep = &l2b->l2b_kva[l2pte_index(va)];
4168 while (va < next_bucket) {
4170 if (!l2pte_minidata(pte)) {
4171 cpu_dcache_wbinv_range(va, PAGE_SIZE);
4172 cpu_tlb_flushD_SE(va);
4173 *ptep = pte & ~L2_B;
4178 PTE_SYNC_RANGE(sptep, (u_int)(ptep - sptep));
4182 #endif /* ARM_MMU_XSCALE == 1 */
4185 * pmap_zero_page zeros the specified hardware page by mapping
4186 * the page into KVM and using bzero to clear its contents.
4189 pmap_zero_page(vm_page_t m)
4191 pmap_zero_page_func(VM_PAGE_TO_PHYS(m), 0, PAGE_SIZE);
4196 * pmap_zero_page_area zeros the specified hardware page by mapping
4197 * the page into KVM and using bzero to clear its contents.
4199 * off and size may not cover an area beyond a single hardware page.
4202 pmap_zero_page_area(vm_page_t m, int off, int size)
4205 pmap_zero_page_func(VM_PAGE_TO_PHYS(m), off, size);
4210 * pmap_zero_page_idle zeros the specified hardware page by mapping
4211 * the page into KVM and using bzero to clear its contents. This
4212 * is intended to be called from the vm_pagezero process only and
4216 pmap_zero_page_idle(vm_page_t m)
4226 * This is a local function used to work out the best strategy to clean
4227 * a single page referenced by its entry in the PV table. It's used by
4228 * pmap_copy_page, pmap_zero page and maybe some others later on.
4230 * Its policy is effectively:
4231 * o If there are no mappings, we don't bother doing anything with the cache.
4232 * o If there is one mapping, we clean just that page.
4233 * o If there are multiple mappings, we clean the entire cache.
4235 * So that some functions can be further optimised, it returns 0 if it didn't
4236 * clean the entire cache, or 1 if it did.
4238 * XXX One bug in this routine is that if the pv_entry has a single page
4239 * mapped at 0x00000000 a whole cache clean will be performed rather than
4240 * just the 1 page. Since this should not occur in everyday use and if it does
4241 * it will just result in not the most efficient clean for the page.
4244 pmap_clean_page(struct pv_entry *pv, boolean_t is_src)
4246 pmap_t pm, pm_to_clean = NULL;
4247 struct pv_entry *npv;
4248 u_int cache_needs_cleaning = 0;
4250 vm_offset_t page_to_clean = 0;
4253 /* nothing mapped in so nothing to flush */
4258 * Since we flush the cache each time we change to a different
4259 * user vmspace, we only need to flush the page if it is in the
4263 pm = vmspace_pmap(curproc->p_vmspace);
4267 for (npv = pv; npv; npv = TAILQ_NEXT(npv, pv_list)) {
4268 if (npv->pv_pmap == pmap_kernel() || npv->pv_pmap == pm) {
4269 flags |= npv->pv_flags;
4271 * The page is mapped non-cacheable in
4272 * this map. No need to flush the cache.
4274 if (npv->pv_flags & PVF_NC) {
4276 if (cache_needs_cleaning)
4277 panic("pmap_clean_page: "
4278 "cache inconsistency");
4281 } else if (is_src && (npv->pv_flags & PVF_WRITE) == 0)
4283 if (cache_needs_cleaning) {
4287 page_to_clean = npv->pv_va;
4288 pm_to_clean = npv->pv_pmap;
4290 cache_needs_cleaning = 1;
4293 if (page_to_clean) {
4294 if (PV_BEEN_EXECD(flags))
4295 pmap_idcache_wbinv_range(pm_to_clean, page_to_clean,
4298 pmap_dcache_wb_range(pm_to_clean, page_to_clean,
4299 PAGE_SIZE, !is_src, (flags & PVF_WRITE) == 0);
4300 } else if (cache_needs_cleaning) {
4301 if (PV_BEEN_EXECD(flags))
4302 pmap_idcache_wbinv_all(pm);
4304 pmap_dcache_wbinv_all(pm);
4312 * pmap_copy_page copies the specified (machine independent)
4313 * page by mapping the page into virtual memory and using
4314 * bcopy to copy the page, one machine dependent page at a
4321 * Copy one physical page into another, by mapping the pages into
4322 * hook points. The same comment regarding cachability as in
4323 * pmap_zero_page also applies here.
4325 #if (ARM_MMU_GENERIC + ARM_MMU_SA1) != 0 || defined (CPU_XSCALE_CORE3)
4327 pmap_copy_page_generic(vm_paddr_t src, vm_paddr_t dst)
4330 struct vm_page *src_pg = PHYS_TO_VM_PAGE(src);
4333 struct vm_page *dst_pg = PHYS_TO_VM_PAGE(dst);
4335 if (dst_pg->md.pvh_list != NULL)
4336 panic("pmap_copy_page: dst page has mappings");
4341 * Clean the source page. Hold the source page's lock for
4342 * the duration of the copy so that no other mappings can
4343 * be created while we have a potentially aliased mapping.
4347 * XXX: Not needed while we call cpu_dcache_wbinv_all() in
4350 (void) pmap_clean_page(TAILQ_FIRST(&src_pg->md.pv_list), TRUE);
4353 * Map the pages into the page hook points, copy them, and purge
4354 * the cache for the appropriate page. Invalidate the TLB
4358 *csrc_pte = L2_S_PROTO | src |
4359 L2_S_PROT(PTE_KERNEL, VM_PROT_READ) | pte_l2_s_cache_mode;
4361 *cdst_pte = L2_S_PROTO | dst |
4362 L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE) | pte_l2_s_cache_mode;
4364 cpu_tlb_flushD_SE(csrcp);
4365 cpu_tlb_flushD_SE(cdstp);
4367 bcopy_page(csrcp, cdstp);
4369 cpu_dcache_inv_range(csrcp, PAGE_SIZE);
4370 cpu_dcache_wbinv_range(cdstp, PAGE_SIZE);
4372 #endif /* (ARM_MMU_GENERIC + ARM_MMU_SA1) != 0 */
4374 #if ARM_MMU_XSCALE == 1
4376 pmap_copy_page_xscale(vm_paddr_t src, vm_paddr_t dst)
4379 /* XXX: Only needed for pmap_clean_page(), which is commented out. */
4380 struct vm_page *src_pg = PHYS_TO_VM_PAGE(src);
4383 struct vm_page *dst_pg = PHYS_TO_VM_PAGE(dst);
4385 if (dst_pg->md.pvh_list != NULL)
4386 panic("pmap_copy_page: dst page has mappings");
4391 * Clean the source page. Hold the source page's lock for
4392 * the duration of the copy so that no other mappings can
4393 * be created while we have a potentially aliased mapping.
4397 * XXX: Not needed while we call cpu_dcache_wbinv_all() in
4400 (void) pmap_clean_page(TAILQ_FIRST(&src_pg->md.pv_list), TRUE);
4403 * Map the pages into the page hook points, copy them, and purge
4404 * the cache for the appropriate page. Invalidate the TLB
4408 *csrc_pte = L2_S_PROTO | src |
4409 L2_S_PROT(PTE_KERNEL, VM_PROT_READ) |
4410 L2_C | L2_XSCALE_T_TEX(TEX_XSCALE_X); /* mini-data */
4412 *cdst_pte = L2_S_PROTO | dst |
4413 L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE) |
4414 L2_C | L2_XSCALE_T_TEX(TEX_XSCALE_X); /* mini-data */
4416 cpu_tlb_flushD_SE(csrcp);
4417 cpu_tlb_flushD_SE(cdstp);
4419 bcopy_page(csrcp, cdstp);
4421 xscale_cache_clean_minidata();
4423 #endif /* ARM_MMU_XSCALE == 1 */
4426 pmap_copy_page(vm_page_t src, vm_page_t dst)
4428 #ifdef ARM_USE_SMALL_ALLOC
4429 vm_offset_t srcpg, dstpg;
4432 cpu_dcache_wbinv_all();
4433 if (_arm_memcpy && PAGE_SIZE >= _min_memcpy_size &&
4434 _arm_memcpy((void *)VM_PAGE_TO_PHYS(dst),
4435 (void *)VM_PAGE_TO_PHYS(src), PAGE_SIZE, IS_PHYSICAL) == 0)
4437 #ifdef ARM_USE_SMALL_ALLOC
4438 srcpg = arm_ptovirt(VM_PAGE_TO_PHYS(src));
4439 dstpg = arm_ptovirt(VM_PAGE_TO_PHYS(dst));
4440 bcopy_page(srcpg, dstpg);
4441 cpu_dcache_wbinv_range(dstpg, PAGE_SIZE);
4443 pmap_copy_page_func(VM_PAGE_TO_PHYS(src), VM_PAGE_TO_PHYS(dst));
4451 * this routine returns true if a physical page resides
4452 * in the given pmap.
4455 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
4460 if (m->flags & PG_FICTITIOUS)
4464 * Not found, check current mappings returning immediately
4466 for (pv = TAILQ_FIRST(&m->md.pv_list);
4468 pv = TAILQ_NEXT(pv, pv_list)) {
4469 if (pv->pv_pmap == pmap) {
4481 * pmap_ts_referenced:
4483 * Return the count of reference bits for a page, clearing all of them.
4486 pmap_ts_referenced(vm_page_t m)
4489 if (m->flags & PG_FICTITIOUS)
4491 return (pmap_clearbit(m, PVF_REF));
4496 pmap_is_modified(vm_page_t m)
4499 if (m->md.pvh_attrs & PVF_MOD)
4507 * Clear the modify bits on the specified physical page.
4510 pmap_clear_modify(vm_page_t m)
4513 if (m->md.pvh_attrs & PVF_MOD)
4514 pmap_clearbit(m, PVF_MOD);
4519 * pmap_clear_reference:
4521 * Clear the reference bit on the specified physical page.
4524 pmap_clear_reference(vm_page_t m)
4527 if (m->md.pvh_attrs & PVF_REF)
4528 pmap_clearbit(m, PVF_REF);
4533 * Clear the write and modified bits in each of the given page's mappings.
4536 pmap_remove_write(vm_page_t m)
4539 if (m->flags & PG_WRITEABLE)
4540 pmap_clearbit(m, PVF_WRITE);
4545 * perform the pmap work for mincore
4548 pmap_mincore(pmap_t pmap, vm_offset_t addr)
4550 printf("pmap_mincore()\n");
4557 pmap_addr_hint(vm_object_t obj, vm_offset_t addr, vm_size_t size)
4564 * Increase the starting virtual address of the given mapping if a
4565 * different alignment might result in more superpage mappings.
4568 pmap_align_superpage(vm_object_t object, vm_ooffset_t offset,
4569 vm_offset_t *addr, vm_size_t size)
4575 * Map a set of physical memory pages into the kernel virtual
4576 * address space. Return a pointer to where it is mapped. This
4577 * routine is intended to be used for mapping device memory,
4581 pmap_mapdev(vm_offset_t pa, vm_size_t size)
4583 vm_offset_t va, tmpva, offset;
4585 offset = pa & PAGE_MASK;
4586 size = roundup(size, PAGE_SIZE);
4590 va = kmem_alloc_nofault(kernel_map, size);
4592 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
4593 for (tmpva = va; size > 0;) {
4594 pmap_kenter_internal(tmpva, pa, 0);
4600 return ((void *)(va + offset));
4603 #define BOOTSTRAP_DEBUG
4608 * Create a single section mapping.
4611 pmap_map_section(vm_offset_t l1pt, vm_offset_t va, vm_offset_t pa,
4612 int prot, int cache)
4614 pd_entry_t *pde = (pd_entry_t *) l1pt;
4617 KASSERT(((va | pa) & L1_S_OFFSET) == 0, ("ouin2"));
4626 fl = pte_l1_s_cache_mode;
4630 fl = pte_l1_s_cache_mode_pt;
4634 pde[va >> L1_S_SHIFT] = L1_S_PROTO | pa |
4635 L1_S_PROT(PTE_KERNEL, prot) | fl | L1_S_DOM(PMAP_DOMAIN_KERNEL);
4636 PTE_SYNC(&pde[va >> L1_S_SHIFT]);
4643 * Link the L2 page table specified by l2pv.pv_pa into the L1
4644 * page table at the slot for "va".
4647 pmap_link_l2pt(vm_offset_t l1pt, vm_offset_t va, struct pv_addr *l2pv)
4649 pd_entry_t *pde = (pd_entry_t *) l1pt, proto;
4650 u_int slot = va >> L1_S_SHIFT;
4652 proto = L1_S_DOM(PMAP_DOMAIN_KERNEL) | L1_C_PROTO;
4654 #ifdef VERBOSE_INIT_ARM
4655 printf("pmap_link_l2pt: pa=0x%x va=0x%x\n", l2pv->pv_pa, l2pv->pv_va);
4658 pde[slot + 0] = proto | (l2pv->pv_pa + 0x000);
4660 PTE_SYNC(&pde[slot]);
4662 SLIST_INSERT_HEAD(&kernel_pt_list, l2pv, pv_list);
4670 * Create a single page mapping.
4673 pmap_map_entry(vm_offset_t l1pt, vm_offset_t va, vm_offset_t pa, int prot,
4676 pd_entry_t *pde = (pd_entry_t *) l1pt;
4680 KASSERT(((va | pa) & PAGE_MASK) == 0, ("ouin"));
4689 fl = pte_l2_s_cache_mode;
4693 fl = pte_l2_s_cache_mode_pt;
4697 if ((pde[va >> L1_S_SHIFT] & L1_TYPE_MASK) != L1_TYPE_C)
4698 panic("pmap_map_entry: no L2 table for VA 0x%08x", va);
4700 pte = (pt_entry_t *) kernel_pt_lookup(pde[L1_IDX(va)] & L1_C_ADDR_MASK);
4703 panic("pmap_map_entry: can't find L2 table for VA 0x%08x", va);
4705 pte[l2pte_index(va)] =
4706 L2_S_PROTO | pa | L2_S_PROT(PTE_KERNEL, prot) | fl;
4707 PTE_SYNC(&pte[l2pte_index(va)]);
4713 * Map a chunk of memory using the most efficient mappings
4714 * possible (section. large page, small page) into the
4715 * provided L1 and L2 tables at the specified virtual address.
4718 pmap_map_chunk(vm_offset_t l1pt, vm_offset_t va, vm_offset_t pa,
4719 vm_size_t size, int prot, int cache)
4721 pd_entry_t *pde = (pd_entry_t *) l1pt;
4722 pt_entry_t *pte, f1, f2s, f2l;
4726 resid = (size + (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
4729 panic("pmap_map_chunk: no L1 table provided");
4731 #ifdef VERBOSE_INIT_ARM
4732 printf("pmap_map_chunk: pa=0x%x va=0x%x size=0x%x resid=0x%x "
4733 "prot=0x%x cache=%d\n", pa, va, size, resid, prot, cache);
4745 f1 = pte_l1_s_cache_mode;
4746 f2l = pte_l2_l_cache_mode;
4747 f2s = pte_l2_s_cache_mode;
4751 f1 = pte_l1_s_cache_mode_pt;
4752 f2l = pte_l2_l_cache_mode_pt;
4753 f2s = pte_l2_s_cache_mode_pt;
4760 /* See if we can use a section mapping. */
4761 if (L1_S_MAPPABLE_P(va, pa, resid)) {
4762 #ifdef VERBOSE_INIT_ARM
4765 pde[va >> L1_S_SHIFT] = L1_S_PROTO | pa |
4766 L1_S_PROT(PTE_KERNEL, prot) | f1 |
4767 L1_S_DOM(PMAP_DOMAIN_KERNEL);
4768 PTE_SYNC(&pde[va >> L1_S_SHIFT]);
4776 * Ok, we're going to use an L2 table. Make sure
4777 * one is actually in the corresponding L1 slot
4778 * for the current VA.
4780 if ((pde[va >> L1_S_SHIFT] & L1_TYPE_MASK) != L1_TYPE_C)
4781 panic("pmap_map_chunk: no L2 table for VA 0x%08x", va);
4783 pte = (pt_entry_t *) kernel_pt_lookup(
4784 pde[L1_IDX(va)] & L1_C_ADDR_MASK);
4786 panic("pmap_map_chunk: can't find L2 table for VA"
4788 /* See if we can use a L2 large page mapping. */
4789 if (L2_L_MAPPABLE_P(va, pa, resid)) {
4790 #ifdef VERBOSE_INIT_ARM
4793 for (i = 0; i < 16; i++) {
4794 pte[l2pte_index(va) + i] =
4796 L2_L_PROT(PTE_KERNEL, prot) | f2l;
4797 PTE_SYNC(&pte[l2pte_index(va) + i]);
4805 /* Use a small page mapping. */
4806 #ifdef VERBOSE_INIT_ARM
4809 pte[l2pte_index(va)] =
4810 L2_S_PROTO | pa | L2_S_PROT(PTE_KERNEL, prot) | f2s;
4811 PTE_SYNC(&pte[l2pte_index(va)]);
4816 #ifdef VERBOSE_INIT_ARM
4823 /********************** Static device map routines ***************************/
4825 static const struct pmap_devmap *pmap_devmap_table;
4828 * Register the devmap table. This is provided in case early console
4829 * initialization needs to register mappings created by bootstrap code
4830 * before pmap_devmap_bootstrap() is called.
4833 pmap_devmap_register(const struct pmap_devmap *table)
4836 pmap_devmap_table = table;
4840 * Map all of the static regions in the devmap table, and remember
4841 * the devmap table so other parts of the kernel can look up entries
4845 pmap_devmap_bootstrap(vm_offset_t l1pt, const struct pmap_devmap *table)
4849 pmap_devmap_table = table;
4851 for (i = 0; pmap_devmap_table[i].pd_size != 0; i++) {
4852 #ifdef VERBOSE_INIT_ARM
4853 printf("devmap: %08x -> %08x @ %08x\n",
4854 pmap_devmap_table[i].pd_pa,
4855 pmap_devmap_table[i].pd_pa +
4856 pmap_devmap_table[i].pd_size - 1,
4857 pmap_devmap_table[i].pd_va);
4859 pmap_map_chunk(l1pt, pmap_devmap_table[i].pd_va,
4860 pmap_devmap_table[i].pd_pa,
4861 pmap_devmap_table[i].pd_size,
4862 pmap_devmap_table[i].pd_prot,
4863 pmap_devmap_table[i].pd_cache);
4867 const struct pmap_devmap *
4868 pmap_devmap_find_pa(vm_paddr_t pa, vm_size_t size)
4872 if (pmap_devmap_table == NULL)
4875 for (i = 0; pmap_devmap_table[i].pd_size != 0; i++) {
4876 if (pa >= pmap_devmap_table[i].pd_pa &&
4877 pa + size <= pmap_devmap_table[i].pd_pa +
4878 pmap_devmap_table[i].pd_size)
4879 return (&pmap_devmap_table[i]);
4885 const struct pmap_devmap *
4886 pmap_devmap_find_va(vm_offset_t va, vm_size_t size)
4890 if (pmap_devmap_table == NULL)
4893 for (i = 0; pmap_devmap_table[i].pd_size != 0; i++) {
4894 if (va >= pmap_devmap_table[i].pd_va &&
4895 va + size <= pmap_devmap_table[i].pd_va +
4896 pmap_devmap_table[i].pd_size)
4897 return (&pmap_devmap_table[i]);