pmap.c revision 1.178 1 /* $NetBSD: pmap.c,v 1.178 2008/06/24 22:00:32 scw Exp $ */
2
3 /*
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 /*-
67 * Copyright (c) 1999 The NetBSD Foundation, Inc.
68 * All rights reserved.
69 *
70 * This code is derived from software contributed to The NetBSD Foundation
71 * by Charles M. Hannum.
72 *
73 * Redistribution and use in source and binary forms, with or without
74 * modification, are permitted provided that the following conditions
75 * are met:
76 * 1. Redistributions of source code must retain the above copyright
77 * notice, this list of conditions and the following disclaimer.
78 * 2. Redistributions in binary form must reproduce the above copyright
79 * notice, this list of conditions and the following disclaimer in the
80 * documentation and/or other materials provided with the distribution.
81 *
82 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
83 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
84 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
85 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
86 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
87 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
88 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
89 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
90 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
91 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
92 * POSSIBILITY OF SUCH DAMAGE.
93 */
94
95 /*
96 * Copyright (c) 1994-1998 Mark Brinicombe.
97 * Copyright (c) 1994 Brini.
98 * All rights reserved.
99 *
100 * This code is derived from software written for Brini by Mark Brinicombe
101 *
102 * Redistribution and use in source and binary forms, with or without
103 * modification, are permitted provided that the following conditions
104 * are met:
105 * 1. Redistributions of source code must retain the above copyright
106 * notice, this list of conditions and the following disclaimer.
107 * 2. Redistributions in binary form must reproduce the above copyright
108 * notice, this list of conditions and the following disclaimer in the
109 * documentation and/or other materials provided with the distribution.
110 * 3. All advertising materials mentioning features or use of this software
111 * must display the following acknowledgement:
112 * This product includes software developed by Mark Brinicombe.
113 * 4. The name of the author may not be used to endorse or promote products
114 * derived from this software without specific prior written permission.
115 *
116 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
117 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
118 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
119 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
120 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
121 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
122 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
123 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
124 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
125 *
126 * RiscBSD kernel project
127 *
128 * pmap.c
129 *
130 * Machine dependant vm stuff
131 *
132 * Created : 20/09/94
133 */
134
135 /*
136 * armv6 and VIPT cache support by 3am Software Foundry,
137 * Copyright (c) 2007 Microsoft
138 */
139
140 /*
141 * Performance improvements, UVM changes, overhauls and part-rewrites
142 * were contributed by Neil A. Carson <neil (at) causality.com>.
143 */
144
145 /*
146 * Overhauled again to speedup the pmap, use MMU Domains so that L1 tables
147 * can be shared, and re-work the KVM layout, by Steve Woodford of Wasabi
148 * Systems, Inc.
149 *
150 * There are still a few things outstanding at this time:
151 *
152 * - There are some unresolved issues for MP systems:
153 *
154 * o The L1 metadata needs a lock, or more specifically, some places
155 * need to acquire an exclusive lock when modifying L1 translation
156 * table entries.
157 *
158 * o When one cpu modifies an L1 entry, and that L1 table is also
159 * being used by another cpu, then the latter will need to be told
160 * that a tlb invalidation may be necessary. (But only if the old
161 * domain number in the L1 entry being over-written is currently
162 * the active domain on that cpu). I guess there are lots more tlb
163 * shootdown issues too...
164 *
165 * o If the vector_page is at 0x00000000 instead of 0xffff0000, then
166 * MP systems will lose big-time because of the MMU domain hack.
167 * The only way this can be solved (apart from moving the vector
168 * page to 0xffff0000) is to reserve the first 1MB of user address
169 * space for kernel use only. This would require re-linking all
170 * applications so that the text section starts above this 1MB
171 * boundary.
172 *
173 * o Tracking which VM space is resident in the cache/tlb has not yet
174 * been implemented for MP systems.
175 *
176 * o Finally, there is a pathological condition where two cpus running
177 * two separate processes (not lwps) which happen to share an L1
178 * can get into a fight over one or more L1 entries. This will result
179 * in a significant slow-down if both processes are in tight loops.
180 */
181
182 /*
183 * Special compilation symbols
184 * PMAP_DEBUG - Build in pmap_debug_level code
185 */
186
187 /* Include header files */
188
189 #include "opt_cpuoptions.h"
190 #include "opt_pmap_debug.h"
191 #include "opt_ddb.h"
192 #include "opt_lockdebug.h"
193 #include "opt_multiprocessor.h"
194
195 #include <sys/param.h>
196 #include <sys/types.h>
197 #include <sys/kernel.h>
198 #include <sys/systm.h>
199 #include <sys/proc.h>
200 #include <sys/malloc.h>
201 #include <sys/user.h>
202 #include <sys/pool.h>
203 #include <sys/cdefs.h>
204 #include <sys/cpu.h>
205
206 #include <uvm/uvm.h>
207
208 #include <machine/bus.h>
209 #include <machine/pmap.h>
210 #include <machine/pcb.h>
211 #include <machine/param.h>
212 #include <arm/arm32/katelib.h>
213
214 __KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.178 2008/06/24 22:00:32 scw Exp $");
215
216 #ifdef PMAP_DEBUG
217
218 /* XXX need to get rid of all refs to this */
219 int pmap_debug_level = 0;
220
221 /*
222 * for switching to potentially finer grained debugging
223 */
224 #define PDB_FOLLOW 0x0001
225 #define PDB_INIT 0x0002
226 #define PDB_ENTER 0x0004
227 #define PDB_REMOVE 0x0008
228 #define PDB_CREATE 0x0010
229 #define PDB_PTPAGE 0x0020
230 #define PDB_GROWKERN 0x0040
231 #define PDB_BITS 0x0080
232 #define PDB_COLLECT 0x0100
233 #define PDB_PROTECT 0x0200
234 #define PDB_MAP_L1 0x0400
235 #define PDB_BOOTSTRAP 0x1000
236 #define PDB_PARANOIA 0x2000
237 #define PDB_WIRING 0x4000
238 #define PDB_PVDUMP 0x8000
239 #define PDB_VAC 0x10000
240 #define PDB_KENTER 0x20000
241 #define PDB_KREMOVE 0x40000
242 #define PDB_EXEC 0x80000
243
244 int debugmap = 1;
245 int pmapdebug = 0;
246 #define NPDEBUG(_lev_,_stat_) \
247 if (pmapdebug & (_lev_)) \
248 ((_stat_))
249
250 #else /* PMAP_DEBUG */
251 #define NPDEBUG(_lev_,_stat_) /* Nothing */
252 #endif /* PMAP_DEBUG */
253
254 /*
255 * pmap_kernel() points here
256 */
257 struct pmap kernel_pmap_store;
258
259 /*
260 * Which pmap is currently 'live' in the cache
261 *
262 * XXXSCW: Fix for SMP ...
263 */
264 static pmap_t pmap_recent_user;
265
266 /*
267 * Pointer to last active lwp, or NULL if it exited.
268 */
269 struct lwp *pmap_previous_active_lwp;
270
271 /*
272 * Pool and cache that pmap structures are allocated from.
273 * We use a cache to avoid clearing the pm_l2[] array (1KB)
274 * in pmap_create().
275 */
276 static struct pool_cache pmap_cache;
277 static LIST_HEAD(, pmap) pmap_pmaps;
278
279 /*
280 * Pool of PV structures
281 */
282 static struct pool pmap_pv_pool;
283 static void *pmap_bootstrap_pv_page_alloc(struct pool *, int);
284 static void pmap_bootstrap_pv_page_free(struct pool *, void *);
285 static struct pool_allocator pmap_bootstrap_pv_allocator = {
286 pmap_bootstrap_pv_page_alloc, pmap_bootstrap_pv_page_free
287 };
288
289 /*
290 * Pool and cache of l2_dtable structures.
291 * We use a cache to avoid clearing the structures when they're
292 * allocated. (196 bytes)
293 */
294 static struct pool_cache pmap_l2dtable_cache;
295 static vaddr_t pmap_kernel_l2dtable_kva;
296
297 /*
298 * Pool and cache of L2 page descriptors.
299 * We use a cache to avoid clearing the descriptor table
300 * when they're allocated. (1KB)
301 */
302 static struct pool_cache pmap_l2ptp_cache;
303 static vaddr_t pmap_kernel_l2ptp_kva;
304 static paddr_t pmap_kernel_l2ptp_phys;
305
306 #ifdef PMAPCOUNT
307 #define PMAP_EVCNT_INITIALIZER(name) \
308 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap", name)
309
310 #ifdef PMAP_CACHE_VIPT
311 static struct evcnt pmap_ev_vac_color_new =
312 PMAP_EVCNT_INITIALIZER("new page color");
313 static struct evcnt pmap_ev_vac_color_reuse =
314 PMAP_EVCNT_INITIALIZER("ok first page color");
315 static struct evcnt pmap_ev_vac_color_ok =
316 PMAP_EVCNT_INITIALIZER("ok page color");
317 static struct evcnt pmap_ev_vac_color_change =
318 PMAP_EVCNT_INITIALIZER("change page color");
319 static struct evcnt pmap_ev_vac_color_erase =
320 PMAP_EVCNT_INITIALIZER("erase page color");
321 static struct evcnt pmap_ev_vac_color_none =
322 PMAP_EVCNT_INITIALIZER("no page color");
323 static struct evcnt pmap_ev_vac_color_restore =
324 PMAP_EVCNT_INITIALIZER("restore page color");
325
326 EVCNT_ATTACH_STATIC(pmap_ev_vac_color_new);
327 EVCNT_ATTACH_STATIC(pmap_ev_vac_color_reuse);
328 EVCNT_ATTACH_STATIC(pmap_ev_vac_color_ok);
329 EVCNT_ATTACH_STATIC(pmap_ev_vac_color_change);
330 EVCNT_ATTACH_STATIC(pmap_ev_vac_color_erase);
331 EVCNT_ATTACH_STATIC(pmap_ev_vac_color_none);
332 EVCNT_ATTACH_STATIC(pmap_ev_vac_color_restore);
333 #endif
334
335 static struct evcnt pmap_ev_mappings =
336 PMAP_EVCNT_INITIALIZER("pages mapped");
337 static struct evcnt pmap_ev_unmappings =
338 PMAP_EVCNT_INITIALIZER("pages unmapped");
339 static struct evcnt pmap_ev_remappings =
340 PMAP_EVCNT_INITIALIZER("pages remapped");
341
342 EVCNT_ATTACH_STATIC(pmap_ev_mappings);
343 EVCNT_ATTACH_STATIC(pmap_ev_unmappings);
344 EVCNT_ATTACH_STATIC(pmap_ev_remappings);
345
346 static struct evcnt pmap_ev_kernel_mappings =
347 PMAP_EVCNT_INITIALIZER("kernel pages mapped");
348 static struct evcnt pmap_ev_kernel_unmappings =
349 PMAP_EVCNT_INITIALIZER("kernel pages unmapped");
350 static struct evcnt pmap_ev_kernel_remappings =
351 PMAP_EVCNT_INITIALIZER("kernel pages remapped");
352
353 EVCNT_ATTACH_STATIC(pmap_ev_kernel_mappings);
354 EVCNT_ATTACH_STATIC(pmap_ev_kernel_unmappings);
355 EVCNT_ATTACH_STATIC(pmap_ev_kernel_remappings);
356
357 static struct evcnt pmap_ev_kenter_mappings =
358 PMAP_EVCNT_INITIALIZER("kenter pages mapped");
359 static struct evcnt pmap_ev_kenter_unmappings =
360 PMAP_EVCNT_INITIALIZER("kenter pages unmapped");
361 static struct evcnt pmap_ev_kenter_remappings =
362 PMAP_EVCNT_INITIALIZER("kenter pages remapped");
363 static struct evcnt pmap_ev_pt_mappings =
364 PMAP_EVCNT_INITIALIZER("page table pages mapped");
365
366 EVCNT_ATTACH_STATIC(pmap_ev_kenter_mappings);
367 EVCNT_ATTACH_STATIC(pmap_ev_kenter_unmappings);
368 EVCNT_ATTACH_STATIC(pmap_ev_kenter_remappings);
369 EVCNT_ATTACH_STATIC(pmap_ev_pt_mappings);
370
371 #ifdef PMAP_CACHE_VIPT
372 static struct evcnt pmap_ev_exec_mappings =
373 PMAP_EVCNT_INITIALIZER("exec pages mapped");
374 static struct evcnt pmap_ev_exec_cached =
375 PMAP_EVCNT_INITIALIZER("exec pages cached");
376
377 EVCNT_ATTACH_STATIC(pmap_ev_exec_mappings);
378 EVCNT_ATTACH_STATIC(pmap_ev_exec_cached);
379
380 static struct evcnt pmap_ev_exec_synced =
381 PMAP_EVCNT_INITIALIZER("exec pages synced");
382 static struct evcnt pmap_ev_exec_synced_map =
383 PMAP_EVCNT_INITIALIZER("exec pages synced (MP)");
384 static struct evcnt pmap_ev_exec_synced_unmap =
385 PMAP_EVCNT_INITIALIZER("exec pages synced (UM)");
386 static struct evcnt pmap_ev_exec_synced_remap =
387 PMAP_EVCNT_INITIALIZER("exec pages synced (RM)");
388 static struct evcnt pmap_ev_exec_synced_clearbit =
389 PMAP_EVCNT_INITIALIZER("exec pages synced (DG)");
390 static struct evcnt pmap_ev_exec_synced_kremove =
391 PMAP_EVCNT_INITIALIZER("exec pages synced (KU)");
392
393 EVCNT_ATTACH_STATIC(pmap_ev_exec_synced);
394 EVCNT_ATTACH_STATIC(pmap_ev_exec_synced_map);
395 EVCNT_ATTACH_STATIC(pmap_ev_exec_synced_unmap);
396 EVCNT_ATTACH_STATIC(pmap_ev_exec_synced_remap);
397 EVCNT_ATTACH_STATIC(pmap_ev_exec_synced_clearbit);
398 EVCNT_ATTACH_STATIC(pmap_ev_exec_synced_kremove);
399
400 static struct evcnt pmap_ev_exec_discarded_unmap =
401 PMAP_EVCNT_INITIALIZER("exec pages discarded (UM)");
402 static struct evcnt pmap_ev_exec_discarded_zero =
403 PMAP_EVCNT_INITIALIZER("exec pages discarded (ZP)");
404 static struct evcnt pmap_ev_exec_discarded_copy =
405 PMAP_EVCNT_INITIALIZER("exec pages discarded (CP)");
406 static struct evcnt pmap_ev_exec_discarded_page_protect =
407 PMAP_EVCNT_INITIALIZER("exec pages discarded (PP)");
408 static struct evcnt pmap_ev_exec_discarded_clearbit =
409 PMAP_EVCNT_INITIALIZER("exec pages discarded (DG)");
410 static struct evcnt pmap_ev_exec_discarded_kremove =
411 PMAP_EVCNT_INITIALIZER("exec pages discarded (KU)");
412
413 EVCNT_ATTACH_STATIC(pmap_ev_exec_discarded_unmap);
414 EVCNT_ATTACH_STATIC(pmap_ev_exec_discarded_zero);
415 EVCNT_ATTACH_STATIC(pmap_ev_exec_discarded_copy);
416 EVCNT_ATTACH_STATIC(pmap_ev_exec_discarded_page_protect);
417 EVCNT_ATTACH_STATIC(pmap_ev_exec_discarded_clearbit);
418 EVCNT_ATTACH_STATIC(pmap_ev_exec_discarded_kremove);
419 #endif /* PMAP_CACHE_VIPT */
420
421 static struct evcnt pmap_ev_updates = PMAP_EVCNT_INITIALIZER("updates");
422 static struct evcnt pmap_ev_collects = PMAP_EVCNT_INITIALIZER("collects");
423 static struct evcnt pmap_ev_activations = PMAP_EVCNT_INITIALIZER("activations");
424
425 EVCNT_ATTACH_STATIC(pmap_ev_updates);
426 EVCNT_ATTACH_STATIC(pmap_ev_collects);
427 EVCNT_ATTACH_STATIC(pmap_ev_activations);
428
429 #define PMAPCOUNT(x) ((void)(pmap_ev_##x.ev_count++))
430 #else
431 #define PMAPCOUNT(x) ((void)0)
432 #endif
433
434 /*
435 * pmap copy/zero page, and mem(5) hook point
436 */
437 static pt_entry_t *csrc_pte, *cdst_pte;
438 static vaddr_t csrcp, cdstp;
439 vaddr_t memhook;
440 extern void *msgbufaddr;
441
442 /*
443 * Flag to indicate if pmap_init() has done its thing
444 */
445 bool pmap_initialized;
446
447 /*
448 * Misc. locking data structures
449 */
450
451 #if 0 /* defined(MULTIPROCESSOR) || defined(LOCKDEBUG) */
452 static struct lock pmap_main_lock;
453
454 #define PMAP_MAP_TO_HEAD_LOCK() \
455 (void) spinlockmgr(&pmap_main_lock, LK_SHARED, NULL)
456 #define PMAP_MAP_TO_HEAD_UNLOCK() \
457 (void) spinlockmgr(&pmap_main_lock, LK_RELEASE, NULL)
458 #define PMAP_HEAD_TO_MAP_LOCK() \
459 (void) spinlockmgr(&pmap_main_lock, LK_EXCLUSIVE, NULL)
460 #define PMAP_HEAD_TO_MAP_UNLOCK() \
461 spinlockmgr(&pmap_main_lock, LK_RELEASE, (void *) 0)
462 #else
463 #define PMAP_MAP_TO_HEAD_LOCK() /* null */
464 #define PMAP_MAP_TO_HEAD_UNLOCK() /* null */
465 #define PMAP_HEAD_TO_MAP_LOCK() /* null */
466 #define PMAP_HEAD_TO_MAP_UNLOCK() /* null */
467 #endif
468
469 #define pmap_acquire_pmap_lock(pm) \
470 do { \
471 if ((pm) != pmap_kernel()) \
472 mutex_enter(&(pm)->pm_lock); \
473 } while (/*CONSTCOND*/0)
474
475 #define pmap_release_pmap_lock(pm) \
476 do { \
477 if ((pm) != pmap_kernel()) \
478 mutex_exit(&(pm)->pm_lock); \
479 } while (/*CONSTCOND*/0)
480
481
482 /*
483 * Metadata for L1 translation tables.
484 */
485 struct l1_ttable {
486 /* Entry on the L1 Table list */
487 SLIST_ENTRY(l1_ttable) l1_link;
488
489 /* Entry on the L1 Least Recently Used list */
490 TAILQ_ENTRY(l1_ttable) l1_lru;
491
492 /* Track how many domains are allocated from this L1 */
493 volatile u_int l1_domain_use_count;
494
495 /*
496 * A free-list of domain numbers for this L1.
497 * We avoid using ffs() and a bitmap to track domains since ffs()
498 * is slow on ARM.
499 */
500 u_int8_t l1_domain_first;
501 u_int8_t l1_domain_free[PMAP_DOMAINS];
502
503 /* Physical address of this L1 page table */
504 paddr_t l1_physaddr;
505
506 /* KVA of this L1 page table */
507 pd_entry_t *l1_kva;
508 };
509
510 /*
511 * Convert a virtual address into its L1 table index. That is, the
512 * index used to locate the L2 descriptor table pointer in an L1 table.
513 * This is basically used to index l1->l1_kva[].
514 *
515 * Each L2 descriptor table represents 1MB of VA space.
516 */
517 #define L1_IDX(va) (((vaddr_t)(va)) >> L1_S_SHIFT)
518
519 /*
520 * L1 Page Tables are tracked using a Least Recently Used list.
521 * - New L1s are allocated from the HEAD.
522 * - Freed L1s are added to the TAIl.
523 * - Recently accessed L1s (where an 'access' is some change to one of
524 * the userland pmaps which owns this L1) are moved to the TAIL.
525 */
526 static TAILQ_HEAD(, l1_ttable) l1_lru_list;
527 static struct simplelock l1_lru_lock;
528
529 /*
530 * A list of all L1 tables
531 */
532 static SLIST_HEAD(, l1_ttable) l1_list;
533
534 /*
535 * The l2_dtable tracks L2_BUCKET_SIZE worth of L1 slots.
536 *
537 * This is normally 16MB worth L2 page descriptors for any given pmap.
538 * Reference counts are maintained for L2 descriptors so they can be
539 * freed when empty.
540 */
541 struct l2_dtable {
542 /* The number of L2 page descriptors allocated to this l2_dtable */
543 u_int l2_occupancy;
544
545 /* List of L2 page descriptors */
546 struct l2_bucket {
547 pt_entry_t *l2b_kva; /* KVA of L2 Descriptor Table */
548 paddr_t l2b_phys; /* Physical address of same */
549 u_short l2b_l1idx; /* This L2 table's L1 index */
550 u_short l2b_occupancy; /* How many active descriptors */
551 } l2_bucket[L2_BUCKET_SIZE];
552 };
553
554 /*
555 * Given an L1 table index, calculate the corresponding l2_dtable index
556 * and bucket index within the l2_dtable.
557 */
558 #define L2_IDX(l1idx) (((l1idx) >> L2_BUCKET_LOG2) & \
559 (L2_SIZE - 1))
560 #define L2_BUCKET(l1idx) ((l1idx) & (L2_BUCKET_SIZE - 1))
561
562 /*
563 * Given a virtual address, this macro returns the
564 * virtual address required to drop into the next L2 bucket.
565 */
566 #define L2_NEXT_BUCKET(va) (((va) & L1_S_FRAME) + L1_S_SIZE)
567
568 /*
569 * L2 allocation.
570 */
571 #define pmap_alloc_l2_dtable() \
572 pool_cache_get(&pmap_l2dtable_cache, PR_NOWAIT)
573 #define pmap_free_l2_dtable(l2) \
574 pool_cache_put(&pmap_l2dtable_cache, (l2))
575 #define pmap_alloc_l2_ptp(pap) \
576 ((pt_entry_t *)pool_cache_get_paddr(&pmap_l2ptp_cache,\
577 PR_NOWAIT, (pap)))
578
579 /*
580 * We try to map the page tables write-through, if possible. However, not
581 * all CPUs have a write-through cache mode, so on those we have to sync
582 * the cache when we frob page tables.
583 *
584 * We try to evaluate this at compile time, if possible. However, it's
585 * not always possible to do that, hence this run-time var.
586 */
587 int pmap_needs_pte_sync;
588
589 /*
590 * Real definition of pv_entry.
591 */
592 struct pv_entry {
593 struct pv_entry *pv_next; /* next pv_entry */
594 pmap_t pv_pmap; /* pmap where mapping lies */
595 vaddr_t pv_va; /* virtual address for mapping */
596 u_int pv_flags; /* flags */
597 };
598
599 /*
600 * Macro to determine if a mapping might be resident in the
601 * instruction cache and/or TLB
602 */
603 #define PV_BEEN_EXECD(f) (((f) & (PVF_REF | PVF_EXEC)) == (PVF_REF | PVF_EXEC))
604 #define PV_IS_EXEC_P(f) (((f) & PVF_EXEC) != 0)
605
606 /*
607 * Macro to determine if a mapping might be resident in the
608 * data cache and/or TLB
609 */
610 #define PV_BEEN_REFD(f) (((f) & PVF_REF) != 0)
611
612 /*
613 * Local prototypes
614 */
615 static int pmap_set_pt_cache_mode(pd_entry_t *, vaddr_t);
616 static void pmap_alloc_specials(vaddr_t *, int, vaddr_t *,
617 pt_entry_t **);
618 static bool pmap_is_current(pmap_t);
619 static bool pmap_is_cached(pmap_t);
620 static void pmap_enter_pv(struct vm_page *, struct pv_entry *,
621 pmap_t, vaddr_t, u_int);
622 static struct pv_entry *pmap_find_pv(struct vm_page *, pmap_t, vaddr_t);
623 static struct pv_entry *pmap_remove_pv(struct vm_page *, pmap_t, vaddr_t, int);
624 static u_int pmap_modify_pv(struct vm_page *, pmap_t, vaddr_t,
625 u_int, u_int);
626
627 static void pmap_pinit(pmap_t);
628 static int pmap_pmap_ctor(void *, void *, int);
629
630 static void pmap_alloc_l1(pmap_t);
631 static void pmap_free_l1(pmap_t);
632 static void pmap_use_l1(pmap_t);
633
634 static struct l2_bucket *pmap_get_l2_bucket(pmap_t, vaddr_t);
635 static struct l2_bucket *pmap_alloc_l2_bucket(pmap_t, vaddr_t);
636 static void pmap_free_l2_bucket(pmap_t, struct l2_bucket *, u_int);
637 static int pmap_l2ptp_ctor(void *, void *, int);
638 static int pmap_l2dtable_ctor(void *, void *, int);
639
640 static void pmap_vac_me_harder(struct vm_page *, pmap_t, vaddr_t);
641 #ifdef PMAP_CACHE_VIVT
642 static void pmap_vac_me_kpmap(struct vm_page *, pmap_t, vaddr_t);
643 static void pmap_vac_me_user(struct vm_page *, pmap_t, vaddr_t);
644 #endif
645
646 static void pmap_clearbit(struct vm_page *, u_int);
647 #ifdef PMAP_CACHE_VIVT
648 static int pmap_clean_page(struct pv_entry *, bool);
649 #endif
650 #ifdef PMAP_CACHE_VIPT
651 static void pmap_syncicache_page(struct vm_page *);
652 static void pmap_flush_page(struct vm_page *);
653 #endif
654 static void pmap_page_remove(struct vm_page *);
655
656 static void pmap_init_l1(struct l1_ttable *, pd_entry_t *);
657 static vaddr_t kernel_pt_lookup(paddr_t);
658
659
660 /*
661 * External function prototypes
662 */
663 extern void bzero_page(vaddr_t);
664 extern void bcopy_page(vaddr_t, vaddr_t);
665
666 /*
667 * Misc variables
668 */
669 vaddr_t virtual_avail;
670 vaddr_t virtual_end;
671 vaddr_t pmap_curmaxkvaddr;
672
673 vaddr_t avail_start;
674 vaddr_t avail_end;
675
676 pv_addrqh_t pmap_boot_freeq = SLIST_HEAD_INITIALIZER(&pmap_boot_freeq);
677 pv_addr_t kernelpages;
678 pv_addr_t kernel_l1pt;
679 pv_addr_t systempage;
680
681 /* Function to set the debug level of the pmap code */
682
683 #ifdef PMAP_DEBUG
684 void
685 pmap_debug(int level)
686 {
687 pmap_debug_level = level;
688 printf("pmap_debug: level=%d\n", pmap_debug_level);
689 }
690 #endif /* PMAP_DEBUG */
691
692 /*
693 * A bunch of routines to conditionally flush the caches/TLB depending
694 * on whether the specified pmap actually needs to be flushed at any
695 * given time.
696 */
697 static inline void
698 pmap_tlb_flushID_SE(pmap_t pm, vaddr_t va)
699 {
700
701 if (pm->pm_cstate.cs_tlb_id)
702 cpu_tlb_flushID_SE(va);
703 }
704
705 static inline void
706 pmap_tlb_flushD_SE(pmap_t pm, vaddr_t va)
707 {
708
709 if (pm->pm_cstate.cs_tlb_d)
710 cpu_tlb_flushD_SE(va);
711 }
712
713 static inline void
714 pmap_tlb_flushID(pmap_t pm)
715 {
716
717 if (pm->pm_cstate.cs_tlb_id) {
718 cpu_tlb_flushID();
719 pm->pm_cstate.cs_tlb = 0;
720 }
721 }
722
723 static inline void
724 pmap_tlb_flushD(pmap_t pm)
725 {
726
727 if (pm->pm_cstate.cs_tlb_d) {
728 cpu_tlb_flushD();
729 pm->pm_cstate.cs_tlb_d = 0;
730 }
731 }
732
733 #ifdef PMAP_CACHE_VIVT
734 static inline void
735 pmap_idcache_wbinv_range(pmap_t pm, vaddr_t va, vsize_t len)
736 {
737 if (pm->pm_cstate.cs_cache_id) {
738 cpu_idcache_wbinv_range(va, len);
739 }
740 }
741
742 static inline void
743 pmap_dcache_wb_range(pmap_t pm, vaddr_t va, vsize_t len,
744 bool do_inv, bool rd_only)
745 {
746
747 if (pm->pm_cstate.cs_cache_d) {
748 if (do_inv) {
749 if (rd_only)
750 cpu_dcache_inv_range(va, len);
751 else
752 cpu_dcache_wbinv_range(va, len);
753 } else
754 if (!rd_only)
755 cpu_dcache_wb_range(va, len);
756 }
757 }
758
759 static inline void
760 pmap_idcache_wbinv_all(pmap_t pm)
761 {
762 if (pm->pm_cstate.cs_cache_id) {
763 cpu_idcache_wbinv_all();
764 pm->pm_cstate.cs_cache = 0;
765 }
766 }
767
768 static inline void
769 pmap_dcache_wbinv_all(pmap_t pm)
770 {
771 if (pm->pm_cstate.cs_cache_d) {
772 cpu_dcache_wbinv_all();
773 pm->pm_cstate.cs_cache_d = 0;
774 }
775 }
776 #endif /* PMAP_CACHE_VIVT */
777
778 static inline bool
779 pmap_is_current(pmap_t pm)
780 {
781
782 if (pm == pmap_kernel() ||
783 (curproc && curproc->p_vmspace->vm_map.pmap == pm))
784 return true;
785
786 return false;
787 }
788
789 static inline bool
790 pmap_is_cached(pmap_t pm)
791 {
792
793 if (pm == pmap_kernel() || pmap_recent_user == NULL ||
794 pmap_recent_user == pm)
795 return (true);
796
797 return false;
798 }
799
800 /*
801 * PTE_SYNC_CURRENT:
802 *
803 * Make sure the pte is written out to RAM.
804 * We need to do this for one of two cases:
805 * - We're dealing with the kernel pmap
806 * - There is no pmap active in the cache/tlb.
807 * - The specified pmap is 'active' in the cache/tlb.
808 */
809 #ifdef PMAP_INCLUDE_PTE_SYNC
810 #define PTE_SYNC_CURRENT(pm, ptep) \
811 do { \
812 if (PMAP_NEEDS_PTE_SYNC && \
813 pmap_is_cached(pm)) \
814 PTE_SYNC(ptep); \
815 } while (/*CONSTCOND*/0)
816 #else
817 #define PTE_SYNC_CURRENT(pm, ptep) /* nothing */
818 #endif
819
820 /*
821 * main pv_entry manipulation functions:
822 * pmap_enter_pv: enter a mapping onto a vm_page list
823 * pmap_remove_pv: remove a mappiing from a vm_page list
824 *
825 * NOTE: pmap_enter_pv expects to lock the pvh itself
826 * pmap_remove_pv expects te caller to lock the pvh before calling
827 */
828
829 /*
830 * pmap_enter_pv: enter a mapping onto a vm_page lst
831 *
832 * => caller should hold the proper lock on pmap_main_lock
833 * => caller should have pmap locked
834 * => we will gain the lock on the vm_page and allocate the new pv_entry
835 * => caller should adjust ptp's wire_count before calling
836 * => caller should not adjust pmap's wire_count
837 */
838 static void
839 pmap_enter_pv(struct vm_page *pg, struct pv_entry *pve, pmap_t pm,
840 vaddr_t va, u_int flags)
841 {
842
843 NPDEBUG(PDB_PVDUMP,
844 printf("pmap_enter_pv: pm %p, pg %p, flags 0x%x\n", pm, pg, flags));
845
846 pve->pv_pmap = pm;
847 pve->pv_va = va;
848 pve->pv_flags = flags;
849
850 simple_lock(&pg->mdpage.pvh_slock); /* lock vm_page */
851 pve->pv_next = pg->mdpage.pvh_list; /* add to ... */
852 pg->mdpage.pvh_list = pve; /* ... locked list */
853 pg->mdpage.pvh_attrs |= flags & (PVF_REF | PVF_MOD);
854 if (pm == pmap_kernel()) {
855 PMAPCOUNT(kernel_mappings);
856 if (flags & PVF_WRITE)
857 pg->mdpage.krw_mappings++;
858 else
859 pg->mdpage.kro_mappings++;
860 } else
861 if (flags & PVF_WRITE)
862 pg->mdpage.urw_mappings++;
863 else
864 pg->mdpage.uro_mappings++;
865
866 #ifdef PMAP_CACHE_VIPT
867 /*
868 * If this is an exec mapping and its the first exec mapping
869 * for this page, make sure to sync the I-cache.
870 */
871 if (PV_IS_EXEC_P(flags)) {
872 if (!PV_IS_EXEC_P(pg->mdpage.pvh_attrs)) {
873 pmap_syncicache_page(pg);
874 PMAPCOUNT(exec_synced_map);
875 }
876 PMAPCOUNT(exec_mappings);
877 }
878 #endif
879
880 PMAPCOUNT(mappings);
881 simple_unlock(&pg->mdpage.pvh_slock); /* unlock, done! */
882
883 if (pve->pv_flags & PVF_WIRED)
884 ++pm->pm_stats.wired_count;
885 }
886
887 /*
888 *
889 * pmap_find_pv: Find a pv entry
890 *
891 * => caller should hold lock on vm_page
892 */
893 static inline struct pv_entry *
894 pmap_find_pv(struct vm_page *pg, pmap_t pm, vaddr_t va)
895 {
896 struct pv_entry *pv;
897
898 for (pv = pg->mdpage.pvh_list; pv; pv = pv->pv_next) {
899 if (pm == pv->pv_pmap && va == pv->pv_va)
900 break;
901 }
902
903 return (pv);
904 }
905
906 /*
907 * pmap_remove_pv: try to remove a mapping from a pv_list
908 *
909 * => caller should hold proper lock on pmap_main_lock
910 * => pmap should be locked
911 * => caller should hold lock on vm_page [so that attrs can be adjusted]
912 * => caller should adjust ptp's wire_count and free PTP if needed
913 * => caller should NOT adjust pmap's wire_count
914 * => we return the removed pve
915 */
916 static struct pv_entry *
917 pmap_remove_pv(struct vm_page *pg, pmap_t pm, vaddr_t va, int skip_wired)
918 {
919 struct pv_entry *pve, **prevptr;
920
921 NPDEBUG(PDB_PVDUMP,
922 printf("pmap_remove_pv: pm %p, pg %p, va 0x%08lx\n", pm, pg, va));
923
924 prevptr = &pg->mdpage.pvh_list; /* previous pv_entry pointer */
925 pve = *prevptr;
926
927 while (pve) {
928 if (pve->pv_pmap == pm && pve->pv_va == va) { /* match? */
929 NPDEBUG(PDB_PVDUMP, printf("pmap_remove_pv: pm %p, pg "
930 "%p, flags 0x%x\n", pm, pg, pve->pv_flags));
931 if (pve->pv_flags & PVF_WIRED) {
932 if (skip_wired)
933 return (NULL);
934 --pm->pm_stats.wired_count;
935 }
936 *prevptr = pve->pv_next; /* remove it! */
937 if (pm == pmap_kernel()) {
938 PMAPCOUNT(kernel_unmappings);
939 if (pve->pv_flags & PVF_WRITE)
940 pg->mdpage.krw_mappings--;
941 else
942 pg->mdpage.kro_mappings--;
943 } else
944 if (pve->pv_flags & PVF_WRITE)
945 pg->mdpage.urw_mappings--;
946 else
947 pg->mdpage.uro_mappings--;
948
949 PMAPCOUNT(unmappings);
950 #ifdef PMAP_CACHE_VIPT
951 if (!(pve->pv_flags & PVF_WRITE))
952 break;
953 /*
954 * If this page has had an exec mapping, then if
955 * this was the last mapping, discard the contents,
956 * otherwise sync the i-cache for this page.
957 */
958 if (PV_IS_EXEC_P(pg->mdpage.pvh_attrs)) {
959 if (pg->mdpage.pvh_list == NULL) {
960 pg->mdpage.pvh_attrs &= ~PVF_EXEC;
961 PMAPCOUNT(exec_discarded_unmap);
962 } else {
963 pmap_syncicache_page(pg);
964 PMAPCOUNT(exec_synced_unmap);
965 }
966 }
967 #endif /* PMAP_CACHE_VIPT */
968 break;
969 }
970 prevptr = &pve->pv_next; /* previous pointer */
971 pve = pve->pv_next; /* advance */
972 }
973
974 return(pve); /* return removed pve */
975 }
976
977 /*
978 *
979 * pmap_modify_pv: Update pv flags
980 *
981 * => caller should hold lock on vm_page [so that attrs can be adjusted]
982 * => caller should NOT adjust pmap's wire_count
983 * => caller must call pmap_vac_me_harder() if writable status of a page
984 * may have changed.
985 * => we return the old flags
986 *
987 * Modify a physical-virtual mapping in the pv table
988 */
989 static u_int
990 pmap_modify_pv(struct vm_page *pg, pmap_t pm, vaddr_t va,
991 u_int clr_mask, u_int set_mask)
992 {
993 struct pv_entry *npv;
994 u_int flags, oflags;
995
996 if ((npv = pmap_find_pv(pg, pm, va)) == NULL)
997 return (0);
998
999 NPDEBUG(PDB_PVDUMP,
1000 printf("pmap_modify_pv: pm %p, pg %p, clr 0x%x, set 0x%x, flags 0x%x\n", pm, pg, clr_mask, set_mask, npv->pv_flags));
1001
1002 /*
1003 * There is at least one VA mapping this page.
1004 */
1005
1006 if (clr_mask & (PVF_REF | PVF_MOD))
1007 pg->mdpage.pvh_attrs |= set_mask & (PVF_REF | PVF_MOD);
1008
1009 oflags = npv->pv_flags;
1010 npv->pv_flags = flags = (oflags & ~clr_mask) | set_mask;
1011
1012 if ((flags ^ oflags) & PVF_WIRED) {
1013 if (flags & PVF_WIRED)
1014 ++pm->pm_stats.wired_count;
1015 else
1016 --pm->pm_stats.wired_count;
1017 }
1018
1019 if ((flags ^ oflags) & PVF_WRITE) {
1020 if (pm == pmap_kernel()) {
1021 if (flags & PVF_WRITE) {
1022 pg->mdpage.krw_mappings++;
1023 pg->mdpage.kro_mappings--;
1024 } else {
1025 pg->mdpage.kro_mappings++;
1026 pg->mdpage.krw_mappings--;
1027 }
1028 } else
1029 if (flags & PVF_WRITE) {
1030 pg->mdpage.urw_mappings++;
1031 pg->mdpage.uro_mappings--;
1032 } else {
1033 pg->mdpage.uro_mappings++;
1034 pg->mdpage.urw_mappings--;
1035 }
1036 }
1037 #ifdef PMAP_CACHE_VIPT
1038 /*
1039 * We have two cases here: the first is from enter_pv (new exec
1040 * page), the second is a combined pmap_remove_pv/pmap_enter_pv.
1041 * Since in latter, pmap_enter_pv won't do anything, we just have
1042 * to do what pmap_remove_pv would do.
1043 */
1044 if ((PV_IS_EXEC_P(flags) && !PV_IS_EXEC_P(pg->mdpage.pvh_attrs))
1045 || (PV_IS_EXEC_P(pg->mdpage.pvh_attrs)
1046 || (!(flags & PVF_WRITE) && (oflags & PVF_WRITE)))) {
1047 pmap_syncicache_page(pg);
1048 PMAPCOUNT(exec_synced_remap);
1049 }
1050 #endif
1051
1052 PMAPCOUNT(remappings);
1053
1054 return (oflags);
1055 }
1056
1057 /*
1058 * Allocate an L1 translation table for the specified pmap.
1059 * This is called at pmap creation time.
1060 */
1061 static void
1062 pmap_alloc_l1(pmap_t pm)
1063 {
1064 struct l1_ttable *l1;
1065 u_int8_t domain;
1066
1067 /*
1068 * Remove the L1 at the head of the LRU list
1069 */
1070 simple_lock(&l1_lru_lock);
1071 l1 = TAILQ_FIRST(&l1_lru_list);
1072 KDASSERT(l1 != NULL);
1073 TAILQ_REMOVE(&l1_lru_list, l1, l1_lru);
1074
1075 /*
1076 * Pick the first available domain number, and update
1077 * the link to the next number.
1078 */
1079 domain = l1->l1_domain_first;
1080 l1->l1_domain_first = l1->l1_domain_free[domain];
1081
1082 /*
1083 * If there are still free domain numbers in this L1,
1084 * put it back on the TAIL of the LRU list.
1085 */
1086 if (++l1->l1_domain_use_count < PMAP_DOMAINS)
1087 TAILQ_INSERT_TAIL(&l1_lru_list, l1, l1_lru);
1088
1089 simple_unlock(&l1_lru_lock);
1090
1091 /*
1092 * Fix up the relevant bits in the pmap structure
1093 */
1094 pm->pm_l1 = l1;
1095 pm->pm_domain = domain;
1096 }
1097
1098 /*
1099 * Free an L1 translation table.
1100 * This is called at pmap destruction time.
1101 */
1102 static void
1103 pmap_free_l1(pmap_t pm)
1104 {
1105 struct l1_ttable *l1 = pm->pm_l1;
1106
1107 simple_lock(&l1_lru_lock);
1108
1109 /*
1110 * If this L1 is currently on the LRU list, remove it.
1111 */
1112 if (l1->l1_domain_use_count < PMAP_DOMAINS)
1113 TAILQ_REMOVE(&l1_lru_list, l1, l1_lru);
1114
1115 /*
1116 * Free up the domain number which was allocated to the pmap
1117 */
1118 l1->l1_domain_free[pm->pm_domain] = l1->l1_domain_first;
1119 l1->l1_domain_first = pm->pm_domain;
1120 l1->l1_domain_use_count--;
1121
1122 /*
1123 * The L1 now must have at least 1 free domain, so add
1124 * it back to the LRU list. If the use count is zero,
1125 * put it at the head of the list, otherwise it goes
1126 * to the tail.
1127 */
1128 if (l1->l1_domain_use_count == 0)
1129 TAILQ_INSERT_HEAD(&l1_lru_list, l1, l1_lru);
1130 else
1131 TAILQ_INSERT_TAIL(&l1_lru_list, l1, l1_lru);
1132
1133 simple_unlock(&l1_lru_lock);
1134 }
1135
1136 static inline void
1137 pmap_use_l1(pmap_t pm)
1138 {
1139 struct l1_ttable *l1;
1140
1141 /*
1142 * Do nothing if we're in interrupt context.
1143 * Access to an L1 by the kernel pmap must not affect
1144 * the LRU list.
1145 */
1146 if (cpu_intr_p() || pm == pmap_kernel())
1147 return;
1148
1149 l1 = pm->pm_l1;
1150
1151 /*
1152 * If the L1 is not currently on the LRU list, just return
1153 */
1154 if (l1->l1_domain_use_count == PMAP_DOMAINS)
1155 return;
1156
1157 simple_lock(&l1_lru_lock);
1158
1159 /*
1160 * Check the use count again, now that we've acquired the lock
1161 */
1162 if (l1->l1_domain_use_count == PMAP_DOMAINS) {
1163 simple_unlock(&l1_lru_lock);
1164 return;
1165 }
1166
1167 /*
1168 * Move the L1 to the back of the LRU list
1169 */
1170 TAILQ_REMOVE(&l1_lru_list, l1, l1_lru);
1171 TAILQ_INSERT_TAIL(&l1_lru_list, l1, l1_lru);
1172
1173 simple_unlock(&l1_lru_lock);
1174 }
1175
1176 /*
1177 * void pmap_free_l2_ptp(pt_entry_t *, paddr_t *)
1178 *
1179 * Free an L2 descriptor table.
1180 */
1181 static inline void
1182 #ifndef PMAP_INCLUDE_PTE_SYNC
1183 pmap_free_l2_ptp(pt_entry_t *l2, paddr_t pa)
1184 #else
1185 pmap_free_l2_ptp(bool need_sync, pt_entry_t *l2, paddr_t pa)
1186 #endif
1187 {
1188 #ifdef PMAP_INCLUDE_PTE_SYNC
1189 #ifdef PMAP_CACHE_VIVT
1190 /*
1191 * Note: With a write-back cache, we may need to sync this
1192 * L2 table before re-using it.
1193 * This is because it may have belonged to a non-current
1194 * pmap, in which case the cache syncs would have been
1195 * skipped for the pages that were being unmapped. If the
1196 * L2 table were then to be immediately re-allocated to
1197 * the *current* pmap, it may well contain stale mappings
1198 * which have not yet been cleared by a cache write-back
1199 * and so would still be visible to the mmu.
1200 */
1201 if (need_sync)
1202 PTE_SYNC_RANGE(l2, L2_TABLE_SIZE_REAL / sizeof(pt_entry_t));
1203 #endif /* PMAP_CACHE_VIVT */
1204 #endif /* PMAP_INCLUDE_PTE_SYNC */
1205 pool_cache_put_paddr(&pmap_l2ptp_cache, (void *)l2, pa);
1206 }
1207
1208 /*
1209 * Returns a pointer to the L2 bucket associated with the specified pmap
1210 * and VA, or NULL if no L2 bucket exists for the address.
1211 */
1212 static inline struct l2_bucket *
1213 pmap_get_l2_bucket(pmap_t pm, vaddr_t va)
1214 {
1215 struct l2_dtable *l2;
1216 struct l2_bucket *l2b;
1217 u_short l1idx;
1218
1219 l1idx = L1_IDX(va);
1220
1221 if ((l2 = pm->pm_l2[L2_IDX(l1idx)]) == NULL ||
1222 (l2b = &l2->l2_bucket[L2_BUCKET(l1idx)])->l2b_kva == NULL)
1223 return (NULL);
1224
1225 return (l2b);
1226 }
1227
1228 /*
1229 * Returns a pointer to the L2 bucket associated with the specified pmap
1230 * and VA.
1231 *
1232 * If no L2 bucket exists, perform the necessary allocations to put an L2
1233 * bucket/page table in place.
1234 *
1235 * Note that if a new L2 bucket/page was allocated, the caller *must*
1236 * increment the bucket occupancy counter appropriately *before*
1237 * releasing the pmap's lock to ensure no other thread or cpu deallocates
1238 * the bucket/page in the meantime.
1239 */
1240 static struct l2_bucket *
1241 pmap_alloc_l2_bucket(pmap_t pm, vaddr_t va)
1242 {
1243 struct l2_dtable *l2;
1244 struct l2_bucket *l2b;
1245 u_short l1idx;
1246
1247 l1idx = L1_IDX(va);
1248
1249 if ((l2 = pm->pm_l2[L2_IDX(l1idx)]) == NULL) {
1250 /*
1251 * No mapping at this address, as there is
1252 * no entry in the L1 table.
1253 * Need to allocate a new l2_dtable.
1254 */
1255 if ((l2 = pmap_alloc_l2_dtable()) == NULL)
1256 return (NULL);
1257
1258 /*
1259 * Link it into the parent pmap
1260 */
1261 pm->pm_l2[L2_IDX(l1idx)] = l2;
1262 }
1263
1264 l2b = &l2->l2_bucket[L2_BUCKET(l1idx)];
1265
1266 /*
1267 * Fetch pointer to the L2 page table associated with the address.
1268 */
1269 if (l2b->l2b_kva == NULL) {
1270 pt_entry_t *ptep;
1271
1272 /*
1273 * No L2 page table has been allocated. Chances are, this
1274 * is because we just allocated the l2_dtable, above.
1275 */
1276 if ((ptep = pmap_alloc_l2_ptp(&l2b->l2b_phys)) == NULL) {
1277 /*
1278 * Oops, no more L2 page tables available at this
1279 * time. We may need to deallocate the l2_dtable
1280 * if we allocated a new one above.
1281 */
1282 if (l2->l2_occupancy == 0) {
1283 pm->pm_l2[L2_IDX(l1idx)] = NULL;
1284 pmap_free_l2_dtable(l2);
1285 }
1286 return (NULL);
1287 }
1288
1289 l2->l2_occupancy++;
1290 l2b->l2b_kva = ptep;
1291 l2b->l2b_l1idx = l1idx;
1292 }
1293
1294 return (l2b);
1295 }
1296
1297 /*
1298 * One or more mappings in the specified L2 descriptor table have just been
1299 * invalidated.
1300 *
1301 * Garbage collect the metadata and descriptor table itself if necessary.
1302 *
1303 * The pmap lock must be acquired when this is called (not necessary
1304 * for the kernel pmap).
1305 */
1306 static void
1307 pmap_free_l2_bucket(pmap_t pm, struct l2_bucket *l2b, u_int count)
1308 {
1309 struct l2_dtable *l2;
1310 pd_entry_t *pl1pd, l1pd;
1311 pt_entry_t *ptep;
1312 u_short l1idx;
1313
1314 KDASSERT(count <= l2b->l2b_occupancy);
1315
1316 /*
1317 * Update the bucket's reference count according to how many
1318 * PTEs the caller has just invalidated.
1319 */
1320 l2b->l2b_occupancy -= count;
1321
1322 /*
1323 * Note:
1324 *
1325 * Level 2 page tables allocated to the kernel pmap are never freed
1326 * as that would require checking all Level 1 page tables and
1327 * removing any references to the Level 2 page table. See also the
1328 * comment elsewhere about never freeing bootstrap L2 descriptors.
1329 *
1330 * We make do with just invalidating the mapping in the L2 table.
1331 *
1332 * This isn't really a big deal in practice and, in fact, leads
1333 * to a performance win over time as we don't need to continually
1334 * alloc/free.
1335 */
1336 if (l2b->l2b_occupancy > 0 || pm == pmap_kernel())
1337 return;
1338
1339 /*
1340 * There are no more valid mappings in this level 2 page table.
1341 * Go ahead and NULL-out the pointer in the bucket, then
1342 * free the page table.
1343 */
1344 l1idx = l2b->l2b_l1idx;
1345 ptep = l2b->l2b_kva;
1346 l2b->l2b_kva = NULL;
1347
1348 pl1pd = &pm->pm_l1->l1_kva[l1idx];
1349
1350 /*
1351 * If the L1 slot matches the pmap's domain
1352 * number, then invalidate it.
1353 */
1354 l1pd = *pl1pd & (L1_TYPE_MASK | L1_C_DOM_MASK);
1355 if (l1pd == (L1_C_DOM(pm->pm_domain) | L1_TYPE_C)) {
1356 *pl1pd = 0;
1357 PTE_SYNC(pl1pd);
1358 }
1359
1360 /*
1361 * Release the L2 descriptor table back to the pool cache.
1362 */
1363 #ifndef PMAP_INCLUDE_PTE_SYNC
1364 pmap_free_l2_ptp(ptep, l2b->l2b_phys);
1365 #else
1366 pmap_free_l2_ptp(!pmap_is_cached(pm), ptep, l2b->l2b_phys);
1367 #endif
1368
1369 /*
1370 * Update the reference count in the associated l2_dtable
1371 */
1372 l2 = pm->pm_l2[L2_IDX(l1idx)];
1373 if (--l2->l2_occupancy > 0)
1374 return;
1375
1376 /*
1377 * There are no more valid mappings in any of the Level 1
1378 * slots managed by this l2_dtable. Go ahead and NULL-out
1379 * the pointer in the parent pmap and free the l2_dtable.
1380 */
1381 pm->pm_l2[L2_IDX(l1idx)] = NULL;
1382 pmap_free_l2_dtable(l2);
1383 }
1384
1385 /*
1386 * Pool cache constructors for L2 descriptor tables, metadata and pmap
1387 * structures.
1388 */
1389 static int
1390 pmap_l2ptp_ctor(void *arg, void *v, int flags)
1391 {
1392 #ifndef PMAP_INCLUDE_PTE_SYNC
1393 struct l2_bucket *l2b;
1394 pt_entry_t *ptep, pte;
1395 vaddr_t va = (vaddr_t)v & ~PGOFSET;
1396
1397 /*
1398 * The mappings for these page tables were initially made using
1399 * pmap_kenter_pa() by the pool subsystem. Therefore, the cache-
1400 * mode will not be right for page table mappings. To avoid
1401 * polluting the pmap_kenter_pa() code with a special case for
1402 * page tables, we simply fix up the cache-mode here if it's not
1403 * correct.
1404 */
1405 l2b = pmap_get_l2_bucket(pmap_kernel(), va);
1406 KDASSERT(l2b != NULL);
1407 ptep = &l2b->l2b_kva[l2pte_index(va)];
1408 pte = *ptep;
1409
1410 if ((pte & L2_S_CACHE_MASK) != pte_l2_s_cache_mode_pt) {
1411 /*
1412 * Page tables must have the cache-mode set to Write-Thru.
1413 */
1414 *ptep = (pte & ~L2_S_CACHE_MASK) | pte_l2_s_cache_mode_pt;
1415 PTE_SYNC(ptep);
1416 cpu_tlb_flushD_SE(va);
1417 cpu_cpwait();
1418 }
1419 #endif
1420
1421 memset(v, 0, L2_TABLE_SIZE_REAL);
1422 PTE_SYNC_RANGE(v, L2_TABLE_SIZE_REAL / sizeof(pt_entry_t));
1423 return (0);
1424 }
1425
1426 static int
1427 pmap_l2dtable_ctor(void *arg, void *v, int flags)
1428 {
1429
1430 memset(v, 0, sizeof(struct l2_dtable));
1431 return (0);
1432 }
1433
1434 static int
1435 pmap_pmap_ctor(void *arg, void *v, int flags)
1436 {
1437
1438 memset(v, 0, sizeof(struct pmap));
1439 return (0);
1440 }
1441
1442 static void
1443 pmap_pinit(pmap_t pm)
1444 {
1445 struct l2_bucket *l2b;
1446
1447 if (vector_page < KERNEL_BASE) {
1448 /*
1449 * Map the vector page.
1450 */
1451 pmap_enter(pm, vector_page, systempage.pv_pa,
1452 VM_PROT_READ, VM_PROT_READ | PMAP_WIRED);
1453 pmap_update(pm);
1454
1455 pm->pm_pl1vec = &pm->pm_l1->l1_kva[L1_IDX(vector_page)];
1456 l2b = pmap_get_l2_bucket(pm, vector_page);
1457 pm->pm_l1vec = l2b->l2b_phys | L1_C_PROTO |
1458 L1_C_DOM(pm->pm_domain);
1459 } else
1460 pm->pm_pl1vec = NULL;
1461 }
1462
1463 #ifdef PMAP_CACHE_VIVT
1464 /*
1465 * Since we have a virtually indexed cache, we may need to inhibit caching if
1466 * there is more than one mapping and at least one of them is writable.
1467 * Since we purge the cache on every context switch, we only need to check for
1468 * other mappings within the same pmap, or kernel_pmap.
1469 * This function is also called when a page is unmapped, to possibly reenable
1470 * caching on any remaining mappings.
1471 *
1472 * The code implements the following logic, where:
1473 *
1474 * KW = # of kernel read/write pages
1475 * KR = # of kernel read only pages
1476 * UW = # of user read/write pages
1477 * UR = # of user read only pages
1478 *
1479 * KC = kernel mapping is cacheable
1480 * UC = user mapping is cacheable
1481 *
1482 * KW=0,KR=0 KW=0,KR>0 KW=1,KR=0 KW>1,KR>=0
1483 * +---------------------------------------------
1484 * UW=0,UR=0 | --- KC=1 KC=1 KC=0
1485 * UW=0,UR>0 | UC=1 KC=1,UC=1 KC=0,UC=0 KC=0,UC=0
1486 * UW=1,UR=0 | UC=1 KC=0,UC=0 KC=0,UC=0 KC=0,UC=0
1487 * UW>1,UR>=0 | UC=0 KC=0,UC=0 KC=0,UC=0 KC=0,UC=0
1488 */
1489
1490 static const int pmap_vac_flags[4][4] = {
1491 {-1, 0, 0, PVF_KNC},
1492 {0, 0, PVF_NC, PVF_NC},
1493 {0, PVF_NC, PVF_NC, PVF_NC},
1494 {PVF_UNC, PVF_NC, PVF_NC, PVF_NC}
1495 };
1496
1497 static inline int
1498 pmap_get_vac_flags(const struct vm_page *pg)
1499 {
1500 int kidx, uidx;
1501
1502 kidx = 0;
1503 if (pg->mdpage.kro_mappings || pg->mdpage.krw_mappings > 1)
1504 kidx |= 1;
1505 if (pg->mdpage.krw_mappings)
1506 kidx |= 2;
1507
1508 uidx = 0;
1509 if (pg->mdpage.uro_mappings || pg->mdpage.urw_mappings > 1)
1510 uidx |= 1;
1511 if (pg->mdpage.urw_mappings)
1512 uidx |= 2;
1513
1514 return (pmap_vac_flags[uidx][kidx]);
1515 }
1516
1517 static inline void
1518 pmap_vac_me_harder(struct vm_page *pg, pmap_t pm, vaddr_t va)
1519 {
1520 int nattr;
1521
1522 nattr = pmap_get_vac_flags(pg);
1523
1524 if (nattr < 0) {
1525 pg->mdpage.pvh_attrs &= ~PVF_NC;
1526 return;
1527 }
1528
1529 if (nattr == 0 && (pg->mdpage.pvh_attrs & PVF_NC) == 0)
1530 return;
1531
1532 if (pm == pmap_kernel())
1533 pmap_vac_me_kpmap(pg, pm, va);
1534 else
1535 pmap_vac_me_user(pg, pm, va);
1536
1537 pg->mdpage.pvh_attrs = (pg->mdpage.pvh_attrs & ~PVF_NC) | nattr;
1538 }
1539
1540 static void
1541 pmap_vac_me_kpmap(struct vm_page *pg, pmap_t pm, vaddr_t va)
1542 {
1543 u_int u_cacheable, u_entries;
1544 struct pv_entry *pv;
1545 pmap_t last_pmap = pm;
1546
1547 /*
1548 * Pass one, see if there are both kernel and user pmaps for
1549 * this page. Calculate whether there are user-writable or
1550 * kernel-writable pages.
1551 */
1552 u_cacheable = 0;
1553 for (pv = pg->mdpage.pvh_list; pv; pv = pv->pv_next) {
1554 if (pv->pv_pmap != pm && (pv->pv_flags & PVF_NC) == 0)
1555 u_cacheable++;
1556 }
1557
1558 u_entries = pg->mdpage.urw_mappings + pg->mdpage.uro_mappings;
1559
1560 /*
1561 * We know we have just been updating a kernel entry, so if
1562 * all user pages are already cacheable, then there is nothing
1563 * further to do.
1564 */
1565 if (pg->mdpage.k_mappings == 0 && u_cacheable == u_entries)
1566 return;
1567
1568 if (u_entries) {
1569 /*
1570 * Scan over the list again, for each entry, if it
1571 * might not be set correctly, call pmap_vac_me_user
1572 * to recalculate the settings.
1573 */
1574 for (pv = pg->mdpage.pvh_list; pv; pv = pv->pv_next) {
1575 /*
1576 * We know kernel mappings will get set
1577 * correctly in other calls. We also know
1578 * that if the pmap is the same as last_pmap
1579 * then we've just handled this entry.
1580 */
1581 if (pv->pv_pmap == pm || pv->pv_pmap == last_pmap)
1582 continue;
1583
1584 /*
1585 * If there are kernel entries and this page
1586 * is writable but non-cacheable, then we can
1587 * skip this entry also.
1588 */
1589 if (pg->mdpage.k_mappings &&
1590 (pv->pv_flags & (PVF_NC | PVF_WRITE)) ==
1591 (PVF_NC | PVF_WRITE))
1592 continue;
1593
1594 /*
1595 * Similarly if there are no kernel-writable
1596 * entries and the page is already
1597 * read-only/cacheable.
1598 */
1599 if (pg->mdpage.krw_mappings == 0 &&
1600 (pv->pv_flags & (PVF_NC | PVF_WRITE)) == 0)
1601 continue;
1602
1603 /*
1604 * For some of the remaining cases, we know
1605 * that we must recalculate, but for others we
1606 * can't tell if they are correct or not, so
1607 * we recalculate anyway.
1608 */
1609 pmap_vac_me_user(pg, (last_pmap = pv->pv_pmap), 0);
1610 }
1611
1612 if (pg->mdpage.k_mappings == 0)
1613 return;
1614 }
1615
1616 pmap_vac_me_user(pg, pm, va);
1617 }
1618
1619 static void
1620 pmap_vac_me_user(struct vm_page *pg, pmap_t pm, vaddr_t va)
1621 {
1622 pmap_t kpmap = pmap_kernel();
1623 struct pv_entry *pv, *npv;
1624 struct l2_bucket *l2b;
1625 pt_entry_t *ptep, pte;
1626 u_int entries = 0;
1627 u_int writable = 0;
1628 u_int cacheable_entries = 0;
1629 u_int kern_cacheable = 0;
1630 u_int other_writable = 0;
1631
1632 /*
1633 * Count mappings and writable mappings in this pmap.
1634 * Include kernel mappings as part of our own.
1635 * Keep a pointer to the first one.
1636 */
1637 for (pv = npv = pg->mdpage.pvh_list; pv; pv = pv->pv_next) {
1638 /* Count mappings in the same pmap */
1639 if (pm == pv->pv_pmap || kpmap == pv->pv_pmap) {
1640 if (entries++ == 0)
1641 npv = pv;
1642
1643 /* Cacheable mappings */
1644 if ((pv->pv_flags & PVF_NC) == 0) {
1645 cacheable_entries++;
1646 if (kpmap == pv->pv_pmap)
1647 kern_cacheable++;
1648 }
1649
1650 /* Writable mappings */
1651 if (pv->pv_flags & PVF_WRITE)
1652 ++writable;
1653 } else
1654 if (pv->pv_flags & PVF_WRITE)
1655 other_writable = 1;
1656 }
1657
1658 /*
1659 * Enable or disable caching as necessary.
1660 * Note: the first entry might be part of the kernel pmap,
1661 * so we can't assume this is indicative of the state of the
1662 * other (maybe non-kpmap) entries.
1663 */
1664 if ((entries > 1 && writable) ||
1665 (entries > 0 && pm == kpmap && other_writable)) {
1666 if (cacheable_entries == 0)
1667 return;
1668
1669 for (pv = npv; pv; pv = pv->pv_next) {
1670 if ((pm != pv->pv_pmap && kpmap != pv->pv_pmap) ||
1671 (pv->pv_flags & PVF_NC))
1672 continue;
1673
1674 pv->pv_flags |= PVF_NC;
1675
1676 l2b = pmap_get_l2_bucket(pv->pv_pmap, pv->pv_va);
1677 ptep = &l2b->l2b_kva[l2pte_index(pv->pv_va)];
1678 pte = *ptep & ~L2_S_CACHE_MASK;
1679
1680 if ((va != pv->pv_va || pm != pv->pv_pmap) &&
1681 l2pte_valid(pte)) {
1682 if (PV_BEEN_EXECD(pv->pv_flags)) {
1683 #ifdef PMAP_CACHE_VIVT
1684 pmap_idcache_wbinv_range(pv->pv_pmap,
1685 pv->pv_va, PAGE_SIZE);
1686 #endif
1687 pmap_tlb_flushID_SE(pv->pv_pmap,
1688 pv->pv_va);
1689 } else
1690 if (PV_BEEN_REFD(pv->pv_flags)) {
1691 #ifdef PMAP_CACHE_VIVT
1692 pmap_dcache_wb_range(pv->pv_pmap,
1693 pv->pv_va, PAGE_SIZE, true,
1694 (pv->pv_flags & PVF_WRITE) == 0);
1695 #endif
1696 pmap_tlb_flushD_SE(pv->pv_pmap,
1697 pv->pv_va);
1698 }
1699 }
1700
1701 *ptep = pte;
1702 PTE_SYNC_CURRENT(pv->pv_pmap, ptep);
1703 }
1704 cpu_cpwait();
1705 } else
1706 if (entries > cacheable_entries) {
1707 /*
1708 * Turn cacheing back on for some pages. If it is a kernel
1709 * page, only do so if there are no other writable pages.
1710 */
1711 for (pv = npv; pv; pv = pv->pv_next) {
1712 if (!(pv->pv_flags & PVF_NC) || (pm != pv->pv_pmap &&
1713 (kpmap != pv->pv_pmap || other_writable)))
1714 continue;
1715
1716 pv->pv_flags &= ~PVF_NC;
1717
1718 l2b = pmap_get_l2_bucket(pv->pv_pmap, pv->pv_va);
1719 ptep = &l2b->l2b_kva[l2pte_index(pv->pv_va)];
1720 pte = (*ptep & ~L2_S_CACHE_MASK) | pte_l2_s_cache_mode;
1721
1722 if (l2pte_valid(pte)) {
1723 if (PV_BEEN_EXECD(pv->pv_flags)) {
1724 pmap_tlb_flushID_SE(pv->pv_pmap,
1725 pv->pv_va);
1726 } else
1727 if (PV_BEEN_REFD(pv->pv_flags)) {
1728 pmap_tlb_flushD_SE(pv->pv_pmap,
1729 pv->pv_va);
1730 }
1731 }
1732
1733 *ptep = pte;
1734 PTE_SYNC_CURRENT(pv->pv_pmap, ptep);
1735 }
1736 }
1737 }
1738 #endif
1739
1740 #ifdef PMAP_CACHE_VIPT
1741 /*
1742 * For virtually indexed / physically tagged caches, what we have to worry
1743 * about is illegal cache aliases. To prevent this, we must ensure that
1744 * virtual addresses that map the physical page use the same bits for those
1745 * bits masked by "arm_cache_prefer_mask" (bits 12+). If there is a conflict,
1746 * all mappings of the page must be non-cached.
1747 */
1748 #if 0
1749 static inline vaddr_t
1750 pmap_check_sets(paddr_t pa)
1751 {
1752 extern int arm_dcache_l2_nsets;
1753 int set, way;
1754 vaddr_t mask = 0;
1755 int v;
1756 pa |= 1;
1757 for (set = 0; set < (1 << arm_dcache_l2_nsets); set++) {
1758 for (way = 0; way < 4; way++) {
1759 v = (way << 30) | (set << 5);
1760 asm("mcr p15, 3, %0, c15, c2, 0" :: "r"(v));
1761 asm("mrc p15, 3, %0, c15, c0, 0" : "=r"(v));
1762
1763 if ((v & (1 | ~(PAGE_SIZE-1))) == pa) {
1764 mask |= 1 << (set >> 7);
1765 }
1766 }
1767 }
1768 return mask;
1769 }
1770 #endif
1771 static void
1772 pmap_vac_me_harder(struct vm_page *pg, pmap_t pm, vaddr_t va)
1773 {
1774 struct pv_entry *pv, pv0;
1775 vaddr_t tst_mask;
1776 bool bad_alias;
1777 struct l2_bucket *l2b;
1778 pt_entry_t *ptep, pte, opte;
1779
1780 /* do we need to do anything? */
1781 if (arm_cache_prefer_mask == 0)
1782 return;
1783
1784 NPDEBUG(PDB_VAC, printf("pmap_vac_me_harder: pg=%p, pmap=%p va=%08lx\n",
1785 pg, pm, va));
1786
1787 #define popc4(x) \
1788 (((0x94 >> ((x & 3) << 1)) & 3) + ((0x94 >> ((x & 12) >> 1)) & 3))
1789 #if 0
1790 tst_mask = pmap_check_sets(pg->phys_addr);
1791 KASSERT(popc4(tst_mask) < 2);
1792 #endif
1793
1794 KASSERT(!va || pm || (pg->mdpage.pvh_attrs & PVF_KENTRY));
1795
1796 /* Already a conflict? */
1797 if (__predict_false(pg->mdpage.pvh_attrs & PVF_NC)) {
1798 /* just an add, things are already non-cached */
1799 bad_alias = false;
1800 if (va) {
1801 PMAPCOUNT(vac_color_none);
1802 bad_alias = true;
1803 goto fixup;
1804 }
1805 pv = pg->mdpage.pvh_list;
1806 /* the list can't be empty because it would be cachable */
1807 if (pg->mdpage.pvh_attrs & PVF_KENTRY) {
1808 tst_mask = pg->mdpage.pvh_attrs;
1809 } else {
1810 KASSERT(pv);
1811 tst_mask = pv->pv_va;
1812 pv = pv->pv_next;
1813 }
1814 tst_mask &= arm_cache_prefer_mask;
1815 for (; pv && !bad_alias; pv = pv->pv_next) {
1816 /* if there's a bad alias, stop checking. */
1817 if (tst_mask != (pv->pv_va & arm_cache_prefer_mask))
1818 bad_alias = true;
1819 }
1820 /* If no conflicting colors, set everything back to cached */
1821 if (!bad_alias) {
1822 PMAPCOUNT(vac_color_restore);
1823 pg->mdpage.pvh_attrs |= PVF_COLORED;
1824 if (!(pg->mdpage.pvh_attrs & PVF_KENTRY)) {
1825 pg->mdpage.pvh_attrs &= PAGE_SIZE - 1;
1826 pg->mdpage.pvh_attrs |= tst_mask;
1827 }
1828 pg->mdpage.pvh_attrs &= ~PVF_NC;
1829 } else {
1830 KASSERT(pg->mdpage.pvh_list != NULL);
1831 KASSERT((pg->mdpage.pvh_attrs & PVF_KENTRY)
1832 || pg->mdpage.pvh_list->pv_next != NULL);
1833 }
1834 } else if (!va) {
1835 KASSERT(pmap_is_page_colored_p(pg));
1836 if (pm == NULL)
1837 pg->mdpage.pvh_attrs &=
1838 (PAGE_SIZE - 1) | arm_cache_prefer_mask;
1839 return;
1840 } else if (!pmap_is_page_colored_p(pg)) {
1841 /* not colored so we just use its color */
1842 PMAPCOUNT(vac_color_new);
1843 pg->mdpage.pvh_attrs &= PAGE_SIZE - 1;
1844 if (pm == NULL)
1845 pg->mdpage.pvh_attrs |= PVF_COLORED | va;
1846 else
1847 pg->mdpage.pvh_attrs |= PVF_COLORED
1848 | (va & arm_cache_prefer_mask);
1849 return;
1850 } else if (!((pg->mdpage.pvh_attrs ^ va) & arm_cache_prefer_mask)) {
1851 if (pm == NULL) {
1852 pg->mdpage.pvh_attrs &= PAGE_SIZE - 1;
1853 pg->mdpage.pvh_attrs |= va;
1854 }
1855 if (pg->mdpage.pvh_list)
1856 PMAPCOUNT(vac_color_reuse);
1857 else
1858 PMAPCOUNT(vac_color_ok);
1859 /* matching color, just return */
1860 return;
1861 } else {
1862 /* color conflict. evict from cache. */
1863 pmap_flush_page(pg);
1864
1865 /* the list can't be empty because this was a enter/modify */
1866 pv = pg->mdpage.pvh_list;
1867 KASSERT((pg->mdpage.pvh_attrs & PVF_KENTRY) || pv);
1868
1869 /*
1870 * If there's only one mapped page, change color to the
1871 * page's new color and return.
1872 */
1873 if (((pg->mdpage.pvh_attrs & PVF_KENTRY)
1874 ? pv : pv->pv_next) == NULL) {
1875 PMAPCOUNT(vac_color_change);
1876 pg->mdpage.pvh_attrs &= PAGE_SIZE - 1;
1877 if (pm == NULL)
1878 pg->mdpage.pvh_attrs |= va;
1879 else
1880 pg->mdpage.pvh_attrs |=
1881 (va & arm_cache_prefer_mask);
1882 return;
1883 }
1884 bad_alias = true;
1885 pg->mdpage.pvh_attrs &= ~PVF_COLORED;
1886 pg->mdpage.pvh_attrs |= PVF_NC;
1887 PMAPCOUNT(vac_color_erase);
1888 }
1889
1890 fixup:
1891 /*
1892 * If the pmap is NULL, then we got called from pmap_kenter_pa
1893 * and we must save the kenter'ed va. And this changes the
1894 * color to match the kenter'ed page. if this is a remove clear
1895 * saved va bits which retaining the color bits.
1896 */
1897 if (pm == NULL) {
1898 if (va) {
1899 pg->mdpage.pvh_attrs &= (PAGE_SIZE - 1);
1900 pg->mdpage.pvh_attrs |= va;
1901 } else {
1902 pg->mdpage.pvh_attrs &=
1903 ((PAGE_SIZE - 1) | arm_cache_prefer_mask);
1904 }
1905 }
1906
1907 pv = pg->mdpage.pvh_list;
1908
1909 /*
1910 * If this page has an kenter'ed mapping, fake up a pv entry.
1911 */
1912 if (__predict_false(pg->mdpage.pvh_attrs & PVF_KENTRY)) {
1913 pv0.pv_pmap = pmap_kernel();
1914 pv0.pv_va = pg->mdpage.pvh_attrs & ~(PAGE_SIZE - 1);
1915 pv0.pv_next = pv;
1916 pv0.pv_flags = PVF_REF;
1917 pv = &pv0;
1918 }
1919
1920 /*
1921 * Turn cacheing on/off for all pages.
1922 */
1923 for (; pv; pv = pv->pv_next) {
1924 l2b = pmap_get_l2_bucket(pv->pv_pmap, pv->pv_va);
1925 ptep = &l2b->l2b_kva[l2pte_index(pv->pv_va)];
1926 opte = *ptep;
1927 pte = opte & ~L2_S_CACHE_MASK;
1928 if (bad_alias) {
1929 pv->pv_flags |= PVF_NC;
1930 } else {
1931 pv->pv_flags &= ~PVF_NC;
1932 pte |= pte_l2_s_cache_mode;
1933 }
1934 if (opte == pte) /* only update is there's a change */
1935 continue;
1936
1937 if (l2pte_valid(pte)) {
1938 if (PV_BEEN_EXECD(pv->pv_flags)) {
1939 pmap_tlb_flushID_SE(pv->pv_pmap, pv->pv_va);
1940 } else if (PV_BEEN_REFD(pv->pv_flags)) {
1941 pmap_tlb_flushD_SE(pv->pv_pmap, pv->pv_va);
1942 }
1943 }
1944
1945 *ptep = pte;
1946 PTE_SYNC_CURRENT(pv->pv_pmap, ptep);
1947 }
1948 }
1949 #endif /* PMAP_CACHE_VIPT */
1950
1951
1952 /*
1953 * Modify pte bits for all ptes corresponding to the given physical address.
1954 * We use `maskbits' rather than `clearbits' because we're always passing
1955 * constants and the latter would require an extra inversion at run-time.
1956 */
1957 static void
1958 pmap_clearbit(struct vm_page *pg, u_int maskbits)
1959 {
1960 struct l2_bucket *l2b;
1961 struct pv_entry *pv;
1962 pt_entry_t *ptep, npte, opte;
1963 pmap_t pm;
1964 vaddr_t va;
1965 u_int oflags;
1966 #ifdef PMAP_CACHE_VIPT
1967 const bool want_syncicache = PV_IS_EXEC_P(pg->mdpage.pvh_attrs);
1968 bool need_syncicache = false;
1969 bool did_syncicache = false;
1970 #endif
1971
1972 NPDEBUG(PDB_BITS,
1973 printf("pmap_clearbit: pg %p (0x%08lx) mask 0x%x\n",
1974 pg, VM_PAGE_TO_PHYS(pg), maskbits));
1975
1976 PMAP_HEAD_TO_MAP_LOCK();
1977 simple_lock(&pg->mdpage.pvh_slock);
1978
1979 #ifdef PMAP_CACHE_VIPT
1980 /*
1981 * If we might want to sync the I-cache and we've modified it,
1982 * then we know we definitely need to sync or discard it.
1983 */
1984 if (want_syncicache)
1985 need_syncicache = pg->mdpage.pvh_attrs & PVF_MOD;
1986 #endif
1987 /*
1988 * Clear saved attributes (modify, reference)
1989 */
1990 pg->mdpage.pvh_attrs &= ~(maskbits & (PVF_MOD | PVF_REF));
1991
1992 if (pg->mdpage.pvh_list == NULL) {
1993 #ifdef PMAP_CACHE_VIPT
1994 if (need_syncicache) {
1995 /*
1996 * No one has it mapped, so just discard it. The next
1997 * exec remapping will cause it to be synced.
1998 */
1999 pg->mdpage.pvh_attrs &= ~PVF_EXEC;
2000 PMAPCOUNT(exec_discarded_clearbit);
2001 }
2002 #endif
2003 simple_unlock(&pg->mdpage.pvh_slock);
2004 PMAP_HEAD_TO_MAP_UNLOCK();
2005 return;
2006 }
2007
2008 /*
2009 * Loop over all current mappings setting/clearing as appropos
2010 */
2011 for (pv = pg->mdpage.pvh_list; pv; pv = pv->pv_next) {
2012 va = pv->pv_va;
2013 pm = pv->pv_pmap;
2014 oflags = pv->pv_flags;
2015 pv->pv_flags &= ~maskbits;
2016
2017 pmap_acquire_pmap_lock(pm);
2018
2019 l2b = pmap_get_l2_bucket(pm, va);
2020 KDASSERT(l2b != NULL);
2021
2022 ptep = &l2b->l2b_kva[l2pte_index(va)];
2023 npte = opte = *ptep;
2024
2025 NPDEBUG(PDB_BITS,
2026 printf(
2027 "pmap_clearbit: pv %p, pm %p, va 0x%08lx, flag 0x%x\n",
2028 pv, pv->pv_pmap, pv->pv_va, oflags));
2029
2030 if (maskbits & (PVF_WRITE|PVF_MOD)) {
2031 #ifdef PMAP_CACHE_VIVT
2032 if ((pv->pv_flags & PVF_NC)) {
2033 /*
2034 * Entry is not cacheable:
2035 *
2036 * Don't turn caching on again if this is a
2037 * modified emulation. This would be
2038 * inconsitent with the settings created by
2039 * pmap_vac_me_harder(). Otherwise, it's safe
2040 * to re-enable cacheing.
2041 *
2042 * There's no need to call pmap_vac_me_harder()
2043 * here: all pages are losing their write
2044 * permission.
2045 */
2046 if (maskbits & PVF_WRITE) {
2047 npte |= pte_l2_s_cache_mode;
2048 pv->pv_flags &= ~PVF_NC;
2049 }
2050 } else
2051 if (opte & L2_S_PROT_W) {
2052 /*
2053 * Entry is writable/cacheable: check if pmap
2054 * is current if it is flush it, otherwise it
2055 * won't be in the cache
2056 */
2057 if (PV_BEEN_EXECD(oflags))
2058 pmap_idcache_wbinv_range(pm, pv->pv_va,
2059 PAGE_SIZE);
2060 else
2061 if (PV_BEEN_REFD(oflags))
2062 pmap_dcache_wb_range(pm, pv->pv_va,
2063 PAGE_SIZE,
2064 (maskbits & PVF_REF) != 0, false);
2065 }
2066 #endif
2067
2068 /* make the pte read only */
2069 npte &= ~L2_S_PROT_W;
2070
2071 if (maskbits & oflags & PVF_WRITE) {
2072 /*
2073 * Keep alias accounting up to date
2074 */
2075 if (pv->pv_pmap == pmap_kernel()) {
2076 pg->mdpage.krw_mappings--;
2077 pg->mdpage.kro_mappings++;
2078 } else {
2079 pg->mdpage.urw_mappings--;
2080 pg->mdpage.uro_mappings++;
2081 }
2082 #ifdef PMAP_CACHE_VIPT
2083 if (want_syncicache)
2084 need_syncicache = true;
2085 #endif
2086 }
2087 }
2088
2089 if (maskbits & PVF_REF) {
2090 #ifdef PMAP_CACHE_VIVT
2091 if ((pv->pv_flags & PVF_NC) == 0 &&
2092 (maskbits & (PVF_WRITE|PVF_MOD)) == 0 &&
2093 l2pte_valid(npte)) {
2094 /*
2095 * Check npte here; we may have already
2096 * done the wbinv above, and the validity
2097 * of the PTE is the same for opte and
2098 * npte.
2099 */
2100 /* XXXJRT need idcache_inv_range */
2101 if (PV_BEEN_EXECD(oflags))
2102 pmap_idcache_wbinv_range(pm,
2103 pv->pv_va, PAGE_SIZE);
2104 else
2105 if (PV_BEEN_REFD(oflags))
2106 pmap_dcache_wb_range(pm,
2107 pv->pv_va, PAGE_SIZE,
2108 true, true);
2109 }
2110 #endif
2111
2112 /*
2113 * Make the PTE invalid so that we will take a
2114 * page fault the next time the mapping is
2115 * referenced.
2116 */
2117 npte &= ~L2_TYPE_MASK;
2118 npte |= L2_TYPE_INV;
2119 }
2120
2121 if (npte != opte) {
2122 *ptep = npte;
2123 PTE_SYNC(ptep);
2124 /* Flush the TLB entry if a current pmap. */
2125 if (PV_BEEN_EXECD(oflags))
2126 pmap_tlb_flushID_SE(pm, pv->pv_va);
2127 else
2128 if (PV_BEEN_REFD(oflags))
2129 pmap_tlb_flushD_SE(pm, pv->pv_va);
2130 }
2131
2132 pmap_release_pmap_lock(pm);
2133
2134 NPDEBUG(PDB_BITS,
2135 printf("pmap_clearbit: pm %p va 0x%lx opte 0x%08x npte 0x%08x\n",
2136 pm, va, opte, npte));
2137 }
2138
2139 #ifdef PMAP_CACHE_VIPT
2140 /*
2141 * If we need to sync the I-cache and we haven't done it yet, do it.
2142 */
2143 if (need_syncicache && !did_syncicache) {
2144 pmap_syncicache_page(pg);
2145 PMAPCOUNT(exec_synced_clearbit);
2146 }
2147 #endif
2148
2149 simple_unlock(&pg->mdpage.pvh_slock);
2150 PMAP_HEAD_TO_MAP_UNLOCK();
2151 }
2152
2153 /*
2154 * pmap_clean_page()
2155 *
2156 * This is a local function used to work out the best strategy to clean
2157 * a single page referenced by its entry in the PV table. It's used by
2158 * pmap_copy_page, pmap_zero page and maybe some others later on.
2159 *
2160 * Its policy is effectively:
2161 * o If there are no mappings, we don't bother doing anything with the cache.
2162 * o If there is one mapping, we clean just that page.
2163 * o If there are multiple mappings, we clean the entire cache.
2164 *
2165 * So that some functions can be further optimised, it returns 0 if it didn't
2166 * clean the entire cache, or 1 if it did.
2167 *
2168 * XXX One bug in this routine is that if the pv_entry has a single page
2169 * mapped at 0x00000000 a whole cache clean will be performed rather than
2170 * just the 1 page. Since this should not occur in everyday use and if it does
2171 * it will just result in not the most efficient clean for the page.
2172 */
2173 #ifdef PMAP_CACHE_VIVT
2174 static int
2175 pmap_clean_page(struct pv_entry *pv, bool is_src)
2176 {
2177 pmap_t pm, pm_to_clean = NULL;
2178 struct pv_entry *npv;
2179 u_int cache_needs_cleaning = 0;
2180 u_int flags = 0;
2181 vaddr_t page_to_clean = 0;
2182
2183 if (pv == NULL) {
2184 /* nothing mapped in so nothing to flush */
2185 return (0);
2186 }
2187
2188 /*
2189 * Since we flush the cache each time we change to a different
2190 * user vmspace, we only need to flush the page if it is in the
2191 * current pmap.
2192 */
2193 if (curproc)
2194 pm = curproc->p_vmspace->vm_map.pmap;
2195 else
2196 pm = pmap_kernel();
2197
2198 for (npv = pv; npv; npv = npv->pv_next) {
2199 if (npv->pv_pmap == pmap_kernel() || npv->pv_pmap == pm) {
2200 flags |= npv->pv_flags;
2201 /*
2202 * The page is mapped non-cacheable in
2203 * this map. No need to flush the cache.
2204 */
2205 if (npv->pv_flags & PVF_NC) {
2206 #ifdef DIAGNOSTIC
2207 if (cache_needs_cleaning)
2208 panic("pmap_clean_page: "
2209 "cache inconsistency");
2210 #endif
2211 break;
2212 } else if (is_src && (npv->pv_flags & PVF_WRITE) == 0)
2213 continue;
2214 if (cache_needs_cleaning) {
2215 page_to_clean = 0;
2216 break;
2217 } else {
2218 page_to_clean = npv->pv_va;
2219 pm_to_clean = npv->pv_pmap;
2220 }
2221 cache_needs_cleaning = 1;
2222 }
2223 }
2224
2225 if (page_to_clean) {
2226 if (PV_BEEN_EXECD(flags))
2227 pmap_idcache_wbinv_range(pm_to_clean, page_to_clean,
2228 PAGE_SIZE);
2229 else
2230 pmap_dcache_wb_range(pm_to_clean, page_to_clean,
2231 PAGE_SIZE, !is_src, (flags & PVF_WRITE) == 0);
2232 } else if (cache_needs_cleaning) {
2233 if (PV_BEEN_EXECD(flags))
2234 pmap_idcache_wbinv_all(pm);
2235 else
2236 pmap_dcache_wbinv_all(pm);
2237 return (1);
2238 }
2239 return (0);
2240 }
2241 #endif
2242
2243 #ifdef PMAP_CACHE_VIPT
2244 /*
2245 * Sync a page with the I-cache. Since this is a VIPT, we must pick the
2246 * right cache alias to make sure we flush the right stuff.
2247 */
2248 void
2249 pmap_syncicache_page(struct vm_page *pg)
2250 {
2251 const vsize_t va_offset = pg->mdpage.pvh_attrs & arm_cache_prefer_mask;
2252 pt_entry_t * const ptep = &cdst_pte[va_offset >> PGSHIFT];
2253
2254 NPDEBUG(PDB_EXEC, printf("pmap_syncicache_page: pg=%p (attrs=%#x)\n",
2255 pg, pg->mdpage.pvh_attrs));
2256 /*
2257 * No need to clean the page if it's non-cached.
2258 */
2259 if (pg->mdpage.pvh_attrs & PVF_NC)
2260 return;
2261 KASSERT(pg->mdpage.pvh_attrs & PVF_COLORED);
2262
2263 pmap_tlb_flushID_SE(pmap_kernel(), cdstp + va_offset);
2264 /*
2265 * Set up a PTE with the right coloring to flush existing cache lines.
2266 */
2267 *ptep = L2_S_PROTO |
2268 VM_PAGE_TO_PHYS(pg)
2269 | L2_S_PROT(PTE_KERNEL, VM_PROT_READ|VM_PROT_WRITE)
2270 | pte_l2_s_cache_mode;
2271 PTE_SYNC(ptep);
2272
2273 /*
2274 * Flush it.
2275 */
2276 cpu_icache_sync_range(cdstp + va_offset, PAGE_SIZE);
2277 /*
2278 * Unmap the page.
2279 */
2280 *ptep = 0;
2281 PTE_SYNC(ptep);
2282 pmap_tlb_flushID_SE(pmap_kernel(), cdstp + va_offset);
2283
2284 pg->mdpage.pvh_attrs |= PVF_EXEC;
2285 PMAPCOUNT(exec_synced);
2286 }
2287
2288 void
2289 pmap_flush_page(struct vm_page *pg)
2290 {
2291 const vsize_t va_offset = pg->mdpage.pvh_attrs & arm_cache_prefer_mask;
2292 const size_t pte_offset = va_offset >> PGSHIFT;
2293 pt_entry_t * const ptep = &cdst_pte[pte_offset];
2294 const pt_entry_t oldpte = *ptep;
2295 #if 0
2296 vaddr_t mask;
2297 #endif
2298
2299 KASSERT(!(pg->mdpage.pvh_attrs & PVF_NC));
2300 #if 0
2301 mask = pmap_check_sets(pg->phys_addr);
2302 KASSERT(popc4(mask) < 2);
2303 #endif
2304
2305 NPDEBUG(PDB_VAC, printf("pmap_flush_page: pg=%p (attrs=%#x)\n",
2306 pg, pg->mdpage.pvh_attrs));
2307 pmap_tlb_flushID_SE(pmap_kernel(), cdstp + va_offset);
2308 /*
2309 * Set up a PTE with the right coloring to flush existing cache entries.
2310 */
2311 *ptep = L2_S_PROTO
2312 | VM_PAGE_TO_PHYS(pg)
2313 | L2_S_PROT(PTE_KERNEL, VM_PROT_READ|VM_PROT_WRITE)
2314 | pte_l2_s_cache_mode;
2315 PTE_SYNC(ptep);
2316
2317 /*
2318 * Flush it.
2319 */
2320 cpu_idcache_wbinv_range(cdstp + va_offset, PAGE_SIZE);
2321
2322 /*
2323 * Restore the page table entry since we might have interrupted
2324 * pmap_zero_page or pmap_copy_page which was already using this pte.
2325 */
2326 *ptep = oldpte;
2327 PTE_SYNC(ptep);
2328 pmap_tlb_flushID_SE(pmap_kernel(), cdstp + va_offset);
2329 #if 0
2330 mask = pmap_check_sets(pg->phys_addr);
2331 KASSERT(mask == 0);
2332 #endif
2333 }
2334 #endif /* PMAP_CACHE_VIPT */
2335
2336 /*
2337 * Routine: pmap_page_remove
2338 * Function:
2339 * Removes this physical page from
2340 * all physical maps in which it resides.
2341 * Reflects back modify bits to the pager.
2342 */
2343 static void
2344 pmap_page_remove(struct vm_page *pg)
2345 {
2346 struct l2_bucket *l2b;
2347 struct pv_entry *pv, *npv;
2348 pmap_t pm, curpm;
2349 pt_entry_t *ptep, pte;
2350 bool flush;
2351 u_int flags;
2352
2353 NPDEBUG(PDB_FOLLOW,
2354 printf("pmap_page_remove: pg %p (0x%08lx)\n", pg,
2355 VM_PAGE_TO_PHYS(pg)));
2356
2357 PMAP_HEAD_TO_MAP_LOCK();
2358 simple_lock(&pg->mdpage.pvh_slock);
2359
2360 pv = pg->mdpage.pvh_list;
2361 if (pv == NULL) {
2362 #ifdef PMAP_CACHE_VIPT
2363 /*
2364 * We *know* the page contents are about to be replaced.
2365 * Discard the exec contents
2366 */
2367 if (PV_IS_EXEC_P(pg->mdpage.pvh_attrs))
2368 PMAPCOUNT(exec_discarded_page_protect);
2369 pg->mdpage.pvh_attrs &= ~PVF_EXEC;
2370 #endif
2371 simple_unlock(&pg->mdpage.pvh_slock);
2372 PMAP_HEAD_TO_MAP_UNLOCK();
2373 return;
2374 }
2375 #ifdef PMAP_CACHE_VIPT
2376 KASSERT(pmap_is_page_colored_p(pg));
2377 #endif
2378
2379 /*
2380 * Clear alias counts
2381 */
2382 pg->mdpage.k_mappings = 0;
2383 pg->mdpage.urw_mappings = pg->mdpage.uro_mappings = 0;
2384
2385 flush = false;
2386 flags = 0;
2387 if (curproc)
2388 curpm = curproc->p_vmspace->vm_map.pmap;
2389 else
2390 curpm = pmap_kernel();
2391
2392 #ifdef PMAP_CACHE_VIVT
2393 pmap_clean_page(pv, false);
2394 #endif
2395
2396 while (pv) {
2397 pm = pv->pv_pmap;
2398 if (flush == false && (pm == curpm || pm == pmap_kernel()))
2399 flush = true;
2400
2401 if (pm == pmap_kernel())
2402 PMAPCOUNT(kernel_unmappings);
2403 PMAPCOUNT(unmappings);
2404
2405 pmap_acquire_pmap_lock(pm);
2406
2407 l2b = pmap_get_l2_bucket(pm, pv->pv_va);
2408 KDASSERT(l2b != NULL);
2409
2410 ptep = &l2b->l2b_kva[l2pte_index(pv->pv_va)];
2411 pte = *ptep;
2412
2413 /*
2414 * Update statistics
2415 */
2416 --pm->pm_stats.resident_count;
2417
2418 /* Wired bit */
2419 if (pv->pv_flags & PVF_WIRED)
2420 --pm->pm_stats.wired_count;
2421
2422 flags |= pv->pv_flags;
2423
2424 /*
2425 * Invalidate the PTEs.
2426 */
2427 *ptep = 0;
2428 PTE_SYNC_CURRENT(pm, ptep);
2429 pmap_free_l2_bucket(pm, l2b, 1);
2430
2431 npv = pv->pv_next;
2432 pool_put(&pmap_pv_pool, pv);
2433 pv = npv;
2434 if (pv == NULL) {
2435 pg->mdpage.pvh_list = NULL;
2436 if (pg->mdpage.pvh_attrs & PVF_KENTRY)
2437 pmap_vac_me_harder(pg, pm, 0);
2438 }
2439 pmap_release_pmap_lock(pm);
2440 }
2441 #ifdef PMAP_CACHE_VIPT
2442 /*
2443 * Since there are now no mappings, there isn't reason to mark it
2444 * as uncached. Its EXEC cache is also gone.
2445 */
2446 if (PV_IS_EXEC_P(pg->mdpage.pvh_attrs))
2447 PMAPCOUNT(exec_discarded_page_protect);
2448 pg->mdpage.pvh_attrs &= ~(PVF_NC|PVF_EXEC);
2449 #endif
2450 #ifdef PMAP_CACHE_VIVT
2451 pg->mdpage.pvh_list = NULL;
2452 #endif
2453 simple_unlock(&pg->mdpage.pvh_slock);
2454 PMAP_HEAD_TO_MAP_UNLOCK();
2455
2456 if (flush) {
2457 /*
2458 * Note: We can't use pmap_tlb_flush{I,}D() here since that
2459 * would need a subsequent call to pmap_update() to ensure
2460 * curpm->pm_cstate.cs_all is reset. Our callers are not
2461 * required to do that (see pmap(9)), so we can't modify
2462 * the current pmap's state.
2463 */
2464 if (PV_BEEN_EXECD(flags))
2465 cpu_tlb_flushID();
2466 else
2467 cpu_tlb_flushD();
2468 }
2469 cpu_cpwait();
2470 }
2471
2472 /*
2473 * pmap_t pmap_create(void)
2474 *
2475 * Create a new pmap structure from scratch.
2476 */
2477 pmap_t
2478 pmap_create(void)
2479 {
2480 pmap_t pm;
2481
2482 pm = pool_cache_get(&pmap_cache, PR_WAITOK);
2483
2484 UVM_OBJ_INIT(&pm->pm_obj, NULL, 1);
2485 pm->pm_stats.wired_count = 0;
2486 pm->pm_stats.resident_count = 1;
2487 pm->pm_cstate.cs_all = 0;
2488 pmap_alloc_l1(pm);
2489
2490 /*
2491 * Note: The pool cache ensures that the pm_l2[] array is already
2492 * initialised to zero.
2493 */
2494
2495 pmap_pinit(pm);
2496
2497 LIST_INSERT_HEAD(&pmap_pmaps, pm, pm_list);
2498
2499 return (pm);
2500 }
2501
2502 /*
2503 * void pmap_enter(pmap_t pm, vaddr_t va, paddr_t pa, vm_prot_t prot,
2504 * int flags)
2505 *
2506 * Insert the given physical page (p) at
2507 * the specified virtual address (v) in the
2508 * target physical map with the protection requested.
2509 *
2510 * NB: This is the only routine which MAY NOT lazy-evaluate
2511 * or lose information. That is, this routine must actually
2512 * insert this page into the given map NOW.
2513 */
2514 int
2515 pmap_enter(pmap_t pm, vaddr_t va, paddr_t pa, vm_prot_t prot, int flags)
2516 {
2517 struct l2_bucket *l2b;
2518 struct vm_page *pg, *opg;
2519 struct pv_entry *pve;
2520 pt_entry_t *ptep, npte, opte;
2521 u_int nflags;
2522 u_int oflags;
2523
2524 NPDEBUG(PDB_ENTER, printf("pmap_enter: pm %p va 0x%lx pa 0x%lx prot %x flag %x\n", pm, va, pa, prot, flags));
2525
2526 KDASSERT((flags & PMAP_WIRED) == 0 || (flags & VM_PROT_ALL) != 0);
2527 KDASSERT(((va | pa) & PGOFSET) == 0);
2528
2529 /*
2530 * Get a pointer to the page. Later on in this function, we
2531 * test for a managed page by checking pg != NULL.
2532 */
2533 pg = pmap_initialized ? PHYS_TO_VM_PAGE(pa) : NULL;
2534
2535 nflags = 0;
2536 if (prot & VM_PROT_WRITE)
2537 nflags |= PVF_WRITE;
2538 if (prot & VM_PROT_EXECUTE)
2539 nflags |= PVF_EXEC;
2540 if (flags & PMAP_WIRED)
2541 nflags |= PVF_WIRED;
2542
2543 PMAP_MAP_TO_HEAD_LOCK();
2544 pmap_acquire_pmap_lock(pm);
2545
2546 /*
2547 * Fetch the L2 bucket which maps this page, allocating one if
2548 * necessary for user pmaps.
2549 */
2550 if (pm == pmap_kernel())
2551 l2b = pmap_get_l2_bucket(pm, va);
2552 else
2553 l2b = pmap_alloc_l2_bucket(pm, va);
2554 if (l2b == NULL) {
2555 if (flags & PMAP_CANFAIL) {
2556 pmap_release_pmap_lock(pm);
2557 PMAP_MAP_TO_HEAD_UNLOCK();
2558 return (ENOMEM);
2559 }
2560 panic("pmap_enter: failed to allocate L2 bucket");
2561 }
2562 ptep = &l2b->l2b_kva[l2pte_index(va)];
2563 opte = *ptep;
2564 npte = pa;
2565 oflags = 0;
2566
2567 if (opte) {
2568 /*
2569 * There is already a mapping at this address.
2570 * If the physical address is different, lookup the
2571 * vm_page.
2572 */
2573 if (l2pte_pa(opte) != pa)
2574 opg = PHYS_TO_VM_PAGE(l2pte_pa(opte));
2575 else
2576 opg = pg;
2577 } else
2578 opg = NULL;
2579
2580 if (pg) {
2581 /*
2582 * This is to be a managed mapping.
2583 */
2584 if ((flags & VM_PROT_ALL) ||
2585 (pg->mdpage.pvh_attrs & PVF_REF)) {
2586 /*
2587 * - The access type indicates that we don't need
2588 * to do referenced emulation.
2589 * OR
2590 * - The physical page has already been referenced
2591 * so no need to re-do referenced emulation here.
2592 */
2593 npte |= L2_S_PROTO;
2594
2595 nflags |= PVF_REF;
2596
2597 if ((prot & VM_PROT_WRITE) != 0 &&
2598 ((flags & VM_PROT_WRITE) != 0 ||
2599 (pg->mdpage.pvh_attrs & PVF_MOD) != 0)) {
2600 /*
2601 * This is a writable mapping, and the
2602 * page's mod state indicates it has
2603 * already been modified. Make it
2604 * writable from the outset.
2605 */
2606 npte |= L2_S_PROT_W;
2607 nflags |= PVF_MOD;
2608 }
2609 } else {
2610 /*
2611 * Need to do page referenced emulation.
2612 */
2613 npte |= L2_TYPE_INV;
2614 }
2615
2616 npte |= pte_l2_s_cache_mode;
2617
2618 if (pg == opg) {
2619 /*
2620 * We're changing the attrs of an existing mapping.
2621 */
2622 simple_lock(&pg->mdpage.pvh_slock);
2623 oflags = pmap_modify_pv(pg, pm, va,
2624 PVF_WRITE | PVF_EXEC | PVF_WIRED |
2625 PVF_MOD | PVF_REF, nflags);
2626 simple_unlock(&pg->mdpage.pvh_slock);
2627
2628 #ifdef PMAP_CACHE_VIVT
2629 /*
2630 * We may need to flush the cache if we're
2631 * doing rw-ro...
2632 */
2633 if (pm->pm_cstate.cs_cache_d &&
2634 (oflags & PVF_NC) == 0 &&
2635 (opte & L2_S_PROT_W) != 0 &&
2636 (prot & VM_PROT_WRITE) == 0)
2637 cpu_dcache_wb_range(va, PAGE_SIZE);
2638 #endif
2639 } else {
2640 /*
2641 * New mapping, or changing the backing page
2642 * of an existing mapping.
2643 */
2644 if (opg) {
2645 /*
2646 * Replacing an existing mapping with a new one.
2647 * It is part of our managed memory so we
2648 * must remove it from the PV list
2649 */
2650 simple_lock(&opg->mdpage.pvh_slock);
2651 pve = pmap_remove_pv(opg, pm, va, 0);
2652 pmap_vac_me_harder(opg, pm, 0);
2653 simple_unlock(&opg->mdpage.pvh_slock);
2654 oflags = pve->pv_flags;
2655
2656 #ifdef PMAP_CACHE_VIVT
2657 /*
2658 * If the old mapping was valid (ref/mod
2659 * emulation creates 'invalid' mappings
2660 * initially) then make sure to frob
2661 * the cache.
2662 */
2663 if ((oflags & PVF_NC) == 0 &&
2664 l2pte_valid(opte)) {
2665 if (PV_BEEN_EXECD(oflags)) {
2666 pmap_idcache_wbinv_range(pm, va,
2667 PAGE_SIZE);
2668 } else
2669 if (PV_BEEN_REFD(oflags)) {
2670 pmap_dcache_wb_range(pm, va,
2671 PAGE_SIZE, true,
2672 (oflags & PVF_WRITE) == 0);
2673 }
2674 }
2675 #endif
2676 } else
2677 if ((pve = pool_get(&pmap_pv_pool, PR_NOWAIT)) == NULL){
2678 if ((flags & PMAP_CANFAIL) == 0)
2679 panic("pmap_enter: no pv entries");
2680
2681 if (pm != pmap_kernel())
2682 pmap_free_l2_bucket(pm, l2b, 0);
2683 pmap_release_pmap_lock(pm);
2684 PMAP_MAP_TO_HEAD_UNLOCK();
2685 NPDEBUG(PDB_ENTER,
2686 printf("pmap_enter: ENOMEM\n"));
2687 return (ENOMEM);
2688 }
2689
2690 pmap_enter_pv(pg, pve, pm, va, nflags);
2691 }
2692 } else {
2693 /*
2694 * We're mapping an unmanaged page.
2695 * These are always readable, and possibly writable, from
2696 * the get go as we don't need to track ref/mod status.
2697 */
2698 npte |= L2_S_PROTO;
2699 if (prot & VM_PROT_WRITE)
2700 npte |= L2_S_PROT_W;
2701
2702 /*
2703 * Make sure the vector table is mapped cacheable
2704 */
2705 if (pm != pmap_kernel() && va == vector_page)
2706 npte |= pte_l2_s_cache_mode;
2707
2708 if (opg) {
2709 /*
2710 * Looks like there's an existing 'managed' mapping
2711 * at this address.
2712 */
2713 simple_lock(&opg->mdpage.pvh_slock);
2714 pve = pmap_remove_pv(opg, pm, va, 0);
2715 pmap_vac_me_harder(opg, pm, 0);
2716 simple_unlock(&opg->mdpage.pvh_slock);
2717 oflags = pve->pv_flags;
2718
2719 #ifdef PMAP_CACHE_VIVT
2720 if ((oflags & PVF_NC) == 0 && l2pte_valid(opte)) {
2721 if (PV_BEEN_EXECD(oflags))
2722 pmap_idcache_wbinv_range(pm, va,
2723 PAGE_SIZE);
2724 else
2725 if (PV_BEEN_REFD(oflags))
2726 pmap_dcache_wb_range(pm, va, PAGE_SIZE,
2727 true, (oflags & PVF_WRITE) == 0);
2728 }
2729 #endif
2730 pool_put(&pmap_pv_pool, pve);
2731 }
2732 }
2733
2734 /*
2735 * Make sure userland mappings get the right permissions
2736 */
2737 if (pm != pmap_kernel() && va != vector_page)
2738 npte |= L2_S_PROT_U;
2739
2740 /*
2741 * Keep the stats up to date
2742 */
2743 if (opte == 0) {
2744 l2b->l2b_occupancy++;
2745 pm->pm_stats.resident_count++;
2746 }
2747
2748 NPDEBUG(PDB_ENTER,
2749 printf("pmap_enter: opte 0x%08x npte 0x%08x\n", opte, npte));
2750
2751 /*
2752 * If this is just a wiring change, the two PTEs will be
2753 * identical, so there's no need to update the page table.
2754 */
2755 if (npte != opte) {
2756 bool is_cached = pmap_is_cached(pm);
2757
2758 *ptep = npte;
2759 if (is_cached) {
2760 /*
2761 * We only need to frob the cache/tlb if this pmap
2762 * is current
2763 */
2764 PTE_SYNC(ptep);
2765 if (va != vector_page && l2pte_valid(npte)) {
2766 /*
2767 * This mapping is likely to be accessed as
2768 * soon as we return to userland. Fix up the
2769 * L1 entry to avoid taking another
2770 * page/domain fault.
2771 */
2772 pd_entry_t *pl1pd, l1pd;
2773
2774 pl1pd = &pm->pm_l1->l1_kva[L1_IDX(va)];
2775 l1pd = l2b->l2b_phys | L1_C_DOM(pm->pm_domain) |
2776 L1_C_PROTO;
2777 if (*pl1pd != l1pd) {
2778 *pl1pd = l1pd;
2779 PTE_SYNC(pl1pd);
2780 }
2781 }
2782 }
2783
2784 if (PV_BEEN_EXECD(oflags))
2785 pmap_tlb_flushID_SE(pm, va);
2786 else
2787 if (PV_BEEN_REFD(oflags))
2788 pmap_tlb_flushD_SE(pm, va);
2789
2790 NPDEBUG(PDB_ENTER,
2791 printf("pmap_enter: is_cached %d cs 0x%08x\n",
2792 is_cached, pm->pm_cstate.cs_all));
2793
2794 if (pg != NULL) {
2795 simple_lock(&pg->mdpage.pvh_slock);
2796 pmap_vac_me_harder(pg, pm, va);
2797 simple_unlock(&pg->mdpage.pvh_slock);
2798 }
2799 }
2800
2801 pmap_release_pmap_lock(pm);
2802 PMAP_MAP_TO_HEAD_UNLOCK();
2803
2804 return (0);
2805 }
2806
2807 /*
2808 * pmap_remove()
2809 *
2810 * pmap_remove is responsible for nuking a number of mappings for a range
2811 * of virtual address space in the current pmap. To do this efficiently
2812 * is interesting, because in a number of cases a wide virtual address
2813 * range may be supplied that contains few actual mappings. So, the
2814 * optimisations are:
2815 * 1. Skip over hunks of address space for which no L1 or L2 entry exists.
2816 * 2. Build up a list of pages we've hit, up to a maximum, so we can
2817 * maybe do just a partial cache clean. This path of execution is
2818 * complicated by the fact that the cache must be flushed _before_
2819 * the PTE is nuked, being a VAC :-)
2820 * 3. If we're called after UVM calls pmap_remove_all(), we can defer
2821 * all invalidations until pmap_update(), since pmap_remove_all() has
2822 * already flushed the cache.
2823 * 4. Maybe later fast-case a single page, but I don't think this is
2824 * going to make _that_ much difference overall.
2825 */
2826
2827 #define PMAP_REMOVE_CLEAN_LIST_SIZE 3
2828
2829 void
2830 pmap_do_remove(pmap_t pm, vaddr_t sva, vaddr_t eva, int skip_wired)
2831 {
2832 struct l2_bucket *l2b;
2833 vaddr_t next_bucket;
2834 pt_entry_t *ptep;
2835 u_int cleanlist_idx, total, cnt;
2836 struct {
2837 vaddr_t va;
2838 pt_entry_t *ptep;
2839 } cleanlist[PMAP_REMOVE_CLEAN_LIST_SIZE];
2840 u_int mappings, is_exec, is_refd;
2841
2842 NPDEBUG(PDB_REMOVE, printf("pmap_do_remove: pmap=%p sva=%08lx "
2843 "eva=%08lx\n", pm, sva, eva));
2844
2845 /*
2846 * we lock in the pmap => pv_head direction
2847 */
2848 PMAP_MAP_TO_HEAD_LOCK();
2849 pmap_acquire_pmap_lock(pm);
2850
2851 if (pm->pm_remove_all || !pmap_is_cached(pm)) {
2852 cleanlist_idx = PMAP_REMOVE_CLEAN_LIST_SIZE + 1;
2853 if (pm->pm_cstate.cs_tlb == 0)
2854 pm->pm_remove_all = true;
2855 } else
2856 cleanlist_idx = 0;
2857
2858 total = 0;
2859
2860 while (sva < eva) {
2861 /*
2862 * Do one L2 bucket's worth at a time.
2863 */
2864 next_bucket = L2_NEXT_BUCKET(sva);
2865 if (next_bucket > eva)
2866 next_bucket = eva;
2867
2868 l2b = pmap_get_l2_bucket(pm, sva);
2869 if (l2b == NULL) {
2870 sva = next_bucket;
2871 continue;
2872 }
2873
2874 ptep = &l2b->l2b_kva[l2pte_index(sva)];
2875
2876 for (mappings = 0; sva < next_bucket; sva += PAGE_SIZE, ptep++){
2877 struct vm_page *pg;
2878 pt_entry_t pte;
2879 paddr_t pa;
2880
2881 pte = *ptep;
2882
2883 if (pte == 0) {
2884 /* Nothing here, move along */
2885 continue;
2886 }
2887
2888 pa = l2pte_pa(pte);
2889 is_exec = 0;
2890 is_refd = 1;
2891
2892 /*
2893 * Update flags. In a number of circumstances,
2894 * we could cluster a lot of these and do a
2895 * number of sequential pages in one go.
2896 */
2897 if ((pg = PHYS_TO_VM_PAGE(pa)) != NULL) {
2898 struct pv_entry *pve;
2899 simple_lock(&pg->mdpage.pvh_slock);
2900 pve = pmap_remove_pv(pg, pm, sva, skip_wired);
2901 pmap_vac_me_harder(pg, pm, 0);
2902 simple_unlock(&pg->mdpage.pvh_slock);
2903 if (pve != NULL) {
2904 if (pm->pm_remove_all == false) {
2905 is_exec =
2906 PV_BEEN_EXECD(pve->pv_flags);
2907 is_refd =
2908 PV_BEEN_REFD(pve->pv_flags);
2909 }
2910 pool_put(&pmap_pv_pool, pve);
2911 } else
2912 if (skip_wired) {
2913 /* The mapping is wired. Skip it */
2914 continue;
2915 }
2916 } else
2917 if (skip_wired) {
2918 /* Unmanaged pages are always wired. */
2919 continue;
2920 }
2921
2922 mappings++;
2923
2924 if (!l2pte_valid(pte)) {
2925 /*
2926 * Ref/Mod emulation is still active for this
2927 * mapping, therefore it is has not yet been
2928 * accessed. No need to frob the cache/tlb.
2929 */
2930 *ptep = 0;
2931 PTE_SYNC_CURRENT(pm, ptep);
2932 continue;
2933 }
2934
2935 if (cleanlist_idx < PMAP_REMOVE_CLEAN_LIST_SIZE) {
2936 /* Add to the clean list. */
2937 cleanlist[cleanlist_idx].ptep = ptep;
2938 cleanlist[cleanlist_idx].va =
2939 sva | (is_exec & 1);
2940 cleanlist_idx++;
2941 } else
2942 if (cleanlist_idx == PMAP_REMOVE_CLEAN_LIST_SIZE) {
2943 /* Nuke everything if needed. */
2944 #ifdef PMAP_CACHE_VIVT
2945 pmap_idcache_wbinv_all(pm);
2946 #endif
2947 pmap_tlb_flushID(pm);
2948
2949 /*
2950 * Roll back the previous PTE list,
2951 * and zero out the current PTE.
2952 */
2953 for (cnt = 0;
2954 cnt < PMAP_REMOVE_CLEAN_LIST_SIZE; cnt++) {
2955 *cleanlist[cnt].ptep = 0;
2956 }
2957 *ptep = 0;
2958 PTE_SYNC(ptep);
2959 cleanlist_idx++;
2960 pm->pm_remove_all = true;
2961 } else {
2962 *ptep = 0;
2963 PTE_SYNC(ptep);
2964 if (pm->pm_remove_all == false) {
2965 if (is_exec)
2966 pmap_tlb_flushID_SE(pm, sva);
2967 else
2968 if (is_refd)
2969 pmap_tlb_flushD_SE(pm, sva);
2970 }
2971 }
2972 }
2973
2974 /*
2975 * Deal with any left overs
2976 */
2977 if (cleanlist_idx <= PMAP_REMOVE_CLEAN_LIST_SIZE) {
2978 total += cleanlist_idx;
2979 for (cnt = 0; cnt < cleanlist_idx; cnt++) {
2980 if (pm->pm_cstate.cs_all != 0) {
2981 vaddr_t clva = cleanlist[cnt].va & ~1;
2982 if (cleanlist[cnt].va & 1) {
2983 #ifdef PMAP_CACHE_VIVT
2984 pmap_idcache_wbinv_range(pm,
2985 clva, PAGE_SIZE);
2986 #endif
2987 pmap_tlb_flushID_SE(pm, clva);
2988 } else {
2989 #ifdef PMAP_CACHE_VIVT
2990 pmap_dcache_wb_range(pm,
2991 clva, PAGE_SIZE, true,
2992 false);
2993 #endif
2994 pmap_tlb_flushD_SE(pm, clva);
2995 }
2996 }
2997 *cleanlist[cnt].ptep = 0;
2998 PTE_SYNC_CURRENT(pm, cleanlist[cnt].ptep);
2999 }
3000
3001 /*
3002 * If it looks like we're removing a whole bunch
3003 * of mappings, it's faster to just write-back
3004 * the whole cache now and defer TLB flushes until
3005 * pmap_update() is called.
3006 */
3007 if (total <= PMAP_REMOVE_CLEAN_LIST_SIZE)
3008 cleanlist_idx = 0;
3009 else {
3010 cleanlist_idx = PMAP_REMOVE_CLEAN_LIST_SIZE + 1;
3011 #ifdef PMAP_CACHE_VIVT
3012 pmap_idcache_wbinv_all(pm);
3013 #endif
3014 pm->pm_remove_all = true;
3015 }
3016 }
3017
3018 pmap_free_l2_bucket(pm, l2b, mappings);
3019 pm->pm_stats.resident_count -= mappings;
3020 }
3021
3022 pmap_release_pmap_lock(pm);
3023 PMAP_MAP_TO_HEAD_UNLOCK();
3024 }
3025
3026 /*
3027 * pmap_kenter_pa: enter an unmanaged, wired kernel mapping
3028 *
3029 * We assume there is already sufficient KVM space available
3030 * to do this, as we can't allocate L2 descriptor tables/metadata
3031 * from here.
3032 */
3033 void
3034 pmap_kenter_pa(vaddr_t va, paddr_t pa, vm_prot_t prot)
3035 {
3036 struct l2_bucket *l2b;
3037 pt_entry_t *ptep, opte;
3038 #ifdef PMAP_CACHE_VIPT
3039 struct vm_page *pg = PHYS_TO_VM_PAGE(pa);
3040 struct vm_page *opg;
3041 #endif
3042
3043
3044 NPDEBUG(PDB_KENTER,
3045 printf("pmap_kenter_pa: va 0x%08lx, pa 0x%08lx, prot 0x%x\n",
3046 va, pa, prot));
3047
3048 l2b = pmap_get_l2_bucket(pmap_kernel(), va);
3049 KDASSERT(l2b != NULL);
3050
3051 ptep = &l2b->l2b_kva[l2pte_index(va)];
3052 opte = *ptep;
3053
3054 if (opte == 0) {
3055 PMAPCOUNT(kenter_mappings);
3056 l2b->l2b_occupancy++;
3057 } else {
3058 PMAPCOUNT(kenter_remappings);
3059 #ifdef PMAP_CACHE_VIPT
3060 opg = PHYS_TO_VM_PAGE(l2pte_pa(opte));
3061 if (opg) {
3062 KASSERT(opg != pg);
3063 simple_lock(&opg->mdpage.pvh_slock);
3064 KASSERT(opg->mdpage.pvh_attrs & PVF_KENTRY);
3065 if (PV_IS_EXEC_P(opg->mdpage.pvh_attrs)
3066 && !(opg->mdpage.pvh_attrs & PVF_NC)) {
3067 if (opg->mdpage.pvh_list == NULL) {
3068 opg->mdpage.pvh_attrs &= ~PVF_EXEC;
3069 PMAPCOUNT(exec_discarded_kremove);
3070 } else {
3071 pmap_syncicache_page(opg);
3072 PMAPCOUNT(exec_synced_kremove);
3073 }
3074 }
3075 KASSERT(opg->mdpage.pvh_attrs & (PVF_COLORED|PVF_NC));
3076 opg->mdpage.pvh_attrs &= ~PVF_KENTRY;
3077 pmap_vac_me_harder(opg, NULL, 0);
3078 simple_unlock(&opg->mdpage.pvh_slock);
3079 }
3080 #endif
3081 if (l2pte_valid(opte)) {
3082 #ifdef PMAP_CACHE_VIVT
3083 cpu_dcache_wbinv_range(va, PAGE_SIZE);
3084 #endif
3085 cpu_tlb_flushD_SE(va);
3086 cpu_cpwait();
3087 }
3088 }
3089
3090 *ptep = L2_S_PROTO | pa | L2_S_PROT(PTE_KERNEL, prot) |
3091 pte_l2_s_cache_mode;
3092 PTE_SYNC(ptep);
3093
3094 #ifdef PMAP_CACHE_VIPT
3095 if (pg) {
3096 simple_lock(&pg->mdpage.pvh_slock);
3097 KASSERT((pg->mdpage.pvh_attrs & PVF_KENTRY) == 0);
3098 pg->mdpage.pvh_attrs |= PVF_KENTRY;
3099 pmap_vac_me_harder(pg, NULL, va);
3100 simple_unlock(&pg->mdpage.pvh_slock);
3101 }
3102 #endif
3103 }
3104
3105 void
3106 pmap_kremove(vaddr_t va, vsize_t len)
3107 {
3108 struct l2_bucket *l2b;
3109 pt_entry_t *ptep, *sptep, opte;
3110 vaddr_t next_bucket, eva;
3111 u_int mappings;
3112 #ifdef PMAP_CACHE_VIPT
3113 struct vm_page *opg;
3114 #endif
3115
3116 PMAPCOUNT(kenter_unmappings);
3117
3118 NPDEBUG(PDB_KREMOVE, printf("pmap_kremove: va 0x%08lx, len 0x%08lx\n",
3119 va, len));
3120
3121 eva = va + len;
3122
3123 while (va < eva) {
3124 next_bucket = L2_NEXT_BUCKET(va);
3125 if (next_bucket > eva)
3126 next_bucket = eva;
3127
3128 l2b = pmap_get_l2_bucket(pmap_kernel(), va);
3129 KDASSERT(l2b != NULL);
3130
3131 sptep = ptep = &l2b->l2b_kva[l2pte_index(va)];
3132 mappings = 0;
3133
3134 while (va < next_bucket) {
3135 opte = *ptep;
3136 #ifdef PMAP_CACHE_VIPT
3137 opg = PHYS_TO_VM_PAGE(l2pte_pa(opte));
3138 if (opg) {
3139 simple_lock(&opg->mdpage.pvh_slock);
3140 KASSERT(opg->mdpage.pvh_attrs & PVF_KENTRY);
3141 if (PV_IS_EXEC_P(opg->mdpage.pvh_attrs)
3142 && !(opg->mdpage.pvh_attrs & PVF_NC)) {
3143 if (opg->mdpage.pvh_list == NULL) {
3144 opg->mdpage.pvh_attrs &=
3145 ~PVF_EXEC;
3146 PMAPCOUNT(exec_discarded_kremove);
3147 } else {
3148 pmap_syncicache_page(opg);
3149 PMAPCOUNT(exec_synced_kremove);
3150 }
3151 }
3152 KASSERT(opg->mdpage.pvh_attrs & (PVF_COLORED|PVF_NC));
3153 opg->mdpage.pvh_attrs &= ~PVF_KENTRY;
3154 pmap_vac_me_harder(opg, NULL, 0);
3155 simple_unlock(&opg->mdpage.pvh_slock);
3156 }
3157 #endif
3158 if (l2pte_valid(opte)) {
3159 #ifdef PMAP_CACHE_VIVT
3160 cpu_dcache_wbinv_range(va, PAGE_SIZE);
3161 #endif
3162 cpu_tlb_flushD_SE(va);
3163 }
3164 if (opte) {
3165 *ptep = 0;
3166 mappings++;
3167 }
3168 va += PAGE_SIZE;
3169 ptep++;
3170 }
3171 KDASSERT(mappings <= l2b->l2b_occupancy);
3172 l2b->l2b_occupancy -= mappings;
3173 PTE_SYNC_RANGE(sptep, (u_int)(ptep - sptep));
3174 }
3175 cpu_cpwait();
3176 }
3177
3178 bool
3179 pmap_extract(pmap_t pm, vaddr_t va, paddr_t *pap)
3180 {
3181 struct l2_dtable *l2;
3182 pd_entry_t *pl1pd, l1pd;
3183 pt_entry_t *ptep, pte;
3184 paddr_t pa;
3185 u_int l1idx;
3186
3187 pmap_acquire_pmap_lock(pm);
3188
3189 l1idx = L1_IDX(va);
3190 pl1pd = &pm->pm_l1->l1_kva[l1idx];
3191 l1pd = *pl1pd;
3192
3193 if (l1pte_section_p(l1pd)) {
3194 /*
3195 * These should only happen for pmap_kernel()
3196 */
3197 KDASSERT(pm == pmap_kernel());
3198 pmap_release_pmap_lock(pm);
3199 pa = (l1pd & L1_S_FRAME) | (va & L1_S_OFFSET);
3200 } else {
3201 /*
3202 * Note that we can't rely on the validity of the L1
3203 * descriptor as an indication that a mapping exists.
3204 * We have to look it up in the L2 dtable.
3205 */
3206 l2 = pm->pm_l2[L2_IDX(l1idx)];
3207
3208 if (l2 == NULL ||
3209 (ptep = l2->l2_bucket[L2_BUCKET(l1idx)].l2b_kva) == NULL) {
3210 pmap_release_pmap_lock(pm);
3211 return false;
3212 }
3213
3214 ptep = &ptep[l2pte_index(va)];
3215 pte = *ptep;
3216 pmap_release_pmap_lock(pm);
3217
3218 if (pte == 0)
3219 return false;
3220
3221 switch (pte & L2_TYPE_MASK) {
3222 case L2_TYPE_L:
3223 pa = (pte & L2_L_FRAME) | (va & L2_L_OFFSET);
3224 break;
3225
3226 default:
3227 pa = (pte & L2_S_FRAME) | (va & L2_S_OFFSET);
3228 break;
3229 }
3230 }
3231
3232 if (pap != NULL)
3233 *pap = pa;
3234
3235 return true;
3236 }
3237
3238 void
3239 pmap_protect(pmap_t pm, vaddr_t sva, vaddr_t eva, vm_prot_t prot)
3240 {
3241 struct l2_bucket *l2b;
3242 pt_entry_t *ptep, pte;
3243 vaddr_t next_bucket;
3244 u_int flags;
3245 u_int clr_mask;
3246 int flush;
3247
3248 NPDEBUG(PDB_PROTECT,
3249 printf("pmap_protect: pm %p sva 0x%lx eva 0x%lx prot 0x%x\n",
3250 pm, sva, eva, prot));
3251
3252 if ((prot & VM_PROT_READ) == 0) {
3253 pmap_remove(pm, sva, eva);
3254 return;
3255 }
3256
3257 if (prot & VM_PROT_WRITE) {
3258 /*
3259 * If this is a read->write transition, just ignore it and let
3260 * uvm_fault() take care of it later.
3261 */
3262 return;
3263 }
3264
3265 PMAP_MAP_TO_HEAD_LOCK();
3266 pmap_acquire_pmap_lock(pm);
3267
3268 flush = ((eva - sva) >= (PAGE_SIZE * 4)) ? 0 : -1;
3269 flags = 0;
3270 clr_mask = PVF_WRITE | ((prot & VM_PROT_EXECUTE) ? 0 : PVF_EXEC);
3271
3272 while (sva < eva) {
3273 next_bucket = L2_NEXT_BUCKET(sva);
3274 if (next_bucket > eva)
3275 next_bucket = eva;
3276
3277 l2b = pmap_get_l2_bucket(pm, sva);
3278 if (l2b == NULL) {
3279 sva = next_bucket;
3280 continue;
3281 }
3282
3283 ptep = &l2b->l2b_kva[l2pte_index(sva)];
3284
3285 while (sva < next_bucket) {
3286 pte = *ptep;
3287 if (l2pte_valid(pte) != 0 && (pte & L2_S_PROT_W) != 0) {
3288 struct vm_page *pg;
3289 u_int f;
3290
3291 #ifdef PMAP_CACHE_VIVT
3292 /*
3293 * OK, at this point, we know we're doing
3294 * write-protect operation. If the pmap is
3295 * active, write-back the page.
3296 */
3297 pmap_dcache_wb_range(pm, sva, PAGE_SIZE,
3298 false, false);
3299 #endif
3300
3301 pg = PHYS_TO_VM_PAGE(l2pte_pa(pte));
3302 pte &= ~L2_S_PROT_W;
3303 *ptep = pte;
3304 PTE_SYNC(ptep);
3305
3306 if (pg != NULL) {
3307 simple_lock(&pg->mdpage.pvh_slock);
3308 f = pmap_modify_pv(pg, pm, sva,
3309 clr_mask, 0);
3310 pmap_vac_me_harder(pg, pm, sva);
3311 simple_unlock(&pg->mdpage.pvh_slock);
3312 } else
3313 f = PVF_REF | PVF_EXEC;
3314
3315 if (flush >= 0) {
3316 flush++;
3317 flags |= f;
3318 } else
3319 if (PV_BEEN_EXECD(f))
3320 pmap_tlb_flushID_SE(pm, sva);
3321 else
3322 if (PV_BEEN_REFD(f))
3323 pmap_tlb_flushD_SE(pm, sva);
3324 }
3325
3326 sva += PAGE_SIZE;
3327 ptep++;
3328 }
3329 }
3330
3331 pmap_release_pmap_lock(pm);
3332 PMAP_MAP_TO_HEAD_UNLOCK();
3333
3334 if (flush) {
3335 if (PV_BEEN_EXECD(flags))
3336 pmap_tlb_flushID(pm);
3337 else
3338 if (PV_BEEN_REFD(flags))
3339 pmap_tlb_flushD(pm);
3340 }
3341 }
3342
3343 void
3344 pmap_icache_sync_range(pmap_t pm, vaddr_t sva, vaddr_t eva)
3345 {
3346 struct l2_bucket *l2b;
3347 pt_entry_t *ptep;
3348 vaddr_t next_bucket;
3349 vsize_t page_size = trunc_page(sva) + PAGE_SIZE - sva;
3350
3351 NPDEBUG(PDB_EXEC,
3352 printf("pmap_icache_sync_range: pm %p sva 0x%lx eva 0x%lx\n",
3353 pm, sva, eva));
3354
3355 PMAP_MAP_TO_HEAD_LOCK();
3356 pmap_acquire_pmap_lock(pm);
3357
3358 while (sva < eva) {
3359 next_bucket = L2_NEXT_BUCKET(sva);
3360 if (next_bucket > eva)
3361 next_bucket = eva;
3362
3363 l2b = pmap_get_l2_bucket(pm, sva);
3364 if (l2b == NULL) {
3365 sva = next_bucket;
3366 continue;
3367 }
3368
3369 for (ptep = &l2b->l2b_kva[l2pte_index(sva)];
3370 sva < next_bucket;
3371 sva += page_size, ptep++, page_size = PAGE_SIZE) {
3372 if (l2pte_valid(*ptep)) {
3373 cpu_icache_sync_range(sva,
3374 min(page_size, eva - sva));
3375 }
3376 }
3377 }
3378
3379 pmap_release_pmap_lock(pm);
3380 PMAP_MAP_TO_HEAD_UNLOCK();
3381 }
3382
3383 void
3384 pmap_page_protect(struct vm_page *pg, vm_prot_t prot)
3385 {
3386
3387 NPDEBUG(PDB_PROTECT,
3388 printf("pmap_page_protect: pg %p (0x%08lx), prot 0x%x\n",
3389 pg, VM_PAGE_TO_PHYS(pg), prot));
3390
3391 switch(prot) {
3392 return;
3393 case VM_PROT_READ|VM_PROT_WRITE:
3394 #if defined(PMAP_CHECK_VIPT) && defined(PMAP_APX)
3395 pmap_clearbit(pg, PVF_EXEC);
3396 break;
3397 #endif
3398 case VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE:
3399 break;
3400
3401 case VM_PROT_READ:
3402 #if defined(PMAP_CHECK_VIPT) && defined(PMAP_APX)
3403 pmap_clearbit(pg, PVF_WRITE|PVF_EXEC);
3404 break;
3405 #endif
3406 case VM_PROT_READ|VM_PROT_EXECUTE:
3407 pmap_clearbit(pg, PVF_WRITE);
3408 break;
3409
3410 default:
3411 pmap_page_remove(pg);
3412 break;
3413 }
3414 }
3415
3416 /*
3417 * pmap_clear_modify:
3418 *
3419 * Clear the "modified" attribute for a page.
3420 */
3421 bool
3422 pmap_clear_modify(struct vm_page *pg)
3423 {
3424 bool rv;
3425
3426 if (pg->mdpage.pvh_attrs & PVF_MOD) {
3427 rv = true;
3428 pmap_clearbit(pg, PVF_MOD);
3429 } else
3430 rv = false;
3431
3432 return (rv);
3433 }
3434
3435 /*
3436 * pmap_clear_reference:
3437 *
3438 * Clear the "referenced" attribute for a page.
3439 */
3440 bool
3441 pmap_clear_reference(struct vm_page *pg)
3442 {
3443 bool rv;
3444
3445 if (pg->mdpage.pvh_attrs & PVF_REF) {
3446 rv = true;
3447 pmap_clearbit(pg, PVF_REF);
3448 } else
3449 rv = false;
3450
3451 return (rv);
3452 }
3453
3454 /*
3455 * pmap_is_modified:
3456 *
3457 * Test if a page has the "modified" attribute.
3458 */
3459 /* See <arm/arm32/pmap.h> */
3460
3461 /*
3462 * pmap_is_referenced:
3463 *
3464 * Test if a page has the "referenced" attribute.
3465 */
3466 /* See <arm/arm32/pmap.h> */
3467
3468 int
3469 pmap_fault_fixup(pmap_t pm, vaddr_t va, vm_prot_t ftype, int user)
3470 {
3471 struct l2_dtable *l2;
3472 struct l2_bucket *l2b;
3473 pd_entry_t *pl1pd, l1pd;
3474 pt_entry_t *ptep, pte;
3475 paddr_t pa;
3476 u_int l1idx;
3477 int rv = 0;
3478
3479 PMAP_MAP_TO_HEAD_LOCK();
3480 pmap_acquire_pmap_lock(pm);
3481
3482 l1idx = L1_IDX(va);
3483
3484 /*
3485 * If there is no l2_dtable for this address, then the process
3486 * has no business accessing it.
3487 *
3488 * Note: This will catch userland processes trying to access
3489 * kernel addresses.
3490 */
3491 l2 = pm->pm_l2[L2_IDX(l1idx)];
3492 if (l2 == NULL)
3493 goto out;
3494
3495 /*
3496 * Likewise if there is no L2 descriptor table
3497 */
3498 l2b = &l2->l2_bucket[L2_BUCKET(l1idx)];
3499 if (l2b->l2b_kva == NULL)
3500 goto out;
3501
3502 /*
3503 * Check the PTE itself.
3504 */
3505 ptep = &l2b->l2b_kva[l2pte_index(va)];
3506 pte = *ptep;
3507 if (pte == 0)
3508 goto out;
3509
3510 /*
3511 * Catch a userland access to the vector page mapped at 0x0
3512 */
3513 if (user && (pte & L2_S_PROT_U) == 0)
3514 goto out;
3515
3516 pa = l2pte_pa(pte);
3517
3518 if ((ftype & VM_PROT_WRITE) && (pte & L2_S_PROT_W) == 0) {
3519 /*
3520 * This looks like a good candidate for "page modified"
3521 * emulation...
3522 */
3523 struct pv_entry *pv;
3524 struct vm_page *pg;
3525
3526 /* Extract the physical address of the page */
3527 if ((pg = PHYS_TO_VM_PAGE(pa)) == NULL)
3528 goto out;
3529
3530 /* Get the current flags for this page. */
3531 simple_lock(&pg->mdpage.pvh_slock);
3532
3533 pv = pmap_find_pv(pg, pm, va);
3534 if (pv == NULL) {
3535 simple_unlock(&pg->mdpage.pvh_slock);
3536 goto out;
3537 }
3538
3539 /*
3540 * Do the flags say this page is writable? If not then it
3541 * is a genuine write fault. If yes then the write fault is
3542 * our fault as we did not reflect the write access in the
3543 * PTE. Now we know a write has occurred we can correct this
3544 * and also set the modified bit
3545 */
3546 if ((pv->pv_flags & PVF_WRITE) == 0) {
3547 simple_unlock(&pg->mdpage.pvh_slock);
3548 goto out;
3549 }
3550
3551 NPDEBUG(PDB_FOLLOW,
3552 printf("pmap_fault_fixup: mod emul. pm %p, va 0x%08lx, pa 0x%08lx\n",
3553 pm, va, VM_PAGE_TO_PHYS(pg)));
3554
3555 pg->mdpage.pvh_attrs |= PVF_REF | PVF_MOD;
3556 pv->pv_flags |= PVF_REF | PVF_MOD;
3557 simple_unlock(&pg->mdpage.pvh_slock);
3558
3559 /*
3560 * Re-enable write permissions for the page. No need to call
3561 * pmap_vac_me_harder(), since this is just a
3562 * modified-emulation fault, and the PVF_WRITE bit isn't
3563 * changing. We've already set the cacheable bits based on
3564 * the assumption that we can write to this page.
3565 */
3566 *ptep = (pte & ~L2_TYPE_MASK) | L2_S_PROTO | L2_S_PROT_W;
3567 PTE_SYNC(ptep);
3568 rv = 1;
3569 } else
3570 if ((pte & L2_TYPE_MASK) == L2_TYPE_INV) {
3571 /*
3572 * This looks like a good candidate for "page referenced"
3573 * emulation.
3574 */
3575 struct pv_entry *pv;
3576 struct vm_page *pg;
3577
3578 /* Extract the physical address of the page */
3579 if ((pg = PHYS_TO_VM_PAGE(pa)) == NULL)
3580 goto out;
3581
3582 /* Get the current flags for this page. */
3583 simple_lock(&pg->mdpage.pvh_slock);
3584
3585 pv = pmap_find_pv(pg, pm, va);
3586 if (pv == NULL) {
3587 simple_unlock(&pg->mdpage.pvh_slock);
3588 goto out;
3589 }
3590
3591 pg->mdpage.pvh_attrs |= PVF_REF;
3592 pv->pv_flags |= PVF_REF;
3593 simple_unlock(&pg->mdpage.pvh_slock);
3594
3595 NPDEBUG(PDB_FOLLOW,
3596 printf("pmap_fault_fixup: ref emul. pm %p, va 0x%08lx, pa 0x%08lx\n",
3597 pm, va, VM_PAGE_TO_PHYS(pg)));
3598
3599 *ptep = (pte & ~L2_TYPE_MASK) | L2_S_PROTO;
3600 PTE_SYNC(ptep);
3601 rv = 1;
3602 }
3603
3604 /*
3605 * We know there is a valid mapping here, so simply
3606 * fix up the L1 if necessary.
3607 */
3608 pl1pd = &pm->pm_l1->l1_kva[l1idx];
3609 l1pd = l2b->l2b_phys | L1_C_DOM(pm->pm_domain) | L1_C_PROTO;
3610 if (*pl1pd != l1pd) {
3611 *pl1pd = l1pd;
3612 PTE_SYNC(pl1pd);
3613 rv = 1;
3614 }
3615
3616 #ifdef CPU_SA110
3617 /*
3618 * There are bugs in the rev K SA110. This is a check for one
3619 * of them.
3620 */
3621 if (rv == 0 && curcpu()->ci_arm_cputype == CPU_ID_SA110 &&
3622 curcpu()->ci_arm_cpurev < 3) {
3623 /* Always current pmap */
3624 if (l2pte_valid(pte)) {
3625 extern int kernel_debug;
3626 if (kernel_debug & 1) {
3627 struct proc *p = curlwp->l_proc;
3628 printf("prefetch_abort: page is already "
3629 "mapped - pte=%p *pte=%08x\n", ptep, pte);
3630 printf("prefetch_abort: pc=%08lx proc=%p "
3631 "process=%s\n", va, p, p->p_comm);
3632 printf("prefetch_abort: far=%08x fs=%x\n",
3633 cpu_faultaddress(), cpu_faultstatus());
3634 }
3635 #ifdef DDB
3636 if (kernel_debug & 2)
3637 Debugger();
3638 #endif
3639 rv = 1;
3640 }
3641 }
3642 #endif /* CPU_SA110 */
3643
3644 #ifdef DEBUG
3645 /*
3646 * If 'rv == 0' at this point, it generally indicates that there is a
3647 * stale TLB entry for the faulting address. This happens when two or
3648 * more processes are sharing an L1. Since we don't flush the TLB on
3649 * a context switch between such processes, we can take domain faults
3650 * for mappings which exist at the same VA in both processes. EVEN IF
3651 * WE'VE RECENTLY FIXED UP THE CORRESPONDING L1 in pmap_enter(), for
3652 * example.
3653 *
3654 * This is extremely likely to happen if pmap_enter() updated the L1
3655 * entry for a recently entered mapping. In this case, the TLB is
3656 * flushed for the new mapping, but there may still be TLB entries for
3657 * other mappings belonging to other processes in the 1MB range
3658 * covered by the L1 entry.
3659 *
3660 * Since 'rv == 0', we know that the L1 already contains the correct
3661 * value, so the fault must be due to a stale TLB entry.
3662 *
3663 * Since we always need to flush the TLB anyway in the case where we
3664 * fixed up the L1, or frobbed the L2 PTE, we effectively deal with
3665 * stale TLB entries dynamically.
3666 *
3667 * However, the above condition can ONLY happen if the current L1 is
3668 * being shared. If it happens when the L1 is unshared, it indicates
3669 * that other parts of the pmap are not doing their job WRT managing
3670 * the TLB.
3671 */
3672 if (rv == 0 && pm->pm_l1->l1_domain_use_count == 1) {
3673 extern int last_fault_code;
3674 printf("fixup: pm %p, va 0x%lx, ftype %d - nothing to do!\n",
3675 pm, va, ftype);
3676 printf("fixup: l2 %p, l2b %p, ptep %p, pl1pd %p\n",
3677 l2, l2b, ptep, pl1pd);
3678 printf("fixup: pte 0x%x, l1pd 0x%x, last code 0x%x\n",
3679 pte, l1pd, last_fault_code);
3680 #ifdef DDB
3681 Debugger();
3682 #endif
3683 }
3684 #endif
3685
3686 cpu_tlb_flushID_SE(va);
3687 cpu_cpwait();
3688
3689 rv = 1;
3690
3691 out:
3692 pmap_release_pmap_lock(pm);
3693 PMAP_MAP_TO_HEAD_UNLOCK();
3694
3695 return (rv);
3696 }
3697
3698 /*
3699 * pmap_collect: free resources held by a pmap
3700 *
3701 * => optional function.
3702 * => called when a process is swapped out to free memory.
3703 */
3704 void
3705 pmap_collect(pmap_t pm)
3706 {
3707
3708 #ifdef PMAP_CACHE_VIVT
3709 pmap_idcache_wbinv_all(pm);
3710 #endif
3711 pm->pm_remove_all = true;
3712 pmap_do_remove(pm, VM_MIN_ADDRESS, VM_MAX_ADDRESS, 1);
3713 pmap_update(pm);
3714 PMAPCOUNT(collects);
3715 }
3716
3717 /*
3718 * Routine: pmap_procwr
3719 *
3720 * Function:
3721 * Synchronize caches corresponding to [addr, addr+len) in p.
3722 *
3723 */
3724 void
3725 pmap_procwr(struct proc *p, vaddr_t va, int len)
3726 {
3727 /* We only need to do anything if it is the current process. */
3728 if (p == curproc)
3729 cpu_icache_sync_range(va, len);
3730 }
3731
3732 /*
3733 * Routine: pmap_unwire
3734 * Function: Clear the wired attribute for a map/virtual-address pair.
3735 *
3736 * In/out conditions:
3737 * The mapping must already exist in the pmap.
3738 */
3739 void
3740 pmap_unwire(pmap_t pm, vaddr_t va)
3741 {
3742 struct l2_bucket *l2b;
3743 pt_entry_t *ptep, pte;
3744 struct vm_page *pg;
3745 paddr_t pa;
3746
3747 NPDEBUG(PDB_WIRING, printf("pmap_unwire: pm %p, va 0x%08lx\n", pm, va));
3748
3749 PMAP_MAP_TO_HEAD_LOCK();
3750 pmap_acquire_pmap_lock(pm);
3751
3752 l2b = pmap_get_l2_bucket(pm, va);
3753 KDASSERT(l2b != NULL);
3754
3755 ptep = &l2b->l2b_kva[l2pte_index(va)];
3756 pte = *ptep;
3757
3758 /* Extract the physical address of the page */
3759 pa = l2pte_pa(pte);
3760
3761 if ((pg = PHYS_TO_VM_PAGE(pa)) != NULL) {
3762 /* Update the wired bit in the pv entry for this page. */
3763 simple_lock(&pg->mdpage.pvh_slock);
3764 (void) pmap_modify_pv(pg, pm, va, PVF_WIRED, 0);
3765 simple_unlock(&pg->mdpage.pvh_slock);
3766 }
3767
3768 pmap_release_pmap_lock(pm);
3769 PMAP_MAP_TO_HEAD_UNLOCK();
3770 }
3771
3772 void
3773 pmap_activate(struct lwp *l)
3774 {
3775 extern int block_userspace_access;
3776 pmap_t opm, npm, rpm;
3777 uint32_t odacr, ndacr;
3778 int oldirqstate;
3779
3780 /*
3781 * If activating a non-current lwp or the current lwp is
3782 * already active, just return.
3783 */
3784 if (l != curlwp ||
3785 l->l_proc->p_vmspace->vm_map.pmap->pm_activated == true)
3786 return;
3787
3788 npm = l->l_proc->p_vmspace->vm_map.pmap;
3789 ndacr = (DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL * 2)) |
3790 (DOMAIN_CLIENT << (npm->pm_domain * 2));
3791
3792 /*
3793 * If TTB and DACR are unchanged, short-circuit all the
3794 * TLB/cache management stuff.
3795 */
3796 if (pmap_previous_active_lwp != NULL) {
3797 opm = pmap_previous_active_lwp->l_proc->p_vmspace->vm_map.pmap;
3798 odacr = (DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL * 2)) |
3799 (DOMAIN_CLIENT << (opm->pm_domain * 2));
3800
3801 if (opm->pm_l1 == npm->pm_l1 && odacr == ndacr)
3802 goto all_done;
3803 } else
3804 opm = NULL;
3805
3806 PMAPCOUNT(activations);
3807 block_userspace_access = 1;
3808
3809 /*
3810 * If switching to a user vmspace which is different to the
3811 * most recent one, and the most recent one is potentially
3812 * live in the cache, we must write-back and invalidate the
3813 * entire cache.
3814 */
3815 rpm = pmap_recent_user;
3816 if (npm != pmap_kernel() && rpm && npm != rpm &&
3817 rpm->pm_cstate.cs_cache) {
3818 rpm->pm_cstate.cs_cache = 0;
3819 #ifdef PMAP_CACHE_VIVT
3820 cpu_idcache_wbinv_all();
3821 #endif
3822 }
3823
3824 /* No interrupts while we frob the TTB/DACR */
3825 oldirqstate = disable_interrupts(I32_bit | F32_bit);
3826
3827 /*
3828 * For ARM_VECTORS_LOW, we MUST, I repeat, MUST fix up the L1
3829 * entry corresponding to 'vector_page' in the incoming L1 table
3830 * before switching to it otherwise subsequent interrupts/exceptions
3831 * (including domain faults!) will jump into hyperspace.
3832 */
3833 if (npm->pm_pl1vec != NULL) {
3834 cpu_tlb_flushID_SE((u_int)vector_page);
3835 cpu_cpwait();
3836 *npm->pm_pl1vec = npm->pm_l1vec;
3837 PTE_SYNC(npm->pm_pl1vec);
3838 }
3839
3840 cpu_domains(ndacr);
3841
3842 if (npm == pmap_kernel() || npm == rpm) {
3843 /*
3844 * Switching to a kernel thread, or back to the
3845 * same user vmspace as before... Simply update
3846 * the TTB (no TLB flush required)
3847 */
3848 __asm volatile("mcr p15, 0, %0, c2, c0, 0" ::
3849 "r"(npm->pm_l1->l1_physaddr));
3850 cpu_cpwait();
3851 } else {
3852 /*
3853 * Otherwise, update TTB and flush TLB
3854 */
3855 cpu_context_switch(npm->pm_l1->l1_physaddr);
3856 if (rpm != NULL)
3857 rpm->pm_cstate.cs_tlb = 0;
3858 }
3859
3860 restore_interrupts(oldirqstate);
3861
3862 block_userspace_access = 0;
3863
3864 all_done:
3865 /*
3866 * The new pmap is resident. Make sure it's marked
3867 * as resident in the cache/TLB.
3868 */
3869 npm->pm_cstate.cs_all = PMAP_CACHE_STATE_ALL;
3870 if (npm != pmap_kernel())
3871 pmap_recent_user = npm;
3872
3873 /* The old pmap is not longer active */
3874 if (opm != NULL)
3875 opm->pm_activated = false;
3876
3877 /* But the new one is */
3878 npm->pm_activated = true;
3879 }
3880
3881 void
3882 pmap_deactivate(struct lwp *l)
3883 {
3884
3885 /*
3886 * If the process is exiting, make sure pmap_activate() does
3887 * a full MMU context-switch and cache flush, which we might
3888 * otherwise skip. See PR port-arm/38950.
3889 */
3890 if (l->l_proc->p_sflag & PS_WEXIT)
3891 pmap_previous_active_lwp = NULL;
3892
3893 l->l_proc->p_vmspace->vm_map.pmap->pm_activated = false;
3894 }
3895
3896 void
3897 pmap_update(pmap_t pm)
3898 {
3899
3900 if (pm->pm_remove_all) {
3901 /*
3902 * Finish up the pmap_remove_all() optimisation by flushing
3903 * the TLB.
3904 */
3905 pmap_tlb_flushID(pm);
3906 pm->pm_remove_all = false;
3907 }
3908
3909 if (pmap_is_current(pm)) {
3910 /*
3911 * If we're dealing with a current userland pmap, move its L1
3912 * to the end of the LRU.
3913 */
3914 if (pm != pmap_kernel())
3915 pmap_use_l1(pm);
3916
3917 /*
3918 * We can assume we're done with frobbing the cache/tlb for
3919 * now. Make sure any future pmap ops don't skip cache/tlb
3920 * flushes.
3921 */
3922 pm->pm_cstate.cs_all = PMAP_CACHE_STATE_ALL;
3923 }
3924
3925 PMAPCOUNT(updates);
3926
3927 /*
3928 * make sure TLB/cache operations have completed.
3929 */
3930 cpu_cpwait();
3931 }
3932
3933 void
3934 pmap_remove_all(pmap_t pm)
3935 {
3936
3937 /*
3938 * The vmspace described by this pmap is about to be torn down.
3939 * Until pmap_update() is called, UVM will only make calls
3940 * to pmap_remove(). We can make life much simpler by flushing
3941 * the cache now, and deferring TLB invalidation to pmap_update().
3942 */
3943 #ifdef PMAP_CACHE_VIVT
3944 pmap_idcache_wbinv_all(pm);
3945 #endif
3946 pm->pm_remove_all = true;
3947 }
3948
3949 /*
3950 * Retire the given physical map from service.
3951 * Should only be called if the map contains no valid mappings.
3952 */
3953 void
3954 pmap_destroy(pmap_t pm)
3955 {
3956 u_int count;
3957
3958 if (pm == NULL)
3959 return;
3960
3961 if (pm->pm_remove_all) {
3962 pmap_tlb_flushID(pm);
3963 pm->pm_remove_all = false;
3964 }
3965
3966 /*
3967 * Drop reference count
3968 */
3969 mutex_enter(&pm->pm_lock);
3970 count = --pm->pm_obj.uo_refs;
3971 mutex_exit(&pm->pm_lock);
3972 if (count > 0) {
3973 if (pmap_is_current(pm)) {
3974 if (pm != pmap_kernel())
3975 pmap_use_l1(pm);
3976 pm->pm_cstate.cs_all = PMAP_CACHE_STATE_ALL;
3977 }
3978 return;
3979 }
3980
3981 /*
3982 * reference count is zero, free pmap resources and then free pmap.
3983 */
3984
3985 if (vector_page < KERNEL_BASE) {
3986 KDASSERT(!pmap_is_current(pm));
3987
3988 /* Remove the vector page mapping */
3989 pmap_remove(pm, vector_page, vector_page + PAGE_SIZE);
3990 pmap_update(pm);
3991 }
3992
3993 LIST_REMOVE(pm, pm_list);
3994
3995 pmap_free_l1(pm);
3996
3997 if (pmap_recent_user == pm)
3998 pmap_recent_user = NULL;
3999
4000 UVM_OBJ_DESTROY(&pm->pm_obj);
4001
4002 /* return the pmap to the pool */
4003 pool_cache_put(&pmap_cache, pm);
4004 }
4005
4006
4007 /*
4008 * void pmap_reference(pmap_t pm)
4009 *
4010 * Add a reference to the specified pmap.
4011 */
4012 void
4013 pmap_reference(pmap_t pm)
4014 {
4015
4016 if (pm == NULL)
4017 return;
4018
4019 pmap_use_l1(pm);
4020
4021 mutex_enter(&pm->pm_lock);
4022 pm->pm_obj.uo_refs++;
4023 mutex_exit(&pm->pm_lock);
4024 }
4025
4026 #if ARM_MMU_V6 > 0
4027
4028 static struct evcnt pmap_prefer_nochange_ev =
4029 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap prefer", "nochange");
4030 static struct evcnt pmap_prefer_change_ev =
4031 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap prefer", "change");
4032
4033 EVCNT_ATTACH_STATIC(pmap_prefer_change_ev);
4034 EVCNT_ATTACH_STATIC(pmap_prefer_nochange_ev);
4035
4036 void
4037 pmap_prefer(vaddr_t hint, vaddr_t *vap, int td)
4038 {
4039 vsize_t mask = arm_cache_prefer_mask | (PAGE_SIZE - 1);
4040 vaddr_t va = *vap;
4041 vaddr_t diff = (hint - va) & mask;
4042 if (diff == 0) {
4043 pmap_prefer_nochange_ev.ev_count++;
4044 } else {
4045 pmap_prefer_change_ev.ev_count++;
4046 if (__predict_false(td))
4047 va -= mask + 1;
4048 *vap = va + diff;
4049 }
4050 }
4051 #endif /* ARM_MMU_V6 */
4052
4053 /*
4054 * pmap_zero_page()
4055 *
4056 * Zero a given physical page by mapping it at a page hook point.
4057 * In doing the zero page op, the page we zero is mapped cachable, as with
4058 * StrongARM accesses to non-cached pages are non-burst making writing
4059 * _any_ bulk data very slow.
4060 */
4061 #if (ARM_MMU_GENERIC + ARM_MMU_SA1 + ARM_MMU_V6) != 0
4062 void
4063 pmap_zero_page_generic(paddr_t phys)
4064 {
4065 #if defined(PMAP_CACHE_VIPT) || defined(DEBUG)
4066 struct vm_page *pg = PHYS_TO_VM_PAGE(phys);
4067 #endif
4068 #ifdef PMAP_CACHE_VIPT
4069 /* Choose the last page color it had, if any */
4070 const vsize_t va_offset = pg->mdpage.pvh_attrs & arm_cache_prefer_mask;
4071 #else
4072 const vsize_t va_offset = 0;
4073 #endif
4074 pt_entry_t * const ptep = &cdst_pte[va_offset >> PGSHIFT];
4075
4076 #ifdef DEBUG
4077 if (pg->mdpage.pvh_list != NULL)
4078 panic("pmap_zero_page: page has mappings");
4079 #endif
4080
4081 KDASSERT((phys & PGOFSET) == 0);
4082
4083 /*
4084 * Hook in the page, zero it, and purge the cache for that
4085 * zeroed page. Invalidate the TLB as needed.
4086 */
4087 *ptep = L2_S_PROTO | phys |
4088 L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE) | pte_l2_s_cache_mode;
4089 PTE_SYNC(ptep);
4090 cpu_tlb_flushD_SE(cdstp + va_offset);
4091 cpu_cpwait();
4092 bzero_page(cdstp + va_offset);
4093 /*
4094 * Unmap the page.
4095 */
4096 *ptep = 0;
4097 PTE_SYNC(ptep);
4098 cpu_tlb_flushD_SE(cdstp + va_offset);
4099 #ifdef PMAP_CACHE_VIVT
4100 cpu_dcache_wbinv_range(cdstp + va_offset, PAGE_SIZE);
4101 #endif
4102 #ifdef PMAP_CACHE_VIPT
4103 /*
4104 * This page is now cache resident so it now has a page color.
4105 * Any contents have been obliterated so clear the EXEC flag.
4106 */
4107 if (!pmap_is_page_colored_p(pg)) {
4108 PMAPCOUNT(vac_color_new);
4109 pg->mdpage.pvh_attrs |= PVF_COLORED;
4110 }
4111 if (PV_IS_EXEC_P(pg->mdpage.pvh_attrs)) {
4112 pg->mdpage.pvh_attrs &= ~PVF_EXEC;
4113 PMAPCOUNT(exec_discarded_zero);
4114 }
4115 #endif
4116 }
4117 #endif /* (ARM_MMU_GENERIC + ARM_MMU_SA1 + ARM_MMU_V6) != 0 */
4118
4119 #if ARM_MMU_XSCALE == 1
4120 void
4121 pmap_zero_page_xscale(paddr_t phys)
4122 {
4123 #ifdef DEBUG
4124 struct vm_page *pg = PHYS_TO_VM_PAGE(phys);
4125
4126 if (pg->mdpage.pvh_list != NULL)
4127 panic("pmap_zero_page: page has mappings");
4128 #endif
4129
4130 KDASSERT((phys & PGOFSET) == 0);
4131
4132 /*
4133 * Hook in the page, zero it, and purge the cache for that
4134 * zeroed page. Invalidate the TLB as needed.
4135 */
4136 *cdst_pte = L2_S_PROTO | phys |
4137 L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE) |
4138 L2_C | L2_XS_T_TEX(TEX_XSCALE_X); /* mini-data */
4139 PTE_SYNC(cdst_pte);
4140 cpu_tlb_flushD_SE(cdstp);
4141 cpu_cpwait();
4142 bzero_page(cdstp);
4143 xscale_cache_clean_minidata();
4144 }
4145 #endif /* ARM_MMU_XSCALE == 1 */
4146
4147 /* pmap_pageidlezero()
4148 *
4149 * The same as above, except that we assume that the page is not
4150 * mapped. This means we never have to flush the cache first. Called
4151 * from the idle loop.
4152 */
4153 bool
4154 pmap_pageidlezero(paddr_t phys)
4155 {
4156 unsigned int i;
4157 int *ptr;
4158 bool rv = true;
4159 #if defined(PMAP_CACHE_VIPT) || defined(DEBUG)
4160 struct vm_page * const pg = PHYS_TO_VM_PAGE(phys);
4161 #endif
4162 #ifdef PMAP_CACHE_VIPT
4163 /* Choose the last page color it had, if any */
4164 const vsize_t va_offset = pg->mdpage.pvh_attrs & arm_cache_prefer_mask;
4165 #else
4166 const vsize_t va_offset = 0;
4167 #endif
4168 pt_entry_t * const ptep = &csrc_pte[va_offset >> PGSHIFT];
4169
4170
4171 #ifdef DEBUG
4172 if (pg->mdpage.pvh_list != NULL)
4173 panic("pmap_pageidlezero: page has mappings");
4174 #endif
4175
4176 KDASSERT((phys & PGOFSET) == 0);
4177
4178 /*
4179 * Hook in the page, zero it, and purge the cache for that
4180 * zeroed page. Invalidate the TLB as needed.
4181 */
4182 *ptep = L2_S_PROTO | phys |
4183 L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE) | pte_l2_s_cache_mode;
4184 PTE_SYNC(ptep);
4185 cpu_tlb_flushD_SE(cdstp + va_offset);
4186 cpu_cpwait();
4187
4188 for (i = 0, ptr = (int *)(cdstp + va_offset);
4189 i < (PAGE_SIZE / sizeof(int)); i++) {
4190 if (sched_curcpu_runnable_p() != 0) {
4191 /*
4192 * A process has become ready. Abort now,
4193 * so we don't keep it waiting while we
4194 * do slow memory access to finish this
4195 * page.
4196 */
4197 rv = false;
4198 break;
4199 }
4200 *ptr++ = 0;
4201 }
4202
4203 #ifdef PMAP_CACHE_VIVT
4204 if (rv)
4205 /*
4206 * if we aborted we'll rezero this page again later so don't
4207 * purge it unless we finished it
4208 */
4209 cpu_dcache_wbinv_range(cdstp, PAGE_SIZE);
4210 #elif defined(PMAP_CACHE_VIPT)
4211 /*
4212 * This page is now cache resident so it now has a page color.
4213 * Any contents have been obliterated so clear the EXEC flag.
4214 */
4215 if (!pmap_is_page_colored_p(pg)) {
4216 PMAPCOUNT(vac_color_new);
4217 pg->mdpage.pvh_attrs |= PVF_COLORED;
4218 }
4219 if (PV_IS_EXEC_P(pg->mdpage.pvh_attrs)) {
4220 pg->mdpage.pvh_attrs &= ~PVF_EXEC;
4221 PMAPCOUNT(exec_discarded_zero);
4222 }
4223 #endif
4224 /*
4225 * Unmap the page.
4226 */
4227 *ptep = 0;
4228 PTE_SYNC(ptep);
4229 cpu_tlb_flushD_SE(cdstp + va_offset);
4230
4231 return (rv);
4232 }
4233
4234 /*
4235 * pmap_copy_page()
4236 *
4237 * Copy one physical page into another, by mapping the pages into
4238 * hook points. The same comment regarding cachability as in
4239 * pmap_zero_page also applies here.
4240 */
4241 #if (ARM_MMU_GENERIC + ARM_MMU_SA1 + ARM_MMU_V6) != 0
4242 void
4243 pmap_copy_page_generic(paddr_t src, paddr_t dst)
4244 {
4245 struct vm_page * const src_pg = PHYS_TO_VM_PAGE(src);
4246 #if defined(PMAP_CACHE_VIPT) || defined(DEBUG)
4247 struct vm_page * const dst_pg = PHYS_TO_VM_PAGE(dst);
4248 #endif
4249 #ifdef PMAP_CACHE_VIPT
4250 const vsize_t src_va_offset = src_pg->mdpage.pvh_attrs & arm_cache_prefer_mask;
4251 const vsize_t dst_va_offset = dst_pg->mdpage.pvh_attrs & arm_cache_prefer_mask;
4252 #else
4253 const vsize_t src_va_offset = 0;
4254 const vsize_t dst_va_offset = 0;
4255 #endif
4256 pt_entry_t * const src_ptep = &csrc_pte[src_va_offset >> PGSHIFT];
4257 pt_entry_t * const dst_ptep = &cdst_pte[dst_va_offset >> PGSHIFT];
4258
4259 #ifdef DEBUG
4260 if (dst_pg->mdpage.pvh_list != NULL)
4261 panic("pmap_copy_page: dst page has mappings");
4262 #endif
4263
4264 #ifdef PMAP_CACHE_VIPT
4265 KASSERT(src_pg->mdpage.pvh_attrs & (PVF_COLORED|PVF_NC));
4266 #endif
4267 KDASSERT((src & PGOFSET) == 0);
4268 KDASSERT((dst & PGOFSET) == 0);
4269
4270 /*
4271 * Clean the source page. Hold the source page's lock for
4272 * the duration of the copy so that no other mappings can
4273 * be created while we have a potentially aliased mapping.
4274 */
4275 simple_lock(&src_pg->mdpage.pvh_slock);
4276 #ifdef PMAP_CACHE_VIVT
4277 (void) pmap_clean_page(src_pg->mdpage.pvh_list, true);
4278 #endif
4279
4280 /*
4281 * Map the pages into the page hook points, copy them, and purge
4282 * the cache for the appropriate page. Invalidate the TLB
4283 * as required.
4284 */
4285 *src_ptep = L2_S_PROTO
4286 | src
4287 #ifdef PMAP_CACHE_VIPT
4288 | ((src_pg->mdpage.pvh_attrs & PVF_NC) ? 0 : pte_l2_s_cache_mode)
4289 #endif
4290 #ifdef PMAP_CACHE_VIVT
4291 | pte_l2_s_cache_mode
4292 #endif
4293 | L2_S_PROT(PTE_KERNEL, VM_PROT_READ);
4294 *dst_ptep = L2_S_PROTO | dst |
4295 L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE) | pte_l2_s_cache_mode;
4296 PTE_SYNC(src_ptep);
4297 PTE_SYNC(dst_ptep);
4298 cpu_tlb_flushD_SE(csrcp + src_va_offset);
4299 cpu_tlb_flushD_SE(cdstp + dst_va_offset);
4300 cpu_cpwait();
4301 bcopy_page(csrcp + src_va_offset, cdstp + dst_va_offset);
4302 #ifdef PMAP_CACHE_VIVT
4303 cpu_dcache_inv_range(csrcp + src_va_offset, PAGE_SIZE);
4304 #endif
4305 simple_unlock(&src_pg->mdpage.pvh_slock); /* cache is safe again */
4306 #ifdef PMAP_CACHE_VIVT
4307 cpu_dcache_wbinv_range(cdstp + dst_va_offset, PAGE_SIZE);
4308 #endif
4309 /*
4310 * Unmap the pages.
4311 */
4312 *src_ptep = 0;
4313 *dst_ptep = 0;
4314 PTE_SYNC(src_ptep);
4315 PTE_SYNC(dst_ptep);
4316 cpu_tlb_flushD_SE(csrcp + src_va_offset);
4317 cpu_tlb_flushD_SE(cdstp + dst_va_offset);
4318 #ifdef PMAP_CACHE_VIPT
4319 /*
4320 * Now that the destination page is in the cache, mark it as colored.
4321 * If this was an exec page, discard it.
4322 */
4323 if (!pmap_is_page_colored_p(dst_pg)) {
4324 PMAPCOUNT(vac_color_new);
4325 dst_pg->mdpage.pvh_attrs |= PVF_COLORED;
4326 }
4327 if (PV_IS_EXEC_P(dst_pg->mdpage.pvh_attrs)) {
4328 dst_pg->mdpage.pvh_attrs &= ~PVF_EXEC;
4329 PMAPCOUNT(exec_discarded_copy);
4330 }
4331 #endif
4332 }
4333 #endif /* (ARM_MMU_GENERIC + ARM_MMU_SA1 + ARM_MMU_V6) != 0 */
4334
4335 #if ARM_MMU_XSCALE == 1
4336 void
4337 pmap_copy_page_xscale(paddr_t src, paddr_t dst)
4338 {
4339 struct vm_page *src_pg = PHYS_TO_VM_PAGE(src);
4340 #ifdef DEBUG
4341 struct vm_page *dst_pg = PHYS_TO_VM_PAGE(dst);
4342
4343 if (dst_pg->mdpage.pvh_list != NULL)
4344 panic("pmap_copy_page: dst page has mappings");
4345 #endif
4346
4347 KDASSERT((src & PGOFSET) == 0);
4348 KDASSERT((dst & PGOFSET) == 0);
4349
4350 /*
4351 * Clean the source page. Hold the source page's lock for
4352 * the duration of the copy so that no other mappings can
4353 * be created while we have a potentially aliased mapping.
4354 */
4355 simple_lock(&src_pg->mdpage.pvh_slock);
4356 #ifdef PMAP_CACHE_VIVT
4357 (void) pmap_clean_page(src_pg->mdpage.pvh_list, true);
4358 #endif
4359
4360 /*
4361 * Map the pages into the page hook points, copy them, and purge
4362 * the cache for the appropriate page. Invalidate the TLB
4363 * as required.
4364 */
4365 *csrc_pte = L2_S_PROTO | src |
4366 L2_S_PROT(PTE_KERNEL, VM_PROT_READ) |
4367 L2_C | L2_XS_T_TEX(TEX_XSCALE_X); /* mini-data */
4368 PTE_SYNC(csrc_pte);
4369 *cdst_pte = L2_S_PROTO | dst |
4370 L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE) |
4371 L2_C | L2_XS_T_TEX(TEX_XSCALE_X); /* mini-data */
4372 PTE_SYNC(cdst_pte);
4373 cpu_tlb_flushD_SE(csrcp);
4374 cpu_tlb_flushD_SE(cdstp);
4375 cpu_cpwait();
4376 bcopy_page(csrcp, cdstp);
4377 simple_unlock(&src_pg->mdpage.pvh_slock); /* cache is safe again */
4378 xscale_cache_clean_minidata();
4379 }
4380 #endif /* ARM_MMU_XSCALE == 1 */
4381
4382 /*
4383 * void pmap_virtual_space(vaddr_t *start, vaddr_t *end)
4384 *
4385 * Return the start and end addresses of the kernel's virtual space.
4386 * These values are setup in pmap_bootstrap and are updated as pages
4387 * are allocated.
4388 */
4389 void
4390 pmap_virtual_space(vaddr_t *start, vaddr_t *end)
4391 {
4392 *start = virtual_avail;
4393 *end = virtual_end;
4394 }
4395
4396 /*
4397 * Helper function for pmap_grow_l2_bucket()
4398 */
4399 static inline int
4400 pmap_grow_map(vaddr_t va, pt_entry_t cache_mode, paddr_t *pap)
4401 {
4402 struct l2_bucket *l2b;
4403 pt_entry_t *ptep;
4404 paddr_t pa;
4405
4406 if (uvm.page_init_done == false) {
4407 #ifdef PMAP_STEAL_MEMORY
4408 pv_addr_t pv;
4409 pmap_boot_pagealloc(PAGE_SIZE,
4410 #ifdef PMAP_CACHE_VIPT
4411 arm_cache_prefer_mask,
4412 va & arm_cache_prefer_mask,
4413 #else
4414 0, 0,
4415 #endif
4416 &pv);
4417 pa = pv.pv_pa;
4418 #else
4419 if (uvm_page_physget(&pa) == false)
4420 return (1);
4421 #endif /* PMAP_STEAL_MEMORY */
4422 } else {
4423 struct vm_page *pg;
4424 pg = uvm_pagealloc(NULL, 0, NULL, UVM_PGA_USERESERVE);
4425 if (pg == NULL)
4426 return (1);
4427 pa = VM_PAGE_TO_PHYS(pg);
4428 #ifdef PMAP_CACHE_VIPT
4429 /*
4430 * This new page must not have any mappings. However, it might
4431 * have previously used and therefore present in the cache. If
4432 * it doesn't have the desired color, we have to flush it from
4433 * the cache. And while we are at it, make sure to clear its
4434 * EXEC status.
4435 */
4436 KASSERT(!(pg->mdpage.pvh_attrs & PVF_KENTRY));
4437 KASSERT(pg->mdpage.pvh_list == NULL);
4438 if (pmap_is_page_colored_p(pg)) {
4439 if ((va ^ pg->mdpage.pvh_attrs) & arm_cache_prefer_mask) {
4440 pmap_flush_page(pg);
4441 PMAPCOUNT(vac_color_change);
4442 } else {
4443 PMAPCOUNT(vac_color_reuse);
4444 }
4445 } else {
4446 PMAPCOUNT(vac_color_new);
4447 }
4448 if (PV_IS_EXEC_P(pg->mdpage.pvh_attrs))
4449 PMAPCOUNT(exec_discarded_kremove);
4450 /*
4451 * We'll pretend this page was entered by pmap_kenter_pa
4452 */
4453 pg->mdpage.pvh_attrs &= (PAGE_SIZE - 1) & ~PVF_EXEC;
4454 pg->mdpage.pvh_attrs |= va | PVF_KENTRY | PVF_COLORED | PVF_REF | PVF_MOD;
4455 #endif
4456 }
4457
4458 if (pap)
4459 *pap = pa;
4460
4461 PMAPCOUNT(pt_mappings);
4462 l2b = pmap_get_l2_bucket(pmap_kernel(), va);
4463 KDASSERT(l2b != NULL);
4464
4465 ptep = &l2b->l2b_kva[l2pte_index(va)];
4466 *ptep = L2_S_PROTO | pa | cache_mode |
4467 L2_S_PROT(PTE_KERNEL, VM_PROT_READ | VM_PROT_WRITE);
4468 PTE_SYNC(ptep);
4469 memset((void *)va, 0, PAGE_SIZE);
4470 return (0);
4471 }
4472
4473 /*
4474 * This is the same as pmap_alloc_l2_bucket(), except that it is only
4475 * used by pmap_growkernel().
4476 */
4477 static inline struct l2_bucket *
4478 pmap_grow_l2_bucket(pmap_t pm, vaddr_t va)
4479 {
4480 struct l2_dtable *l2;
4481 struct l2_bucket *l2b;
4482 u_short l1idx;
4483 vaddr_t nva;
4484
4485 l1idx = L1_IDX(va);
4486
4487 if ((l2 = pm->pm_l2[L2_IDX(l1idx)]) == NULL) {
4488 /*
4489 * No mapping at this address, as there is
4490 * no entry in the L1 table.
4491 * Need to allocate a new l2_dtable.
4492 */
4493 nva = pmap_kernel_l2dtable_kva;
4494 if ((nva & PGOFSET) == 0) {
4495 /*
4496 * Need to allocate a backing page
4497 */
4498 if (pmap_grow_map(nva, pte_l2_s_cache_mode, NULL))
4499 return (NULL);
4500 }
4501
4502 l2 = (struct l2_dtable *)nva;
4503 nva += sizeof(struct l2_dtable);
4504
4505 if ((nva & PGOFSET) < (pmap_kernel_l2dtable_kva & PGOFSET)) {
4506 /*
4507 * The new l2_dtable straddles a page boundary.
4508 * Map in another page to cover it.
4509 */
4510 if (pmap_grow_map(nva, pte_l2_s_cache_mode, NULL))
4511 return (NULL);
4512 }
4513
4514 pmap_kernel_l2dtable_kva = nva;
4515
4516 /*
4517 * Link it into the parent pmap
4518 */
4519 pm->pm_l2[L2_IDX(l1idx)] = l2;
4520 }
4521
4522 l2b = &l2->l2_bucket[L2_BUCKET(l1idx)];
4523
4524 /*
4525 * Fetch pointer to the L2 page table associated with the address.
4526 */
4527 if (l2b->l2b_kva == NULL) {
4528 pt_entry_t *ptep;
4529
4530 /*
4531 * No L2 page table has been allocated. Chances are, this
4532 * is because we just allocated the l2_dtable, above.
4533 */
4534 nva = pmap_kernel_l2ptp_kva;
4535 ptep = (pt_entry_t *)nva;
4536 if ((nva & PGOFSET) == 0) {
4537 /*
4538 * Need to allocate a backing page
4539 */
4540 if (pmap_grow_map(nva, pte_l2_s_cache_mode_pt,
4541 &pmap_kernel_l2ptp_phys))
4542 return (NULL);
4543 PTE_SYNC_RANGE(ptep, PAGE_SIZE / sizeof(pt_entry_t));
4544 }
4545
4546 l2->l2_occupancy++;
4547 l2b->l2b_kva = ptep;
4548 l2b->l2b_l1idx = l1idx;
4549 l2b->l2b_phys = pmap_kernel_l2ptp_phys;
4550
4551 pmap_kernel_l2ptp_kva += L2_TABLE_SIZE_REAL;
4552 pmap_kernel_l2ptp_phys += L2_TABLE_SIZE_REAL;
4553 }
4554
4555 return (l2b);
4556 }
4557
4558 vaddr_t
4559 pmap_growkernel(vaddr_t maxkvaddr)
4560 {
4561 pmap_t kpm = pmap_kernel();
4562 struct l1_ttable *l1;
4563 struct l2_bucket *l2b;
4564 pd_entry_t *pl1pd;
4565 int s;
4566
4567 if (maxkvaddr <= pmap_curmaxkvaddr)
4568 goto out; /* we are OK */
4569
4570 NPDEBUG(PDB_GROWKERN,
4571 printf("pmap_growkernel: growing kernel from 0x%lx to 0x%lx\n",
4572 pmap_curmaxkvaddr, maxkvaddr));
4573
4574 KDASSERT(maxkvaddr <= virtual_end);
4575
4576 /*
4577 * whoops! we need to add kernel PTPs
4578 */
4579
4580 s = splhigh(); /* to be safe */
4581 mutex_enter(&kpm->pm_lock);
4582
4583 /* Map 1MB at a time */
4584 for (; pmap_curmaxkvaddr < maxkvaddr; pmap_curmaxkvaddr += L1_S_SIZE) {
4585
4586 l2b = pmap_grow_l2_bucket(kpm, pmap_curmaxkvaddr);
4587 KDASSERT(l2b != NULL);
4588
4589 /* Distribute new L1 entry to all other L1s */
4590 SLIST_FOREACH(l1, &l1_list, l1_link) {
4591 pl1pd = &l1->l1_kva[L1_IDX(pmap_curmaxkvaddr)];
4592 *pl1pd = l2b->l2b_phys | L1_C_DOM(PMAP_DOMAIN_KERNEL) |
4593 L1_C_PROTO;
4594 PTE_SYNC(pl1pd);
4595 }
4596 }
4597
4598 /*
4599 * flush out the cache, expensive but growkernel will happen so
4600 * rarely
4601 */
4602 cpu_dcache_wbinv_all();
4603 cpu_tlb_flushD();
4604 cpu_cpwait();
4605
4606 mutex_exit(&kpm->pm_lock);
4607 splx(s);
4608
4609 out:
4610 return (pmap_curmaxkvaddr);
4611 }
4612
4613 /************************ Utility routines ****************************/
4614
4615 /*
4616 * vector_page_setprot:
4617 *
4618 * Manipulate the protection of the vector page.
4619 */
4620 void
4621 vector_page_setprot(int prot)
4622 {
4623 struct l2_bucket *l2b;
4624 pt_entry_t *ptep;
4625
4626 l2b = pmap_get_l2_bucket(pmap_kernel(), vector_page);
4627 KDASSERT(l2b != NULL);
4628
4629 ptep = &l2b->l2b_kva[l2pte_index(vector_page)];
4630
4631 *ptep = (*ptep & ~L1_S_PROT_MASK) | L2_S_PROT(PTE_KERNEL, prot);
4632 PTE_SYNC(ptep);
4633 cpu_tlb_flushD_SE(vector_page);
4634 cpu_cpwait();
4635 }
4636
4637 /*
4638 * Fetch pointers to the PDE/PTE for the given pmap/VA pair.
4639 * Returns true if the mapping exists, else false.
4640 *
4641 * NOTE: This function is only used by a couple of arm-specific modules.
4642 * It is not safe to take any pmap locks here, since we could be right
4643 * in the middle of debugging the pmap anyway...
4644 *
4645 * It is possible for this routine to return false even though a valid
4646 * mapping does exist. This is because we don't lock, so the metadata
4647 * state may be inconsistent.
4648 *
4649 * NOTE: We can return a NULL *ptp in the case where the L1 pde is
4650 * a "section" mapping.
4651 */
4652 bool
4653 pmap_get_pde_pte(pmap_t pm, vaddr_t va, pd_entry_t **pdp, pt_entry_t **ptp)
4654 {
4655 struct l2_dtable *l2;
4656 pd_entry_t *pl1pd, l1pd;
4657 pt_entry_t *ptep;
4658 u_short l1idx;
4659
4660 if (pm->pm_l1 == NULL)
4661 return false;
4662
4663 l1idx = L1_IDX(va);
4664 *pdp = pl1pd = &pm->pm_l1->l1_kva[l1idx];
4665 l1pd = *pl1pd;
4666
4667 if (l1pte_section_p(l1pd)) {
4668 *ptp = NULL;
4669 return true;
4670 }
4671
4672 if (pm->pm_l2 == NULL)
4673 return false;
4674
4675 l2 = pm->pm_l2[L2_IDX(l1idx)];
4676
4677 if (l2 == NULL ||
4678 (ptep = l2->l2_bucket[L2_BUCKET(l1idx)].l2b_kva) == NULL) {
4679 return false;
4680 }
4681
4682 *ptp = &ptep[l2pte_index(va)];
4683 return true;
4684 }
4685
4686 bool
4687 pmap_get_pde(pmap_t pm, vaddr_t va, pd_entry_t **pdp)
4688 {
4689 u_short l1idx;
4690
4691 if (pm->pm_l1 == NULL)
4692 return false;
4693
4694 l1idx = L1_IDX(va);
4695 *pdp = &pm->pm_l1->l1_kva[l1idx];
4696
4697 return true;
4698 }
4699
4700 /************************ Bootstrapping routines ****************************/
4701
4702 static void
4703 pmap_init_l1(struct l1_ttable *l1, pd_entry_t *l1pt)
4704 {
4705 int i;
4706
4707 l1->l1_kva = l1pt;
4708 l1->l1_domain_use_count = 0;
4709 l1->l1_domain_first = 0;
4710
4711 for (i = 0; i < PMAP_DOMAINS; i++)
4712 l1->l1_domain_free[i] = i + 1;
4713
4714 /*
4715 * Copy the kernel's L1 entries to each new L1.
4716 */
4717 if (pmap_initialized)
4718 memcpy(l1pt, pmap_kernel()->pm_l1->l1_kva, L1_TABLE_SIZE);
4719
4720 if (pmap_extract(pmap_kernel(), (vaddr_t)l1pt,
4721 &l1->l1_physaddr) == false)
4722 panic("pmap_init_l1: can't get PA of L1 at %p", l1pt);
4723
4724 SLIST_INSERT_HEAD(&l1_list, l1, l1_link);
4725 TAILQ_INSERT_TAIL(&l1_lru_list, l1, l1_lru);
4726 }
4727
4728 /*
4729 * pmap_bootstrap() is called from the board-specific initarm() routine
4730 * once the kernel L1/L2 descriptors tables have been set up.
4731 *
4732 * This is a somewhat convoluted process since pmap bootstrap is, effectively,
4733 * spread over a number of disparate files/functions.
4734 *
4735 * We are passed the following parameters
4736 * - kernel_l1pt
4737 * This is a pointer to the base of the kernel's L1 translation table.
4738 * - vstart
4739 * 1MB-aligned start of managed kernel virtual memory.
4740 * - vend
4741 * 1MB-aligned end of managed kernel virtual memory.
4742 *
4743 * We use the first parameter to build the metadata (struct l1_ttable and
4744 * struct l2_dtable) necessary to track kernel mappings.
4745 */
4746 #define PMAP_STATIC_L2_SIZE 16
4747 void
4748 pmap_bootstrap(vaddr_t vstart, vaddr_t vend)
4749 {
4750 static struct l1_ttable static_l1;
4751 static struct l2_dtable static_l2[PMAP_STATIC_L2_SIZE];
4752 struct l1_ttable *l1 = &static_l1;
4753 struct l2_dtable *l2;
4754 struct l2_bucket *l2b;
4755 pd_entry_t *l1pt = (pd_entry_t *) kernel_l1pt.pv_va;
4756 pmap_t pm = pmap_kernel();
4757 pd_entry_t pde;
4758 pt_entry_t *ptep;
4759 paddr_t pa;
4760 vaddr_t va;
4761 vsize_t size;
4762 int nptes, l1idx, l2idx, l2next = 0;
4763
4764 /*
4765 * Initialise the kernel pmap object
4766 */
4767 pm->pm_l1 = l1;
4768 pm->pm_domain = PMAP_DOMAIN_KERNEL;
4769 pm->pm_activated = true;
4770 pm->pm_cstate.cs_all = PMAP_CACHE_STATE_ALL;
4771 UVM_OBJ_INIT(&pm->pm_obj, NULL, 1);
4772
4773 /*
4774 * Scan the L1 translation table created by initarm() and create
4775 * the required metadata for all valid mappings found in it.
4776 */
4777 for (l1idx = 0; l1idx < (L1_TABLE_SIZE / sizeof(pd_entry_t)); l1idx++) {
4778 pde = l1pt[l1idx];
4779
4780 /*
4781 * We're only interested in Coarse mappings.
4782 * pmap_extract() can deal with section mappings without
4783 * recourse to checking L2 metadata.
4784 */
4785 if ((pde & L1_TYPE_MASK) != L1_TYPE_C)
4786 continue;
4787
4788 /*
4789 * Lookup the KVA of this L2 descriptor table
4790 */
4791 pa = (paddr_t)(pde & L1_C_ADDR_MASK);
4792 ptep = (pt_entry_t *)kernel_pt_lookup(pa);
4793 if (ptep == NULL) {
4794 panic("pmap_bootstrap: No L2 for va 0x%x, pa 0x%lx",
4795 (u_int)l1idx << L1_S_SHIFT, pa);
4796 }
4797
4798 /*
4799 * Fetch the associated L2 metadata structure.
4800 * Allocate a new one if necessary.
4801 */
4802 if ((l2 = pm->pm_l2[L2_IDX(l1idx)]) == NULL) {
4803 if (l2next == PMAP_STATIC_L2_SIZE)
4804 panic("pmap_bootstrap: out of static L2s");
4805 pm->pm_l2[L2_IDX(l1idx)] = l2 = &static_l2[l2next++];
4806 }
4807
4808 /*
4809 * One more L1 slot tracked...
4810 */
4811 l2->l2_occupancy++;
4812
4813 /*
4814 * Fill in the details of the L2 descriptor in the
4815 * appropriate bucket.
4816 */
4817 l2b = &l2->l2_bucket[L2_BUCKET(l1idx)];
4818 l2b->l2b_kva = ptep;
4819 l2b->l2b_phys = pa;
4820 l2b->l2b_l1idx = l1idx;
4821
4822 /*
4823 * Establish an initial occupancy count for this descriptor
4824 */
4825 for (l2idx = 0;
4826 l2idx < (L2_TABLE_SIZE_REAL / sizeof(pt_entry_t));
4827 l2idx++) {
4828 if ((ptep[l2idx] & L2_TYPE_MASK) != L2_TYPE_INV) {
4829 l2b->l2b_occupancy++;
4830 }
4831 }
4832
4833 /*
4834 * Make sure the descriptor itself has the correct cache mode.
4835 * If not, fix it, but whine about the problem. Port-meisters
4836 * should consider this a clue to fix up their initarm()
4837 * function. :)
4838 */
4839 if (pmap_set_pt_cache_mode(l1pt, (vaddr_t)ptep)) {
4840 printf("pmap_bootstrap: WARNING! wrong cache mode for "
4841 "L2 pte @ %p\n", ptep);
4842 }
4843 }
4844
4845 /*
4846 * Ensure the primary (kernel) L1 has the correct cache mode for
4847 * a page table. Bitch if it is not correctly set.
4848 */
4849 for (va = (vaddr_t)l1pt;
4850 va < ((vaddr_t)l1pt + L1_TABLE_SIZE); va += PAGE_SIZE) {
4851 if (pmap_set_pt_cache_mode(l1pt, va))
4852 printf("pmap_bootstrap: WARNING! wrong cache mode for "
4853 "primary L1 @ 0x%lx\n", va);
4854 }
4855
4856 cpu_dcache_wbinv_all();
4857 cpu_tlb_flushID();
4858 cpu_cpwait();
4859
4860 /*
4861 * now we allocate the "special" VAs which are used for tmp mappings
4862 * by the pmap (and other modules). we allocate the VAs by advancing
4863 * virtual_avail (note that there are no pages mapped at these VAs).
4864 *
4865 * Managed KVM space start from wherever initarm() tells us.
4866 */
4867 virtual_avail = vstart;
4868 virtual_end = vend;
4869
4870 #ifdef PMAP_CACHE_VIPT
4871 /*
4872 * If we have a VIPT cache, we need one page/pte per possible alias
4873 * page so we won't violate cache aliasing rules.
4874 */
4875 virtual_avail = (virtual_avail + arm_cache_prefer_mask) & ~arm_cache_prefer_mask;
4876 nptes = (arm_cache_prefer_mask >> PGSHIFT) + 1;
4877 #else
4878 nptes = 1;
4879 #endif
4880 pmap_alloc_specials(&virtual_avail, nptes, &csrcp, &csrc_pte);
4881 pmap_set_pt_cache_mode(l1pt, (vaddr_t)csrc_pte);
4882 pmap_alloc_specials(&virtual_avail, nptes, &cdstp, &cdst_pte);
4883 pmap_set_pt_cache_mode(l1pt, (vaddr_t)cdst_pte);
4884 pmap_alloc_specials(&virtual_avail, 1, (void *)&memhook, NULL);
4885 pmap_alloc_specials(&virtual_avail, round_page(MSGBUFSIZE) / PAGE_SIZE,
4886 (void *)&msgbufaddr, NULL);
4887
4888 /*
4889 * Allocate a range of kernel virtual address space to be used
4890 * for L2 descriptor tables and metadata allocation in
4891 * pmap_growkernel().
4892 */
4893 size = ((virtual_end - pmap_curmaxkvaddr) + L1_S_OFFSET) / L1_S_SIZE;
4894 pmap_alloc_specials(&virtual_avail,
4895 round_page(size * L2_TABLE_SIZE_REAL) / PAGE_SIZE,
4896 &pmap_kernel_l2ptp_kva, NULL);
4897
4898 size = (size + (L2_BUCKET_SIZE - 1)) / L2_BUCKET_SIZE;
4899 pmap_alloc_specials(&virtual_avail,
4900 round_page(size * sizeof(struct l2_dtable)) / PAGE_SIZE,
4901 &pmap_kernel_l2dtable_kva, NULL);
4902
4903 /*
4904 * init the static-global locks and global pmap list.
4905 */
4906 /* spinlockinit(&pmap_main_lock, "pmaplk", 0); */
4907
4908 /*
4909 * We can now initialise the first L1's metadata.
4910 */
4911 SLIST_INIT(&l1_list);
4912 TAILQ_INIT(&l1_lru_list);
4913 simple_lock_init(&l1_lru_lock);
4914 pmap_init_l1(l1, l1pt);
4915
4916 /* Set up vector page L1 details, if necessary */
4917 if (vector_page < KERNEL_BASE) {
4918 pm->pm_pl1vec = &pm->pm_l1->l1_kva[L1_IDX(vector_page)];
4919 l2b = pmap_get_l2_bucket(pm, vector_page);
4920 pm->pm_l1vec = l2b->l2b_phys | L1_C_PROTO |
4921 L1_C_DOM(pm->pm_domain);
4922 } else
4923 pm->pm_pl1vec = NULL;
4924
4925 /*
4926 * Initialize the pmap cache
4927 */
4928 pool_cache_bootstrap(&pmap_cache, sizeof(struct pmap), 0, 0, 0,
4929 "pmappl", NULL, IPL_NONE, pmap_pmap_ctor, NULL, NULL);
4930 LIST_INIT(&pmap_pmaps);
4931 LIST_INSERT_HEAD(&pmap_pmaps, pm, pm_list);
4932
4933 /*
4934 * Initialize the pv pool.
4935 */
4936 pool_init(&pmap_pv_pool, sizeof(struct pv_entry), 0, 0, 0, "pvepl",
4937 &pmap_bootstrap_pv_allocator, IPL_NONE);
4938
4939 /*
4940 * Initialize the L2 dtable pool and cache.
4941 */
4942 pool_cache_bootstrap(&pmap_l2dtable_cache, sizeof(struct l2_dtable), 0,
4943 0, 0, "l2dtblpl", NULL, IPL_NONE, pmap_l2dtable_ctor, NULL, NULL);
4944
4945 /*
4946 * Initialise the L2 descriptor table pool and cache
4947 */
4948 pool_cache_bootstrap(&pmap_l2ptp_cache, L2_TABLE_SIZE_REAL, 0,
4949 L2_TABLE_SIZE_REAL, 0, "l2ptppl", NULL, IPL_NONE,
4950 pmap_l2ptp_ctor, NULL, NULL);
4951
4952 cpu_dcache_wbinv_all();
4953 }
4954
4955 static int
4956 pmap_set_pt_cache_mode(pd_entry_t *kl1, vaddr_t va)
4957 {
4958 pd_entry_t *pdep, pde;
4959 pt_entry_t *ptep, pte;
4960 vaddr_t pa;
4961 int rv = 0;
4962
4963 /*
4964 * Make sure the descriptor itself has the correct cache mode
4965 */
4966 pdep = &kl1[L1_IDX(va)];
4967 pde = *pdep;
4968
4969 if (l1pte_section_p(pde)) {
4970 if ((pde & L1_S_CACHE_MASK) != pte_l1_s_cache_mode_pt) {
4971 *pdep = (pde & ~L1_S_CACHE_MASK) |
4972 pte_l1_s_cache_mode_pt;
4973 PTE_SYNC(pdep);
4974 cpu_dcache_wbinv_range((vaddr_t)pdep, sizeof(*pdep));
4975 rv = 1;
4976 }
4977 } else {
4978 pa = (paddr_t)(pde & L1_C_ADDR_MASK);
4979 ptep = (pt_entry_t *)kernel_pt_lookup(pa);
4980 if (ptep == NULL)
4981 panic("pmap_bootstrap: No L2 for L2 @ va %p\n", ptep);
4982
4983 ptep = &ptep[l2pte_index(va)];
4984 pte = *ptep;
4985 if ((pte & L2_S_CACHE_MASK) != pte_l2_s_cache_mode_pt) {
4986 *ptep = (pte & ~L2_S_CACHE_MASK) |
4987 pte_l2_s_cache_mode_pt;
4988 PTE_SYNC(ptep);
4989 cpu_dcache_wbinv_range((vaddr_t)ptep, sizeof(*ptep));
4990 rv = 1;
4991 }
4992 }
4993
4994 return (rv);
4995 }
4996
4997 static void
4998 pmap_alloc_specials(vaddr_t *availp, int pages, vaddr_t *vap, pt_entry_t **ptep)
4999 {
5000 vaddr_t va = *availp;
5001 struct l2_bucket *l2b;
5002
5003 if (ptep) {
5004 l2b = pmap_get_l2_bucket(pmap_kernel(), va);
5005 if (l2b == NULL)
5006 panic("pmap_alloc_specials: no l2b for 0x%lx", va);
5007
5008 if (ptep)
5009 *ptep = &l2b->l2b_kva[l2pte_index(va)];
5010 }
5011
5012 *vap = va;
5013 *availp = va + (PAGE_SIZE * pages);
5014 }
5015
5016 void
5017 pmap_init(void)
5018 {
5019 extern int physmem;
5020
5021 /*
5022 * Set the available memory vars - These do not map to real memory
5023 * addresses and cannot as the physical memory is fragmented.
5024 * They are used by ps for %mem calculations.
5025 * One could argue whether this should be the entire memory or just
5026 * the memory that is useable in a user process.
5027 */
5028 avail_start = 0;
5029 avail_end = physmem * PAGE_SIZE;
5030
5031 /*
5032 * Now we need to free enough pv_entry structures to allow us to get
5033 * the kmem_map/kmem_object allocated and inited (done after this
5034 * function is finished). to do this we allocate one bootstrap page out
5035 * of kernel_map and use it to provide an initial pool of pv_entry
5036 * structures. we never free this page.
5037 */
5038 pool_setlowat(&pmap_pv_pool,
5039 (PAGE_SIZE / sizeof(struct pv_entry)) * 2);
5040
5041 pmap_initialized = true;
5042 }
5043
5044 static vaddr_t last_bootstrap_page = 0;
5045 static void *free_bootstrap_pages = NULL;
5046
5047 static void *
5048 pmap_bootstrap_pv_page_alloc(struct pool *pp, int flags)
5049 {
5050 extern void *pool_page_alloc(struct pool *, int);
5051 vaddr_t new_page;
5052 void *rv;
5053
5054 if (pmap_initialized)
5055 return (pool_page_alloc(pp, flags));
5056
5057 if (free_bootstrap_pages) {
5058 rv = free_bootstrap_pages;
5059 free_bootstrap_pages = *((void **)rv);
5060 return (rv);
5061 }
5062
5063 new_page = uvm_km_alloc(kernel_map, PAGE_SIZE, 0,
5064 UVM_KMF_WIRED | ((flags & PR_WAITOK) ? 0 : UVM_KMF_NOWAIT));
5065
5066 KASSERT(new_page > last_bootstrap_page);
5067 last_bootstrap_page = new_page;
5068 return ((void *)new_page);
5069 }
5070
5071 static void
5072 pmap_bootstrap_pv_page_free(struct pool *pp, void *v)
5073 {
5074 extern void pool_page_free(struct pool *, void *);
5075
5076 if ((vaddr_t)v <= last_bootstrap_page) {
5077 *((void **)v) = free_bootstrap_pages;
5078 free_bootstrap_pages = v;
5079 return;
5080 }
5081
5082 if (pmap_initialized) {
5083 pool_page_free(pp, v);
5084 return;
5085 }
5086 }
5087
5088 /*
5089 * pmap_postinit()
5090 *
5091 * This routine is called after the vm and kmem subsystems have been
5092 * initialised. This allows the pmap code to perform any initialisation
5093 * that can only be done one the memory allocation is in place.
5094 */
5095 void
5096 pmap_postinit(void)
5097 {
5098 extern paddr_t physical_start, physical_end;
5099 struct l2_bucket *l2b;
5100 struct l1_ttable *l1;
5101 struct pglist plist;
5102 struct vm_page *m;
5103 pd_entry_t *pl1pt;
5104 pt_entry_t *ptep, pte;
5105 vaddr_t va, eva;
5106 u_int loop, needed;
5107 int error;
5108
5109 pool_cache_setlowat(&pmap_l2ptp_cache,
5110 (PAGE_SIZE / L2_TABLE_SIZE_REAL) * 4);
5111 pool_cache_setlowat(&pmap_l2dtable_cache,
5112 (PAGE_SIZE / sizeof(struct l2_dtable)) * 2);
5113
5114 needed = (maxproc / PMAP_DOMAINS) + ((maxproc % PMAP_DOMAINS) ? 1 : 0);
5115 needed -= 1;
5116
5117 l1 = malloc(sizeof(*l1) * needed, M_VMPMAP, M_WAITOK);
5118
5119 for (loop = 0; loop < needed; loop++, l1++) {
5120 /* Allocate a L1 page table */
5121 va = uvm_km_alloc(kernel_map, L1_TABLE_SIZE, 0, UVM_KMF_VAONLY);
5122 if (va == 0)
5123 panic("Cannot allocate L1 KVM");
5124
5125 error = uvm_pglistalloc(L1_TABLE_SIZE, physical_start,
5126 physical_end, L1_TABLE_SIZE, 0, &plist, 1, M_WAITOK);
5127 if (error)
5128 panic("Cannot allocate L1 physical pages");
5129
5130 m = TAILQ_FIRST(&plist);
5131 eva = va + L1_TABLE_SIZE;
5132 pl1pt = (pd_entry_t *)va;
5133
5134 while (m && va < eva) {
5135 paddr_t pa = VM_PAGE_TO_PHYS(m);
5136
5137 pmap_kenter_pa(va, pa, VM_PROT_READ | VM_PROT_WRITE);
5138
5139 /*
5140 * Make sure the L1 descriptor table is mapped
5141 * with the cache-mode set to write-through.
5142 */
5143 l2b = pmap_get_l2_bucket(pmap_kernel(), va);
5144 ptep = &l2b->l2b_kva[l2pte_index(va)];
5145 pte = *ptep;
5146 pte = (pte & ~L2_S_CACHE_MASK) | pte_l2_s_cache_mode_pt;
5147 *ptep = pte;
5148 PTE_SYNC(ptep);
5149 cpu_tlb_flushD_SE(va);
5150
5151 va += PAGE_SIZE;
5152 m = TAILQ_NEXT(m, pageq.queue);
5153 }
5154
5155 #ifdef DIAGNOSTIC
5156 if (m)
5157 panic("pmap_alloc_l1pt: pglist not empty");
5158 #endif /* DIAGNOSTIC */
5159
5160 pmap_init_l1(l1, pl1pt);
5161 }
5162
5163 #ifdef DEBUG
5164 printf("pmap_postinit: Allocated %d static L1 descriptor tables\n",
5165 needed);
5166 #endif
5167 }
5168
5169 /*
5170 * Note that the following routines are used by board-specific initialisation
5171 * code to configure the initial kernel page tables.
5172 *
5173 * If ARM32_NEW_VM_LAYOUT is *not* defined, they operate on the assumption that
5174 * L2 page-table pages are 4KB in size and use 4 L1 slots. This mimics the
5175 * behaviour of the old pmap, and provides an easy migration path for
5176 * initial bring-up of the new pmap on existing ports. Fortunately,
5177 * pmap_bootstrap() compensates for this hackery. This is only a stop-gap and
5178 * will be deprecated.
5179 *
5180 * If ARM32_NEW_VM_LAYOUT *is* defined, these functions deal with 1KB L2 page
5181 * tables.
5182 */
5183
5184 /*
5185 * This list exists for the benefit of pmap_map_chunk(). It keeps track
5186 * of the kernel L2 tables during bootstrap, so that pmap_map_chunk() can
5187 * find them as necessary.
5188 *
5189 * Note that the data on this list MUST remain valid after initarm() returns,
5190 * as pmap_bootstrap() uses it to contruct L2 table metadata.
5191 */
5192 SLIST_HEAD(, pv_addr) kernel_pt_list = SLIST_HEAD_INITIALIZER(kernel_pt_list);
5193
5194 static vaddr_t
5195 kernel_pt_lookup(paddr_t pa)
5196 {
5197 pv_addr_t *pv;
5198
5199 SLIST_FOREACH(pv, &kernel_pt_list, pv_list) {
5200 #ifndef ARM32_NEW_VM_LAYOUT
5201 if (pv->pv_pa == (pa & ~PGOFSET))
5202 return (pv->pv_va | (pa & PGOFSET));
5203 #else
5204 if (pv->pv_pa == pa)
5205 return (pv->pv_va);
5206 #endif
5207 }
5208 return (0);
5209 }
5210
5211 /*
5212 * pmap_map_section:
5213 *
5214 * Create a single section mapping.
5215 */
5216 void
5217 pmap_map_section(vaddr_t l1pt, vaddr_t va, paddr_t pa, int prot, int cache)
5218 {
5219 pd_entry_t *pde = (pd_entry_t *) l1pt;
5220 pd_entry_t fl;
5221
5222 KASSERT(((va | pa) & L1_S_OFFSET) == 0);
5223
5224 switch (cache) {
5225 case PTE_NOCACHE:
5226 default:
5227 fl = 0;
5228 break;
5229
5230 case PTE_CACHE:
5231 fl = pte_l1_s_cache_mode;
5232 break;
5233
5234 case PTE_PAGETABLE:
5235 fl = pte_l1_s_cache_mode_pt;
5236 break;
5237 }
5238
5239 pde[va >> L1_S_SHIFT] = L1_S_PROTO | pa |
5240 L1_S_PROT(PTE_KERNEL, prot) | fl | L1_S_DOM(PMAP_DOMAIN_KERNEL);
5241 PTE_SYNC(&pde[va >> L1_S_SHIFT]);
5242 }
5243
5244 /*
5245 * pmap_map_entry:
5246 *
5247 * Create a single page mapping.
5248 */
5249 void
5250 pmap_map_entry(vaddr_t l1pt, vaddr_t va, paddr_t pa, int prot, int cache)
5251 {
5252 pd_entry_t *pde = (pd_entry_t *) l1pt;
5253 pt_entry_t fl;
5254 pt_entry_t *pte;
5255
5256 KASSERT(((va | pa) & PGOFSET) == 0);
5257
5258 switch (cache) {
5259 case PTE_NOCACHE:
5260 default:
5261 fl = 0;
5262 break;
5263
5264 case PTE_CACHE:
5265 fl = pte_l2_s_cache_mode;
5266 break;
5267
5268 case PTE_PAGETABLE:
5269 fl = pte_l2_s_cache_mode_pt;
5270 break;
5271 }
5272
5273 if ((pde[va >> L1_S_SHIFT] & L1_TYPE_MASK) != L1_TYPE_C)
5274 panic("pmap_map_entry: no L2 table for VA 0x%08lx", va);
5275
5276 #ifndef ARM32_NEW_VM_LAYOUT
5277 pte = (pt_entry_t *)
5278 kernel_pt_lookup(pde[va >> L1_S_SHIFT] & L2_S_FRAME);
5279 #else
5280 pte = (pt_entry_t *) kernel_pt_lookup(pde[L1_IDX(va)] & L1_C_ADDR_MASK);
5281 #endif
5282 if (pte == NULL)
5283 panic("pmap_map_entry: can't find L2 table for VA 0x%08lx", va);
5284
5285 fl |= L2_S_PROTO | pa | L2_S_PROT(PTE_KERNEL, prot);
5286 #ifndef ARM32_NEW_VM_LAYOUT
5287 pte += (va >> PGSHIFT) & 0x3ff;
5288 #else
5289 pte += l2pte_index(va);
5290 L2_S_PROTO | pa | L2_S_PROT(PTE_KERNEL, prot) | fl;
5291 #endif
5292 *pte = fl;
5293 PTE_SYNC(pte);
5294 }
5295
5296 /*
5297 * pmap_link_l2pt:
5298 *
5299 * Link the L2 page table specified by "l2pv" into the L1
5300 * page table at the slot for "va".
5301 */
5302 void
5303 pmap_link_l2pt(vaddr_t l1pt, vaddr_t va, pv_addr_t *l2pv)
5304 {
5305 pd_entry_t *pde = (pd_entry_t *) l1pt, proto;
5306 u_int slot = va >> L1_S_SHIFT;
5307
5308 #ifndef ARM32_NEW_VM_LAYOUT
5309 KASSERT((va & ((L1_S_SIZE * 4) - 1)) == 0);
5310 KASSERT((l2pv->pv_pa & PGOFSET) == 0);
5311 #endif
5312
5313 proto = L1_S_DOM(PMAP_DOMAIN_KERNEL) | L1_C_PROTO;
5314
5315 pde[slot + 0] = proto | (l2pv->pv_pa + 0x000);
5316 #ifdef ARM32_NEW_VM_LAYOUT
5317 PTE_SYNC(&pde[slot]);
5318 #else
5319 pde[slot + 1] = proto | (l2pv->pv_pa + 0x400);
5320 pde[slot + 2] = proto | (l2pv->pv_pa + 0x800);
5321 pde[slot + 3] = proto | (l2pv->pv_pa + 0xc00);
5322 PTE_SYNC_RANGE(&pde[slot + 0], 4);
5323 #endif
5324
5325 SLIST_INSERT_HEAD(&kernel_pt_list, l2pv, pv_list);
5326 }
5327
5328 /*
5329 * pmap_map_chunk:
5330 *
5331 * Map a chunk of memory using the most efficient mappings
5332 * possible (section, large page, small page) into the
5333 * provided L1 and L2 tables at the specified virtual address.
5334 */
5335 vsize_t
5336 pmap_map_chunk(vaddr_t l1pt, vaddr_t va, paddr_t pa, vsize_t size,
5337 int prot, int cache)
5338 {
5339 pd_entry_t *pde = (pd_entry_t *) l1pt;
5340 pt_entry_t *pte, f1, f2s, f2l;
5341 vsize_t resid;
5342 int i;
5343
5344 resid = (size + (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
5345
5346 if (l1pt == 0)
5347 panic("pmap_map_chunk: no L1 table provided");
5348
5349 #ifdef VERBOSE_INIT_ARM
5350 printf("pmap_map_chunk: pa=0x%lx va=0x%lx size=0x%lx resid=0x%lx "
5351 "prot=0x%x cache=%d\n", pa, va, size, resid, prot, cache);
5352 #endif
5353
5354 switch (cache) {
5355 case PTE_NOCACHE:
5356 default:
5357 f1 = 0;
5358 f2l = 0;
5359 f2s = 0;
5360 break;
5361
5362 case PTE_CACHE:
5363 f1 = pte_l1_s_cache_mode;
5364 f2l = pte_l2_l_cache_mode;
5365 f2s = pte_l2_s_cache_mode;
5366 break;
5367
5368 case PTE_PAGETABLE:
5369 f1 = pte_l1_s_cache_mode_pt;
5370 f2l = pte_l2_l_cache_mode_pt;
5371 f2s = pte_l2_s_cache_mode_pt;
5372 break;
5373 }
5374
5375 size = resid;
5376
5377 while (resid > 0) {
5378 /* See if we can use a section mapping. */
5379 if (L1_S_MAPPABLE_P(va, pa, resid)) {
5380 #ifdef VERBOSE_INIT_ARM
5381 printf("S");
5382 #endif
5383 pde[va >> L1_S_SHIFT] = L1_S_PROTO | pa |
5384 L1_S_PROT(PTE_KERNEL, prot) | f1 |
5385 L1_S_DOM(PMAP_DOMAIN_KERNEL);
5386 PTE_SYNC(&pde[va >> L1_S_SHIFT]);
5387 va += L1_S_SIZE;
5388 pa += L1_S_SIZE;
5389 resid -= L1_S_SIZE;
5390 continue;
5391 }
5392
5393 /*
5394 * Ok, we're going to use an L2 table. Make sure
5395 * one is actually in the corresponding L1 slot
5396 * for the current VA.
5397 */
5398 if ((pde[va >> L1_S_SHIFT] & L1_TYPE_MASK) != L1_TYPE_C)
5399 panic("pmap_map_chunk: no L2 table for VA 0x%08lx", va);
5400
5401 #ifndef ARM32_NEW_VM_LAYOUT
5402 pte = (pt_entry_t *)
5403 kernel_pt_lookup(pde[va >> L1_S_SHIFT] & L2_S_FRAME);
5404 #else
5405 pte = (pt_entry_t *) kernel_pt_lookup(
5406 pde[L1_IDX(va)] & L1_C_ADDR_MASK);
5407 #endif
5408 if (pte == NULL)
5409 panic("pmap_map_chunk: can't find L2 table for VA"
5410 "0x%08lx", va);
5411
5412 /* See if we can use a L2 large page mapping. */
5413 if (L2_L_MAPPABLE_P(va, pa, resid)) {
5414 #ifdef VERBOSE_INIT_ARM
5415 printf("L");
5416 #endif
5417 for (i = 0; i < 16; i++) {
5418 #ifndef ARM32_NEW_VM_LAYOUT
5419 pte[((va >> PGSHIFT) & 0x3f0) + i] =
5420 L2_L_PROTO | pa |
5421 L2_L_PROT(PTE_KERNEL, prot) | f2l;
5422 PTE_SYNC(&pte[((va >> PGSHIFT) & 0x3f0) + i]);
5423 #else
5424 pte[l2pte_index(va) + i] =
5425 L2_L_PROTO | pa |
5426 L2_L_PROT(PTE_KERNEL, prot) | f2l;
5427 PTE_SYNC(&pte[l2pte_index(va) + i]);
5428 #endif
5429 }
5430 va += L2_L_SIZE;
5431 pa += L2_L_SIZE;
5432 resid -= L2_L_SIZE;
5433 continue;
5434 }
5435
5436 /* Use a small page mapping. */
5437 #ifdef VERBOSE_INIT_ARM
5438 printf("P");
5439 #endif
5440 #ifndef ARM32_NEW_VM_LAYOUT
5441 pte[(va >> PGSHIFT) & 0x3ff] =
5442 L2_S_PROTO | pa | L2_S_PROT(PTE_KERNEL, prot) | f2s;
5443 PTE_SYNC(&pte[(va >> PGSHIFT) & 0x3ff]);
5444 #else
5445 pte[l2pte_index(va)] =
5446 L2_S_PROTO | pa | L2_S_PROT(PTE_KERNEL, prot) | f2s;
5447 PTE_SYNC(&pte[l2pte_index(va)]);
5448 #endif
5449 va += PAGE_SIZE;
5450 pa += PAGE_SIZE;
5451 resid -= PAGE_SIZE;
5452 }
5453 #ifdef VERBOSE_INIT_ARM
5454 printf("\n");
5455 #endif
5456 return (size);
5457 }
5458
5459 /********************** Static device map routines ***************************/
5460
5461 static const struct pmap_devmap *pmap_devmap_table;
5462
5463 /*
5464 * Register the devmap table. This is provided in case early console
5465 * initialization needs to register mappings created by bootstrap code
5466 * before pmap_devmap_bootstrap() is called.
5467 */
5468 void
5469 pmap_devmap_register(const struct pmap_devmap *table)
5470 {
5471
5472 pmap_devmap_table = table;
5473 }
5474
5475 /*
5476 * Map all of the static regions in the devmap table, and remember
5477 * the devmap table so other parts of the kernel can look up entries
5478 * later.
5479 */
5480 void
5481 pmap_devmap_bootstrap(vaddr_t l1pt, const struct pmap_devmap *table)
5482 {
5483 int i;
5484
5485 pmap_devmap_table = table;
5486
5487 for (i = 0; pmap_devmap_table[i].pd_size != 0; i++) {
5488 #ifdef VERBOSE_INIT_ARM
5489 printf("devmap: %08lx -> %08lx @ %08lx\n",
5490 pmap_devmap_table[i].pd_pa,
5491 pmap_devmap_table[i].pd_pa +
5492 pmap_devmap_table[i].pd_size - 1,
5493 pmap_devmap_table[i].pd_va);
5494 #endif
5495 pmap_map_chunk(l1pt, pmap_devmap_table[i].pd_va,
5496 pmap_devmap_table[i].pd_pa,
5497 pmap_devmap_table[i].pd_size,
5498 pmap_devmap_table[i].pd_prot,
5499 pmap_devmap_table[i].pd_cache);
5500 }
5501 }
5502
5503 const struct pmap_devmap *
5504 pmap_devmap_find_pa(paddr_t pa, psize_t size)
5505 {
5506 uint64_t endpa;
5507 int i;
5508
5509 if (pmap_devmap_table == NULL)
5510 return (NULL);
5511
5512 endpa = (uint64_t)pa + (uint64_t)(size - 1);
5513
5514 for (i = 0; pmap_devmap_table[i].pd_size != 0; i++) {
5515 if (pa >= pmap_devmap_table[i].pd_pa &&
5516 endpa <= (uint64_t)pmap_devmap_table[i].pd_pa +
5517 (uint64_t)(pmap_devmap_table[i].pd_size - 1))
5518 return (&pmap_devmap_table[i]);
5519 }
5520
5521 return (NULL);
5522 }
5523
5524 const struct pmap_devmap *
5525 pmap_devmap_find_va(vaddr_t va, vsize_t size)
5526 {
5527 int i;
5528
5529 if (pmap_devmap_table == NULL)
5530 return (NULL);
5531
5532 for (i = 0; pmap_devmap_table[i].pd_size != 0; i++) {
5533 if (va >= pmap_devmap_table[i].pd_va &&
5534 va + size - 1 <= pmap_devmap_table[i].pd_va +
5535 pmap_devmap_table[i].pd_size - 1)
5536 return (&pmap_devmap_table[i]);
5537 }
5538
5539 return (NULL);
5540 }
5541
5542 /********************** PTE initialization routines **************************/
5543
5544 /*
5545 * These routines are called when the CPU type is identified to set up
5546 * the PTE prototypes, cache modes, etc.
5547 *
5548 * The variables are always here, just in case LKMs need to reference
5549 * them (though, they shouldn't).
5550 */
5551
5552 pt_entry_t pte_l1_s_cache_mode;
5553 pt_entry_t pte_l1_s_cache_mode_pt;
5554 pt_entry_t pte_l1_s_cache_mask;
5555
5556 pt_entry_t pte_l2_l_cache_mode;
5557 pt_entry_t pte_l2_l_cache_mode_pt;
5558 pt_entry_t pte_l2_l_cache_mask;
5559
5560 pt_entry_t pte_l2_s_cache_mode;
5561 pt_entry_t pte_l2_s_cache_mode_pt;
5562 pt_entry_t pte_l2_s_cache_mask;
5563
5564 pt_entry_t pte_l2_s_prot_u;
5565 pt_entry_t pte_l2_s_prot_w;
5566 pt_entry_t pte_l2_s_prot_mask;
5567
5568 pt_entry_t pte_l1_s_proto;
5569 pt_entry_t pte_l1_c_proto;
5570 pt_entry_t pte_l2_s_proto;
5571
5572 void (*pmap_copy_page_func)(paddr_t, paddr_t);
5573 void (*pmap_zero_page_func)(paddr_t);
5574
5575 #if (ARM_MMU_GENERIC + ARM_MMU_SA1 + ARM_MMU_V6) != 0
5576 void
5577 pmap_pte_init_generic(void)
5578 {
5579
5580 pte_l1_s_cache_mode = L1_S_B|L1_S_C;
5581 pte_l1_s_cache_mask = L1_S_CACHE_MASK_generic;
5582
5583 pte_l2_l_cache_mode = L2_B|L2_C;
5584 pte_l2_l_cache_mask = L2_L_CACHE_MASK_generic;
5585
5586 pte_l2_s_cache_mode = L2_B|L2_C;
5587 pte_l2_s_cache_mask = L2_S_CACHE_MASK_generic;
5588
5589 /*
5590 * If we have a write-through cache, set B and C. If
5591 * we have a write-back cache, then we assume setting
5592 * only C will make those pages write-through.
5593 */
5594 if (cpufuncs.cf_dcache_wb_range == (void *) cpufunc_nullop) {
5595 pte_l1_s_cache_mode_pt = L1_S_B|L1_S_C;
5596 pte_l2_l_cache_mode_pt = L2_B|L2_C;
5597 pte_l2_s_cache_mode_pt = L2_B|L2_C;
5598 } else {
5599 #if ARM_MMU_V6 > 1
5600 pte_l1_s_cache_mode_pt = L1_S_B|L1_S_C; /* arm116 errata 399234 */
5601 pte_l2_l_cache_mode_pt = L2_B|L2_C; /* arm116 errata 399234 */
5602 pte_l2_s_cache_mode_pt = L2_B|L2_C; /* arm116 errata 399234 */
5603 #else
5604 pte_l1_s_cache_mode_pt = L1_S_C;
5605 pte_l2_l_cache_mode_pt = L2_C;
5606 pte_l2_s_cache_mode_pt = L2_C;
5607 #endif
5608 }
5609
5610 pte_l2_s_prot_u = L2_S_PROT_U_generic;
5611 pte_l2_s_prot_w = L2_S_PROT_W_generic;
5612 pte_l2_s_prot_mask = L2_S_PROT_MASK_generic;
5613
5614 pte_l1_s_proto = L1_S_PROTO_generic;
5615 pte_l1_c_proto = L1_C_PROTO_generic;
5616 pte_l2_s_proto = L2_S_PROTO_generic;
5617
5618 pmap_copy_page_func = pmap_copy_page_generic;
5619 pmap_zero_page_func = pmap_zero_page_generic;
5620 }
5621
5622 #if defined(CPU_ARM8)
5623 void
5624 pmap_pte_init_arm8(void)
5625 {
5626
5627 /*
5628 * ARM8 is compatible with generic, but we need to use
5629 * the page tables uncached.
5630 */
5631 pmap_pte_init_generic();
5632
5633 pte_l1_s_cache_mode_pt = 0;
5634 pte_l2_l_cache_mode_pt = 0;
5635 pte_l2_s_cache_mode_pt = 0;
5636 }
5637 #endif /* CPU_ARM8 */
5638
5639 #if defined(CPU_ARM9) && defined(ARM9_CACHE_WRITE_THROUGH)
5640 void
5641 pmap_pte_init_arm9(void)
5642 {
5643
5644 /*
5645 * ARM9 is compatible with generic, but we want to use
5646 * write-through caching for now.
5647 */
5648 pmap_pte_init_generic();
5649
5650 pte_l1_s_cache_mode = L1_S_C;
5651 pte_l2_l_cache_mode = L2_C;
5652 pte_l2_s_cache_mode = L2_C;
5653
5654 pte_l1_s_cache_mode_pt = L1_S_C;
5655 pte_l2_l_cache_mode_pt = L2_C;
5656 pte_l2_s_cache_mode_pt = L2_C;
5657 }
5658 #endif /* CPU_ARM9 */
5659 #endif /* (ARM_MMU_GENERIC + ARM_MMU_SA1 + ARM_MMU_V6) != 0 */
5660
5661 #if defined(CPU_ARM10)
5662 void
5663 pmap_pte_init_arm10(void)
5664 {
5665
5666 /*
5667 * ARM10 is compatible with generic, but we want to use
5668 * write-through caching for now.
5669 */
5670 pmap_pte_init_generic();
5671
5672 pte_l1_s_cache_mode = L1_S_B | L1_S_C;
5673 pte_l2_l_cache_mode = L2_B | L2_C;
5674 pte_l2_s_cache_mode = L2_B | L2_C;
5675
5676 pte_l1_s_cache_mode_pt = L1_S_C;
5677 pte_l2_l_cache_mode_pt = L2_C;
5678 pte_l2_s_cache_mode_pt = L2_C;
5679
5680 }
5681 #endif /* CPU_ARM10 */
5682
5683 #if ARM_MMU_SA1 == 1
5684 void
5685 pmap_pte_init_sa1(void)
5686 {
5687
5688 /*
5689 * The StrongARM SA-1 cache does not have a write-through
5690 * mode. So, do the generic initialization, then reset
5691 * the page table cache mode to B=1,C=1, and note that
5692 * the PTEs need to be sync'd.
5693 */
5694 pmap_pte_init_generic();
5695
5696 pte_l1_s_cache_mode_pt = L1_S_B|L1_S_C;
5697 pte_l2_l_cache_mode_pt = L2_B|L2_C;
5698 pte_l2_s_cache_mode_pt = L2_B|L2_C;
5699
5700 pmap_needs_pte_sync = 1;
5701 }
5702 #endif /* ARM_MMU_SA1 == 1*/
5703
5704 #if ARM_MMU_XSCALE == 1
5705 #if (ARM_NMMUS > 1)
5706 static u_int xscale_use_minidata;
5707 #endif
5708
5709 void
5710 pmap_pte_init_xscale(void)
5711 {
5712 uint32_t auxctl;
5713 int write_through = 0;
5714
5715 pte_l1_s_cache_mode = L1_S_B|L1_S_C;
5716 pte_l1_s_cache_mask = L1_S_CACHE_MASK_xscale;
5717
5718 pte_l2_l_cache_mode = L2_B|L2_C;
5719 pte_l2_l_cache_mask = L2_L_CACHE_MASK_xscale;
5720
5721 pte_l2_s_cache_mode = L2_B|L2_C;
5722 pte_l2_s_cache_mask = L2_S_CACHE_MASK_xscale;
5723
5724 pte_l1_s_cache_mode_pt = L1_S_C;
5725 pte_l2_l_cache_mode_pt = L2_C;
5726 pte_l2_s_cache_mode_pt = L2_C;
5727
5728 #ifdef XSCALE_CACHE_READ_WRITE_ALLOCATE
5729 /*
5730 * The XScale core has an enhanced mode where writes that
5731 * miss the cache cause a cache line to be allocated. This
5732 * is significantly faster than the traditional, write-through
5733 * behavior of this case.
5734 */
5735 pte_l1_s_cache_mode |= L1_S_XS_TEX(TEX_XSCALE_X);
5736 pte_l2_l_cache_mode |= L2_XS_L_TEX(TEX_XSCALE_X);
5737 pte_l2_s_cache_mode |= L2_XS_T_TEX(TEX_XSCALE_X);
5738 #endif /* XSCALE_CACHE_READ_WRITE_ALLOCATE */
5739
5740 #ifdef XSCALE_CACHE_WRITE_THROUGH
5741 /*
5742 * Some versions of the XScale core have various bugs in
5743 * their cache units, the work-around for which is to run
5744 * the cache in write-through mode. Unfortunately, this
5745 * has a major (negative) impact on performance. So, we
5746 * go ahead and run fast-and-loose, in the hopes that we
5747 * don't line up the planets in a way that will trip the
5748 * bugs.
5749 *
5750 * However, we give you the option to be slow-but-correct.
5751 */
5752 write_through = 1;
5753 #elif defined(XSCALE_CACHE_WRITE_BACK)
5754 /* force write back cache mode */
5755 write_through = 0;
5756 #elif defined(CPU_XSCALE_PXA250) || defined(CPU_XSCALE_PXA270)
5757 /*
5758 * Intel PXA2[15]0 processors are known to have a bug in
5759 * write-back cache on revision 4 and earlier (stepping
5760 * A[01] and B[012]). Fixed for C0 and later.
5761 */
5762 {
5763 uint32_t id, type;
5764
5765 id = cpufunc_id();
5766 type = id & ~(CPU_ID_XSCALE_COREREV_MASK|CPU_ID_REVISION_MASK);
5767
5768 if (type == CPU_ID_PXA250 || type == CPU_ID_PXA210) {
5769 if ((id & CPU_ID_REVISION_MASK) < 5) {
5770 /* write through for stepping A0-1 and B0-2 */
5771 write_through = 1;
5772 }
5773 }
5774 }
5775 #endif /* XSCALE_CACHE_WRITE_THROUGH */
5776
5777 if (write_through) {
5778 pte_l1_s_cache_mode = L1_S_C;
5779 pte_l2_l_cache_mode = L2_C;
5780 pte_l2_s_cache_mode = L2_C;
5781 }
5782
5783 #if (ARM_NMMUS > 1)
5784 xscale_use_minidata = 1;
5785 #endif
5786
5787 pte_l2_s_prot_u = L2_S_PROT_U_xscale;
5788 pte_l2_s_prot_w = L2_S_PROT_W_xscale;
5789 pte_l2_s_prot_mask = L2_S_PROT_MASK_xscale;
5790
5791 pte_l1_s_proto = L1_S_PROTO_xscale;
5792 pte_l1_c_proto = L1_C_PROTO_xscale;
5793 pte_l2_s_proto = L2_S_PROTO_xscale;
5794
5795 pmap_copy_page_func = pmap_copy_page_xscale;
5796 pmap_zero_page_func = pmap_zero_page_xscale;
5797
5798 /*
5799 * Disable ECC protection of page table access, for now.
5800 */
5801 __asm volatile("mrc p15, 0, %0, c1, c0, 1" : "=r" (auxctl));
5802 auxctl &= ~XSCALE_AUXCTL_P;
5803 __asm volatile("mcr p15, 0, %0, c1, c0, 1" : : "r" (auxctl));
5804 }
5805
5806 /*
5807 * xscale_setup_minidata:
5808 *
5809 * Set up the mini-data cache clean area. We require the
5810 * caller to allocate the right amount of physically and
5811 * virtually contiguous space.
5812 */
5813 void
5814 xscale_setup_minidata(vaddr_t l1pt, vaddr_t va, paddr_t pa)
5815 {
5816 extern vaddr_t xscale_minidata_clean_addr;
5817 extern vsize_t xscale_minidata_clean_size; /* already initialized */
5818 pd_entry_t *pde = (pd_entry_t *) l1pt;
5819 pt_entry_t *pte;
5820 vsize_t size;
5821 uint32_t auxctl;
5822
5823 xscale_minidata_clean_addr = va;
5824
5825 /* Round it to page size. */
5826 size = (xscale_minidata_clean_size + L2_S_OFFSET) & L2_S_FRAME;
5827
5828 for (; size != 0;
5829 va += L2_S_SIZE, pa += L2_S_SIZE, size -= L2_S_SIZE) {
5830 #ifndef ARM32_NEW_VM_LAYOUT
5831 pte = (pt_entry_t *)
5832 kernel_pt_lookup(pde[va >> L1_S_SHIFT] & L2_S_FRAME);
5833 #else
5834 pte = (pt_entry_t *) kernel_pt_lookup(
5835 pde[L1_IDX(va)] & L1_C_ADDR_MASK);
5836 #endif
5837 if (pte == NULL)
5838 panic("xscale_setup_minidata: can't find L2 table for "
5839 "VA 0x%08lx", va);
5840 #ifndef ARM32_NEW_VM_LAYOUT
5841 pte[(va >> PGSHIFT) & 0x3ff] =
5842 #else
5843 pte[l2pte_index(va)] =
5844 #endif
5845 L2_S_PROTO | pa | L2_S_PROT(PTE_KERNEL, VM_PROT_READ) |
5846 L2_C | L2_XS_T_TEX(TEX_XSCALE_X);
5847 }
5848
5849 /*
5850 * Configure the mini-data cache for write-back with
5851 * read/write-allocate.
5852 *
5853 * NOTE: In order to reconfigure the mini-data cache, we must
5854 * make sure it contains no valid data! In order to do that,
5855 * we must issue a global data cache invalidate command!
5856 *
5857 * WE ASSUME WE ARE RUNNING UN-CACHED WHEN THIS ROUTINE IS CALLED!
5858 * THIS IS VERY IMPORTANT!
5859 */
5860
5861 /* Invalidate data and mini-data. */
5862 __asm volatile("mcr p15, 0, %0, c7, c6, 0" : : "r" (0));
5863 __asm volatile("mrc p15, 0, %0, c1, c0, 1" : "=r" (auxctl));
5864 auxctl = (auxctl & ~XSCALE_AUXCTL_MD_MASK) | XSCALE_AUXCTL_MD_WB_RWA;
5865 __asm volatile("mcr p15, 0, %0, c1, c0, 1" : : "r" (auxctl));
5866 }
5867
5868 /*
5869 * Change the PTEs for the specified kernel mappings such that they
5870 * will use the mini data cache instead of the main data cache.
5871 */
5872 void
5873 pmap_uarea(vaddr_t va)
5874 {
5875 struct l2_bucket *l2b;
5876 pt_entry_t *ptep, *sptep, pte;
5877 vaddr_t next_bucket, eva;
5878
5879 #if (ARM_NMMUS > 1)
5880 if (xscale_use_minidata == 0)
5881 return;
5882 #endif
5883
5884 eva = va + USPACE;
5885
5886 while (va < eva) {
5887 next_bucket = L2_NEXT_BUCKET(va);
5888 if (next_bucket > eva)
5889 next_bucket = eva;
5890
5891 l2b = pmap_get_l2_bucket(pmap_kernel(), va);
5892 KDASSERT(l2b != NULL);
5893
5894 sptep = ptep = &l2b->l2b_kva[l2pte_index(va)];
5895
5896 while (va < next_bucket) {
5897 pte = *ptep;
5898 if (!l2pte_minidata(pte)) {
5899 cpu_dcache_wbinv_range(va, PAGE_SIZE);
5900 cpu_tlb_flushD_SE(va);
5901 *ptep = pte & ~L2_B;
5902 }
5903 ptep++;
5904 va += PAGE_SIZE;
5905 }
5906 PTE_SYNC_RANGE(sptep, (u_int)(ptep - sptep));
5907 }
5908 cpu_cpwait();
5909 }
5910 #endif /* ARM_MMU_XSCALE == 1 */
5911
5912 /*
5913 * return the PA of the current L1 table, for use when handling a crash dump
5914 */
5915 uint32_t pmap_kernel_L1_addr()
5916 {
5917 return pmap_kernel()->pm_l1->l1_physaddr;
5918 }
5919
5920 #if defined(DDB)
5921 /*
5922 * A couple of ddb-callable functions for dumping pmaps
5923 */
5924 void pmap_dump_all(void);
5925 void pmap_dump(pmap_t);
5926
5927 void
5928 pmap_dump_all(void)
5929 {
5930 pmap_t pm;
5931
5932 LIST_FOREACH(pm, &pmap_pmaps, pm_list) {
5933 if (pm == pmap_kernel())
5934 continue;
5935 pmap_dump(pm);
5936 printf("\n");
5937 }
5938 }
5939
5940 static pt_entry_t ncptes[64];
5941 static void pmap_dump_ncpg(pmap_t);
5942
5943 void
5944 pmap_dump(pmap_t pm)
5945 {
5946 struct l2_dtable *l2;
5947 struct l2_bucket *l2b;
5948 pt_entry_t *ptep, pte;
5949 vaddr_t l2_va, l2b_va, va;
5950 int i, j, k, occ, rows = 0;
5951
5952 if (pm == pmap_kernel())
5953 printf("pmap_kernel (%p): ", pm);
5954 else
5955 printf("user pmap (%p): ", pm);
5956
5957 printf("domain %d, l1 at %p\n", pm->pm_domain, pm->pm_l1->l1_kva);
5958
5959 l2_va = 0;
5960 for (i = 0; i < L2_SIZE; i++, l2_va += 0x01000000) {
5961 l2 = pm->pm_l2[i];
5962
5963 if (l2 == NULL || l2->l2_occupancy == 0)
5964 continue;
5965
5966 l2b_va = l2_va;
5967 for (j = 0; j < L2_BUCKET_SIZE; j++, l2b_va += 0x00100000) {
5968 l2b = &l2->l2_bucket[j];
5969
5970 if (l2b->l2b_occupancy == 0 || l2b->l2b_kva == NULL)
5971 continue;
5972
5973 ptep = l2b->l2b_kva;
5974
5975 for (k = 0; k < 256 && ptep[k] == 0; k++)
5976 ;
5977
5978 k &= ~63;
5979 occ = l2b->l2b_occupancy;
5980 va = l2b_va + (k * 4096);
5981 for (; k < 256; k++, va += 0x1000) {
5982 char ch = ' ';
5983 if ((k % 64) == 0) {
5984 if ((rows % 8) == 0) {
5985 printf(
5986 " |0000 |8000 |10000 |18000 |20000 |28000 |30000 |38000\n");
5987 }
5988 printf("%08lx: ", va);
5989 }
5990
5991 ncptes[k & 63] = 0;
5992 pte = ptep[k];
5993 if (pte == 0) {
5994 ch = '.';
5995 } else {
5996 occ--;
5997 switch (pte & 0x0c) {
5998 case 0x00:
5999 ch = 'D'; /* No cache No buff */
6000 break;
6001 case 0x04:
6002 ch = 'B'; /* No cache buff */
6003 break;
6004 case 0x08:
6005 if (pte & 0x40)
6006 ch = 'm';
6007 else
6008 ch = 'C'; /* Cache No buff */
6009 break;
6010 case 0x0c:
6011 ch = 'F'; /* Cache Buff */
6012 break;
6013 }
6014
6015 if ((pte & L2_S_PROT_U) == L2_S_PROT_U)
6016 ch += 0x20;
6017
6018 if ((pte & 0xc) == 0)
6019 ncptes[k & 63] = pte;
6020 }
6021
6022 if ((k % 64) == 63) {
6023 rows++;
6024 printf("%c\n", ch);
6025 pmap_dump_ncpg(pm);
6026 if (occ == 0)
6027 break;
6028 } else
6029 printf("%c", ch);
6030 }
6031 }
6032 }
6033 }
6034
6035 static void
6036 pmap_dump_ncpg(pmap_t pm)
6037 {
6038 struct vm_page *pg;
6039 struct pv_entry *pv;
6040 int i;
6041
6042 for (i = 0; i < 63; i++) {
6043 if (ncptes[i] == 0)
6044 continue;
6045
6046 pg = PHYS_TO_VM_PAGE(l2pte_pa(ncptes[i]));
6047 if (pg == NULL)
6048 continue;
6049
6050 printf(" pa 0x%08lx: krw %d kro %d urw %d uro %d\n",
6051 VM_PAGE_TO_PHYS(pg),
6052 pg->mdpage.krw_mappings, pg->mdpage.kro_mappings,
6053 pg->mdpage.urw_mappings, pg->mdpage.uro_mappings);
6054
6055 for (pv = pg->mdpage.pvh_list; pv; pv = pv->pv_next) {
6056 printf(" %c va 0x%08lx, flags 0x%x\n",
6057 (pm == pv->pv_pmap) ? '*' : ' ',
6058 pv->pv_va, pv->pv_flags);
6059 }
6060 }
6061 }
6062 #endif
6063
6064 #ifdef PMAP_STEAL_MEMORY
6065 void
6066 pmap_boot_pageadd(pv_addr_t *newpv)
6067 {
6068 pv_addr_t *pv, *npv;
6069
6070 if ((pv = SLIST_FIRST(&pmap_boot_freeq)) != NULL) {
6071 if (newpv->pv_pa < pv->pv_va) {
6072 KASSERT(newpv->pv_pa + newpv->pv_size <= pv->pv_pa);
6073 if (newpv->pv_pa + newpv->pv_size == pv->pv_pa) {
6074 newpv->pv_size += pv->pv_size;
6075 SLIST_REMOVE_HEAD(&pmap_boot_freeq, pv_list);
6076 }
6077 pv = NULL;
6078 } else {
6079 for (; (npv = SLIST_NEXT(pv, pv_list)) != NULL;
6080 pv = npv) {
6081 KASSERT(pv->pv_pa + pv->pv_size < npv->pv_pa);
6082 KASSERT(pv->pv_pa < newpv->pv_pa);
6083 if (newpv->pv_pa > npv->pv_pa)
6084 continue;
6085 if (pv->pv_pa + pv->pv_size == newpv->pv_pa) {
6086 pv->pv_size += newpv->pv_size;
6087 return;
6088 }
6089 if (newpv->pv_pa + newpv->pv_size < npv->pv_pa)
6090 break;
6091 newpv->pv_size += npv->pv_size;
6092 SLIST_INSERT_AFTER(pv, newpv, pv_list);
6093 SLIST_REMOVE_AFTER(newpv, pv_list);
6094 return;
6095 }
6096 }
6097 }
6098
6099 if (pv) {
6100 SLIST_INSERT_AFTER(pv, newpv, pv_list);
6101 } else {
6102 SLIST_INSERT_HEAD(&pmap_boot_freeq, newpv, pv_list);
6103 }
6104 }
6105
6106 void
6107 pmap_boot_pagealloc(psize_t amount, psize_t mask, psize_t match,
6108 pv_addr_t *rpv)
6109 {
6110 pv_addr_t *pv, **pvp;
6111 struct vm_physseg *ps;
6112 size_t i;
6113
6114 KASSERT(amount & PGOFSET);
6115 KASSERT((mask & PGOFSET) == 0);
6116 KASSERT((match & PGOFSET) == 0);
6117 KASSERT(amount != 0);
6118
6119 for (pvp = &SLIST_FIRST(&pmap_boot_freeq);
6120 (pv = *pvp) != NULL;
6121 pvp = &SLIST_NEXT(pv, pv_list)) {
6122 pv_addr_t *newpv;
6123 psize_t off;
6124 /*
6125 * If this entry is too small to satify the request...
6126 */
6127 KASSERT(pv->pv_size > 0);
6128 if (pv->pv_size < amount)
6129 continue;
6130
6131 for (off = 0; off <= mask; off += PAGE_SIZE) {
6132 if (((pv->pv_pa + off) & mask) == match
6133 && off + amount <= pv->pv_size)
6134 break;
6135 }
6136 if (off > mask)
6137 continue;
6138
6139 rpv->pv_va = pv->pv_va + off;
6140 rpv->pv_pa = pv->pv_pa + off;
6141 rpv->pv_size = amount;
6142 pv->pv_size -= amount;
6143 if (pv->pv_size == 0) {
6144 KASSERT(off == 0);
6145 KASSERT((vaddr_t) pv == rpv->pv_va);
6146 *pvp = SLIST_NEXT(pv, pv_list);
6147 } else if (off == 0) {
6148 KASSERT((vaddr_t) pv == rpv->pv_va);
6149 newpv = (pv_addr_t *) (rpv->pv_va + amount);
6150 *newpv = *pv;
6151 newpv->pv_pa += amount;
6152 newpv->pv_va += amount;
6153 *pvp = newpv;
6154 } else if (off < pv->pv_size) {
6155 newpv = (pv_addr_t *) (rpv->pv_va + amount);
6156 *newpv = *pv;
6157 newpv->pv_size -= off;
6158 newpv->pv_pa += off + amount;
6159 newpv->pv_va += off + amount;
6160
6161 SLIST_NEXT(pv, pv_list) = newpv;
6162 pv->pv_size = off;
6163 } else {
6164 KASSERT((vaddr_t) pv != rpv->pv_va);
6165 }
6166 memset((void *)rpv->pv_va, 0, amount);
6167 return;
6168 }
6169
6170 if (vm_nphysseg == 0)
6171 panic("pmap_boot_pagealloc: couldn't allocate memory");
6172
6173 for (pvp = &SLIST_FIRST(&pmap_boot_freeq);
6174 (pv = *pvp) != NULL;
6175 pvp = &SLIST_NEXT(pv, pv_list)) {
6176 if (SLIST_NEXT(pv, pv_list) == NULL)
6177 break;
6178 }
6179 KASSERT(mask == 0);
6180 for (ps = vm_physmem, i = 0; i < vm_nphysseg; ps++, i++) {
6181 if (ps->avail_start == atop(pv->pv_pa + pv->pv_size)
6182 && pv->pv_va + pv->pv_size <= ptoa(ps->avail_end)) {
6183 rpv->pv_va = pv->pv_va;
6184 rpv->pv_pa = pv->pv_pa;
6185 rpv->pv_size = amount;
6186 *pvp = NULL;
6187 pmap_map_chunk(kernel_l1pt.pv_va,
6188 ptoa(ps->avail_start) + (pv->pv_va - pv->pv_pa),
6189 ptoa(ps->avail_start),
6190 amount - pv->pv_size,
6191 VM_PROT_READ|VM_PROT_WRITE,
6192 PTE_CACHE);
6193 ps->avail_start += atop(amount - pv->pv_size);
6194 /*
6195 * If we consumed the entire physseg, remove it.
6196 */
6197 if (ps->avail_start == ps->avail_end) {
6198 for (--vm_nphysseg; i < vm_nphysseg; i++, ps++)
6199 ps[0] = ps[1];
6200 }
6201 memset((void *)rpv->pv_va, 0, rpv->pv_size);
6202 return;
6203 }
6204 }
6205
6206 panic("pmap_boot_pagealloc: couldn't allocate memory");
6207 }
6208
6209 vaddr_t
6210 pmap_steal_memory(vsize_t size, vaddr_t *vstartp, vaddr_t *vendp)
6211 {
6212 pv_addr_t pv;
6213
6214 pmap_boot_pagealloc(size, 0, 0, &pv);
6215
6216 return pv.pv_va;
6217 }
6218 #endif /* PMAP_STEAL_MEMORY */
6219