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