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