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