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