FreeBSD/Linux Kernel Cross Reference
sys/arm/arm/pmap.c
1 /* From: $NetBSD: pmap.c,v 1.148 2004/04/03 04:35:48 bsh Exp $ */
2 /*-
3 * Copyright 2004 Olivier Houchard.
4 * Copyright 2003 Wasabi Systems, Inc.
5 * All rights reserved.
6 *
7 * Written by Steve C. Woodford for Wasabi Systems, Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
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
23 * written permission.
24 *
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.
36 */
37
38 /*-
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.
43 *
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.
52 *
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
63 * SUCH DAMAGE.
64 */
65 /*-
66 * Copyright (c) 1999 The NetBSD Foundation, Inc.
67 * All rights reserved.
68 *
69 * This code is derived from software contributed to The NetBSD Foundation
70 * by Charles M. Hannum.
71 *
72 * Redistribution and use in source and binary forms, with or without
73 * modification, are permitted provided that the following conditions
74 * are met:
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.
87 *
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.
99 */
100
101 /*-
102 * Copyright (c) 1994-1998 Mark Brinicombe.
103 * Copyright (c) 1994 Brini.
104 * All rights reserved.
105 *
106 * This code is derived from software written for Brini by Mark Brinicombe
107 *
108 * Redistribution and use in source and binary forms, with or without
109 * modification, are permitted provided that the following conditions
110 * are met:
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.
121 *
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
131 *
132 * RiscBSD kernel project
133 *
134 * pmap.c
135 *
136 * Machine dependant vm stuff
137 *
138 * Created : 20/09/94
139 */
140
141 /*
142 * Special compilation symbols
143 * PMAP_DEBUG - Build in pmap_debug_level code
144 */
145 /* Include header files */
146
147 #include "opt_vm.h"
148
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>
159 #include <sys/smp.h>
160 #include <sys/sched.h>
161
162 #include <vm/vm.h>
163 #include <vm/uma.h>
164 #include <vm/pmap.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>
178
179 #ifdef PMAP_DEBUG
180 #define PDEBUG(_lev_,_stat_) \
181 if (pmap_debug_level >= (_lev_)) \
182 ((_stat_))
183 #define dprintf printf
184
185 int pmap_debug_level = 0;
186 #define PMAP_INLINE
187 #else /* PMAP_DEBUG */
188 #define PDEBUG(_lev_,_stat_) /* Nothing */
189 #define dprintf(x, arg...)
190 #define PMAP_INLINE __inline
191 #endif /* PMAP_DEBUG */
192
193 extern struct pv_addr systempage;
194 /*
195 * Internal function prototypes
196 */
197 static void pmap_free_pv_entry (pv_entry_t);
198 static pv_entry_t pmap_get_pv_entry(void);
199
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,
203 vm_offset_t);
204 static void pmap_vac_me_kpmap(struct vm_page *, pmap_t,
205 vm_offset_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);
210
211 static int pmap_clearbit(struct vm_page *, u_int);
212
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);
217
218 static MALLOC_DEFINE(M_VMPMAP, "pmap", "PMAP L1");
219
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;
224
225 extern void *end;
226 vm_offset_t kernel_vm_end = 0;
227
228 struct pmap kernel_pmap_store;
229 pmap_t kernel_pmap;
230
231 static pt_entry_t *csrc_pte, *cdst_pte;
232 static vm_offset_t csrcp, cdstp;
233 static struct mtx cmtx;
234
235 static void pmap_init_l1(struct l1_ttable *, pd_entry_t *);
236 /*
237 * These routines are called when the CPU type is identified to set up
238 * the PTE prototypes, cache modes, etc.
239 *
240 * The variables are always here, just in case LKMs need to reference
241 * them (though, they shouldn't).
242 */
243
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;
247
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;
251
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;
255
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;
259
260 pt_entry_t pte_l1_s_proto;
261 pt_entry_t pte_l1_c_proto;
262 pt_entry_t pte_l2_s_proto;
263
264 void (*pmap_copy_page_func)(vm_paddr_t, vm_paddr_t);
265 void (*pmap_zero_page_func)(vm_paddr_t, int, int);
266 /*
267 * Which pmap is currently 'live' in the cache
268 *
269 * XXXSCW: Fix for SMP ...
270 */
271 union pmap_cache_state *pmap_cache_state;
272
273 struct msgbuf *msgbufp = 0;
274
275 extern void bcopy_page(vm_offset_t, vm_offset_t);
276 extern void bzero_page(vm_offset_t);
277
278 extern vm_offset_t alloc_firstaddr;
279
280 char *_tmppt;
281
282 /*
283 * Metadata for L1 translation tables.
284 */
285 struct l1_ttable {
286 /* Entry on the L1 Table list */
287 SLIST_ENTRY(l1_ttable) l1_link;
288
289 /* Entry on the L1 Least Recently Used list */
290 TAILQ_ENTRY(l1_ttable) l1_lru;
291
292 /* Track how many domains are allocated from this L1 */
293 volatile u_int l1_domain_use_count;
294
295 /*
296 * A free-list of domain numbers for this L1.
297 * We avoid using ffs() and a bitmap to track domains since ffs()
298 * is slow on ARM.
299 */
300 u_int8_t l1_domain_first;
301 u_int8_t l1_domain_free[PMAP_DOMAINS];
302
303 /* Physical address of this L1 page table */
304 vm_paddr_t l1_physaddr;
305
306 /* KVA of this L1 page table */
307 pd_entry_t *l1_kva;
308 };
309
310 /*
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[].
314 *
315 * Each L2 descriptor table represents 1MB of VA space.
316 */
317 #define L1_IDX(va) (((vm_offset_t)(va)) >> L1_S_SHIFT)
318
319 /*
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.
325 */
326 static TAILQ_HEAD(, l1_ttable) l1_lru_list;
327 /*
328 * A list of all L1 tables
329 */
330 static SLIST_HEAD(, l1_ttable) l1_list;
331 static struct mtx l1_lru_lock;
332
333 /*
334 * The l2_dtable tracks L2_BUCKET_SIZE worth of L1 slots.
335 *
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
338 * freed when empty.
339 */
340 struct l2_dtable {
341 /* The number of L2 page descriptors allocated to this l2_dtable */
342 u_int l2_occupancy;
343
344 /* List of L2 page descriptors */
345 struct l2_bucket {
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];
351 };
352
353 /* pmap_kenter_internal flags */
354 #define KENTER_CACHE 0x1
355 #define KENTER_USER 0x2
356
357 /*
358 * Given an L1 table index, calculate the corresponding l2_dtable index
359 * and bucket index within the l2_dtable.
360 */
361 #define L2_IDX(l1idx) (((l1idx) >> L2_BUCKET_LOG2) & \
362 (L2_SIZE - 1))
363 #define L2_BUCKET(l1idx) ((l1idx) & (L2_BUCKET_SIZE - 1))
364
365 /*
366 * Given a virtual address, this macro returns the
367 * virtual address required to drop into the next L2 bucket.
368 */
369 #define L2_NEXT_BUCKET(va) (((va) & L1_S_FRAME) + L1_S_SIZE)
370
371 /*
372 * L2 allocation.
373 */
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)
378
379 /*
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.
383 *
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.
386 */
387 int pmap_needs_pte_sync;
388
389 /*
390 * Macro to determine if a mapping might be resident in the
391 * instruction cache and/or TLB
392 */
393 #define PV_BEEN_EXECD(f) (((f) & (PVF_REF | PVF_EXEC)) == (PVF_REF | PVF_EXEC))
394
395 /*
396 * Macro to determine if a mapping might be resident in the
397 * data cache and/or TLB
398 */
399 #define PV_BEEN_REFD(f) (((f) & PVF_REF) != 0)
400
401 #ifndef PMAP_SHPGPERPROC
402 #define PMAP_SHPGPERPROC 200
403 #endif
404
405 #define pmap_is_current(pm) ((pm) == pmap_kernel() || \
406 curproc->p_vmspace->vm_map.pmap == (pm))
407 static uma_zone_t pvzone;
408 uma_zone_t l2zone;
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;
415
416 /*
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.
420 *
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.
423 */
424 SLIST_HEAD(, pv_addr) kernel_pt_list = SLIST_HEAD_INITIALIZER(kernel_pt_list);
425
426 static void
427 pmap_init_l1(struct l1_ttable *l1, pd_entry_t *l1pt)
428 {
429 int i;
430
431 l1->l1_kva = l1pt;
432 l1->l1_domain_use_count = 0;
433 l1->l1_domain_first = 0;
434
435 for (i = 0; i < PMAP_DOMAINS; i++)
436 l1->l1_domain_free[i] = i + 1;
437
438 /*
439 * Copy the kernel's L1 entries to each new L1.
440 */
441 if (l1pt != pmap_kernel()->pm_l1->l1_kva)
442 memcpy(l1pt, pmap_kernel()->pm_l1->l1_kva, L1_TABLE_SIZE);
443
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);
448 }
449
450 static vm_offset_t
451 kernel_pt_lookup(vm_paddr_t pa)
452 {
453 struct pv_addr *pv;
454
455 SLIST_FOREACH(pv, &kernel_pt_list, pv_list) {
456 if (pv->pv_pa == pa)
457 return (pv->pv_va);
458 }
459 return (0);
460 }
461
462 #if (ARM_MMU_GENERIC + ARM_MMU_SA1) != 0
463 void
464 pmap_pte_init_generic(void)
465 {
466
467 pte_l1_s_cache_mode = L1_S_B|L1_S_C;
468 pte_l1_s_cache_mask = L1_S_CACHE_MASK_generic;
469
470 pte_l2_l_cache_mode = L2_B|L2_C;
471 pte_l2_l_cache_mask = L2_L_CACHE_MASK_generic;
472
473 pte_l2_s_cache_mode = L2_B|L2_C;
474 pte_l2_s_cache_mask = L2_S_CACHE_MASK_generic;
475
476 /*
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.
480 */
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;
485 } else {
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;
489 }
490
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;
494
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;
498
499 pmap_copy_page_func = pmap_copy_page_generic;
500 pmap_zero_page_func = pmap_zero_page_generic;
501 }
502
503 #if defined(CPU_ARM8)
504 void
505 pmap_pte_init_arm8(void)
506 {
507
508 /*
509 * ARM8 is compatible with generic, but we need to use
510 * the page tables uncached.
511 */
512 pmap_pte_init_generic();
513
514 pte_l1_s_cache_mode_pt = 0;
515 pte_l2_l_cache_mode_pt = 0;
516 pte_l2_s_cache_mode_pt = 0;
517 }
518 #endif /* CPU_ARM8 */
519
520 #if defined(CPU_ARM9) && defined(ARM9_CACHE_WRITE_THROUGH)
521 void
522 pmap_pte_init_arm9(void)
523 {
524
525 /*
526 * ARM9 is compatible with generic, but we want to use
527 * write-through caching for now.
528 */
529 pmap_pte_init_generic();
530
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;
534
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;
538 }
539 #endif /* CPU_ARM9 */
540 #endif /* (ARM_MMU_GENERIC + ARM_MMU_SA1) != 0 */
541
542 #if defined(CPU_ARM10)
543 void
544 pmap_pte_init_arm10(void)
545 {
546
547 /*
548 * ARM10 is compatible with generic, but we want to use
549 * write-through caching for now.
550 */
551 pmap_pte_init_generic();
552
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;
556
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;
560
561 }
562 #endif /* CPU_ARM10 */
563
564 #if ARM_MMU_SA1 == 1
565 void
566 pmap_pte_init_sa1(void)
567 {
568
569 /*
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.
574 */
575 pmap_pte_init_generic();
576
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;
580
581 pmap_needs_pte_sync = 1;
582 }
583 #endif /* ARM_MMU_SA1 == 1*/
584
585 #if ARM_MMU_XSCALE == 1
586 #if (ARM_NMMUS > 1) || defined (CPU_XSCALE_CORE3)
587 static u_int xscale_use_minidata;
588 #endif
589
590 void
591 pmap_pte_init_xscale(void)
592 {
593 uint32_t auxctl;
594 int write_through = 0;
595
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;
598
599 pte_l2_l_cache_mode = L2_B|L2_C;
600 pte_l2_l_cache_mask = L2_L_CACHE_MASK_xscale;
601
602 pte_l2_s_cache_mode = L2_B|L2_C;
603 pte_l2_s_cache_mask = L2_S_CACHE_MASK_xscale;
604
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
609 /*
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.
614 */
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
620 /*
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
627 * bugs.
628 *
629 * However, we give you the option to be slow-but-correct.
630 */
631 write_through = 1;
632 #elif defined(XSCALE_CACHE_WRITE_BACK)
633 /* force write back cache mode */
634 write_through = 0;
635 #elif defined(CPU_XSCALE_PXA2X0)
636 /*
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.
640 */
641 {
642 uint32_t id, type;
643
644 id = cpufunc_id();
645 type = id & ~(CPU_ID_XSCALE_COREREV_MASK|CPU_ID_REVISION_MASK);
646
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 */
650 write_through = 1;
651 }
652 }
653 }
654 #endif /* XSCALE_CACHE_WRITE_THROUGH */
655
656 if (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;
660 }
661
662 #if (ARM_NMMUS > 1)
663 xscale_use_minidata = 1;
664 #endif
665
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;
669
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;
673
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;
685
686 #else
687 pmap_copy_page_func = pmap_copy_page_xscale;
688 pmap_zero_page_func = pmap_zero_page_xscale;
689 #endif
690
691 /*
692 * Disable ECC protection of page table access, for now.
693 */
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));
697 }
698
699 /*
700 * xscale_setup_minidata:
701 *
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.
705 */
706 extern vm_offset_t xscale_minidata_clean_addr;
707 extern vm_size_t xscale_minidata_clean_size; /* already initialized */
708 void
709 xscale_setup_minidata(vm_offset_t l1pt, vm_offset_t va, vm_paddr_t pa)
710 {
711 pd_entry_t *pde = (pd_entry_t *) l1pt;
712 pt_entry_t *pte;
713 vm_size_t size;
714 uint32_t auxctl;
715
716 xscale_minidata_clean_addr = va;
717
718 /* Round it to page size. */
719 size = (xscale_minidata_clean_size + L2_S_OFFSET) & L2_S_FRAME;
720
721 for (; size != 0;
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);
725 if (pte == NULL)
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);
731 }
732
733 /*
734 * Configure the mini-data cache for write-back with
735 * read/write-allocate.
736 *
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!
740 *
741 * WE ASSUME WE ARE RUNNING UN-CACHED WHEN THIS ROUTINE IS CALLED!
742 * THIS IS VERY IMPORTANT!
743 */
744
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));
750 }
751 #endif
752
753 /*
754 * Allocate an L1 translation table for the specified pmap.
755 * This is called at pmap creation time.
756 */
757 static void
758 pmap_alloc_l1(pmap_t pm)
759 {
760 struct l1_ttable *l1;
761 u_int8_t domain;
762
763 /*
764 * Remove the L1 at the head of the LRU list
765 */
766 mtx_lock(&l1_lru_lock);
767 l1 = TAILQ_FIRST(&l1_lru_list);
768 TAILQ_REMOVE(&l1_lru_list, l1, l1_lru);
769
770 /*
771 * Pick the first available domain number, and update
772 * the link to the next number.
773 */
774 domain = l1->l1_domain_first;
775 l1->l1_domain_first = l1->l1_domain_free[domain];
776
777 /*
778 * If there are still free domain numbers in this L1,
779 * put it back on the TAIL of the LRU list.
780 */
781 if (++l1->l1_domain_use_count < PMAP_DOMAINS)
782 TAILQ_INSERT_TAIL(&l1_lru_list, l1, l1_lru);
783
784 mtx_unlock(&l1_lru_lock);
785
786 /*
787 * Fix up the relevant bits in the pmap structure
788 */
789 pm->pm_l1 = l1;
790 pm->pm_domain = domain + 1;
791 }
792
793 /*
794 * Free an L1 translation table.
795 * This is called at pmap destruction time.
796 */
797 static void
798 pmap_free_l1(pmap_t pm)
799 {
800 struct l1_ttable *l1 = pm->pm_l1;
801
802 mtx_lock(&l1_lru_lock);
803
804 /*
805 * If this L1 is currently on the LRU list, remove it.
806 */
807 if (l1->l1_domain_use_count < PMAP_DOMAINS)
808 TAILQ_REMOVE(&l1_lru_list, l1, l1_lru);
809
810 /*
811 * Free up the domain number which was allocated to the pmap
812 */
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--;
816
817 /*
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
821 * to the tail.
822 */
823 if (l1->l1_domain_use_count == 0) {
824 TAILQ_INSERT_HEAD(&l1_lru_list, l1, l1_lru);
825 } else
826 TAILQ_INSERT_TAIL(&l1_lru_list, l1, l1_lru);
827
828 mtx_unlock(&l1_lru_lock);
829 }
830
831 static PMAP_INLINE void
832 pmap_use_l1(pmap_t pm)
833 {
834 struct l1_ttable *l1;
835
836 /*
837 * Do nothing if we're in interrupt context.
838 * Access to an L1 by the kernel pmap must not affect
839 * the LRU list.
840 */
841 if (pm == pmap_kernel())
842 return;
843
844 l1 = pm->pm_l1;
845
846 /*
847 * If the L1 is not currently on the LRU list, just return
848 */
849 if (l1->l1_domain_use_count == PMAP_DOMAINS)
850 return;
851
852 mtx_lock(&l1_lru_lock);
853
854 /*
855 * Check the use count again, now that we've acquired the lock
856 */
857 if (l1->l1_domain_use_count == PMAP_DOMAINS) {
858 mtx_unlock(&l1_lru_lock);
859 return;
860 }
861
862 /*
863 * Move the L1 to the back of the LRU list
864 */
865 TAILQ_REMOVE(&l1_lru_list, l1, l1_lru);
866 TAILQ_INSERT_TAIL(&l1_lru_list, l1, l1_lru);
867
868 mtx_unlock(&l1_lru_lock);
869 }
870
871
872 /*
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.
875 */
876 static PMAP_INLINE struct l2_bucket *
877 pmap_get_l2_bucket(pmap_t pm, vm_offset_t va)
878 {
879 struct l2_dtable *l2;
880 struct l2_bucket *l2b;
881 u_short l1idx;
882
883 l1idx = L1_IDX(va);
884
885 if ((l2 = pm->pm_l2[L2_IDX(l1idx)]) == NULL ||
886 (l2b = &l2->l2_bucket[L2_BUCKET(l1idx)])->l2b_kva == NULL)
887 return (NULL);
888
889 return (l2b);
890 }
891
892 /*
893 * Returns a pointer to the L2 bucket associated with the specified pmap
894 * and VA.
895 *
896 * If no L2 bucket exists, perform the necessary allocations to put an L2
897 * bucket/page table in place.
898 *
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.
903 */
904 static struct l2_bucket *
905 pmap_alloc_l2_bucket(pmap_t pm, vm_offset_t va)
906 {
907 struct l2_dtable *l2;
908 struct l2_bucket *l2b;
909 u_short l1idx;
910
911 l1idx = L1_IDX(va);
912
913 PMAP_ASSERT_LOCKED(pm);
914 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
915 if ((l2 = pm->pm_l2[L2_IDX(l1idx)]) == NULL) {
916 /*
917 * No mapping at this address, as there is
918 * no entry in the L1 table.
919 * Need to allocate a new l2_dtable.
920 */
921 again_l2table:
922 PMAP_UNLOCK(pm);
923 vm_page_unlock_queues();
924 if ((l2 = pmap_alloc_l2_dtable()) == NULL) {
925 vm_page_lock_queues();
926 PMAP_LOCK(pm);
927 return (NULL);
928 }
929 vm_page_lock_queues();
930 PMAP_LOCK(pm);
931 if (pm->pm_l2[L2_IDX(l1idx)] != NULL) {
932 PMAP_UNLOCK(pm);
933 vm_page_unlock_queues();
934 uma_zfree(l2table_zone, l2);
935 vm_page_lock_queues();
936 PMAP_LOCK(pm);
937 l2 = pm->pm_l2[L2_IDX(l1idx)];
938 if (l2 == NULL)
939 goto again_l2table;
940 /*
941 * Someone already allocated the l2_dtable while
942 * we were doing the same.
943 */
944 } else {
945 bzero(l2, sizeof(*l2));
946 /*
947 * Link it into the parent pmap
948 */
949 pm->pm_l2[L2_IDX(l1idx)] = l2;
950 }
951 }
952
953 l2b = &l2->l2_bucket[L2_BUCKET(l1idx)];
954
955 /*
956 * Fetch pointer to the L2 page table associated with the address.
957 */
958 if (l2b->l2b_kva == NULL) {
959 pt_entry_t *ptep;
960
961 /*
962 * No L2 page table has been allocated. Chances are, this
963 * is because we just allocated the l2_dtable, above.
964 */
965 again_ptep:
966 PMAP_UNLOCK(pm);
967 vm_page_unlock_queues();
968 ptep = (void*)uma_zalloc(l2zone, M_NOWAIT|M_USE_RESERVE);
969 vm_page_lock_queues();
970 PMAP_LOCK(pm);
971 if (l2b->l2b_kva != 0) {
972 /* We lost the race. */
973 PMAP_UNLOCK(pm);
974 vm_page_unlock_queues();
975 uma_zfree(l2zone, ptep);
976 vm_page_lock_queues();
977 PMAP_LOCK(pm);
978 if (l2b->l2b_kva == 0)
979 goto again_ptep;
980 return (l2b);
981 }
982 l2b->l2b_phys = vtophys(ptep);
983 if (ptep == NULL) {
984 /*
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.
988 */
989 if (l2->l2_occupancy == 0) {
990 pm->pm_l2[L2_IDX(l1idx)] = NULL;
991 pmap_free_l2_dtable(l2);
992 }
993 return (NULL);
994 }
995
996 l2->l2_occupancy++;
997 l2b->l2b_kva = ptep;
998 l2b->l2b_l1idx = l1idx;
999 }
1000
1001 return (l2b);
1002 }
1003
1004 static PMAP_INLINE void
1005 #ifndef PMAP_INCLUDE_PTE_SYNC
1006 pmap_free_l2_ptp(pt_entry_t *l2)
1007 #else
1008 pmap_free_l2_ptp(boolean_t need_sync, pt_entry_t *l2)
1009 #endif
1010 {
1011 #ifdef PMAP_INCLUDE_PTE_SYNC
1012 /*
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.
1022 */
1023 if (need_sync)
1024 PTE_SYNC_RANGE(l2, L2_TABLE_SIZE_REAL / sizeof(pt_entry_t));
1025 #endif
1026 uma_zfree(l2zone, l2);
1027 }
1028 /*
1029 * One or more mappings in the specified L2 descriptor table have just been
1030 * invalidated.
1031 *
1032 * Garbage collect the metadata and descriptor table itself if necessary.
1033 *
1034 * The pmap lock must be acquired when this is called (not necessary
1035 * for the kernel pmap).
1036 */
1037 static void
1038 pmap_free_l2_bucket(pmap_t pm, struct l2_bucket *l2b, u_int count)
1039 {
1040 struct l2_dtable *l2;
1041 pd_entry_t *pl1pd, l1pd;
1042 pt_entry_t *ptep;
1043 u_short l1idx;
1044
1045
1046 /*
1047 * Update the bucket's reference count according to how many
1048 * PTEs the caller has just invalidated.
1049 */
1050 l2b->l2b_occupancy -= count;
1051
1052 /*
1053 * Note:
1054 *
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.
1059 *
1060 * We make do with just invalidating the mapping in the L2 table.
1061 *
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
1064 * alloc/free.
1065 */
1066 if (l2b->l2b_occupancy > 0 || pm == pmap_kernel())
1067 return;
1068
1069 /*
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.
1073 */
1074 l1idx = l2b->l2b_l1idx;
1075 ptep = l2b->l2b_kva;
1076 l2b->l2b_kva = NULL;
1077
1078 pl1pd = &pm->pm_l1->l1_kva[l1idx];
1079
1080 /*
1081 * If the L1 slot matches the pmap's domain
1082 * number, then invalidate it.
1083 */
1084 l1pd = *pl1pd & (L1_TYPE_MASK | L1_C_DOM_MASK);
1085 if (l1pd == (L1_C_DOM(pm->pm_domain) | L1_TYPE_C)) {
1086 *pl1pd = 0;
1087 PTE_SYNC(pl1pd);
1088 }
1089
1090 /*
1091 * Release the L2 descriptor table back to the pool cache.
1092 */
1093 #ifndef PMAP_INCLUDE_PTE_SYNC
1094 pmap_free_l2_ptp(ptep);
1095 #else
1096 pmap_free_l2_ptp(!pmap_is_current(pm), ptep);
1097 #endif
1098
1099 /*
1100 * Update the reference count in the associated l2_dtable
1101 */
1102 l2 = pm->pm_l2[L2_IDX(l1idx)];
1103 if (--l2->l2_occupancy > 0)
1104 return;
1105
1106 /*
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.
1110 */
1111 pm->pm_l2[L2_IDX(l1idx)] = NULL;
1112 pmap_free_l2_dtable(l2);
1113 }
1114
1115 /*
1116 * Pool cache constructors for L2 descriptor tables, metadata and pmap
1117 * structures.
1118 */
1119 static int
1120 pmap_l2ptp_ctor(void *mem, int size, void *arg, int flags)
1121 {
1122 #ifndef PMAP_INCLUDE_PTE_SYNC
1123 struct l2_bucket *l2b;
1124 pt_entry_t *ptep, pte;
1125 #ifdef ARM_USE_SMALL_ALLOC
1126 pd_entry_t *pde;
1127 #endif
1128 vm_offset_t va = (vm_offset_t)mem & ~PAGE_MASK;
1129
1130 /*
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
1136 * correct.
1137 */
1138 #ifdef ARM_USE_SMALL_ALLOC
1139 pde = &kernel_pmap->pm_l1->l1_kva[L1_IDX(va)];
1140 if (!l1pte_section_p(*pde)) {
1141 #endif
1142 l2b = pmap_get_l2_bucket(pmap_kernel(), va);
1143 ptep = &l2b->l2b_kva[l2pte_index(va)];
1144 pte = *ptep;
1145
1146 if ((pte & L2_S_CACHE_MASK) != pte_l2_s_cache_mode_pt) {
1147 /*
1148 * Page tables must have the cache-mode set to
1149 * Write-Thru.
1150 */
1151 *ptep = (pte & ~L2_S_CACHE_MASK) | pte_l2_s_cache_mode_pt;
1152 PTE_SYNC(ptep);
1153 cpu_tlb_flushD_SE(va);
1154 cpu_cpwait();
1155 }
1156 #ifdef ARM_USE_SMALL_ALLOC
1157 }
1158 #endif
1159 #endif
1160 memset(mem, 0, L2_TABLE_SIZE_REAL);
1161 PTE_SYNC_RANGE(mem, L2_TABLE_SIZE_REAL / sizeof(pt_entry_t));
1162 return (0);
1163 }
1164
1165 /*
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
1168 * given time.
1169 */
1170 static PMAP_INLINE void
1171 pmap_tlb_flushID_SE(pmap_t pm, vm_offset_t va)
1172 {
1173
1174 if (pmap_is_current(pm))
1175 cpu_tlb_flushID_SE(va);
1176 }
1177
1178 static PMAP_INLINE void
1179 pmap_tlb_flushD_SE(pmap_t pm, vm_offset_t va)
1180 {
1181
1182 if (pmap_is_current(pm))
1183 cpu_tlb_flushD_SE(va);
1184 }
1185
1186 static PMAP_INLINE void
1187 pmap_tlb_flushID(pmap_t pm)
1188 {
1189
1190 if (pmap_is_current(pm))
1191 cpu_tlb_flushID();
1192 }
1193 static PMAP_INLINE void
1194 pmap_tlb_flushD(pmap_t pm)
1195 {
1196
1197 if (pmap_is_current(pm))
1198 cpu_tlb_flushD();
1199 }
1200
1201 static PMAP_INLINE void
1202 pmap_idcache_wbinv_range(pmap_t pm, vm_offset_t va, vm_size_t len)
1203 {
1204
1205 if (pmap_is_current(pm))
1206 cpu_idcache_wbinv_range(va, len);
1207 }
1208
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)
1212 {
1213
1214 if (pmap_is_current(pm)) {
1215 if (do_inv) {
1216 if (rd_only)
1217 cpu_dcache_inv_range(va, len);
1218 else
1219 cpu_dcache_wbinv_range(va, len);
1220 } else
1221 if (!rd_only)
1222 cpu_dcache_wb_range(va, len);
1223 }
1224 }
1225
1226 static PMAP_INLINE void
1227 pmap_idcache_wbinv_all(pmap_t pm)
1228 {
1229
1230 if (pmap_is_current(pm))
1231 cpu_idcache_wbinv_all();
1232 }
1233
1234 static PMAP_INLINE void
1235 pmap_dcache_wbinv_all(pmap_t pm)
1236 {
1237
1238 if (pmap_is_current(pm))
1239 cpu_dcache_wbinv_all();
1240 }
1241
1242 /*
1243 * PTE_SYNC_CURRENT:
1244 *
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.
1250 */
1251 #ifdef PMAP_INCLUDE_PTE_SYNC
1252 #define PTE_SYNC_CURRENT(pm, ptep) \
1253 do { \
1254 if (PMAP_NEEDS_PTE_SYNC && \
1255 pmap_is_current(pm)) \
1256 PTE_SYNC(ptep); \
1257 } while (/*CONSTCOND*/0)
1258 #else
1259 #define PTE_SYNC_CURRENT(pm, ptep) /* nothing */
1260 #endif
1261
1262 /*
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.
1269 *
1270 * The code implements the following logic, where:
1271 *
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
1276 *
1277 * KC = kernel mapping is cacheable
1278 * UC = user mapping is cacheable
1279 *
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
1286 */
1287
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}
1293 };
1294
1295 static PMAP_INLINE int
1296 pmap_get_vac_flags(const struct vm_page *pg)
1297 {
1298 int kidx, uidx;
1299
1300 kidx = 0;
1301 if (pg->md.kro_mappings || pg->md.krw_mappings > 1)
1302 kidx |= 1;
1303 if (pg->md.krw_mappings)
1304 kidx |= 2;
1305
1306 uidx = 0;
1307 if (pg->md.uro_mappings || pg->md.urw_mappings > 1)
1308 uidx |= 1;
1309 if (pg->md.urw_mappings)
1310 uidx |= 2;
1311
1312 return (pmap_vac_flags[uidx][kidx]);
1313 }
1314
1315 static __inline void
1316 pmap_vac_me_harder(struct vm_page *pg, pmap_t pm, vm_offset_t va)
1317 {
1318 int nattr;
1319
1320 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1321 nattr = pmap_get_vac_flags(pg);
1322
1323 if (nattr < 0) {
1324 pg->md.pvh_attrs &= ~PVF_NC;
1325 return;
1326 }
1327
1328 if (nattr == 0 && (pg->md.pvh_attrs & PVF_NC) == 0) {
1329 return;
1330 }
1331
1332 if (pm == pmap_kernel())
1333 pmap_vac_me_kpmap(pg, pm, va);
1334 else
1335 pmap_vac_me_user(pg, pm, va);
1336
1337 pg->md.pvh_attrs = (pg->md.pvh_attrs & ~PVF_NC) | nattr;
1338 }
1339
1340 static void
1341 pmap_vac_me_kpmap(struct vm_page *pg, pmap_t pm, vm_offset_t va)
1342 {
1343 u_int u_cacheable, u_entries;
1344 struct pv_entry *pv;
1345 pmap_t last_pmap = pm;
1346
1347 /*
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.
1351 */
1352 u_cacheable = 0;
1353 TAILQ_FOREACH(pv, &pg->md.pv_list, pv_list) {
1354 if (pv->pv_pmap != pm && (pv->pv_flags & PVF_NC) == 0)
1355 u_cacheable++;
1356 }
1357
1358 u_entries = pg->md.urw_mappings + pg->md.uro_mappings;
1359
1360 /*
1361 * We know we have just been updating a kernel entry, so if
1362 * all user pages are already cacheable, then there is nothing
1363 * further to do.
1364 */
1365 if (pg->md.k_mappings == 0 && u_cacheable == u_entries)
1366 return;
1367
1368 if (u_entries) {
1369 /*
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.
1373 */
1374 TAILQ_FOREACH(pv, &pg->md.pv_list, pv_list) {
1375 /*
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.
1380 */
1381 if (pv->pv_pmap == pm || pv->pv_pmap == last_pmap)
1382 continue;
1383
1384 /*
1385 * If there are kernel entries and this page
1386 * is writable but non-cacheable, then we can
1387 * skip this entry also.
1388 */
1389 if (pg->md.k_mappings &&
1390 (pv->pv_flags & (PVF_NC | PVF_WRITE)) ==
1391 (PVF_NC | PVF_WRITE))
1392 continue;
1393
1394 /*
1395 * Similarly if there are no kernel-writable
1396 * entries and the page is already
1397 * read-only/cacheable.
1398 */
1399 if (pg->md.krw_mappings == 0 &&
1400 (pv->pv_flags & (PVF_NC | PVF_WRITE)) == 0)
1401 continue;
1402
1403 /*
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.
1408 */
1409 pmap_vac_me_user(pg, (last_pmap = pv->pv_pmap), 0);
1410 }
1411
1412 if (pg->md.k_mappings == 0)
1413 return;
1414 }
1415
1416 pmap_vac_me_user(pg, pm, va);
1417 }
1418
1419 static void
1420 pmap_vac_me_user(struct vm_page *pg, pmap_t pm, vm_offset_t va)
1421 {
1422 pmap_t kpmap = pmap_kernel();
1423 struct pv_entry *pv, *npv;
1424 struct l2_bucket *l2b;
1425 pt_entry_t *ptep, pte;
1426 u_int entries = 0;
1427 u_int writable = 0;
1428 u_int cacheable_entries = 0;
1429 u_int kern_cacheable = 0;
1430 u_int other_writable = 0;
1431
1432 /*
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.
1436 */
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) {
1441 if (entries++ == 0)
1442 npv = pv;
1443
1444 /* Cacheable mappings */
1445 if ((pv->pv_flags & PVF_NC) == 0) {
1446 cacheable_entries++;
1447 if (kpmap == pv->pv_pmap)
1448 kern_cacheable++;
1449 }
1450
1451 /* Writable mappings */
1452 if (pv->pv_flags & PVF_WRITE)
1453 ++writable;
1454 } else
1455 if (pv->pv_flags & PVF_WRITE)
1456 other_writable = 1;
1457 }
1458
1459 /*
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.
1464 */
1465 if ((entries > 1 && writable) ||
1466 (entries > 0 && pm == kpmap && other_writable)) {
1467 if (cacheable_entries == 0)
1468 return;
1469
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))
1473 continue;
1474
1475 pv->pv_flags |= PVF_NC;
1476
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;
1480
1481 if ((va != pv->pv_va || pm != pv->pv_pmap) &&
1482 l2pte_valid(pte)) {
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,
1487 pv->pv_va);
1488 } else
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,
1494 pv->pv_va);
1495 }
1496 }
1497
1498 *ptep = pte;
1499 PTE_SYNC_CURRENT(pv->pv_pmap, ptep);
1500 }
1501 cpu_cpwait();
1502 } else
1503 if (entries > cacheable_entries) {
1504 /*
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.
1507 */
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)))
1511 continue;
1512
1513 pv->pv_flags &= ~PVF_NC;
1514
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;
1518
1519 if (l2pte_valid(pte)) {
1520 if (PV_BEEN_EXECD(pv->pv_flags)) {
1521 pmap_tlb_flushID_SE(pv->pv_pmap,
1522 pv->pv_va);
1523 } else
1524 if (PV_BEEN_REFD(pv->pv_flags)) {
1525 pmap_tlb_flushD_SE(pv->pv_pmap,
1526 pv->pv_va);
1527 }
1528 }
1529
1530 *ptep = pte;
1531 PTE_SYNC_CURRENT(pv->pv_pmap, ptep);
1532 }
1533 }
1534 }
1535
1536 /*
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.
1540 */
1541 static int
1542 pmap_clearbit(struct vm_page *pg, u_int maskbits)
1543 {
1544 struct l2_bucket *l2b;
1545 struct pv_entry *pv;
1546 pt_entry_t *ptep, npte, opte;
1547 pmap_t pm;
1548 vm_offset_t va;
1549 u_int oflags;
1550 int count = 0;
1551
1552 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1553
1554 /*
1555 * Clear saved attributes (modify, reference)
1556 */
1557 pg->md.pvh_attrs &= ~(maskbits & (PVF_MOD | PVF_REF));
1558
1559 if (TAILQ_EMPTY(&pg->md.pv_list)) {
1560 return (0);
1561 }
1562
1563 /*
1564 * Loop over all current mappings setting/clearing as appropos
1565 */
1566 TAILQ_FOREACH(pv, &pg->md.pv_list, pv_list) {
1567 va = pv->pv_va;
1568 pm = pv->pv_pmap;
1569 oflags = pv->pv_flags;
1570 pv->pv_flags &= ~maskbits;
1571
1572 PMAP_LOCK(pm);
1573
1574 l2b = pmap_get_l2_bucket(pm, va);
1575
1576 ptep = &l2b->l2b_kva[l2pte_index(va)];
1577 npte = opte = *ptep;
1578
1579 if (maskbits & (PVF_WRITE|PVF_MOD)) {
1580 if ((pv->pv_flags & PVF_NC)) {
1581 /*
1582 * Entry is not cacheable:
1583 *
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.
1589 *
1590 * There's no need to call pmap_vac_me_harder()
1591 * here: all pages are losing their write
1592 * permission.
1593 */
1594 if (maskbits & PVF_WRITE) {
1595 npte |= pte_l2_s_cache_mode;
1596 pv->pv_flags &= ~PVF_NC;
1597 }
1598 } else
1599 if (opte & L2_S_PROT_W) {
1600 vm_page_dirty(pg);
1601 /*
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
1605 */
1606 if (PV_BEEN_EXECD(oflags))
1607 pmap_idcache_wbinv_range(pm, pv->pv_va,
1608 PAGE_SIZE);
1609 else
1610 if (PV_BEEN_REFD(oflags))
1611 pmap_dcache_wb_range(pm, pv->pv_va,
1612 PAGE_SIZE,
1613 (maskbits & PVF_REF) ? TRUE : FALSE,
1614 FALSE);
1615 }
1616
1617 /* make the pte read only */
1618 npte &= ~L2_S_PROT_W;
1619
1620 if (maskbits & PVF_WRITE) {
1621 /*
1622 * Keep alias accounting up to date
1623 */
1624 if (pv->pv_pmap == pmap_kernel()) {
1625 if (oflags & PVF_WRITE) {
1626 pg->md.krw_mappings--;
1627 pg->md.kro_mappings++;
1628 }
1629 } else
1630 if (oflags & PVF_WRITE) {
1631 pg->md.urw_mappings--;
1632 pg->md.uro_mappings++;
1633 }
1634 }
1635 }
1636
1637 if (maskbits & PVF_REF) {
1638 if ((pv->pv_flags & PVF_NC) == 0 &&
1639 (maskbits & (PVF_WRITE|PVF_MOD)) == 0) {
1640 /*
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
1644 * npte.
1645 */
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);
1650 else
1651 if (PV_BEEN_REFD(oflags))
1652 pmap_dcache_wb_range(pm,
1653 pv->pv_va, PAGE_SIZE,
1654 TRUE, FALSE);
1655 } else
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);
1661 else
1662 if (PV_BEEN_REFD(oflags))
1663 pmap_dcache_wb_range(pm,
1664 pv->pv_va, PAGE_SIZE,
1665 TRUE, TRUE);
1666 }
1667 }
1668
1669 /*
1670 * Make the PTE invalid so that we will take a
1671 * page fault the next time the mapping is
1672 * referenced.
1673 */
1674 npte &= ~L2_TYPE_MASK;
1675 npte |= L2_TYPE_INV;
1676 }
1677
1678 if (npte != opte) {
1679 count++;
1680 *ptep = npte;
1681 PTE_SYNC(ptep);
1682 /* Flush the TLB entry if a current pmap. */
1683 if (PV_BEEN_EXECD(oflags))
1684 pmap_tlb_flushID_SE(pm, pv->pv_va);
1685 else
1686 if (PV_BEEN_REFD(oflags))
1687 pmap_tlb_flushD_SE(pm, pv->pv_va);
1688 }
1689
1690 PMAP_UNLOCK(pm);
1691
1692 }
1693
1694 if (maskbits & PVF_WRITE)
1695 vm_page_flag_clear(pg, PG_WRITEABLE);
1696 return (count);
1697 }
1698
1699 /*
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
1703 *
1704 * NOTE: pmap_enter_pv expects to lock the pvh itself
1705 * pmap_remove_pv expects te caller to lock the pvh before calling
1706 */
1707
1708 /*
1709 * pmap_enter_pv: enter a mapping onto a vm_page lst
1710 *
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
1716 */
1717 static void
1718 pmap_enter_pv(struct vm_page *pg, struct pv_entry *pve, pmap_t pm,
1719 vm_offset_t va, u_int flags)
1720 {
1721
1722 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1723 PMAP_ASSERT_LOCKED(pm);
1724 pve->pv_pmap = pm;
1725 pve->pv_va = va;
1726 pve->pv_flags = flags;
1727
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++;
1734 else
1735 pg->md.kro_mappings++;
1736 }
1737 if (flags & PVF_WRITE)
1738 pg->md.urw_mappings++;
1739 else
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);
1745 }
1746
1747 /*
1748 *
1749 * pmap_find_pv: Find a pv entry
1750 *
1751 * => caller should hold lock on vm_page
1752 */
1753 static PMAP_INLINE struct pv_entry *
1754 pmap_find_pv(struct vm_page *pg, pmap_t pm, vm_offset_t va)
1755 {
1756 struct pv_entry *pv;
1757
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)
1761 break;
1762 return (pv);
1763 }
1764
1765 /*
1766 * vector_page_setprot:
1767 *
1768 * Manipulate the protection of the vector page.
1769 */
1770 void
1771 vector_page_setprot(int prot)
1772 {
1773 struct l2_bucket *l2b;
1774 pt_entry_t *ptep;
1775
1776 l2b = pmap_get_l2_bucket(pmap_kernel(), vector_page);
1777
1778 ptep = &l2b->l2b_kva[l2pte_index(vector_page)];
1779
1780 *ptep = (*ptep & ~L1_S_PROT_MASK) | L2_S_PROT(PTE_KERNEL, prot);
1781 PTE_SYNC(ptep);
1782 cpu_tlb_flushD_SE(vector_page);
1783 cpu_cpwait();
1784 }
1785
1786 /*
1787 * pmap_remove_pv: try to remove a mapping from a pv_list
1788 *
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
1795 */
1796
1797 static void
1798 pmap_nuke_pv(struct vm_page *pg, pmap_t pm, struct pv_entry *pve)
1799 {
1800
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)
1809 vm_page_dirty(pg);
1810 if (pm == pmap_kernel()) {
1811 if (pve->pv_flags & PVF_WRITE)
1812 pg->md.krw_mappings--;
1813 else
1814 pg->md.kro_mappings--;
1815 } else
1816 if (pve->pv_flags & PVF_WRITE)
1817 pg->md.urw_mappings--;
1818 else
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);
1826 }
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);
1831 }
1832
1833 static struct pv_entry *
1834 pmap_remove_pv(struct vm_page *pg, pmap_t pm, vm_offset_t va)
1835 {
1836 struct pv_entry *pve;
1837
1838 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1839 pve = TAILQ_FIRST(&pg->md.pv_list);
1840
1841 while (pve) {
1842 if (pve->pv_pmap == pm && pve->pv_va == va) { /* match? */
1843 pmap_nuke_pv(pg, pm, pve);
1844 break;
1845 }
1846 pve = TAILQ_NEXT(pve, pv_list);
1847 }
1848
1849 return(pve); /* return removed pve */
1850 }
1851 /*
1852 *
1853 * pmap_modify_pv: Update pv flags
1854 *
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
1858 * may have changed.
1859 * => we return the old flags
1860 *
1861 * Modify a physical-virtual mapping in the pv table
1862 */
1863 static u_int
1864 pmap_modify_pv(struct vm_page *pg, pmap_t pm, vm_offset_t va,
1865 u_int clr_mask, u_int set_mask)
1866 {
1867 struct pv_entry *npv;
1868 u_int flags, oflags;
1869
1870 PMAP_ASSERT_LOCKED(pm);
1871 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1872 if ((npv = pmap_find_pv(pg, pm, va)) == NULL)
1873 return (0);
1874
1875 /*
1876 * There is at least one VA mapping this page.
1877 */
1878
1879 if (clr_mask & (PVF_REF | PVF_MOD))
1880 pg->md.pvh_attrs |= set_mask & (PVF_REF | PVF_MOD);
1881
1882 oflags = npv->pv_flags;
1883 npv->pv_flags = flags = (oflags & ~clr_mask) | set_mask;
1884
1885 if ((flags ^ oflags) & PVF_WIRED) {
1886 if (flags & PVF_WIRED)
1887 ++pm->pm_stats.wired_count;
1888 else
1889 --pm->pm_stats.wired_count;
1890 }
1891
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--;
1897 } else {
1898 pg->md.kro_mappings++;
1899 pg->md.krw_mappings--;
1900 }
1901 } else
1902 if (flags & PVF_WRITE) {
1903 pg->md.urw_mappings++;
1904 pg->md.uro_mappings--;
1905 } else {
1906 pg->md.uro_mappings++;
1907 pg->md.urw_mappings--;
1908 }
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);
1912 }
1913 pmap_vac_me_harder(pg, pm, 0);
1914 }
1915
1916 return (oflags);
1917 }
1918
1919 /* Function to set the debug level of the pmap code */
1920 #ifdef PMAP_DEBUG
1921 void
1922 pmap_debug(int level)
1923 {
1924 pmap_debug_level = level;
1925 dprintf("pmap_debug: level=%d\n", pmap_debug_level);
1926 }
1927 #endif /* PMAP_DEBUG */
1928
1929 void
1930 pmap_pinit0(struct pmap *pmap)
1931 {
1932 PDEBUG(1, printf("pmap_pinit0: pmap = %08x\n", (u_int32_t) pmap));
1933
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);
1939 }
1940
1941 /*
1942 * Initialize a vm_page's machine-dependent fields.
1943 */
1944 void
1945 pmap_page_init(vm_page_t m)
1946 {
1947
1948 TAILQ_INIT(&m->md.pv_list);
1949 m->md.pv_list_count = 0;
1950 }
1951
1952 /*
1953 * Initialize the pmap module.
1954 * Called by vm_init, to initialize any structures that the pmap
1955 * system needs to map virtual memory.
1956 */
1957 void
1958 pmap_init(void)
1959 {
1960 int shpgperproc = PMAP_SHPGPERPROC;
1961
1962 PDEBUG(1, printf("pmap_init: phys_start = %08x\n"));
1963
1964 /*
1965 * init the pv free list
1966 */
1967 pvzone = uma_zcreate("PV ENTRY", sizeof (struct pv_entry), NULL, NULL,
1968 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_VM | UMA_ZONE_NOFREE);
1969 /*
1970 * Now it is safe to enable pv_table recording.
1971 */
1972 PDEBUG(1, printf("pmap_init: done!\n"));
1973
1974 TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
1975
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);
1983
1984 uma_zone_set_obj(pvzone, &pvzone_obj, pv_entry_max);
1985
1986 }
1987
1988 int
1989 pmap_fault_fixup(pmap_t pm, vm_offset_t va, vm_prot_t ftype, int user)
1990 {
1991 struct l2_dtable *l2;
1992 struct l2_bucket *l2b;
1993 pd_entry_t *pl1pd, l1pd;
1994 pt_entry_t *ptep, pte;
1995 vm_paddr_t pa;
1996 u_int l1idx;
1997 int rv = 0;
1998
1999 l1idx = L1_IDX(va);
2000 vm_page_lock_queues();
2001 PMAP_LOCK(pm);
2002
2003 /*
2004 * If there is no l2_dtable for this address, then the process
2005 * has no business accessing it.
2006 *
2007 * Note: This will catch userland processes trying to access
2008 * kernel addresses.
2009 */
2010 l2 = pm->pm_l2[L2_IDX(l1idx)];
2011 if (l2 == NULL)
2012 goto out;
2013
2014 /*
2015 * Likewise if there is no L2 descriptor table
2016 */
2017 l2b = &l2->l2_bucket[L2_BUCKET(l1idx)];
2018 if (l2b->l2b_kva == NULL)
2019 goto out;
2020
2021 /*
2022 * Check the PTE itself.
2023 */
2024 ptep = &l2b->l2b_kva[l2pte_index(va)];
2025 pte = *ptep;
2026 if (pte == 0)
2027 goto out;
2028
2029 /*
2030 * Catch a userland access to the vector page mapped at 0x0
2031 */
2032 if (user && (pte & L2_S_PROT_U) == 0)
2033 goto out;
2034 if (va == vector_page)
2035 goto out;
2036
2037 pa = l2pte_pa(pte);
2038
2039 if ((ftype & VM_PROT_WRITE) && (pte & L2_S_PROT_W) == 0) {
2040 /*
2041 * This looks like a good candidate for "page modified"
2042 * emulation...
2043 */
2044 struct pv_entry *pv;
2045 struct vm_page *pg;
2046
2047 /* Extract the physical address of the page */
2048 if ((pg = PHYS_TO_VM_PAGE(pa)) == NULL) {
2049 goto out;
2050 }
2051 /* Get the current flags for this page. */
2052
2053 pv = pmap_find_pv(pg, pm, va);
2054 if (pv == NULL) {
2055 goto out;
2056 }
2057
2058 /*
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
2064 */
2065 if ((pv->pv_flags & PVF_WRITE) == 0) {
2066 goto out;
2067 }
2068
2069 pg->md.pvh_attrs |= PVF_REF | PVF_MOD;
2070 vm_page_dirty(pg);
2071 pv->pv_flags |= PVF_REF | PVF_MOD;
2072
2073 /*
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.
2079 */
2080 *ptep = (pte & ~L2_TYPE_MASK) | L2_S_PROTO | L2_S_PROT_W;
2081 PTE_SYNC(ptep);
2082 rv = 1;
2083 } else
2084 if ((pte & L2_TYPE_MASK) == L2_TYPE_INV) {
2085 /*
2086 * This looks like a good candidate for "page referenced"
2087 * emulation.
2088 */
2089 struct pv_entry *pv;
2090 struct vm_page *pg;
2091
2092 /* Extract the physical address of the page */
2093 if ((pg = PHYS_TO_VM_PAGE(pa)) == NULL)
2094 goto out;
2095 /* Get the current flags for this page. */
2096
2097 pv = pmap_find_pv(pg, pm, va);
2098 if (pv == NULL)
2099 goto out;
2100
2101 pg->md.pvh_attrs |= PVF_REF;
2102 pv->pv_flags |= PVF_REF;
2103
2104
2105 *ptep = (pte & ~L2_TYPE_MASK) | L2_S_PROTO;
2106 PTE_SYNC(ptep);
2107 rv = 1;
2108 }
2109
2110 /*
2111 * We know there is a valid mapping here, so simply
2112 * fix up the L1 if necessary.
2113 */
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) {
2117 *pl1pd = l1pd;
2118 PTE_SYNC(pl1pd);
2119 rv = 1;
2120 }
2121
2122 #ifdef CPU_SA110
2123 /*
2124 * There are bugs in the rev K SA110. This is a check for one
2125 * of them.
2126 */
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());
2140 }
2141 #ifdef DDB
2142 if (kernel_debug & 2)
2143 Debugger();
2144 #endif
2145 rv = 1;
2146 }
2147 }
2148 #endif /* CPU_SA110 */
2149
2150 #ifdef DEBUG
2151 /*
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
2158 * example.
2159 *
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.
2165 *
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.
2168 *
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.
2172 *
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
2176 * the TLB.
2177 */
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",
2181 pm, va, ftype);
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);
2186 #ifdef DDB
2187 Debugger();
2188 #endif
2189 }
2190 #endif
2191
2192 cpu_tlb_flushID_SE(va);
2193 cpu_cpwait();
2194
2195 rv = 1;
2196
2197 out:
2198 vm_page_unlock_queues();
2199 PMAP_UNLOCK(pm);
2200 return (rv);
2201 }
2202
2203 void
2204 pmap_postinit(void)
2205 {
2206 struct l2_bucket *l2b;
2207 struct l1_ttable *l1;
2208 pd_entry_t *pl1pt;
2209 pt_entry_t *ptep, pte;
2210 vm_offset_t va, eva;
2211 u_int loop, needed;
2212
2213 needed = (maxproc / PMAP_DOMAINS) + ((maxproc % PMAP_DOMAINS) ? 1 : 0);
2214 needed -= 1;
2215 l1 = malloc(sizeof(*l1) * needed, M_VMPMAP, M_WAITOK);
2216
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);
2221
2222 if (va == 0)
2223 panic("Cannot allocate L1 KVM");
2224
2225 eva = va + L1_TABLE_SIZE;
2226 pl1pt = (pd_entry_t *)va;
2227
2228 while (va < eva) {
2229 l2b = pmap_get_l2_bucket(pmap_kernel(), va);
2230 ptep = &l2b->l2b_kva[l2pte_index(va)];
2231 pte = *ptep;
2232 pte = (pte & ~L2_S_CACHE_MASK) | pte_l2_s_cache_mode_pt;
2233 *ptep = pte;
2234 PTE_SYNC(ptep);
2235 cpu_tlb_flushD_SE(va);
2236
2237 va += PAGE_SIZE;
2238 }
2239 pmap_init_l1(l1, pl1pt);
2240 }
2241
2242
2243 #ifdef DEBUG
2244 printf("pmap_postinit: Allocated %d static L1 descriptor tables\n",
2245 needed);
2246 #endif
2247 }
2248
2249 /*
2250 * This is used to stuff certain critical values into the PCB where they
2251 * can be accessed quickly from cpu_switch() et al.
2252 */
2253 void
2254 pmap_set_pcb_pagedir(pmap_t pm, struct pcb *pcb)
2255 {
2256 struct l2_bucket *l2b;
2257
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));
2261
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);
2267 } else
2268 pcb->pcb_pl1vec = NULL;
2269 }
2270
2271 void
2272 pmap_activate(struct thread *td)
2273 {
2274 pmap_t pm;
2275 struct pcb *pcb;
2276
2277 pm = vmspace_pmap(td->td_proc->p_vmspace);
2278 pcb = td->td_pcb;
2279
2280 critical_enter();
2281 pmap_set_pcb_pagedir(pm, pcb);
2282
2283 if (td == curthread) {
2284 u_int cur_dacr, cur_ttb;
2285
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));
2288
2289 cur_ttb &= ~(L1_TABLE_SIZE - 1);
2290
2291 if (cur_ttb == (u_int)pcb->pcb_pagedir &&
2292 cur_dacr == pcb->pcb_dacr) {
2293 /*
2294 * No need to switch address spaces.
2295 */
2296 critical_exit();
2297 return;
2298 }
2299
2300
2301 /*
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.
2306 */
2307 if (pcb->pcb_pl1vec) {
2308
2309 *pcb->pcb_pl1vec = pcb->pcb_l1vec;
2310 /*
2311 * Don't need to PTE_SYNC() at this point since
2312 * cpu_setttb() is about to flush both the cache
2313 * and the TLB.
2314 */
2315 }
2316
2317 cpu_domains(pcb->pcb_dacr);
2318 cpu_setttb(pcb->pcb_pagedir);
2319 }
2320 critical_exit();
2321 }
2322
2323 static int
2324 pmap_set_pt_cache_mode(pd_entry_t *kl1, vm_offset_t va)
2325 {
2326 pd_entry_t *pdep, pde;
2327 pt_entry_t *ptep, pte;
2328 vm_offset_t pa;
2329 int rv = 0;
2330
2331 /*
2332 * Make sure the descriptor itself has the correct cache mode
2333 */
2334 pdep = &kl1[L1_IDX(va)];
2335 pde = *pdep;
2336
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;
2341 PTE_SYNC(pdep);
2342 cpu_dcache_wbinv_range((vm_offset_t)pdep,
2343 sizeof(*pdep));
2344 rv = 1;
2345 }
2346 } else {
2347 pa = (vm_paddr_t)(pde & L1_C_ADDR_MASK);
2348 ptep = (pt_entry_t *)kernel_pt_lookup(pa);
2349 if (ptep == NULL)
2350 panic("pmap_bootstrap: No L2 for L2 @ va %p\n", ptep);
2351
2352 ptep = &ptep[l2pte_index(va)];
2353 pte = *ptep;
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;
2357 PTE_SYNC(ptep);
2358 cpu_dcache_wbinv_range((vm_offset_t)ptep,
2359 sizeof(*ptep));
2360 rv = 1;
2361 }
2362 }
2363
2364 return (rv);
2365 }
2366
2367 static void
2368 pmap_alloc_specials(vm_offset_t *availp, int pages, vm_offset_t *vap,
2369 pt_entry_t **ptep)
2370 {
2371 vm_offset_t va = *availp;
2372 struct l2_bucket *l2b;
2373
2374 if (ptep) {
2375 l2b = pmap_get_l2_bucket(pmap_kernel(), va);
2376 if (l2b == NULL)
2377 panic("pmap_alloc_specials: no l2b for 0x%x", va);
2378
2379 *ptep = &l2b->l2b_kva[l2pte_index(va)];
2380 }
2381
2382 *vap = va;
2383 *availp = va + (PAGE_SIZE * pages);
2384 }
2385
2386 /*
2387 * Bootstrap the system enough to run with virtual memory.
2388 *
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]
2395 */
2396 #define PMAP_STATIC_L2_SIZE 16
2397 #ifdef ARM_USE_SMALL_ALLOC
2398 extern struct mtx smallalloc_mtx;
2399 #endif
2400
2401 void
2402 pmap_bootstrap(vm_offset_t firstaddr, vm_offset_t lastaddr, struct pv_addr *l1pt)
2403 {
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;
2409 pd_entry_t pde;
2410 pd_entry_t *kernel_l1pt = (pd_entry_t *)l1pt->pv_va;
2411 pt_entry_t *ptep;
2412 vm_paddr_t pa;
2413 vm_offset_t va;
2414 vm_size_t size;
2415 int l1idx, l2idx, l2next = 0;
2416
2417 PDEBUG(1, printf("firstaddr = %08x, loadaddr = %08x\n",
2418 firstaddr, loadaddr));
2419
2420 virtual_avail = firstaddr;
2421 kernel_pmap = &kernel_pmap_store;
2422 kernel_pmap->pm_l1 = l1;
2423 kernel_l1pa = l1pt->pv_pa;
2424
2425 /*
2426 * Scan the L1 translation table created by initarm() and create
2427 * the required metadata for all valid mappings found in it.
2428 */
2429 for (l1idx = 0; l1idx < (L1_TABLE_SIZE / sizeof(pd_entry_t)); l1idx++) {
2430 pde = kernel_l1pt[l1idx];
2431
2432 /*
2433 * We're only interested in Coarse mappings.
2434 * pmap_extract() can deal with section mappings without
2435 * recourse to checking L2 metadata.
2436 */
2437 if ((pde & L1_TYPE_MASK) != L1_TYPE_C)
2438 continue;
2439
2440 /*
2441 * Lookup the KVA of this L2 descriptor table
2442 */
2443 pa = (vm_paddr_t)(pde & L1_C_ADDR_MASK);
2444 ptep = (pt_entry_t *)kernel_pt_lookup(pa);
2445
2446 if (ptep == NULL) {
2447 panic("pmap_bootstrap: No L2 for va 0x%x, pa 0x%lx",
2448 (u_int)l1idx << L1_S_SHIFT, (long unsigned int)pa);
2449 }
2450
2451 /*
2452 * Fetch the associated L2 metadata structure.
2453 * Allocate a new one if necessary.
2454 */
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++];
2460 }
2461
2462 /*
2463 * One more L1 slot tracked...
2464 */
2465 l2->l2_occupancy++;
2466
2467 /*
2468 * Fill in the details of the L2 descriptor in the
2469 * appropriate bucket.
2470 */
2471 l2b = &l2->l2_bucket[L2_BUCKET(l1idx)];
2472 l2b->l2b_kva = ptep;
2473 l2b->l2b_phys = pa;
2474 l2b->l2b_l1idx = l1idx;
2475
2476 /*
2477 * Establish an initial occupancy count for this descriptor
2478 */
2479 for (l2idx = 0;
2480 l2idx < (L2_TABLE_SIZE_REAL / sizeof(pt_entry_t));
2481 l2idx++) {
2482 if ((ptep[l2idx] & L2_TYPE_MASK) != L2_TYPE_INV) {
2483 l2b->l2b_occupancy++;
2484 }
2485 }
2486
2487 /*
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()
2491 * function. :)
2492 */
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);
2496 }
2497 }
2498
2499
2500 /*
2501 * Ensure the primary (kernel) L1 has the correct cache mode for
2502 * a page table. Bitch if it is not correctly set.
2503 */
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);
2509 }
2510
2511 cpu_dcache_wbinv_all();
2512 cpu_tlb_flushID();
2513 cpu_cpwait();
2514
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);
2519
2520 /*
2521 * Reserve some special page table entries/VA space for temporary
2522 * mapping of pages.
2523 */
2524 #define SYSMAP(c, p, v, n) \
2525 v = (c)va; va += ((n)*PAGE_SIZE); p = pte; pte += (n);
2526
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);
2535
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);
2540
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();
2548
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);
2554
2555 #ifdef ARM_USE_SMALL_ALLOC
2556 mtx_init(&smallalloc_mtx, "Small alloc page list", NULL, MTX_DEF);
2557 arm_init_smallalloc();
2558 #endif
2559 pmap_set_pcb_pagedir(kernel_pmap, thread0.td_pcb);
2560 }
2561
2562 /***************************************************
2563 * Pmap allocation/deallocation routines.
2564 ***************************************************/
2565
2566 /*
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.
2570 */
2571 void
2572 pmap_release(pmap_t pmap)
2573 {
2574 struct pcb *pcb;
2575
2576 pmap_idcache_wbinv_all(pmap);
2577 pmap_tlb_flushID(pmap);
2578 cpu_cpwait();
2579 if (vector_page < KERNBASE) {
2580 struct pcb *curpcb = PCPU_GET(curpcb);
2581 pcb = thread0.td_pcb;
2582 if (pmap_is_current(pmap)) {
2583 /*
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.
2588 */
2589 critical_enter();
2590 *pcb->pcb_pl1vec = pcb->pcb_l1vec;
2591 cpu_domains(pcb->pcb_dacr);
2592 cpu_setttb(pcb->pcb_pagedir);
2593 critical_exit();
2594 }
2595 pmap_remove(pmap, vector_page, vector_page + PAGE_SIZE);
2596 /*
2597 * Make sure cpu_switch(), et al, DTRT. This is safe to do
2598 * since this process has no remaining mappings of its own.
2599 */
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;
2604
2605 }
2606 pmap_free_l1(pmap);
2607 PMAP_LOCK_DESTROY(pmap);
2608
2609 dprintf("pmap_release()\n");
2610 }
2611
2612
2613
2614 /*
2615 * Helper function for pmap_grow_l2_bucket()
2616 */
2617 static __inline int
2618 pmap_grow_map(vm_offset_t va, pt_entry_t cache_mode, vm_paddr_t *pap)
2619 {
2620 struct l2_bucket *l2b;
2621 pt_entry_t *ptep;
2622 vm_paddr_t pa;
2623 struct vm_page *pg;
2624
2625 pg = vm_page_alloc(NULL, 0, VM_ALLOC_NOOBJ | VM_ALLOC_WIRED);
2626 if (pg == NULL)
2627 return (1);
2628 pa = VM_PAGE_TO_PHYS(pg);
2629
2630 if (pap)
2631 *pap = pa;
2632
2633 l2b = pmap_get_l2_bucket(pmap_kernel(), va);
2634
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);
2638 PTE_SYNC(ptep);
2639 return (0);
2640 }
2641
2642 /*
2643 * This is the same as pmap_alloc_l2_bucket(), except that it is only
2644 * used by pmap_growkernel().
2645 */
2646 static __inline struct l2_bucket *
2647 pmap_grow_l2_bucket(pmap_t pm, vm_offset_t va)
2648 {
2649 struct l2_dtable *l2;
2650 struct l2_bucket *l2b;
2651 struct l1_ttable *l1;
2652 pd_entry_t *pl1pd;
2653 u_short l1idx;
2654 vm_offset_t nva;
2655
2656 l1idx = L1_IDX(va);
2657
2658 if ((l2 = pm->pm_l2[L2_IDX(l1idx)]) == NULL) {
2659 /*
2660 * No mapping at this address, as there is
2661 * no entry in the L1 table.
2662 * Need to allocate a new l2_dtable.
2663 */
2664 nva = pmap_kernel_l2dtable_kva;
2665 if ((nva & PAGE_MASK) == 0) {
2666 /*
2667 * Need to allocate a backing page
2668 */
2669 if (pmap_grow_map(nva, pte_l2_s_cache_mode, NULL))
2670 return (NULL);
2671 }
2672
2673 l2 = (struct l2_dtable *)nva;
2674 nva += sizeof(struct l2_dtable);
2675
2676 if ((nva & PAGE_MASK) < (pmap_kernel_l2dtable_kva &
2677 PAGE_MASK)) {
2678 /*
2679 * The new l2_dtable straddles a page boundary.
2680 * Map in another page to cover it.
2681 */
2682 if (pmap_grow_map(nva, pte_l2_s_cache_mode, NULL))
2683 return (NULL);
2684 }
2685
2686 pmap_kernel_l2dtable_kva = nva;
2687
2688 /*
2689 * Link it into the parent pmap
2690 */
2691 pm->pm_l2[L2_IDX(l1idx)] = l2;
2692 memset(l2, 0, sizeof(*l2));
2693 }
2694
2695 l2b = &l2->l2_bucket[L2_BUCKET(l1idx)];
2696
2697 /*
2698 * Fetch pointer to the L2 page table associated with the address.
2699 */
2700 if (l2b->l2b_kva == NULL) {
2701 pt_entry_t *ptep;
2702
2703 /*
2704 * No L2 page table has been allocated. Chances are, this
2705 * is because we just allocated the l2_dtable, above.
2706 */
2707 nva = pmap_kernel_l2ptp_kva;
2708 ptep = (pt_entry_t *)nva;
2709 if ((nva & PAGE_MASK) == 0) {
2710 /*
2711 * Need to allocate a backing page
2712 */
2713 if (pmap_grow_map(nva, pte_l2_s_cache_mode_pt,
2714 &pmap_kernel_l2ptp_phys))
2715 return (NULL);
2716 PTE_SYNC_RANGE(ptep, PAGE_SIZE / sizeof(pt_entry_t));
2717 }
2718 memset(ptep, 0, L2_TABLE_SIZE_REAL);
2719 l2->l2_occupancy++;
2720 l2b->l2b_kva = ptep;
2721 l2b->l2b_l1idx = l1idx;
2722 l2b->l2b_phys = pmap_kernel_l2ptp_phys;
2723
2724 pmap_kernel_l2ptp_kva += L2_TABLE_SIZE_REAL;
2725 pmap_kernel_l2ptp_phys += L2_TABLE_SIZE_REAL;
2726 }
2727
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) |
2732 L1_C_PROTO;
2733 PTE_SYNC(pl1pd);
2734 }
2735
2736 return (l2b);
2737 }
2738
2739
2740 /*
2741 * grow the number of kernel page table entries, if needed
2742 */
2743 void
2744 pmap_growkernel(vm_offset_t addr)
2745 {
2746 pmap_t kpm = pmap_kernel();
2747
2748 if (addr <= pmap_curmaxkvaddr)
2749 return; /* we are OK */
2750
2751 /*
2752 * whoops! we need to add kernel PTPs
2753 */
2754
2755 /* Map 1MB at a time */
2756 for (; pmap_curmaxkvaddr < addr; pmap_curmaxkvaddr += L1_S_SIZE)
2757 pmap_grow_l2_bucket(kpm, pmap_curmaxkvaddr);
2758
2759 /*
2760 * flush out the cache, expensive but growkernel will happen so
2761 * rarely
2762 */
2763 cpu_dcache_wbinv_all();
2764 cpu_tlb_flushD();
2765 cpu_cpwait();
2766 kernel_vm_end = pmap_curmaxkvaddr;
2767
2768 }
2769
2770
2771 /*
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.
2778 */
2779 void
2780 pmap_remove_pages(pmap_t pmap)
2781 {
2782 struct pv_entry *pv, *npv;
2783 struct l2_bucket *l2b = NULL;
2784 vm_page_t m;
2785 pt_entry_t *pt;
2786
2787 vm_page_lock_queues();
2788 PMAP_LOCK(pmap);
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);
2793 continue;
2794 }
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));
2802 #else
2803 KASSERT((vm_offset_t)m >= KERNBASE, ("Trying to access non-existent page va %x pte %x", pv->pv_va, *pt));
2804 #endif
2805 *pt = 0;
2806 PTE_SYNC(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);
2813 }
2814 vm_page_unlock_queues();
2815 cpu_idcache_wbinv_all();
2816 cpu_tlb_flushID();
2817 cpu_cpwait();
2818 PMAP_UNLOCK(pmap);
2819 }
2820
2821
2822 /***************************************************
2823 * Low level mapping routines.....
2824 ***************************************************/
2825
2826 #ifdef ARM_HAVE_SUPERSECTIONS
2827 /* Map a super section into the KVA. */
2828
2829 void
2830 pmap_kenter_supersection(vm_offset_t va, uint64_t pa, int flags)
2831 {
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;
2837
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) {
2847 va = va0;
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)]);
2851 }
2852 }
2853 }
2854 #endif
2855
2856 /* Map a section into the KVA. */
2857
2858 void
2859 pmap_kenter_section(vm_offset_t va, vm_offset_t pa, int flags)
2860 {
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;
2864
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)]);
2874 }
2875 }
2876
2877 /*
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...
2881 */
2882 static PMAP_INLINE void
2883 pmap_kenter_internal(vm_offset_t va, vm_offset_t pa, int flags)
2884 {
2885 struct l2_bucket *l2b;
2886 pt_entry_t *pte;
2887 pt_entry_t opte;
2888 PDEBUG(1, printf("pmap_kenter: va = %08x, pa = %08x\n",
2889 (uint32_t) va, (uint32_t) pa));
2890
2891
2892 l2b = pmap_get_l2_bucket(pmap_kernel(), va);
2893 if (l2b == NULL)
2894 l2b = pmap_grow_l2_bucket(pmap_kernel(), va);
2895 KASSERT(l2b != NULL, ("No L2 Bucket"));
2896 pte = &l2b->l2b_kva[l2pte_index(va)];
2897 opte = *pte;
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);
2903 cpu_cpwait();
2904 } else {
2905 if (opte == 0)
2906 l2b->l2b_occupancy++;
2907 }
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;
2914 PTE_SYNC(pte);
2915 }
2916
2917 void
2918 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
2919 {
2920 pmap_kenter_internal(va, pa, KENTER_CACHE);
2921 }
2922
2923 void
2924 pmap_kenter_nocache(vm_offset_t va, vm_paddr_t pa)
2925 {
2926
2927 pmap_kenter_internal(va, pa, 0);
2928 }
2929
2930 void
2931 pmap_kenter_user(vm_offset_t va, vm_paddr_t pa)
2932 {
2933
2934 pmap_kenter_internal(va, pa, KENTER_CACHE|KENTER_USER);
2935 /*
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.
2939 */
2940 pmap_fault_fixup(pmap_kernel(), va, VM_PROT_READ|VM_PROT_WRITE, 1);
2941 }
2942
2943 /*
2944 * remove a page rom the kernel pagetables
2945 */
2946 void
2947 pmap_kremove(vm_offset_t va)
2948 {
2949 struct l2_bucket *l2b;
2950 pt_entry_t *pte, opte;
2951
2952 l2b = pmap_get_l2_bucket(pmap_kernel(), va);
2953 if (!l2b)
2954 return;
2955 KASSERT(l2b != NULL, ("No L2 Bucket"));
2956 pte = &l2b->l2b_kva[l2pte_index(va)];
2957 opte = *pte;
2958 if (l2pte_valid(opte)) {
2959 cpu_dcache_wbinv_range(va, PAGE_SIZE);
2960 cpu_tlb_flushD_SE(va);
2961 cpu_cpwait();
2962 *pte = 0;
2963 }
2964 }
2965
2966
2967 /*
2968 * Used to map a range of physical addresses into kernel
2969 * virtual address space.
2970 *
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
2977 * region.
2978 */
2979 vm_offset_t
2980 pmap_map(vm_offset_t *virt, vm_offset_t start, vm_offset_t end, int prot)
2981 {
2982 #ifdef ARM_USE_SMALL_ALLOC
2983 return (arm_ptovirt(start));
2984 #else
2985 vm_offset_t sva = *virt;
2986 vm_offset_t va = sva;
2987
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,
2990 prot));
2991
2992 while (start < end) {
2993 pmap_kenter(va, start);
2994 va += PAGE_SIZE;
2995 start += PAGE_SIZE;
2996 }
2997 *virt = va;
2998 return (sva);
2999 #endif
3000 }
3001
3002 static void
3003 pmap_wb_page(vm_page_t m)
3004 {
3005 struct pv_entry *pv;
3006
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);
3010 }
3011
3012 static void
3013 pmap_inv_page(vm_page_t m)
3014 {
3015 struct pv_entry *pv;
3016
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);
3019 }
3020 /*
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.
3027 */
3028 void
3029 pmap_qenter(vm_offset_t va, vm_page_t *m, int count)
3030 {
3031 int i;
3032
3033 for (i = 0; i < count; i++) {
3034 pmap_wb_page(m[i]);
3035 pmap_kenter_internal(va, VM_PAGE_TO_PHYS(m[i]),
3036 KENTER_CACHE);
3037 va += PAGE_SIZE;
3038 }
3039 }
3040
3041
3042 /*
3043 * this routine jerks page mappings from the
3044 * kernel -- it is meant only for temporary mappings.
3045 */
3046 void
3047 pmap_qremove(vm_offset_t va, int count)
3048 {
3049 vm_paddr_t pa;
3050 int i;
3051
3052 for (i = 0; i < count; i++) {
3053 pa = vtophys(va);
3054 if (pa) {
3055 pmap_inv_page(PHYS_TO_VM_PAGE(pa));
3056 pmap_kremove(va);
3057 }
3058 va += PAGE_SIZE;
3059 }
3060 }
3061
3062
3063 /*
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.
3067 */
3068 void
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)
3071 {
3072
3073 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
3074 KASSERT(object->type == OBJT_DEVICE || object->type == OBJT_SG,
3075 ("pmap_object_init_pt: non-device object"));
3076 }
3077
3078
3079 /*
3080 * pmap_is_prefaultable:
3081 *
3082 * Return whether or not the specified virtual address is elgible
3083 * for prefault.
3084 */
3085 boolean_t
3086 pmap_is_prefaultable(pmap_t pmap, vm_offset_t addr)
3087 {
3088 pd_entry_t *pde;
3089 pt_entry_t *pte;
3090
3091 if (!pmap_get_pde_pte(pmap, addr, &pde, &pte))
3092 return (FALSE);
3093 KASSERT(pte != NULL, ("Valid mapping but no pte ?"));
3094 if (*pte == 0)
3095 return (TRUE);
3096 return (FALSE);
3097 }
3098
3099 /*
3100 * Fetch pointers to the PDE/PTE for the given pmap/VA pair.
3101 * Returns TRUE if the mapping exists, else FALSE.
3102 *
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...
3106 *
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.
3110 *
3111 * NOTE: We can return a NULL *ptp in the case where the L1 pde is
3112 * a "section" mapping.
3113 */
3114 boolean_t
3115 pmap_get_pde_pte(pmap_t pm, vm_offset_t va, pd_entry_t **pdp, pt_entry_t **ptp)
3116 {
3117 struct l2_dtable *l2;
3118 pd_entry_t *pl1pd, l1pd;
3119 pt_entry_t *ptep;
3120 u_short l1idx;
3121
3122 if (pm->pm_l1 == NULL)
3123 return (FALSE);
3124
3125 l1idx = L1_IDX(va);
3126 *pdp = pl1pd = &pm->pm_l1->l1_kva[l1idx];
3127 l1pd = *pl1pd;
3128
3129 if (l1pte_section_p(l1pd)) {
3130 *ptp = NULL;
3131 return (TRUE);
3132 }
3133
3134 if (pm->pm_l2 == NULL)
3135 return (FALSE);
3136
3137 l2 = pm->pm_l2[L2_IDX(l1idx)];
3138
3139 if (l2 == NULL ||
3140 (ptep = l2->l2_bucket[L2_BUCKET(l1idx)].l2b_kva) == NULL) {
3141 return (FALSE);
3142 }
3143
3144 *ptp = &ptep[l2pte_index(va)];
3145 return (TRUE);
3146 }
3147
3148 /*
3149 * Routine: pmap_remove_all
3150 * Function:
3151 * Removes this physical page from
3152 * all physical maps in which it resides.
3153 * Reflects back modify bits to the pager.
3154 *
3155 * Notes:
3156 * Original versions of this routine were very
3157 * inefficient because they iteratively called
3158 * pmap_remove (slow...)
3159 */
3160 void
3161 pmap_remove_all(vm_page_t m)
3162 {
3163 pv_entry_t pv;
3164 pt_entry_t *ptep, pte;
3165 struct l2_bucket *l2b;
3166 boolean_t flush = FALSE;
3167 pmap_t curpm;
3168 int flags = 0;
3169
3170 #if defined(PMAP_DEBUG)
3171 /*
3172 * XXX this makes pmap_page_protect(NONE) illegal for non-managed
3173 * pages!
3174 */
3175 if (m->flags & PG_FICTITIOUS) {
3176 panic("pmap_page_protect: illegal for unmanaged page, va: 0x%x", VM_PAGE_TO_PHYS(m));
3177 }
3178 #endif
3179
3180 if (TAILQ_EMPTY(&m->md.pv_list))
3181 return;
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()))
3187 flush = TRUE;
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)];
3192 pte = *ptep;
3193 *ptep = 0;
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);
3203 }
3204
3205 if (flush) {
3206 if (PV_BEEN_EXECD(flags))
3207 pmap_tlb_flushID(curpm);
3208 else
3209 pmap_tlb_flushD(curpm);
3210 }
3211 vm_page_flag_clear(m, PG_WRITEABLE);
3212 }
3213
3214
3215 /*
3216 * Set the physical protection on the
3217 * specified range of this map as requested.
3218 */
3219 void
3220 pmap_protect(pmap_t pm, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
3221 {
3222 struct l2_bucket *l2b;
3223 pt_entry_t *ptep, pte;
3224 vm_offset_t next_bucket;
3225 u_int flags;
3226 int flush;
3227
3228 if ((prot & VM_PROT_READ) == 0) {
3229 pmap_remove(pm, sva, eva);
3230 return;
3231 }
3232
3233 if (prot & VM_PROT_WRITE) {
3234 /*
3235 * If this is a read->write transition, just ignore it and let
3236 * vm_fault() take care of it later.
3237 */
3238 return;
3239 }
3240
3241 vm_page_lock_queues();
3242 PMAP_LOCK(pm);
3243
3244 /*
3245 * OK, at this point, we know we're doing write-protect operation.
3246 * If the pmap is active, write-back the range.
3247 */
3248 pmap_dcache_wb_range(pm, sva, eva - sva, FALSE, FALSE);
3249
3250 flush = ((eva - sva) >= (PAGE_SIZE * 4)) ? 0 : -1;
3251 flags = 0;
3252
3253 while (sva < eva) {
3254 next_bucket = L2_NEXT_BUCKET(sva);
3255 if (next_bucket > eva)
3256 next_bucket = eva;
3257
3258 l2b = pmap_get_l2_bucket(pm, sva);
3259 if (l2b == NULL) {
3260 sva = next_bucket;
3261 continue;
3262 }
3263
3264 ptep = &l2b->l2b_kva[l2pte_index(sva)];
3265
3266 while (sva < next_bucket) {
3267 if ((pte = *ptep) != 0 && (pte & L2_S_PROT_W) != 0) {
3268 struct vm_page *pg;
3269 u_int f;
3270
3271 pg = PHYS_TO_VM_PAGE(l2pte_pa(pte));
3272 pte &= ~L2_S_PROT_W;
3273 *ptep = pte;
3274 PTE_SYNC(ptep);
3275
3276 if (pg != NULL) {
3277 f = pmap_modify_pv(pg, pm, sva,
3278 PVF_WRITE, 0);
3279 pmap_vac_me_harder(pg, pm, sva);
3280 vm_page_dirty(pg);
3281 } else
3282 f = PVF_REF | PVF_EXEC;
3283
3284 if (flush >= 0) {
3285 flush++;
3286 flags |= f;
3287 } else
3288 if (PV_BEEN_EXECD(f))
3289 pmap_tlb_flushID_SE(pm, sva);
3290 else
3291 if (PV_BEEN_REFD(f))
3292 pmap_tlb_flushD_SE(pm, sva);
3293 }
3294
3295 sva += PAGE_SIZE;
3296 ptep++;
3297 }
3298 }
3299
3300
3301 if (flush) {
3302 if (PV_BEEN_EXECD(flags))
3303 pmap_tlb_flushID(pm);
3304 else
3305 if (PV_BEEN_REFD(flags))
3306 pmap_tlb_flushD(pm);
3307 }
3308 vm_page_unlock_queues();
3309
3310 PMAP_UNLOCK(pm);
3311 }
3312
3313
3314 /*
3315 * Insert the given physical page (p) at
3316 * the specified virtual address (v) in the
3317 * target physical map with the protection requested.
3318 *
3319 * If specified, the page will be wired down, meaning
3320 * that the related pte can not be reclaimed.
3321 *
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.
3325 */
3326
3327 void
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)
3330 {
3331
3332 vm_page_lock_queues();
3333 PMAP_LOCK(pmap);
3334 pmap_enter_locked(pmap, va, m, prot, wired, M_WAITOK);
3335 vm_page_unlock_queues();
3336 PMAP_UNLOCK(pmap);
3337 }
3338
3339 /*
3340 * The page queues and pmap must be locked.
3341 */
3342 static void
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)
3345 {
3346 struct l2_bucket *l2b = NULL;
3347 struct vm_page *opg;
3348 struct pv_entry *pve = NULL;
3349 pt_entry_t *ptep, npte, opte;
3350 u_int nflags;
3351 u_int oflags;
3352 vm_paddr_t pa;
3353
3354 PMAP_ASSERT_LOCKED(pmap);
3355 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
3356 if (va == vector_page) {
3357 pa = systempage.pv_pa;
3358 m = NULL;
3359 } else
3360 pa = VM_PAGE_TO_PHYS(m);
3361 nflags = 0;
3362 if (prot & VM_PROT_WRITE)
3363 nflags |= PVF_WRITE;
3364 if (prot & VM_PROT_EXECUTE)
3365 nflags |= PVF_EXEC;
3366 if (wired)
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));
3370
3371 if (pmap == pmap_kernel()) {
3372 l2b = pmap_get_l2_bucket(pmap, va);
3373 if (l2b == NULL)
3374 l2b = pmap_grow_l2_bucket(pmap, va);
3375 } else {
3376 do_l2b_alloc:
3377 l2b = pmap_alloc_l2_bucket(pmap, va);
3378 if (l2b == NULL) {
3379 if (flags & M_WAITOK) {
3380 PMAP_UNLOCK(pmap);
3381 vm_page_unlock_queues();
3382 VM_WAIT;
3383 vm_page_lock_queues();
3384 PMAP_LOCK(pmap);
3385 goto do_l2b_alloc;
3386 }
3387 return;
3388 }
3389 }
3390
3391 ptep = &l2b->l2b_kva[l2pte_index(va)];
3392
3393 opte = *ptep;
3394 npte = pa;
3395 oflags = 0;
3396 if (opte) {
3397 /*
3398 * There is already a mapping at this address.
3399 * If the physical address is different, lookup the
3400 * vm_page.
3401 */
3402 if (l2pte_pa(opte) != pa)
3403 opg = PHYS_TO_VM_PAGE(l2pte_pa(opte));
3404 else
3405 opg = m;
3406 } else
3407 opg = NULL;
3408
3409 if ((prot & (VM_PROT_ALL)) ||
3410 (!m || m->md.pvh_attrs & PVF_REF)) {
3411 /*
3412 * - The access type indicates that we don't need
3413 * to do referenced emulation.
3414 * OR
3415 * - The physical page has already been referenced
3416 * so no need to re-do referenced emulation here.
3417 */
3418 npte |= L2_S_PROTO;
3419
3420 nflags |= PVF_REF;
3421
3422 if (m && ((prot & VM_PROT_WRITE) != 0 ||
3423 (m->md.pvh_attrs & PVF_MOD))) {
3424 /*
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.
3429 */
3430 nflags |= PVF_MOD;
3431 if (!(m->md.pvh_attrs & PVF_MOD))
3432 vm_page_dirty(m);
3433 }
3434 if (m && opte)
3435 vm_page_flag_set(m, PG_REFERENCED);
3436 } else {
3437 /*
3438 * Need to do page referenced emulation.
3439 */
3440 npte |= L2_TYPE_INV;
3441 }
3442
3443 if (prot & VM_PROT_WRITE) {
3444 npte |= L2_S_PROT_W;
3445 if (m != NULL)
3446 vm_page_flag_set(m, PG_WRITEABLE);
3447 }
3448 npte |= pte_l2_s_cache_mode;
3449 if (m && m == opg) {
3450 /*
3451 * We're changing the attrs of an existing mapping.
3452 */
3453 oflags = pmap_modify_pv(m, pmap, va,
3454 PVF_WRITE | PVF_EXEC | PVF_WIRED |
3455 PVF_MOD | PVF_REF, nflags);
3456
3457 /*
3458 * We may need to flush the cache if we're
3459 * doing rw-ro...
3460 */
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);
3466 } else {
3467 /*
3468 * New mapping, or changing the backing page
3469 * of an existing mapping.
3470 */
3471 if (opg) {
3472 /*
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
3476 */
3477 pve = pmap_remove_pv(opg, pmap, va);
3478 if (m && (m->flags & (PG_UNMANAGED | PG_FICTITIOUS)) &&
3479 pve)
3480 pmap_free_pv_entry(pve);
3481 else if (!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;
3487
3488 /*
3489 * If the old mapping was valid (ref/mod
3490 * emulation creates 'invalid' mappings
3491 * initially) then make sure to frob
3492 * the cache.
3493 */
3494 if ((oflags & PVF_NC) == 0 &&
3495 l2pte_valid(opte)) {
3496 if (PV_BEEN_EXECD(oflags)) {
3497 pmap_idcache_wbinv_range(pmap, va,
3498 PAGE_SIZE);
3499 } else
3500 if (PV_BEEN_REFD(oflags)) {
3501 pmap_dcache_wb_range(pmap, va,
3502 PAGE_SIZE, TRUE,
3503 (oflags & PVF_WRITE) == 0);
3504 }
3505 }
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");
3509 }
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);
3514 }
3515 }
3516 /*
3517 * Make sure userland mappings get the right permissions
3518 */
3519 if (pmap != pmap_kernel() && va != vector_page) {
3520 npte |= L2_S_PROT_U;
3521 }
3522
3523 /*
3524 * Keep the stats up to date
3525 */
3526 if (opte == 0) {
3527 l2b->l2b_occupancy++;
3528 pmap->pm_stats.resident_count++;
3529 }
3530
3531
3532 /*
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.
3535 */
3536 if (npte != opte) {
3537 boolean_t is_cached = pmap_is_current(pmap);
3538
3539 *ptep = npte;
3540 if (is_cached) {
3541 /*
3542 * We only need to frob the cache/tlb if this pmap
3543 * is current
3544 */
3545 PTE_SYNC(ptep);
3546 if (L1_IDX(va) != L1_IDX(vector_page) &&
3547 l2pte_valid(npte)) {
3548 /*
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.
3553 */
3554 pd_entry_t *pl1pd, l1pd;
3555
3556 pl1pd = &pmap->pm_l1->l1_kva[L1_IDX(va)];
3557 l1pd = l2b->l2b_phys | L1_C_DOM(pmap->pm_domain) |
3558 L1_C_PROTO;
3559 if (*pl1pd != l1pd) {
3560 *pl1pd = l1pd;
3561 PTE_SYNC(pl1pd);
3562 }
3563 }
3564 }
3565
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);
3570
3571
3572 if (m)
3573 pmap_vac_me_harder(m, pmap, va);
3574 }
3575 }
3576
3577 /*
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.
3588 */
3589 void
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)
3592 {
3593 vm_page_t m;
3594 vm_pindex_t diff, psize;
3595
3596 psize = atop(end - start);
3597 m = m_start;
3598 PMAP_LOCK(pmap);
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);
3603 }
3604 PMAP_UNLOCK(pmap);
3605 }
3606
3607 /*
3608 * this code makes some *MAJOR* assumptions:
3609 * 1. Current pmap & pmap exists.
3610 * 2. Not wired.
3611 * 3. Read access.
3612 * 4. No page table pages.
3613 * but is *MUCH* faster than pmap_enter...
3614 */
3615
3616 void
3617 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot)
3618 {
3619
3620 PMAP_LOCK(pmap);
3621 pmap_enter_locked(pmap, va, m, prot & (VM_PROT_READ | VM_PROT_EXECUTE),
3622 FALSE, M_NOWAIT);
3623 PMAP_UNLOCK(pmap);
3624 }
3625
3626 /*
3627 * Routine: pmap_change_wiring
3628 * Function: Change the wiring attribute for a map/virtual-address
3629 * pair.
3630 * In/out conditions:
3631 * The mapping must already exist in the pmap.
3632 */
3633 void
3634 pmap_change_wiring(pmap_t pmap, vm_offset_t va, boolean_t wired)
3635 {
3636 struct l2_bucket *l2b;
3637 pt_entry_t *ptep, pte;
3638 vm_page_t pg;
3639
3640 vm_page_lock_queues();
3641 PMAP_LOCK(pmap);
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)];
3645 pte = *ptep;
3646 pg = PHYS_TO_VM_PAGE(l2pte_pa(pte));
3647 if (pg)
3648 pmap_modify_pv(pg, pmap, va, PVF_WIRED, wired);
3649 vm_page_unlock_queues();
3650 PMAP_UNLOCK(pmap);
3651 }
3652
3653
3654 /*
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.
3658 *
3659 * This routine is only advisory and need not do anything.
3660 */
3661 void
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)
3664 {
3665 }
3666
3667
3668 /*
3669 * Routine: pmap_extract
3670 * Function:
3671 * Extract the physical page address associated
3672 * with the given map/virtual_address pair.
3673 */
3674 vm_paddr_t
3675 pmap_extract(pmap_t pm, vm_offset_t va)
3676 {
3677 struct l2_dtable *l2;
3678 pd_entry_t l1pd;
3679 pt_entry_t *ptep, pte;
3680 vm_paddr_t pa;
3681 u_int l1idx;
3682 l1idx = L1_IDX(va);
3683
3684 PMAP_LOCK(pm);
3685 l1pd = pm->pm_l1->l1_kva[l1idx];
3686 if (l1pte_section_p(l1pd)) {
3687 /*
3688 * These should only happen for pmap_kernel()
3689 */
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);
3694 else
3695 pa = (l1pd & L1_S_FRAME) | (va & L1_S_OFFSET);
3696 } else {
3697 /*
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.
3701 */
3702 l2 = pm->pm_l2[L2_IDX(l1idx)];
3703
3704 if (l2 == NULL ||
3705 (ptep = l2->l2_bucket[L2_BUCKET(l1idx)].l2b_kva) == NULL) {
3706 PMAP_UNLOCK(pm);
3707 return (0);
3708 }
3709
3710 ptep = &ptep[l2pte_index(va)];
3711 pte = *ptep;
3712
3713 if (pte == 0) {
3714 PMAP_UNLOCK(pm);
3715 return (0);
3716 }
3717
3718 switch (pte & L2_TYPE_MASK) {
3719 case L2_TYPE_L:
3720 pa = (pte & L2_L_FRAME) | (va & L2_L_OFFSET);
3721 break;
3722
3723 default:
3724 pa = (pte & L2_S_FRAME) | (va & L2_S_OFFSET);
3725 break;
3726 }
3727 }
3728
3729 PMAP_UNLOCK(pm);
3730 return (pa);
3731 }
3732
3733 /*
3734 * Atomically extract and hold the physical page with the given
3735 * pmap and virtual address pair if that mapping permits the given
3736 * protection.
3737 *
3738 */
3739 vm_page_t
3740 pmap_extract_and_hold(pmap_t pmap, vm_offset_t va, vm_prot_t prot)
3741 {
3742 struct l2_dtable *l2;
3743 pd_entry_t l1pd;
3744 pt_entry_t *ptep, pte;
3745 vm_paddr_t pa;
3746 vm_page_t m = NULL;
3747 u_int l1idx;
3748 l1idx = L1_IDX(va);
3749
3750 vm_page_lock_queues();
3751 PMAP_LOCK(pmap);
3752 l1pd = pmap->pm_l1->l1_kva[l1idx];
3753 if (l1pte_section_p(l1pd)) {
3754 /*
3755 * These should only happen for pmap_kernel()
3756 */
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);
3761 else
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);
3765 vm_page_hold(m);
3766 }
3767
3768 } else {
3769 /*
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.
3773 */
3774 l2 = pmap->pm_l2[L2_IDX(l1idx)];
3775
3776 if (l2 == NULL ||
3777 (ptep = l2->l2_bucket[L2_BUCKET(l1idx)].l2b_kva) == NULL) {
3778 PMAP_UNLOCK(pmap);
3779 vm_page_unlock_queues();
3780 return (NULL);
3781 }
3782
3783 ptep = &ptep[l2pte_index(va)];
3784 pte = *ptep;
3785
3786 if (pte == 0) {
3787 PMAP_UNLOCK(pmap);
3788 vm_page_unlock_queues();
3789 return (NULL);
3790 }
3791 if (pte & L2_S_PROT_W || (prot & VM_PROT_WRITE) == 0) {
3792 switch (pte & L2_TYPE_MASK) {
3793 case L2_TYPE_L:
3794 pa = (pte & L2_L_FRAME) | (va & L2_L_OFFSET);
3795 break;
3796
3797 default:
3798 pa = (pte & L2_S_FRAME) | (va & L2_S_OFFSET);
3799 break;
3800 }
3801 m = PHYS_TO_VM_PAGE(pa);
3802 vm_page_hold(m);
3803 }
3804 }
3805
3806 PMAP_UNLOCK(pmap);
3807 vm_page_unlock_queues();
3808 return (m);
3809 }
3810
3811 /*
3812 * Initialize a preallocated and zeroed pmap structure,
3813 * such as one in a vmspace structure.
3814 */
3815
3816 int
3817 pmap_pinit(pmap_t pmap)
3818 {
3819 PDEBUG(1, printf("pmap_pinit: pmap = %08x\n", (uint32_t) pmap));
3820
3821 PMAP_LOCK_INIT(pmap);
3822 pmap_alloc_l1(pmap);
3823 bzero(pmap->pm_l2, sizeof(pmap->pm_l2));
3824
3825 pmap->pm_count = 1;
3826 pmap->pm_active = 0;
3827
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),
3834 VM_PROT_READ, 1);
3835 }
3836 return (1);
3837 }
3838
3839
3840 /***************************************************
3841 * page management routines.
3842 ***************************************************/
3843
3844
3845 static void
3846 pmap_free_pv_entry(pv_entry_t pv)
3847 {
3848 pv_entry_count--;
3849 uma_zfree(pvzone, pv);
3850 }
3851
3852
3853 /*
3854 * get a new pv_entry, allocating a block from the system
3855 * when needed.
3856 * the memory allocation is performed bypassing the malloc code
3857 * because of the possibility of allocations at interrupt time.
3858 */
3859 static pv_entry_t
3860 pmap_get_pv_entry(void)
3861 {
3862 pv_entry_t ret_value;
3863
3864 pv_entry_count++;
3865 if (pv_entry_count > pv_entry_high_water)
3866 pagedaemon_wakeup();
3867 ret_value = uma_zalloc(pvzone, M_NOWAIT);
3868 return ret_value;
3869 }
3870
3871
3872 /*
3873 * Remove the given range of addresses from the specified map.
3874 *
3875 * It is assumed that the start and end are properly
3876 * rounded to the page size.
3877 */
3878 #define PMAP_REMOVE_CLEAN_LIST_SIZE 3
3879 void
3880 pmap_remove(pmap_t pm, vm_offset_t sva, vm_offset_t eva)
3881 {
3882 struct l2_bucket *l2b;
3883 vm_offset_t next_bucket;
3884 pt_entry_t *ptep;
3885 u_int cleanlist_idx, total, cnt;
3886 struct {
3887 vm_offset_t va;
3888 pt_entry_t *pte;
3889 } cleanlist[PMAP_REMOVE_CLEAN_LIST_SIZE];
3890 u_int mappings, is_exec, is_refd;
3891 int flushall = 0;
3892
3893
3894 /*
3895 * we lock in the pmap => pv_head direction
3896 */
3897
3898 vm_page_lock_queues();
3899 PMAP_LOCK(pm);
3900 if (!pmap_is_current(pm)) {
3901 cleanlist_idx = PMAP_REMOVE_CLEAN_LIST_SIZE + 1;
3902 } else
3903 cleanlist_idx = 0;
3904
3905 total = 0;
3906 while (sva < eva) {
3907 /*
3908 * Do one L2 bucket's worth at a time.
3909 */
3910 next_bucket = L2_NEXT_BUCKET(sva);
3911 if (next_bucket > eva)
3912 next_bucket = eva;
3913
3914 l2b = pmap_get_l2_bucket(pm, sva);
3915 if (l2b == NULL) {
3916 sva = next_bucket;
3917 continue;
3918 }
3919
3920 ptep = &l2b->l2b_kva[l2pte_index(sva)];
3921 mappings = 0;
3922
3923 while (sva < next_bucket) {
3924 struct vm_page *pg;
3925 pt_entry_t pte;
3926 vm_paddr_t pa;
3927
3928 pte = *ptep;
3929
3930 if (pte == 0) {
3931 /*
3932 * Nothing here, move along
3933 */
3934 sva += PAGE_SIZE;
3935 ptep++;
3936 continue;
3937 }
3938
3939 pm->pm_stats.resident_count--;
3940 pa = l2pte_pa(pte);
3941 is_exec = 0;
3942 is_refd = 1;
3943
3944 /*
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.
3948 */
3949 if ((pg = PHYS_TO_VM_PAGE(pa)) != NULL) {
3950 struct pv_entry *pve;
3951
3952 pve = pmap_remove_pv(pg, pm, sva);
3953 if (pve) {
3954 is_exec = PV_BEEN_EXECD(pve->pv_flags);
3955 is_refd = PV_BEEN_REFD(pve->pv_flags);
3956 pmap_free_pv_entry(pve);
3957 }
3958 }
3959
3960 if (!l2pte_valid(pte)) {
3961 *ptep = 0;
3962 PTE_SYNC_CURRENT(pm, ptep);
3963 sva += PAGE_SIZE;
3964 ptep++;
3965 mappings++;
3966 continue;
3967 }
3968
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);
3974 cleanlist_idx++;
3975 } else
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);
3980
3981 /*
3982 * Roll back the previous PTE list,
3983 * and zero out the current PTE.
3984 */
3985 for (cnt = 0;
3986 cnt < PMAP_REMOVE_CLEAN_LIST_SIZE; cnt++) {
3987 *cleanlist[cnt].pte = 0;
3988 }
3989 *ptep = 0;
3990 PTE_SYNC(ptep);
3991 cleanlist_idx++;
3992 flushall = 1;
3993 } else {
3994 *ptep = 0;
3995 PTE_SYNC(ptep);
3996 if (is_exec)
3997 pmap_tlb_flushID_SE(pm, sva);
3998 else
3999 if (is_refd)
4000 pmap_tlb_flushD_SE(pm, sva);
4001 }
4002
4003 sva += PAGE_SIZE;
4004 ptep++;
4005 mappings++;
4006 }
4007
4008 /*
4009 * Deal with any left overs
4010 */
4011 if (cleanlist_idx <= PMAP_REMOVE_CLEAN_LIST_SIZE) {
4012 total += cleanlist_idx;
4013 for (cnt = 0; cnt < cleanlist_idx; cnt++) {
4014 vm_offset_t clva =
4015 cleanlist[cnt].va & ~1;
4016 if (cleanlist[cnt].va & 1) {
4017 pmap_idcache_wbinv_range(pm,
4018 clva, PAGE_SIZE);
4019 pmap_tlb_flushID_SE(pm, clva);
4020 } else {
4021 pmap_dcache_wb_range(pm,
4022 clva, PAGE_SIZE, TRUE,
4023 FALSE);
4024 pmap_tlb_flushD_SE(pm, clva);
4025 }
4026 *cleanlist[cnt].pte = 0;
4027 PTE_SYNC_CURRENT(pm, cleanlist[cnt].pte);
4028 }
4029
4030 if (total <= PMAP_REMOVE_CLEAN_LIST_SIZE)
4031 cleanlist_idx = 0;
4032 else {
4033 /*
4034 * We are removing so much entries it's just
4035 * easier to flush the whole cache.
4036 */
4037 cleanlist_idx = PMAP_REMOVE_CLEAN_LIST_SIZE + 1;
4038 pmap_idcache_wbinv_all(pm);
4039 flushall = 1;
4040 }
4041 }
4042
4043 pmap_free_l2_bucket(pm, l2b, mappings);
4044 }
4045
4046 vm_page_unlock_queues();
4047 if (flushall)
4048 cpu_tlb_flushID();
4049 PMAP_UNLOCK(pm);
4050 }
4051
4052
4053
4054
4055 /*
4056 * pmap_zero_page()
4057 *
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.
4062 */
4063 #if (ARM_MMU_GENERIC + ARM_MMU_SA1) != 0 || defined(CPU_XSCALE_CORE3)
4064 void
4065 pmap_zero_page_generic(vm_paddr_t phys, int off, int size)
4066 {
4067 #ifdef ARM_USE_SMALL_ALLOC
4068 char *dstpg;
4069 #endif
4070
4071 #ifdef DEBUG
4072 struct vm_page *pg = PHYS_TO_VM_PAGE(phys);
4073
4074 if (pg->md.pvh_list != NULL)
4075 panic("pmap_zero_page: page has mappings");
4076 #endif
4077
4078 if (_arm_bzero && size >= _min_bzero_size &&
4079 _arm_bzero((void *)(phys + off), size, IS_PHYSICAL) == 0)
4080 return;
4081
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);
4087 } else {
4088 bzero_page((vm_offset_t)dstpg);
4089 cpu_dcache_wbinv_range((vm_offset_t)dstpg, PAGE_SIZE);
4090 }
4091 #else
4092
4093 mtx_lock(&cmtx);
4094 /*
4095 * Hook in the page, zero it, and purge the cache for that
4096 * zeroed page. Invalidate the TLB as needed.
4097 */
4098 *cdst_pte = L2_S_PROTO | phys |
4099 L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE) | pte_l2_s_cache_mode;
4100 PTE_SYNC(cdst_pte);
4101 cpu_tlb_flushD_SE(cdstp);
4102 cpu_cpwait();
4103 if (off || size != PAGE_SIZE) {
4104 bzero((void *)(cdstp + off), size);
4105 cpu_dcache_wbinv_range(cdstp + off, size);
4106 } else {
4107 bzero_page(cdstp);
4108 cpu_dcache_wbinv_range(cdstp, PAGE_SIZE);
4109 }
4110 mtx_unlock(&cmtx);
4111 #endif
4112 }
4113 #endif /* (ARM_MMU_GENERIC + ARM_MMU_SA1) != 0 */
4114
4115 #if ARM_MMU_XSCALE == 1
4116 void
4117 pmap_zero_page_xscale(vm_paddr_t phys, int off, int size)
4118 {
4119 if (_arm_bzero && size >= _min_bzero_size &&
4120 _arm_bzero((void *)(phys + off), size, IS_PHYSICAL) == 0)
4121 return;
4122 mtx_lock(&cmtx);
4123 /*
4124 * Hook in the page, zero it, and purge the cache for that
4125 * zeroed page. Invalidate the TLB as needed.
4126 */
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 */
4130 PTE_SYNC(cdst_pte);
4131 cpu_tlb_flushD_SE(cdstp);
4132 cpu_cpwait();
4133 if (off || size != PAGE_SIZE)
4134 bzero((void *)(cdstp + off), size);
4135 else
4136 bzero_page(cdstp);
4137 mtx_unlock(&cmtx);
4138 xscale_cache_clean_minidata();
4139 }
4140
4141 /*
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.
4144 */
4145 void
4146 pmap_use_minicache(vm_offset_t va, vm_size_t size)
4147 {
4148 struct l2_bucket *l2b;
4149 pt_entry_t *ptep, *sptep, pte;
4150 vm_offset_t next_bucket, eva;
4151
4152 #if (ARM_NMMUS > 1) || defined(CPU_XSCALE_CORE3)
4153 if (xscale_use_minidata == 0)
4154 return;
4155 #endif
4156
4157 eva = va + size;
4158
4159 while (va < eva) {
4160 next_bucket = L2_NEXT_BUCKET(va);
4161 if (next_bucket > eva)
4162 next_bucket = eva;
4163
4164 l2b = pmap_get_l2_bucket(pmap_kernel(), va);
4165
4166 sptep = ptep = &l2b->l2b_kva[l2pte_index(va)];
4167
4168 while (va < next_bucket) {
4169 pte = *ptep;
4170 if (!l2pte_minidata(pte)) {
4171 cpu_dcache_wbinv_range(va, PAGE_SIZE);
4172 cpu_tlb_flushD_SE(va);
4173 *ptep = pte & ~L2_B;
4174 }
4175 ptep++;
4176 va += PAGE_SIZE;
4177 }
4178 PTE_SYNC_RANGE(sptep, (u_int)(ptep - sptep));
4179 }
4180 cpu_cpwait();
4181 }
4182 #endif /* ARM_MMU_XSCALE == 1 */
4183
4184 /*
4185 * pmap_zero_page zeros the specified hardware page by mapping
4186 * the page into KVM and using bzero to clear its contents.
4187 */
4188 void
4189 pmap_zero_page(vm_page_t m)
4190 {
4191 pmap_zero_page_func(VM_PAGE_TO_PHYS(m), 0, PAGE_SIZE);
4192 }
4193
4194
4195 /*
4196 * pmap_zero_page_area zeros the specified hardware page by mapping
4197 * the page into KVM and using bzero to clear its contents.
4198 *
4199 * off and size may not cover an area beyond a single hardware page.
4200 */
4201 void
4202 pmap_zero_page_area(vm_page_t m, int off, int size)
4203 {
4204
4205 pmap_zero_page_func(VM_PAGE_TO_PHYS(m), off, size);
4206 }
4207
4208
4209 /*
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
4213 * outside of Giant.
4214 */
4215 void
4216 pmap_zero_page_idle(vm_page_t m)
4217 {
4218
4219 pmap_zero_page(m);
4220 }
4221
4222 #if 0
4223 /*
4224 * pmap_clean_page()
4225 *
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.
4229 *
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.
4234 *
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.
4237 *
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.
4242 */
4243 static int
4244 pmap_clean_page(struct pv_entry *pv, boolean_t is_src)
4245 {
4246 pmap_t pm, pm_to_clean = NULL;
4247 struct pv_entry *npv;
4248 u_int cache_needs_cleaning = 0;
4249 u_int flags = 0;
4250 vm_offset_t page_to_clean = 0;
4251
4252 if (pv == NULL) {
4253 /* nothing mapped in so nothing to flush */
4254 return (0);
4255 }
4256
4257 /*
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
4260 * current pmap.
4261 */
4262 if (curthread)
4263 pm = vmspace_pmap(curproc->p_vmspace);
4264 else
4265 pm = pmap_kernel();
4266
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;
4270 /*
4271 * The page is mapped non-cacheable in
4272 * this map. No need to flush the cache.
4273 */
4274 if (npv->pv_flags & PVF_NC) {
4275 #ifdef DIAGNOSTIC
4276 if (cache_needs_cleaning)
4277 panic("pmap_clean_page: "
4278 "cache inconsistency");
4279 #endif
4280 break;
4281 } else if (is_src && (npv->pv_flags & PVF_WRITE) == 0)
4282 continue;
4283 if (cache_needs_cleaning) {
4284 page_to_clean = 0;
4285 break;
4286 } else {
4287 page_to_clean = npv->pv_va;
4288 pm_to_clean = npv->pv_pmap;
4289 }
4290 cache_needs_cleaning = 1;
4291 }
4292 }
4293 if (page_to_clean) {
4294 if (PV_BEEN_EXECD(flags))
4295 pmap_idcache_wbinv_range(pm_to_clean, page_to_clean,
4296 PAGE_SIZE);
4297 else
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);
4303 else
4304 pmap_dcache_wbinv_all(pm);
4305 return (1);
4306 }
4307 return (0);
4308 }
4309 #endif
4310
4311 /*
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
4315 * time.
4316 */
4317
4318 /*
4319 * pmap_copy_page()
4320 *
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.
4324 */
4325 #if (ARM_MMU_GENERIC + ARM_MMU_SA1) != 0 || defined (CPU_XSCALE_CORE3)
4326 void
4327 pmap_copy_page_generic(vm_paddr_t src, vm_paddr_t dst)
4328 {
4329 #if 0
4330 struct vm_page *src_pg = PHYS_TO_VM_PAGE(src);
4331 #endif
4332 #ifdef DEBUG
4333 struct vm_page *dst_pg = PHYS_TO_VM_PAGE(dst);
4334
4335 if (dst_pg->md.pvh_list != NULL)
4336 panic("pmap_copy_page: dst page has mappings");
4337 #endif
4338
4339
4340 /*
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.
4344 */
4345 #if 0
4346 /*
4347 * XXX: Not needed while we call cpu_dcache_wbinv_all() in
4348 * pmap_copy_page().
4349 */
4350 (void) pmap_clean_page(TAILQ_FIRST(&src_pg->md.pv_list), TRUE);
4351 #endif
4352 /*
4353 * Map the pages into the page hook points, copy them, and purge
4354 * the cache for the appropriate page. Invalidate the TLB
4355 * as required.
4356 */
4357 mtx_lock(&cmtx);
4358 *csrc_pte = L2_S_PROTO | src |
4359 L2_S_PROT(PTE_KERNEL, VM_PROT_READ) | pte_l2_s_cache_mode;
4360 PTE_SYNC(csrc_pte);
4361 *cdst_pte = L2_S_PROTO | dst |
4362 L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE) | pte_l2_s_cache_mode;
4363 PTE_SYNC(cdst_pte);
4364 cpu_tlb_flushD_SE(csrcp);
4365 cpu_tlb_flushD_SE(cdstp);
4366 cpu_cpwait();
4367 bcopy_page(csrcp, cdstp);
4368 mtx_unlock(&cmtx);
4369 cpu_dcache_inv_range(csrcp, PAGE_SIZE);
4370 cpu_dcache_wbinv_range(cdstp, PAGE_SIZE);
4371 }
4372 #endif /* (ARM_MMU_GENERIC + ARM_MMU_SA1) != 0 */
4373
4374 #if ARM_MMU_XSCALE == 1
4375 void
4376 pmap_copy_page_xscale(vm_paddr_t src, vm_paddr_t dst)
4377 {
4378 #if 0
4379 /* XXX: Only needed for pmap_clean_page(), which is commented out. */
4380 struct vm_page *src_pg = PHYS_TO_VM_PAGE(src);
4381 #endif
4382 #ifdef DEBUG
4383 struct vm_page *dst_pg = PHYS_TO_VM_PAGE(dst);
4384
4385 if (dst_pg->md.pvh_list != NULL)
4386 panic("pmap_copy_page: dst page has mappings");
4387 #endif
4388
4389
4390 /*
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.
4394 */
4395 #if 0
4396 /*
4397 * XXX: Not needed while we call cpu_dcache_wbinv_all() in
4398 * pmap_copy_page().
4399 */
4400 (void) pmap_clean_page(TAILQ_FIRST(&src_pg->md.pv_list), TRUE);
4401 #endif
4402 /*
4403 * Map the pages into the page hook points, copy them, and purge
4404 * the cache for the appropriate page. Invalidate the TLB
4405 * as required.
4406 */
4407 mtx_lock(&cmtx);
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 */
4411 PTE_SYNC(csrc_pte);
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 */
4415 PTE_SYNC(cdst_pte);
4416 cpu_tlb_flushD_SE(csrcp);
4417 cpu_tlb_flushD_SE(cdstp);
4418 cpu_cpwait();
4419 bcopy_page(csrcp, cdstp);
4420 mtx_unlock(&cmtx);
4421 xscale_cache_clean_minidata();
4422 }
4423 #endif /* ARM_MMU_XSCALE == 1 */
4424
4425 void
4426 pmap_copy_page(vm_page_t src, vm_page_t dst)
4427 {
4428 #ifdef ARM_USE_SMALL_ALLOC
4429 vm_offset_t srcpg, dstpg;
4430 #endif
4431
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)
4436 return;
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);
4442 #else
4443 pmap_copy_page_func(VM_PAGE_TO_PHYS(src), VM_PAGE_TO_PHYS(dst));
4444 #endif
4445 }
4446
4447
4448
4449
4450 /*
4451 * this routine returns true if a physical page resides
4452 * in the given pmap.
4453 */
4454 boolean_t
4455 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
4456 {
4457 pv_entry_t pv;
4458 int loops = 0;
4459
4460 if (m->flags & PG_FICTITIOUS)
4461 return (FALSE);
4462
4463 /*
4464 * Not found, check current mappings returning immediately
4465 */
4466 for (pv = TAILQ_FIRST(&m->md.pv_list);
4467 pv;
4468 pv = TAILQ_NEXT(pv, pv_list)) {
4469 if (pv->pv_pmap == pmap) {
4470 return (TRUE);
4471 }
4472 loops++;
4473 if (loops >= 16)
4474 break;
4475 }
4476 return (FALSE);
4477 }
4478
4479
4480 /*
4481 * pmap_ts_referenced:
4482 *
4483 * Return the count of reference bits for a page, clearing all of them.
4484 */
4485 int
4486 pmap_ts_referenced(vm_page_t m)
4487 {
4488
4489 if (m->flags & PG_FICTITIOUS)
4490 return (0);
4491 return (pmap_clearbit(m, PVF_REF));
4492 }
4493
4494
4495 boolean_t
4496 pmap_is_modified(vm_page_t m)
4497 {
4498
4499 if (m->md.pvh_attrs & PVF_MOD)
4500 return (TRUE);
4501
4502 return(FALSE);
4503 }
4504
4505
4506 /*
4507 * Clear the modify bits on the specified physical page.
4508 */
4509 void
4510 pmap_clear_modify(vm_page_t m)
4511 {
4512
4513 if (m->md.pvh_attrs & PVF_MOD)
4514 pmap_clearbit(m, PVF_MOD);
4515 }
4516
4517
4518 /*
4519 * pmap_clear_reference:
4520 *
4521 * Clear the reference bit on the specified physical page.
4522 */
4523 void
4524 pmap_clear_reference(vm_page_t m)
4525 {
4526
4527 if (m->md.pvh_attrs & PVF_REF)
4528 pmap_clearbit(m, PVF_REF);
4529 }
4530
4531
4532 /*
4533 * Clear the write and modified bits in each of the given page's mappings.
4534 */
4535 void
4536 pmap_remove_write(vm_page_t m)
4537 {
4538
4539 if (m->flags & PG_WRITEABLE)
4540 pmap_clearbit(m, PVF_WRITE);
4541 }
4542
4543
4544 /*
4545 * perform the pmap work for mincore
4546 */
4547 int
4548 pmap_mincore(pmap_t pmap, vm_offset_t addr)
4549 {
4550 printf("pmap_mincore()\n");
4551
4552 return (0);
4553 }
4554
4555
4556 vm_offset_t
4557 pmap_addr_hint(vm_object_t obj, vm_offset_t addr, vm_size_t size)
4558 {
4559
4560 return(addr);
4561 }
4562
4563 /*
4564 * Increase the starting virtual address of the given mapping if a
4565 * different alignment might result in more superpage mappings.
4566 */
4567 void
4568 pmap_align_superpage(vm_object_t object, vm_ooffset_t offset,
4569 vm_offset_t *addr, vm_size_t size)
4570 {
4571 }
4572
4573
4574 /*
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,
4578 * NOT real memory.
4579 */
4580 void *
4581 pmap_mapdev(vm_offset_t pa, vm_size_t size)
4582 {
4583 vm_offset_t va, tmpva, offset;
4584
4585 offset = pa & PAGE_MASK;
4586 size = roundup(size, PAGE_SIZE);
4587
4588 GIANT_REQUIRED;
4589
4590 va = kmem_alloc_nofault(kernel_map, size);
4591 if (!va)
4592 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
4593 for (tmpva = va; size > 0;) {
4594 pmap_kenter_internal(tmpva, pa, 0);
4595 size -= PAGE_SIZE;
4596 tmpva += PAGE_SIZE;
4597 pa += PAGE_SIZE;
4598 }
4599
4600 return ((void *)(va + offset));
4601 }
4602
4603 #define BOOTSTRAP_DEBUG
4604
4605 /*
4606 * pmap_map_section:
4607 *
4608 * Create a single section mapping.
4609 */
4610 void
4611 pmap_map_section(vm_offset_t l1pt, vm_offset_t va, vm_offset_t pa,
4612 int prot, int cache)
4613 {
4614 pd_entry_t *pde = (pd_entry_t *) l1pt;
4615 pd_entry_t fl;
4616
4617 KASSERT(((va | pa) & L1_S_OFFSET) == 0, ("ouin2"));
4618
4619 switch (cache) {
4620 case PTE_NOCACHE:
4621 default:
4622 fl = 0;
4623 break;
4624
4625 case PTE_CACHE:
4626 fl = pte_l1_s_cache_mode;
4627 break;
4628
4629 case PTE_PAGETABLE:
4630 fl = pte_l1_s_cache_mode_pt;
4631 break;
4632 }
4633
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]);
4637
4638 }
4639
4640 /*
4641 * pmap_link_l2pt:
4642 *
4643 * Link the L2 page table specified by l2pv.pv_pa into the L1
4644 * page table at the slot for "va".
4645 */
4646 void
4647 pmap_link_l2pt(vm_offset_t l1pt, vm_offset_t va, struct pv_addr *l2pv)
4648 {
4649 pd_entry_t *pde = (pd_entry_t *) l1pt, proto;
4650 u_int slot = va >> L1_S_SHIFT;
4651
4652 proto = L1_S_DOM(PMAP_DOMAIN_KERNEL) | L1_C_PROTO;
4653
4654 #ifdef VERBOSE_INIT_ARM
4655 printf("pmap_link_l2pt: pa=0x%x va=0x%x\n", l2pv->pv_pa, l2pv->pv_va);
4656 #endif
4657
4658 pde[slot + 0] = proto | (l2pv->pv_pa + 0x000);
4659
4660 PTE_SYNC(&pde[slot]);
4661
4662 SLIST_INSERT_HEAD(&kernel_pt_list, l2pv, pv_list);
4663
4664
4665 }
4666
4667 /*
4668 * pmap_map_entry
4669 *
4670 * Create a single page mapping.
4671 */
4672 void
4673 pmap_map_entry(vm_offset_t l1pt, vm_offset_t va, vm_offset_t pa, int prot,
4674 int cache)
4675 {
4676 pd_entry_t *pde = (pd_entry_t *) l1pt;
4677 pt_entry_t fl;
4678 pt_entry_t *pte;
4679
4680 KASSERT(((va | pa) & PAGE_MASK) == 0, ("ouin"));
4681
4682 switch (cache) {
4683 case PTE_NOCACHE:
4684 default:
4685 fl = 0;
4686 break;
4687
4688 case PTE_CACHE:
4689 fl = pte_l2_s_cache_mode;
4690 break;
4691
4692 case PTE_PAGETABLE:
4693 fl = pte_l2_s_cache_mode_pt;
4694 break;
4695 }
4696
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);
4699
4700 pte = (pt_entry_t *) kernel_pt_lookup(pde[L1_IDX(va)] & L1_C_ADDR_MASK);
4701
4702 if (pte == NULL)
4703 panic("pmap_map_entry: can't find L2 table for VA 0x%08x", va);
4704
4705 pte[l2pte_index(va)] =
4706 L2_S_PROTO | pa | L2_S_PROT(PTE_KERNEL, prot) | fl;
4707 PTE_SYNC(&pte[l2pte_index(va)]);
4708 }
4709
4710 /*
4711 * pmap_map_chunk:
4712 *
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.
4716 */
4717 vm_size_t
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)
4720 {
4721 pd_entry_t *pde = (pd_entry_t *) l1pt;
4722 pt_entry_t *pte, f1, f2s, f2l;
4723 vm_size_t resid;
4724 int i;
4725
4726 resid = (size + (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
4727
4728 if (l1pt == 0)
4729 panic("pmap_map_chunk: no L1 table provided");
4730
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);
4734 #endif
4735
4736 switch (cache) {
4737 case PTE_NOCACHE:
4738 default:
4739 f1 = 0;
4740 f2l = 0;
4741 f2s = 0;
4742 break;
4743
4744 case PTE_CACHE:
4745 f1 = pte_l1_s_cache_mode;
4746 f2l = pte_l2_l_cache_mode;
4747 f2s = pte_l2_s_cache_mode;
4748 break;
4749
4750 case PTE_PAGETABLE:
4751 f1 = pte_l1_s_cache_mode_pt;
4752 f2l = pte_l2_l_cache_mode_pt;
4753 f2s = pte_l2_s_cache_mode_pt;
4754 break;
4755 }
4756
4757 size = resid;
4758
4759 while (resid > 0) {
4760 /* See if we can use a section mapping. */
4761 if (L1_S_MAPPABLE_P(va, pa, resid)) {
4762 #ifdef VERBOSE_INIT_ARM
4763 printf("S");
4764 #endif
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]);
4769 va += L1_S_SIZE;
4770 pa += L1_S_SIZE;
4771 resid -= L1_S_SIZE;
4772 continue;
4773 }
4774
4775 /*
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.
4779 */
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);
4782
4783 pte = (pt_entry_t *) kernel_pt_lookup(
4784 pde[L1_IDX(va)] & L1_C_ADDR_MASK);
4785 if (pte == NULL)
4786 panic("pmap_map_chunk: can't find L2 table for VA"
4787 "0x%08x", 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
4791 printf("L");
4792 #endif
4793 for (i = 0; i < 16; i++) {
4794 pte[l2pte_index(va) + i] =
4795 L2_L_PROTO | pa |
4796 L2_L_PROT(PTE_KERNEL, prot) | f2l;
4797 PTE_SYNC(&pte[l2pte_index(va) + i]);
4798 }
4799 va += L2_L_SIZE;
4800 pa += L2_L_SIZE;
4801 resid -= L2_L_SIZE;
4802 continue;
4803 }
4804
4805 /* Use a small page mapping. */
4806 #ifdef VERBOSE_INIT_ARM
4807 printf("P");
4808 #endif
4809 pte[l2pte_index(va)] =
4810 L2_S_PROTO | pa | L2_S_PROT(PTE_KERNEL, prot) | f2s;
4811 PTE_SYNC(&pte[l2pte_index(va)]);
4812 va += PAGE_SIZE;
4813 pa += PAGE_SIZE;
4814 resid -= PAGE_SIZE;
4815 }
4816 #ifdef VERBOSE_INIT_ARM
4817 printf("\n");
4818 #endif
4819 return (size);
4820
4821 }
4822
4823 /********************** Static device map routines ***************************/
4824
4825 static const struct pmap_devmap *pmap_devmap_table;
4826
4827 /*
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.
4831 */
4832 void
4833 pmap_devmap_register(const struct pmap_devmap *table)
4834 {
4835
4836 pmap_devmap_table = table;
4837 }
4838
4839 /*
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
4842 * later.
4843 */
4844 void
4845 pmap_devmap_bootstrap(vm_offset_t l1pt, const struct pmap_devmap *table)
4846 {
4847 int i;
4848
4849 pmap_devmap_table = table;
4850
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);
4858 #endif
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);
4864 }
4865 }
4866
4867 const struct pmap_devmap *
4868 pmap_devmap_find_pa(vm_paddr_t pa, vm_size_t size)
4869 {
4870 int i;
4871
4872 if (pmap_devmap_table == NULL)
4873 return (NULL);
4874
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]);
4880 }
4881
4882 return (NULL);
4883 }
4884
4885 const struct pmap_devmap *
4886 pmap_devmap_find_va(vm_offset_t va, vm_size_t size)
4887 {
4888 int i;
4889
4890 if (pmap_devmap_table == NULL)
4891 return (NULL);
4892
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]);
4898 }
4899
4900 return (NULL);
4901 }
4902
Cache object: 3d1fe574e71582dc10886b5d8fb7383b
|