pmap.c revision 1.401 1 /* $NetBSD: pmap.c,v 1.401 2020/03/29 09:10:26 skrll 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 dependent 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 in kernel VA space,
166 * then 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_arm_debug.h"
190 #include "opt_cpuoptions.h"
191 #include "opt_pmap_debug.h"
192 #include "opt_ddb.h"
193 #include "opt_lockdebug.h"
194 #include "opt_multiprocessor.h"
195
196 #ifdef MULTIPROCESSOR
197 #define _INTR_PRIVATE
198 #endif
199
200 #include <sys/cdefs.h>
201 __KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.401 2020/03/29 09:10:26 skrll Exp $");
202
203 #include <sys/atomic.h>
204 #include <sys/param.h>
205 #include <sys/types.h>
206 #include <sys/atomic.h>
207 #include <sys/bus.h>
208 #include <sys/cpu.h>
209 #include <sys/intr.h>
210 #include <sys/kernel.h>
211 #include <sys/kernhist.h>
212 #include <sys/kmem.h>
213 #include <sys/pool.h>
214 #include <sys/proc.h>
215 #include <sys/sysctl.h>
216 #include <sys/systm.h>
217
218 #include <uvm/uvm.h>
219 #include <uvm/pmap/pmap_pvt.h>
220
221 #include <arm/locore.h>
222
223 #ifdef DDB
224 #include <arm/db_machdep.h>
225 #endif
226
227 //#define PMAP_DEBUG
228 #ifdef PMAP_DEBUG
229
230 /* XXX need to get rid of all refs to this */
231 int pmap_debug_level = 0;
232
233 /*
234 * for switching to potentially finer grained debugging
235 */
236 #define PDB_FOLLOW 0x0001
237 #define PDB_INIT 0x0002
238 #define PDB_ENTER 0x0004
239 #define PDB_REMOVE 0x0008
240 #define PDB_CREATE 0x0010
241 #define PDB_PTPAGE 0x0020
242 #define PDB_GROWKERN 0x0040
243 #define PDB_BITS 0x0080
244 #define PDB_COLLECT 0x0100
245 #define PDB_PROTECT 0x0200
246 #define PDB_MAP_L1 0x0400
247 #define PDB_BOOTSTRAP 0x1000
248 #define PDB_PARANOIA 0x2000
249 #define PDB_WIRING 0x4000
250 #define PDB_PVDUMP 0x8000
251 #define PDB_VAC 0x10000
252 #define PDB_KENTER 0x20000
253 #define PDB_KREMOVE 0x40000
254 #define PDB_EXEC 0x80000
255
256 int debugmap = 1;
257 int pmapdebug = 0;
258 #define NPDEBUG(_lev_,_stat_) \
259 if (pmapdebug & (_lev_)) \
260 ((_stat_))
261
262 #else /* PMAP_DEBUG */
263 #define NPDEBUG(_lev_,_stat_) /* Nothing */
264 #endif /* PMAP_DEBUG */
265
266
267 #ifdef VERBOSE_INIT_ARM
268 #define VPRINTF(...) printf(__VA_ARGS__)
269 #else
270 #define VPRINTF(...) __nothing
271 #endif
272
273 /*
274 * pmap_kernel() points here
275 */
276 static struct pmap kernel_pmap_store = {
277 #ifndef ARM_MMU_EXTENDED
278 .pm_activated = true,
279 .pm_domain = PMAP_DOMAIN_KERNEL,
280 .pm_cstate.cs_all = PMAP_CACHE_STATE_ALL,
281 #endif
282 };
283 struct pmap * const kernel_pmap_ptr = &kernel_pmap_store;
284 #undef pmap_kernel
285 #define pmap_kernel() (&kernel_pmap_store)
286 #ifdef PMAP_NEED_ALLOC_POOLPAGE
287 int arm_poolpage_vmfreelist = VM_FREELIST_DEFAULT;
288 #endif
289
290 /*
291 * Pool and cache that pmap structures are allocated from.
292 * We use a cache to avoid clearing the pm_l2[] array (1KB)
293 * in pmap_create().
294 */
295 static struct pool_cache pmap_cache;
296
297 /*
298 * Pool of PV structures
299 */
300 static struct pool pmap_pv_pool;
301 static void *pmap_bootstrap_pv_page_alloc(struct pool *, int);
302 static void pmap_bootstrap_pv_page_free(struct pool *, void *);
303 static struct pool_allocator pmap_bootstrap_pv_allocator = {
304 pmap_bootstrap_pv_page_alloc, pmap_bootstrap_pv_page_free
305 };
306
307 /*
308 * Pool and cache of l2_dtable structures.
309 * We use a cache to avoid clearing the structures when they're
310 * allocated. (196 bytes)
311 */
312 static struct pool_cache pmap_l2dtable_cache;
313 static vaddr_t pmap_kernel_l2dtable_kva;
314
315 /*
316 * Pool and cache of L2 page descriptors.
317 * We use a cache to avoid clearing the descriptor table
318 * when they're allocated. (1KB)
319 */
320 static struct pool_cache pmap_l2ptp_cache;
321 static vaddr_t pmap_kernel_l2ptp_kva;
322 static paddr_t pmap_kernel_l2ptp_phys;
323
324 #ifdef PMAPCOUNTERS
325 #define PMAP_EVCNT_INITIALIZER(name) \
326 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap", name)
327
328 #if defined(PMAP_CACHE_VIPT) && !defined(ARM_MMU_EXTENDED)
329 static struct evcnt pmap_ev_vac_clean_one =
330 PMAP_EVCNT_INITIALIZER("clean page (1 color)");
331 static struct evcnt pmap_ev_vac_flush_one =
332 PMAP_EVCNT_INITIALIZER("flush page (1 color)");
333 static struct evcnt pmap_ev_vac_flush_lots =
334 PMAP_EVCNT_INITIALIZER("flush page (2+ colors)");
335 static struct evcnt pmap_ev_vac_flush_lots2 =
336 PMAP_EVCNT_INITIALIZER("flush page (2+ colors, kmpage)");
337 EVCNT_ATTACH_STATIC(pmap_ev_vac_clean_one);
338 EVCNT_ATTACH_STATIC(pmap_ev_vac_flush_one);
339 EVCNT_ATTACH_STATIC(pmap_ev_vac_flush_lots);
340 EVCNT_ATTACH_STATIC(pmap_ev_vac_flush_lots2);
341
342 static struct evcnt pmap_ev_vac_color_new =
343 PMAP_EVCNT_INITIALIZER("new page color");
344 static struct evcnt pmap_ev_vac_color_reuse =
345 PMAP_EVCNT_INITIALIZER("ok first page color");
346 static struct evcnt pmap_ev_vac_color_ok =
347 PMAP_EVCNT_INITIALIZER("ok page color");
348 static struct evcnt pmap_ev_vac_color_blind =
349 PMAP_EVCNT_INITIALIZER("blind page color");
350 static struct evcnt pmap_ev_vac_color_change =
351 PMAP_EVCNT_INITIALIZER("change page color");
352 static struct evcnt pmap_ev_vac_color_erase =
353 PMAP_EVCNT_INITIALIZER("erase page color");
354 static struct evcnt pmap_ev_vac_color_none =
355 PMAP_EVCNT_INITIALIZER("no page color");
356 static struct evcnt pmap_ev_vac_color_restore =
357 PMAP_EVCNT_INITIALIZER("restore page color");
358
359 EVCNT_ATTACH_STATIC(pmap_ev_vac_color_new);
360 EVCNT_ATTACH_STATIC(pmap_ev_vac_color_reuse);
361 EVCNT_ATTACH_STATIC(pmap_ev_vac_color_ok);
362 EVCNT_ATTACH_STATIC(pmap_ev_vac_color_blind);
363 EVCNT_ATTACH_STATIC(pmap_ev_vac_color_change);
364 EVCNT_ATTACH_STATIC(pmap_ev_vac_color_erase);
365 EVCNT_ATTACH_STATIC(pmap_ev_vac_color_none);
366 EVCNT_ATTACH_STATIC(pmap_ev_vac_color_restore);
367 #endif
368
369 static struct evcnt pmap_ev_mappings =
370 PMAP_EVCNT_INITIALIZER("pages mapped");
371 static struct evcnt pmap_ev_unmappings =
372 PMAP_EVCNT_INITIALIZER("pages unmapped");
373 static struct evcnt pmap_ev_remappings =
374 PMAP_EVCNT_INITIALIZER("pages remapped");
375
376 EVCNT_ATTACH_STATIC(pmap_ev_mappings);
377 EVCNT_ATTACH_STATIC(pmap_ev_unmappings);
378 EVCNT_ATTACH_STATIC(pmap_ev_remappings);
379
380 static struct evcnt pmap_ev_kernel_mappings =
381 PMAP_EVCNT_INITIALIZER("kernel pages mapped");
382 static struct evcnt pmap_ev_kernel_unmappings =
383 PMAP_EVCNT_INITIALIZER("kernel pages unmapped");
384 static struct evcnt pmap_ev_kernel_remappings =
385 PMAP_EVCNT_INITIALIZER("kernel pages remapped");
386
387 EVCNT_ATTACH_STATIC(pmap_ev_kernel_mappings);
388 EVCNT_ATTACH_STATIC(pmap_ev_kernel_unmappings);
389 EVCNT_ATTACH_STATIC(pmap_ev_kernel_remappings);
390
391 static struct evcnt pmap_ev_kenter_mappings =
392 PMAP_EVCNT_INITIALIZER("kenter pages mapped");
393 static struct evcnt pmap_ev_kenter_unmappings =
394 PMAP_EVCNT_INITIALIZER("kenter pages unmapped");
395 static struct evcnt pmap_ev_kenter_remappings =
396 PMAP_EVCNT_INITIALIZER("kenter pages remapped");
397 static struct evcnt pmap_ev_pt_mappings =
398 PMAP_EVCNT_INITIALIZER("page table pages mapped");
399
400 EVCNT_ATTACH_STATIC(pmap_ev_kenter_mappings);
401 EVCNT_ATTACH_STATIC(pmap_ev_kenter_unmappings);
402 EVCNT_ATTACH_STATIC(pmap_ev_kenter_remappings);
403 EVCNT_ATTACH_STATIC(pmap_ev_pt_mappings);
404
405 static struct evcnt pmap_ev_fixup_mod =
406 PMAP_EVCNT_INITIALIZER("page modification emulations");
407 static struct evcnt pmap_ev_fixup_ref =
408 PMAP_EVCNT_INITIALIZER("page reference emulations");
409 static struct evcnt pmap_ev_fixup_exec =
410 PMAP_EVCNT_INITIALIZER("exec pages fixed up");
411 static struct evcnt pmap_ev_fixup_pdes =
412 PMAP_EVCNT_INITIALIZER("pdes fixed up");
413 #ifndef ARM_MMU_EXTENDED
414 static struct evcnt pmap_ev_fixup_ptesync =
415 PMAP_EVCNT_INITIALIZER("ptesync fixed");
416 #endif
417
418 EVCNT_ATTACH_STATIC(pmap_ev_fixup_mod);
419 EVCNT_ATTACH_STATIC(pmap_ev_fixup_ref);
420 EVCNT_ATTACH_STATIC(pmap_ev_fixup_exec);
421 EVCNT_ATTACH_STATIC(pmap_ev_fixup_pdes);
422 #ifndef ARM_MMU_EXTENDED
423 EVCNT_ATTACH_STATIC(pmap_ev_fixup_ptesync);
424 #endif
425
426 #ifdef PMAP_CACHE_VIPT
427 static struct evcnt pmap_ev_exec_mappings =
428 PMAP_EVCNT_INITIALIZER("exec pages mapped");
429 static struct evcnt pmap_ev_exec_cached =
430 PMAP_EVCNT_INITIALIZER("exec pages cached");
431
432 EVCNT_ATTACH_STATIC(pmap_ev_exec_mappings);
433 EVCNT_ATTACH_STATIC(pmap_ev_exec_cached);
434
435 static struct evcnt pmap_ev_exec_synced =
436 PMAP_EVCNT_INITIALIZER("exec pages synced");
437 static struct evcnt pmap_ev_exec_synced_map =
438 PMAP_EVCNT_INITIALIZER("exec pages synced (MP)");
439 static struct evcnt pmap_ev_exec_synced_unmap =
440 PMAP_EVCNT_INITIALIZER("exec pages synced (UM)");
441 static struct evcnt pmap_ev_exec_synced_remap =
442 PMAP_EVCNT_INITIALIZER("exec pages synced (RM)");
443 static struct evcnt pmap_ev_exec_synced_clearbit =
444 PMAP_EVCNT_INITIALIZER("exec pages synced (DG)");
445 #ifndef ARM_MMU_EXTENDED
446 static struct evcnt pmap_ev_exec_synced_kremove =
447 PMAP_EVCNT_INITIALIZER("exec pages synced (KU)");
448 #endif
449
450 EVCNT_ATTACH_STATIC(pmap_ev_exec_synced);
451 EVCNT_ATTACH_STATIC(pmap_ev_exec_synced_map);
452 #ifndef ARM_MMU_EXTENDED
453 EVCNT_ATTACH_STATIC(pmap_ev_exec_synced_unmap);
454 EVCNT_ATTACH_STATIC(pmap_ev_exec_synced_remap);
455 EVCNT_ATTACH_STATIC(pmap_ev_exec_synced_clearbit);
456 EVCNT_ATTACH_STATIC(pmap_ev_exec_synced_kremove);
457 #endif
458
459 static struct evcnt pmap_ev_exec_discarded_unmap =
460 PMAP_EVCNT_INITIALIZER("exec pages discarded (UM)");
461 static struct evcnt pmap_ev_exec_discarded_zero =
462 PMAP_EVCNT_INITIALIZER("exec pages discarded (ZP)");
463 static struct evcnt pmap_ev_exec_discarded_copy =
464 PMAP_EVCNT_INITIALIZER("exec pages discarded (CP)");
465 static struct evcnt pmap_ev_exec_discarded_page_protect =
466 PMAP_EVCNT_INITIALIZER("exec pages discarded (PP)");
467 static struct evcnt pmap_ev_exec_discarded_clearbit =
468 PMAP_EVCNT_INITIALIZER("exec pages discarded (DG)");
469 static struct evcnt pmap_ev_exec_discarded_kremove =
470 PMAP_EVCNT_INITIALIZER("exec pages discarded (KU)");
471 #ifdef ARM_MMU_EXTENDED
472 static struct evcnt pmap_ev_exec_discarded_modfixup =
473 PMAP_EVCNT_INITIALIZER("exec pages discarded (MF)");
474 #endif
475
476 EVCNT_ATTACH_STATIC(pmap_ev_exec_discarded_unmap);
477 EVCNT_ATTACH_STATIC(pmap_ev_exec_discarded_zero);
478 EVCNT_ATTACH_STATIC(pmap_ev_exec_discarded_copy);
479 EVCNT_ATTACH_STATIC(pmap_ev_exec_discarded_page_protect);
480 EVCNT_ATTACH_STATIC(pmap_ev_exec_discarded_clearbit);
481 EVCNT_ATTACH_STATIC(pmap_ev_exec_discarded_kremove);
482 #ifdef ARM_MMU_EXTENDED
483 EVCNT_ATTACH_STATIC(pmap_ev_exec_discarded_modfixup);
484 #endif
485 #endif /* PMAP_CACHE_VIPT */
486
487 static struct evcnt pmap_ev_updates = PMAP_EVCNT_INITIALIZER("updates");
488 static struct evcnt pmap_ev_collects = PMAP_EVCNT_INITIALIZER("collects");
489 static struct evcnt pmap_ev_activations = PMAP_EVCNT_INITIALIZER("activations");
490
491 EVCNT_ATTACH_STATIC(pmap_ev_updates);
492 EVCNT_ATTACH_STATIC(pmap_ev_collects);
493 EVCNT_ATTACH_STATIC(pmap_ev_activations);
494
495 #define PMAPCOUNT(x) ((void)(pmap_ev_##x.ev_count++))
496 #else
497 #define PMAPCOUNT(x) ((void)0)
498 #endif
499
500 #ifdef ARM_MMU_EXTENDED
501 void pmap_md_pdetab_activate(pmap_t, struct lwp *);
502 void pmap_md_pdetab_deactivate(pmap_t pm);
503 #endif
504
505 /*
506 * pmap copy/zero page, and mem(5) hook point
507 */
508 static pt_entry_t *csrc_pte, *cdst_pte;
509 static vaddr_t csrcp, cdstp;
510 #ifdef MULTIPROCESSOR
511 static size_t cnptes;
512 #define cpu_csrc_pte(o) (csrc_pte + cnptes * cpu_number() + ((o) >> L2_S_SHIFT))
513 #define cpu_cdst_pte(o) (cdst_pte + cnptes * cpu_number() + ((o) >> L2_S_SHIFT))
514 #define cpu_csrcp(o) (csrcp + L2_S_SIZE * cnptes * cpu_number() + (o))
515 #define cpu_cdstp(o) (cdstp + L2_S_SIZE * cnptes * cpu_number() + (o))
516 #else
517 #define cpu_csrc_pte(o) (csrc_pte + ((o) >> L2_S_SHIFT))
518 #define cpu_cdst_pte(o) (cdst_pte + ((o) >> L2_S_SHIFT))
519 #define cpu_csrcp(o) (csrcp + (o))
520 #define cpu_cdstp(o) (cdstp + (o))
521 #endif
522 vaddr_t memhook; /* used by mem.c & others */
523 kmutex_t memlock __cacheline_aligned; /* used by mem.c & others */
524 kmutex_t pmap_lock __cacheline_aligned;
525 kmutex_t kpm_lock __cacheline_aligned;
526 extern void *msgbufaddr;
527 int pmap_kmpages;
528 /*
529 * Flag to indicate if pmap_init() has done its thing
530 */
531 bool pmap_initialized;
532
533 #if defined(ARM_MMU_EXTENDED) && defined(__HAVE_MM_MD_DIRECT_MAPPED_PHYS)
534 /*
535 * Virtual end of direct-mapped memory
536 */
537 vaddr_t pmap_directlimit;
538 #endif
539
540 /*
541 * Misc. locking data structures
542 */
543
544 static inline void
545 pmap_acquire_pmap_lock(pmap_t pm)
546 {
547 #if defined(MULTIPROCESSOR) && defined(DDB)
548 if (__predict_false(db_onproc != NULL))
549 return;
550 #endif
551
552 mutex_enter(&pm->pm_lock);
553 }
554
555 static inline void
556 pmap_release_pmap_lock(pmap_t pm)
557 {
558 #if defined(MULTIPROCESSOR) && defined(DDB)
559 if (__predict_false(db_onproc != NULL))
560 return;
561 #endif
562 mutex_exit(&pm->pm_lock);
563 }
564
565 static inline void
566 pmap_acquire_page_lock(struct vm_page_md *md)
567 {
568 mutex_enter(&pmap_lock);
569 }
570
571 static inline void
572 pmap_release_page_lock(struct vm_page_md *md)
573 {
574 mutex_exit(&pmap_lock);
575 }
576
577 #ifdef DIAGNOSTIC
578 static inline int
579 pmap_page_locked_p(struct vm_page_md *md)
580 {
581 return mutex_owned(&pmap_lock);
582 }
583 #endif
584
585
586 /*
587 * Metadata for L1 translation tables.
588 */
589 #ifndef ARM_MMU_EXTENDED
590 struct l1_ttable {
591 /* Entry on the L1 Table list */
592 SLIST_ENTRY(l1_ttable) l1_link;
593
594 /* Entry on the L1 Least Recently Used list */
595 TAILQ_ENTRY(l1_ttable) l1_lru;
596
597 /* Track how many domains are allocated from this L1 */
598 volatile u_int l1_domain_use_count;
599
600 /*
601 * A free-list of domain numbers for this L1.
602 * We avoid using ffs() and a bitmap to track domains since ffs()
603 * is slow on ARM.
604 */
605 uint8_t l1_domain_first;
606 uint8_t l1_domain_free[PMAP_DOMAINS];
607
608 /* Physical address of this L1 page table */
609 paddr_t l1_physaddr;
610
611 /* KVA of this L1 page table */
612 pd_entry_t *l1_kva;
613 };
614
615 /*
616 * L1 Page Tables are tracked using a Least Recently Used list.
617 * - New L1s are allocated from the HEAD.
618 * - Freed L1s are added to the TAIL.
619 * - Recently accessed L1s (where an 'access' is some change to one of
620 * the userland pmaps which owns this L1) are moved to the TAIL.
621 */
622 static TAILQ_HEAD(, l1_ttable) l1_lru_list;
623 static kmutex_t l1_lru_lock __cacheline_aligned;
624
625 /*
626 * A list of all L1 tables
627 */
628 static SLIST_HEAD(, l1_ttable) l1_list;
629 #endif /* ARM_MMU_EXTENDED */
630
631 /*
632 * The l2_dtable tracks L2_BUCKET_SIZE worth of L1 slots.
633 *
634 * This is normally 16MB worth L2 page descriptors for any given pmap.
635 * Reference counts are maintained for L2 descriptors so they can be
636 * freed when empty.
637 */
638 struct l2_bucket {
639 pt_entry_t *l2b_kva; /* KVA of L2 Descriptor Table */
640 paddr_t l2b_pa; /* Physical address of same */
641 u_short l2b_l1slot; /* This L2 table's L1 index */
642 u_short l2b_occupancy; /* How many active descriptors */
643 };
644
645 struct l2_dtable {
646 /* The number of L2 page descriptors allocated to this l2_dtable */
647 u_int l2_occupancy;
648
649 /* List of L2 page descriptors */
650 struct l2_bucket l2_bucket[L2_BUCKET_SIZE];
651 };
652
653 /*
654 * Given an L1 table index, calculate the corresponding l2_dtable index
655 * and bucket index within the l2_dtable.
656 */
657 #define L2_BUCKET_XSHIFT (L2_BUCKET_XLOG2 - L1_S_SHIFT)
658 #define L2_BUCKET_XFRAME (~(vaddr_t)0 << L2_BUCKET_XLOG2)
659 #define L2_BUCKET_IDX(l1slot) ((l1slot) >> L2_BUCKET_XSHIFT)
660 #define L2_IDX(l1slot) (L2_BUCKET_IDX(l1slot) >> L2_BUCKET_LOG2)
661 #define L2_BUCKET(l1slot) (L2_BUCKET_IDX(l1slot) & (L2_BUCKET_SIZE - 1))
662
663 __CTASSERT(0x100000000ULL == ((uint64_t)L2_SIZE * L2_BUCKET_SIZE * L1_S_SIZE));
664 __CTASSERT(L2_BUCKET_XFRAME == ~(L2_BUCKET_XSIZE-1));
665
666 /*
667 * Given a virtual address, this macro returns the
668 * virtual address required to drop into the next L2 bucket.
669 */
670 #define L2_NEXT_BUCKET_VA(va) (((va) & L2_BUCKET_XFRAME) + L2_BUCKET_XSIZE)
671
672 /*
673 * L2 allocation.
674 */
675 #define pmap_alloc_l2_dtable() \
676 pool_cache_get(&pmap_l2dtable_cache, PR_NOWAIT)
677 #define pmap_free_l2_dtable(l2) \
678 pool_cache_put(&pmap_l2dtable_cache, (l2))
679 #define pmap_alloc_l2_ptp(pap) \
680 ((pt_entry_t *)pool_cache_get_paddr(&pmap_l2ptp_cache,\
681 PR_NOWAIT, (pap)))
682
683 /*
684 * We try to map the page tables write-through, if possible. However, not
685 * all CPUs have a write-through cache mode, so on those we have to sync
686 * the cache when we frob page tables.
687 *
688 * We try to evaluate this at compile time, if possible. However, it's
689 * not always possible to do that, hence this run-time var.
690 */
691 int pmap_needs_pte_sync;
692
693 /*
694 * Real definition of pv_entry.
695 */
696 struct pv_entry {
697 SLIST_ENTRY(pv_entry) pv_link; /* next pv_entry */
698 pmap_t pv_pmap; /* pmap where mapping lies */
699 vaddr_t pv_va; /* virtual address for mapping */
700 u_int pv_flags; /* flags */
701 };
702
703 /*
704 * Macros to determine if a mapping might be resident in the
705 * instruction/data cache and/or TLB
706 */
707 #if ARM_MMU_V7 > 0 && !defined(ARM_MMU_EXTENDED)
708 /*
709 * Speculative loads by Cortex cores can cause TLB entries to be filled even if
710 * there are no explicit accesses, so there may be always be TLB entries to
711 * flush. If we used ASIDs then this would not be a problem.
712 */
713 #define PV_BEEN_EXECD(f) (((f) & PVF_EXEC) == PVF_EXEC)
714 #define PV_BEEN_REFD(f) (true)
715 #else
716 #define PV_BEEN_EXECD(f) (((f) & (PVF_REF | PVF_EXEC)) == (PVF_REF | PVF_EXEC))
717 #define PV_BEEN_REFD(f) (((f) & PVF_REF) != 0)
718 #endif
719 #define PV_IS_EXEC_P(f) (((f) & PVF_EXEC) != 0)
720 #define PV_IS_KENTRY_P(f) (((f) & PVF_KENTRY) != 0)
721 #define PV_IS_WRITE_P(f) (((f) & PVF_WRITE) != 0)
722
723 /*
724 * Local prototypes
725 */
726 static bool pmap_set_pt_cache_mode(pd_entry_t *, vaddr_t, size_t);
727 static void pmap_alloc_specials(vaddr_t *, int, vaddr_t *,
728 pt_entry_t **);
729 static bool pmap_is_current(pmap_t) __unused;
730 static bool pmap_is_cached(pmap_t);
731 static void pmap_enter_pv(struct vm_page_md *, paddr_t, struct pv_entry *,
732 pmap_t, vaddr_t, u_int);
733 static struct pv_entry *pmap_find_pv(struct vm_page_md *, pmap_t, vaddr_t);
734 static struct pv_entry *pmap_remove_pv(struct vm_page_md *, paddr_t, pmap_t, vaddr_t);
735 static u_int pmap_modify_pv(struct vm_page_md *, paddr_t, pmap_t, vaddr_t,
736 u_int, u_int);
737
738 static void pmap_pinit(pmap_t);
739 static int pmap_pmap_ctor(void *, void *, int);
740
741 static void pmap_alloc_l1(pmap_t);
742 static void pmap_free_l1(pmap_t);
743 #ifndef ARM_MMU_EXTENDED
744 static void pmap_use_l1(pmap_t);
745 #endif
746
747 static struct l2_bucket *pmap_get_l2_bucket(pmap_t, vaddr_t);
748 static struct l2_bucket *pmap_alloc_l2_bucket(pmap_t, vaddr_t);
749 static void pmap_free_l2_bucket(pmap_t, struct l2_bucket *, u_int);
750 static int pmap_l2ptp_ctor(void *, void *, int);
751 static int pmap_l2dtable_ctor(void *, void *, int);
752
753 static void pmap_vac_me_harder(struct vm_page_md *, paddr_t, pmap_t, vaddr_t);
754 #ifdef PMAP_CACHE_VIVT
755 static void pmap_vac_me_kpmap(struct vm_page_md *, paddr_t, pmap_t, vaddr_t);
756 static void pmap_vac_me_user(struct vm_page_md *, paddr_t, pmap_t, vaddr_t);
757 #endif
758
759 static void pmap_clearbit(struct vm_page_md *, paddr_t, u_int);
760 #ifdef PMAP_CACHE_VIVT
761 static bool pmap_clean_page(struct vm_page_md *, bool);
762 #endif
763 #ifdef PMAP_CACHE_VIPT
764 static void pmap_syncicache_page(struct vm_page_md *, paddr_t);
765 enum pmap_flush_op {
766 PMAP_FLUSH_PRIMARY,
767 PMAP_FLUSH_SECONDARY,
768 PMAP_CLEAN_PRIMARY
769 };
770 #ifndef ARM_MMU_EXTENDED
771 static void pmap_flush_page(struct vm_page_md *, paddr_t, enum pmap_flush_op);
772 #endif
773 #endif
774 static void pmap_page_remove(struct vm_page_md *, paddr_t);
775 static void pmap_pv_remove(paddr_t);
776
777 #ifndef ARM_MMU_EXTENDED
778 static void pmap_init_l1(struct l1_ttable *, pd_entry_t *);
779 #endif
780 static vaddr_t kernel_pt_lookup(paddr_t);
781
782 #ifdef ARM_MMU_EXTENDED
783 static struct pool_cache pmap_l1tt_cache;
784
785 static int pmap_l1tt_ctor(void *, void *, int);
786 static void * pmap_l1tt_alloc(struct pool *, int);
787 static void pmap_l1tt_free(struct pool *, void *);
788
789 static struct pool_allocator pmap_l1tt_allocator = {
790 .pa_alloc = pmap_l1tt_alloc,
791 .pa_free = pmap_l1tt_free,
792 .pa_pagesz = L1TT_SIZE,
793 };
794 #endif
795
796 /*
797 * Misc variables
798 */
799 vaddr_t virtual_avail;
800 vaddr_t virtual_end;
801 vaddr_t pmap_curmaxkvaddr;
802
803 paddr_t avail_start;
804 paddr_t avail_end;
805
806 pv_addrqh_t pmap_boot_freeq = SLIST_HEAD_INITIALIZER(&pmap_boot_freeq);
807 pv_addr_t kernelpages;
808 pv_addr_t kernel_l1pt;
809 pv_addr_t systempage;
810
811 /* Function to set the debug level of the pmap code */
812
813 #ifdef PMAP_DEBUG
814 void
815 pmap_debug(int level)
816 {
817 pmap_debug_level = level;
818 printf("pmap_debug: level=%d\n", pmap_debug_level);
819 }
820 #endif /* PMAP_DEBUG */
821
822 #ifdef PMAP_CACHE_VIPT
823 #define PMAP_VALIDATE_MD_PAGE(md) \
824 KASSERTMSG(arm_cache_prefer_mask == 0 || (((md)->pvh_attrs & PVF_WRITE) == 0) == ((md)->urw_mappings + (md)->krw_mappings == 0), \
825 "(md) %p: attrs=%#x urw=%u krw=%u", (md), \
826 (md)->pvh_attrs, (md)->urw_mappings, (md)->krw_mappings);
827 #endif /* PMAP_CACHE_VIPT */
828 /*
829 * A bunch of routines to conditionally flush the caches/TLB depending
830 * on whether the specified pmap actually needs to be flushed at any
831 * given time.
832 */
833 static inline void
834 pmap_tlb_flush_SE(pmap_t pm, vaddr_t va, u_int flags)
835 {
836 #ifdef ARM_MMU_EXTENDED
837 pmap_tlb_invalidate_addr(pm, va);
838 #else
839 if (pm->pm_cstate.cs_tlb_id != 0) {
840 if (PV_BEEN_EXECD(flags)) {
841 cpu_tlb_flushID_SE(va);
842 } else if (PV_BEEN_REFD(flags)) {
843 cpu_tlb_flushD_SE(va);
844 }
845 }
846 #endif /* ARM_MMU_EXTENDED */
847 }
848
849 #ifndef ARM_MMU_EXTENDED
850 static inline void
851 pmap_tlb_flushID(pmap_t pm)
852 {
853 if (pm->pm_cstate.cs_tlb_id) {
854 cpu_tlb_flushID();
855 #if ARM_MMU_V7 == 0
856 /*
857 * Speculative loads by Cortex cores can cause TLB entries to
858 * be filled even if there are no explicit accesses, so there
859 * may be always be TLB entries to flush. If we used ASIDs
860 * then it would not be a problem.
861 * This is not true for other CPUs.
862 */
863 pm->pm_cstate.cs_tlb = 0;
864 #endif /* ARM_MMU_V7 */
865 }
866 }
867
868 static inline void
869 pmap_tlb_flushD(pmap_t pm)
870 {
871 if (pm->pm_cstate.cs_tlb_d) {
872 cpu_tlb_flushD();
873 #if ARM_MMU_V7 == 0
874 /*
875 * Speculative loads by Cortex cores can cause TLB entries to
876 * be filled even if there are no explicit accesses, so there
877 * may be always be TLB entries to flush. If we used ASIDs
878 * then it would not be a problem.
879 * This is not true for other CPUs.
880 */
881 pm->pm_cstate.cs_tlb_d = 0;
882 #endif /* ARM_MMU_V7 */
883 }
884 }
885 #endif /* ARM_MMU_EXTENDED */
886
887 #ifdef PMAP_CACHE_VIVT
888 static inline void
889 pmap_cache_wbinv_page(pmap_t pm, vaddr_t va, bool do_inv, u_int flags)
890 {
891 if (PV_BEEN_EXECD(flags) && pm->pm_cstate.cs_cache_id) {
892 cpu_idcache_wbinv_range(va, PAGE_SIZE);
893 } else if (PV_BEEN_REFD(flags) && pm->pm_cstate.cs_cache_d) {
894 if (do_inv) {
895 if (flags & PVF_WRITE)
896 cpu_dcache_wbinv_range(va, PAGE_SIZE);
897 else
898 cpu_dcache_inv_range(va, PAGE_SIZE);
899 } else if (flags & PVF_WRITE) {
900 cpu_dcache_wb_range(va, PAGE_SIZE);
901 }
902 }
903 }
904
905 static inline void
906 pmap_cache_wbinv_all(pmap_t pm, u_int flags)
907 {
908 if (PV_BEEN_EXECD(flags)) {
909 if (pm->pm_cstate.cs_cache_id) {
910 cpu_idcache_wbinv_all();
911 pm->pm_cstate.cs_cache = 0;
912 }
913 } else if (pm->pm_cstate.cs_cache_d) {
914 cpu_dcache_wbinv_all();
915 pm->pm_cstate.cs_cache_d = 0;
916 }
917 }
918 #endif /* PMAP_CACHE_VIVT */
919
920 static inline uint8_t
921 pmap_domain(pmap_t pm)
922 {
923 #ifdef ARM_MMU_EXTENDED
924 return pm == pmap_kernel() ? PMAP_DOMAIN_KERNEL : PMAP_DOMAIN_USER;
925 #else
926 return pm->pm_domain;
927 #endif
928 }
929
930 static inline pd_entry_t *
931 pmap_l1_kva(pmap_t pm)
932 {
933 #ifdef ARM_MMU_EXTENDED
934 return pm->pm_l1;
935 #else
936 return pm->pm_l1->l1_kva;
937 #endif
938 }
939
940 static inline bool
941 pmap_is_current(pmap_t pm)
942 {
943 if (pm == pmap_kernel() || curproc->p_vmspace->vm_map.pmap == pm)
944 return true;
945
946 return false;
947 }
948
949 static inline bool
950 pmap_is_cached(pmap_t pm)
951 {
952 #ifdef ARM_MMU_EXTENDED
953 if (pm == pmap_kernel())
954 return true;
955 #ifdef MULTIPROCESSOR
956 // Is this pmap active on any CPU?
957 if (!kcpuset_iszero(pm->pm_active))
958 return true;
959 #else
960 struct pmap_tlb_info * const ti = cpu_tlb_info(curcpu());
961 // Is this pmap active?
962 if (PMAP_PAI_ASIDVALID_P(PMAP_PAI(pm, ti), ti))
963 return true;
964 #endif
965 #else
966 struct cpu_info * const ci = curcpu();
967 if (pm == pmap_kernel() || ci->ci_pmap_lastuser == NULL
968 || ci->ci_pmap_lastuser == pm)
969 return true;
970 #endif /* ARM_MMU_EXTENDED */
971
972 return false;
973 }
974
975 /*
976 * PTE_SYNC_CURRENT:
977 *
978 * Make sure the pte is written out to RAM.
979 * We need to do this for one of two cases:
980 * - We're dealing with the kernel pmap
981 * - There is no pmap active in the cache/tlb.
982 * - The specified pmap is 'active' in the cache/tlb.
983 */
984
985 #ifdef PMAP_INCLUDE_PTE_SYNC
986 static inline void
987 pmap_pte_sync_current(pmap_t pm, pt_entry_t *ptep)
988 {
989 if (PMAP_NEEDS_PTE_SYNC && pmap_is_cached(pm))
990 PTE_SYNC(ptep);
991 arm_dsb();
992 }
993
994 # define PTE_SYNC_CURRENT(pm, ptep) pmap_pte_sync_current(pm, ptep)
995 #else
996 # define PTE_SYNC_CURRENT(pm, ptep) __nothing
997 #endif
998
999 /*
1000 * main pv_entry manipulation functions:
1001 * pmap_enter_pv: enter a mapping onto a vm_page list
1002 * pmap_remove_pv: remove a mapping from a vm_page list
1003 *
1004 * NOTE: pmap_enter_pv expects to lock the pvh itself
1005 * pmap_remove_pv expects the caller to lock the pvh before calling
1006 */
1007
1008 /*
1009 * pmap_enter_pv: enter a mapping onto a vm_page lst
1010 *
1011 * => caller should hold the proper lock on pmap_main_lock
1012 * => caller should have pmap locked
1013 * => we will gain the lock on the vm_page and allocate the new pv_entry
1014 * => caller should adjust ptp's wire_count before calling
1015 * => caller should not adjust pmap's wire_count
1016 */
1017 static void
1018 pmap_enter_pv(struct vm_page_md *md, paddr_t pa, struct pv_entry *pv, pmap_t pm,
1019 vaddr_t va, u_int flags)
1020 {
1021 struct pv_entry **pvp;
1022
1023 NPDEBUG(PDB_PVDUMP,
1024 printf("pmap_enter_pv: pm %p, md %p, flags 0x%x\n", pm, md, flags));
1025
1026 pv->pv_pmap = pm;
1027 pv->pv_va = va;
1028 pv->pv_flags = flags;
1029
1030 pvp = &SLIST_FIRST(&md->pvh_list);
1031 #ifdef PMAP_CACHE_VIPT
1032 /*
1033 * Insert unmanaged entries, writeable first, at the head of
1034 * the pv list.
1035 */
1036 if (__predict_true(!PV_IS_KENTRY_P(flags))) {
1037 while (*pvp != NULL && PV_IS_KENTRY_P((*pvp)->pv_flags))
1038 pvp = &SLIST_NEXT(*pvp, pv_link);
1039 }
1040 if (!PV_IS_WRITE_P(flags)) {
1041 while (*pvp != NULL && PV_IS_WRITE_P((*pvp)->pv_flags))
1042 pvp = &SLIST_NEXT(*pvp, pv_link);
1043 }
1044 #endif
1045 SLIST_NEXT(pv, pv_link) = *pvp; /* add to ... */
1046 *pvp = pv; /* ... locked list */
1047 md->pvh_attrs |= flags & (PVF_REF | PVF_MOD);
1048 #if defined(PMAP_CACHE_VIPT) && !defined(ARM_MMU_EXTENDED)
1049 if ((pv->pv_flags & PVF_KWRITE) == PVF_KWRITE)
1050 md->pvh_attrs |= PVF_KMOD;
1051 if ((md->pvh_attrs & (PVF_DMOD|PVF_NC)) != PVF_NC)
1052 md->pvh_attrs |= PVF_DIRTY;
1053 KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC)));
1054 #endif
1055 if (pm == pmap_kernel()) {
1056 PMAPCOUNT(kernel_mappings);
1057 if (flags & PVF_WRITE)
1058 md->krw_mappings++;
1059 else
1060 md->kro_mappings++;
1061 } else {
1062 if (flags & PVF_WRITE)
1063 md->urw_mappings++;
1064 else
1065 md->uro_mappings++;
1066 }
1067
1068 #ifdef PMAP_CACHE_VIPT
1069 #ifndef ARM_MMU_EXTENDED
1070 /*
1071 * Even though pmap_vac_me_harder will set PVF_WRITE for us,
1072 * do it here as well to keep the mappings & KVF_WRITE consistent.
1073 */
1074 if (arm_cache_prefer_mask != 0 && (flags & PVF_WRITE) != 0) {
1075 md->pvh_attrs |= PVF_WRITE;
1076 }
1077 #endif
1078 /*
1079 * If this is an exec mapping and its the first exec mapping
1080 * for this page, make sure to sync the I-cache.
1081 */
1082 if (PV_IS_EXEC_P(flags)) {
1083 if (!PV_IS_EXEC_P(md->pvh_attrs)) {
1084 pmap_syncicache_page(md, pa);
1085 PMAPCOUNT(exec_synced_map);
1086 }
1087 PMAPCOUNT(exec_mappings);
1088 }
1089 #endif
1090
1091 PMAPCOUNT(mappings);
1092
1093 if (pv->pv_flags & PVF_WIRED)
1094 ++pm->pm_stats.wired_count;
1095 }
1096
1097 /*
1098 *
1099 * pmap_find_pv: Find a pv entry
1100 *
1101 * => caller should hold lock on vm_page
1102 */
1103 static inline struct pv_entry *
1104 pmap_find_pv(struct vm_page_md *md, pmap_t pm, vaddr_t va)
1105 {
1106 struct pv_entry *pv;
1107
1108 SLIST_FOREACH(pv, &md->pvh_list, pv_link) {
1109 if (pm == pv->pv_pmap && va == pv->pv_va)
1110 break;
1111 }
1112
1113 return pv;
1114 }
1115
1116 /*
1117 * pmap_remove_pv: try to remove a mapping from a pv_list
1118 *
1119 * => caller should hold proper lock on pmap_main_lock
1120 * => pmap should be locked
1121 * => caller should hold lock on vm_page [so that attrs can be adjusted]
1122 * => caller should adjust ptp's wire_count and free PTP if needed
1123 * => caller should NOT adjust pmap's wire_count
1124 * => we return the removed pv
1125 */
1126 static struct pv_entry *
1127 pmap_remove_pv(struct vm_page_md *md, paddr_t pa, pmap_t pm, vaddr_t va)
1128 {
1129 struct pv_entry *pv, **prevptr;
1130
1131 NPDEBUG(PDB_PVDUMP,
1132 printf("pmap_remove_pv: pm %p, md %p, va 0x%08lx\n", pm, md, va));
1133
1134 prevptr = &SLIST_FIRST(&md->pvh_list); /* prev pv_entry ptr */
1135 pv = *prevptr;
1136
1137 while (pv) {
1138 if (pv->pv_pmap == pm && pv->pv_va == va) { /* match? */
1139 NPDEBUG(PDB_PVDUMP, printf("pmap_remove_pv: pm %p, md "
1140 "%p, flags 0x%x\n", pm, md, pv->pv_flags));
1141 if (pv->pv_flags & PVF_WIRED) {
1142 --pm->pm_stats.wired_count;
1143 }
1144 *prevptr = SLIST_NEXT(pv, pv_link); /* remove it! */
1145 if (pm == pmap_kernel()) {
1146 PMAPCOUNT(kernel_unmappings);
1147 if (pv->pv_flags & PVF_WRITE)
1148 md->krw_mappings--;
1149 else
1150 md->kro_mappings--;
1151 } else {
1152 if (pv->pv_flags & PVF_WRITE)
1153 md->urw_mappings--;
1154 else
1155 md->uro_mappings--;
1156 }
1157
1158 PMAPCOUNT(unmappings);
1159 #ifdef PMAP_CACHE_VIPT
1160 /*
1161 * If this page has had an exec mapping, then if
1162 * this was the last mapping, discard the contents,
1163 * otherwise sync the i-cache for this page.
1164 */
1165 if (PV_IS_EXEC_P(md->pvh_attrs)) {
1166 if (SLIST_EMPTY(&md->pvh_list)) {
1167 md->pvh_attrs &= ~PVF_EXEC;
1168 PMAPCOUNT(exec_discarded_unmap);
1169 } else if (pv->pv_flags & PVF_WRITE) {
1170 pmap_syncicache_page(md, pa);
1171 PMAPCOUNT(exec_synced_unmap);
1172 }
1173 }
1174 #endif /* PMAP_CACHE_VIPT */
1175 break;
1176 }
1177 prevptr = &SLIST_NEXT(pv, pv_link); /* previous pointer */
1178 pv = *prevptr; /* advance */
1179 }
1180
1181 #if defined(PMAP_CACHE_VIPT) && !defined(ARM_MMU_EXTENDED)
1182 /*
1183 * If we no longer have a WRITEABLE KENTRY at the head of list,
1184 * clear the KMOD attribute from the page.
1185 */
1186 if (SLIST_FIRST(&md->pvh_list) == NULL
1187 || (SLIST_FIRST(&md->pvh_list)->pv_flags & PVF_KWRITE) != PVF_KWRITE)
1188 md->pvh_attrs &= ~PVF_KMOD;
1189
1190 /*
1191 * If this was a writeable page and there are no more writeable
1192 * mappings (ignoring KMPAGE), clear the WRITE flag and writeback
1193 * the contents to memory.
1194 */
1195 if (arm_cache_prefer_mask != 0) {
1196 if (md->krw_mappings + md->urw_mappings == 0)
1197 md->pvh_attrs &= ~PVF_WRITE;
1198 PMAP_VALIDATE_MD_PAGE(md);
1199 }
1200 KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC)));
1201 #endif /* PMAP_CACHE_VIPT && !ARM_MMU_EXTENDED */
1202
1203 /* return removed pv */
1204 return pv;
1205 }
1206
1207 /*
1208 *
1209 * pmap_modify_pv: Update pv flags
1210 *
1211 * => caller should hold lock on vm_page [so that attrs can be adjusted]
1212 * => caller should NOT adjust pmap's wire_count
1213 * => caller must call pmap_vac_me_harder() if writable status of a page
1214 * may have changed.
1215 * => we return the old flags
1216 *
1217 * Modify a physical-virtual mapping in the pv table
1218 */
1219 static u_int
1220 pmap_modify_pv(struct vm_page_md *md, paddr_t pa, pmap_t pm, vaddr_t va,
1221 u_int clr_mask, u_int set_mask)
1222 {
1223 struct pv_entry *npv;
1224 u_int flags, oflags;
1225
1226 KASSERT(!PV_IS_KENTRY_P(clr_mask));
1227 KASSERT(!PV_IS_KENTRY_P(set_mask));
1228
1229 if ((npv = pmap_find_pv(md, pm, va)) == NULL)
1230 return 0;
1231
1232 NPDEBUG(PDB_PVDUMP,
1233 printf("pmap_modify_pv: pm %p, md %p, clr 0x%x, set 0x%x, flags 0x%x\n", pm, md, clr_mask, set_mask, npv->pv_flags));
1234
1235 /*
1236 * There is at least one VA mapping this page.
1237 */
1238
1239 if (clr_mask & (PVF_REF | PVF_MOD)) {
1240 md->pvh_attrs |= set_mask & (PVF_REF | PVF_MOD);
1241 #if defined(PMAP_CACHE_VIPT) && !defined(ARM_MMU_EXTENDED)
1242 if ((md->pvh_attrs & (PVF_DMOD|PVF_NC)) != PVF_NC)
1243 md->pvh_attrs |= PVF_DIRTY;
1244 KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC)));
1245 #endif /* PMAP_CACHE_VIPT && !ARM_MMU_EXTENDED */
1246 }
1247
1248 oflags = npv->pv_flags;
1249 npv->pv_flags = flags = (oflags & ~clr_mask) | set_mask;
1250
1251 if ((flags ^ oflags) & PVF_WIRED) {
1252 if (flags & PVF_WIRED)
1253 ++pm->pm_stats.wired_count;
1254 else
1255 --pm->pm_stats.wired_count;
1256 }
1257
1258 if ((flags ^ oflags) & PVF_WRITE) {
1259 if (pm == pmap_kernel()) {
1260 if (flags & PVF_WRITE) {
1261 md->krw_mappings++;
1262 md->kro_mappings--;
1263 } else {
1264 md->kro_mappings++;
1265 md->krw_mappings--;
1266 }
1267 } else {
1268 if (flags & PVF_WRITE) {
1269 md->urw_mappings++;
1270 md->uro_mappings--;
1271 } else {
1272 md->uro_mappings++;
1273 md->urw_mappings--;
1274 }
1275 }
1276 }
1277 #ifdef PMAP_CACHE_VIPT
1278 if (arm_cache_prefer_mask != 0) {
1279 if (md->urw_mappings + md->krw_mappings == 0) {
1280 md->pvh_attrs &= ~PVF_WRITE;
1281 } else {
1282 md->pvh_attrs |= PVF_WRITE;
1283 }
1284 }
1285 /*
1286 * We have two cases here: the first is from enter_pv (new exec
1287 * page), the second is a combined pmap_remove_pv/pmap_enter_pv.
1288 * Since in latter, pmap_enter_pv won't do anything, we just have
1289 * to do what pmap_remove_pv would do.
1290 */
1291 if ((PV_IS_EXEC_P(flags) && !PV_IS_EXEC_P(md->pvh_attrs))
1292 || (PV_IS_EXEC_P(md->pvh_attrs)
1293 || (!(flags & PVF_WRITE) && (oflags & PVF_WRITE)))) {
1294 pmap_syncicache_page(md, pa);
1295 PMAPCOUNT(exec_synced_remap);
1296 }
1297 #ifndef ARM_MMU_EXTENDED
1298 KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC)));
1299 #endif /* !ARM_MMU_EXTENDED */
1300 #endif /* PMAP_CACHE_VIPT */
1301
1302 PMAPCOUNT(remappings);
1303
1304 return oflags;
1305 }
1306
1307
1308 #if defined(ARM_MMU_EXTENDED)
1309 int
1310 pmap_maxproc_set(int nmaxproc)
1311 {
1312 static const char pmap_l1ttpool_warnmsg[] =
1313 "WARNING: l1ttpool limit reached; increase kern.maxproc";
1314
1315 // pool_cache_setlowat(&pmap_l1tt_cache, nmaxproc);
1316
1317 /*
1318 * Set the hard limit on the pmap_l1tt_cache to the number
1319 * of processes the kernel is to support. Log the limit
1320 * reached message max once a minute.
1321 */
1322 pool_cache_sethardlimit(&pmap_l1tt_cache, nmaxproc,
1323 pmap_l1ttpool_warnmsg, 60);
1324
1325 return 0;
1326 }
1327
1328 #endif
1329
1330 /*
1331 * Allocate an L1 translation table for the specified pmap.
1332 * This is called at pmap creation time.
1333 */
1334 static void
1335 pmap_alloc_l1(pmap_t pm)
1336 {
1337 #ifdef ARM_MMU_EXTENDED
1338 vaddr_t va = (vaddr_t)pool_cache_get_paddr(&pmap_l1tt_cache, PR_WAITOK,
1339 &pm->pm_l1_pa);
1340
1341 pm->pm_l1 = (pd_entry_t *)va;
1342 PTE_SYNC_RANGE(pm->pm_l1, L1TT_SIZE / sizeof(pt_entry_t));
1343 #else
1344 struct l1_ttable *l1;
1345 uint8_t domain;
1346
1347 /*
1348 * Remove the L1 at the head of the LRU list
1349 */
1350 mutex_spin_enter(&l1_lru_lock);
1351 l1 = TAILQ_FIRST(&l1_lru_list);
1352 KDASSERT(l1 != NULL);
1353 TAILQ_REMOVE(&l1_lru_list, l1, l1_lru);
1354
1355 /*
1356 * Pick the first available domain number, and update
1357 * the link to the next number.
1358 */
1359 domain = l1->l1_domain_first;
1360 l1->l1_domain_first = l1->l1_domain_free[domain];
1361
1362 /*
1363 * If there are still free domain numbers in this L1,
1364 * put it back on the TAIL of the LRU list.
1365 */
1366 if (++l1->l1_domain_use_count < PMAP_DOMAINS)
1367 TAILQ_INSERT_TAIL(&l1_lru_list, l1, l1_lru);
1368
1369 mutex_spin_exit(&l1_lru_lock);
1370
1371 /*
1372 * Fix up the relevant bits in the pmap structure
1373 */
1374 pm->pm_l1 = l1;
1375 pm->pm_domain = domain + 1;
1376 #endif
1377 }
1378
1379 /*
1380 * Free an L1 translation table.
1381 * This is called at pmap destruction time.
1382 */
1383 static void
1384 pmap_free_l1(pmap_t pm)
1385 {
1386 #ifdef ARM_MMU_EXTENDED
1387 pool_cache_put_paddr(&pmap_l1tt_cache, (void *)pm->pm_l1, pm->pm_l1_pa);
1388
1389 pm->pm_l1 = NULL;
1390 pm->pm_l1_pa = 0;
1391 #else
1392 struct l1_ttable *l1 = pm->pm_l1;
1393
1394 mutex_spin_enter(&l1_lru_lock);
1395
1396 /*
1397 * If this L1 is currently on the LRU list, remove it.
1398 */
1399 if (l1->l1_domain_use_count < PMAP_DOMAINS)
1400 TAILQ_REMOVE(&l1_lru_list, l1, l1_lru);
1401
1402 /*
1403 * Free up the domain number which was allocated to the pmap
1404 */
1405 l1->l1_domain_free[pmap_domain(pm) - 1] = l1->l1_domain_first;
1406 l1->l1_domain_first = pmap_domain(pm) - 1;
1407 l1->l1_domain_use_count--;
1408
1409 /*
1410 * The L1 now must have at least 1 free domain, so add
1411 * it back to the LRU list. If the use count is zero,
1412 * put it at the head of the list, otherwise it goes
1413 * to the tail.
1414 */
1415 if (l1->l1_domain_use_count == 0)
1416 TAILQ_INSERT_HEAD(&l1_lru_list, l1, l1_lru);
1417 else
1418 TAILQ_INSERT_TAIL(&l1_lru_list, l1, l1_lru);
1419
1420 mutex_spin_exit(&l1_lru_lock);
1421 #endif /* ARM_MMU_EXTENDED */
1422 }
1423
1424 #ifndef ARM_MMU_EXTENDED
1425 static inline void
1426 pmap_use_l1(pmap_t pm)
1427 {
1428 struct l1_ttable *l1;
1429
1430 /*
1431 * Do nothing if we're in interrupt context.
1432 * Access to an L1 by the kernel pmap must not affect
1433 * the LRU list.
1434 */
1435 if (cpu_intr_p() || pm == pmap_kernel())
1436 return;
1437
1438 l1 = pm->pm_l1;
1439
1440 /*
1441 * If the L1 is not currently on the LRU list, just return
1442 */
1443 if (l1->l1_domain_use_count == PMAP_DOMAINS)
1444 return;
1445
1446 mutex_spin_enter(&l1_lru_lock);
1447
1448 /*
1449 * Check the use count again, now that we've acquired the lock
1450 */
1451 if (l1->l1_domain_use_count == PMAP_DOMAINS) {
1452 mutex_spin_exit(&l1_lru_lock);
1453 return;
1454 }
1455
1456 /*
1457 * Move the L1 to the back of the LRU list
1458 */
1459 TAILQ_REMOVE(&l1_lru_list, l1, l1_lru);
1460 TAILQ_INSERT_TAIL(&l1_lru_list, l1, l1_lru);
1461
1462 mutex_spin_exit(&l1_lru_lock);
1463 }
1464 #endif /* !ARM_MMU_EXTENDED */
1465
1466 /*
1467 * void pmap_free_l2_ptp(pt_entry_t *, paddr_t *)
1468 *
1469 * Free an L2 descriptor table.
1470 */
1471 static inline void
1472 #if defined(PMAP_INCLUDE_PTE_SYNC) && defined(PMAP_CACHE_VIVT)
1473 pmap_free_l2_ptp(bool need_sync, pt_entry_t *l2, paddr_t pa)
1474 #else
1475 pmap_free_l2_ptp(pt_entry_t *l2, paddr_t pa)
1476 #endif
1477 {
1478 #if defined(PMAP_INCLUDE_PTE_SYNC) && defined(PMAP_CACHE_VIVT)
1479 /*
1480 * Note: With a write-back cache, we may need to sync this
1481 * L2 table before re-using it.
1482 * This is because it may have belonged to a non-current
1483 * pmap, in which case the cache syncs would have been
1484 * skipped for the pages that were being unmapped. If the
1485 * L2 table were then to be immediately re-allocated to
1486 * the *current* pmap, it may well contain stale mappings
1487 * which have not yet been cleared by a cache write-back
1488 * and so would still be visible to the mmu.
1489 */
1490 if (need_sync)
1491 PTE_SYNC_RANGE(l2, L2_TABLE_SIZE_REAL / sizeof(pt_entry_t));
1492 #endif /* PMAP_INCLUDE_PTE_SYNC && PMAP_CACHE_VIVT */
1493 pool_cache_put_paddr(&pmap_l2ptp_cache, (void *)l2, pa);
1494 }
1495
1496 /*
1497 * Returns a pointer to the L2 bucket associated with the specified pmap
1498 * and VA, or NULL if no L2 bucket exists for the address.
1499 */
1500 static inline struct l2_bucket *
1501 pmap_get_l2_bucket(pmap_t pm, vaddr_t va)
1502 {
1503 const size_t l1slot = l1pte_index(va);
1504 struct l2_dtable *l2;
1505 struct l2_bucket *l2b;
1506
1507 if ((l2 = pm->pm_l2[L2_IDX(l1slot)]) == NULL ||
1508 (l2b = &l2->l2_bucket[L2_BUCKET(l1slot)])->l2b_kva == NULL)
1509 return NULL;
1510
1511 return l2b;
1512 }
1513
1514 /*
1515 * Returns a pointer to the L2 bucket associated with the specified pmap
1516 * and VA.
1517 *
1518 * If no L2 bucket exists, perform the necessary allocations to put an L2
1519 * bucket/page table in place.
1520 *
1521 * Note that if a new L2 bucket/page was allocated, the caller *must*
1522 * increment the bucket occupancy counter appropriately *before*
1523 * releasing the pmap's lock to ensure no other thread or cpu deallocates
1524 * the bucket/page in the meantime.
1525 */
1526 static struct l2_bucket *
1527 pmap_alloc_l2_bucket(pmap_t pm, vaddr_t va)
1528 {
1529 const size_t l1slot = l1pte_index(va);
1530 struct l2_dtable *l2;
1531
1532 if ((l2 = pm->pm_l2[L2_IDX(l1slot)]) == NULL) {
1533 /*
1534 * No mapping at this address, as there is
1535 * no entry in the L1 table.
1536 * Need to allocate a new l2_dtable.
1537 */
1538 if ((l2 = pmap_alloc_l2_dtable()) == NULL)
1539 return NULL;
1540
1541 /*
1542 * Link it into the parent pmap
1543 */
1544 pm->pm_l2[L2_IDX(l1slot)] = l2;
1545 }
1546
1547 struct l2_bucket * const l2b = &l2->l2_bucket[L2_BUCKET(l1slot)];
1548
1549 /*
1550 * Fetch pointer to the L2 page table associated with the address.
1551 */
1552 if (l2b->l2b_kva == NULL) {
1553 pt_entry_t *ptep;
1554
1555 /*
1556 * No L2 page table has been allocated. Chances are, this
1557 * is because we just allocated the l2_dtable, above.
1558 */
1559 if ((ptep = pmap_alloc_l2_ptp(&l2b->l2b_pa)) == NULL) {
1560 /*
1561 * Oops, no more L2 page tables available at this
1562 * time. We may need to deallocate the l2_dtable
1563 * if we allocated a new one above.
1564 */
1565 if (l2->l2_occupancy == 0) {
1566 pm->pm_l2[L2_IDX(l1slot)] = NULL;
1567 pmap_free_l2_dtable(l2);
1568 }
1569 return NULL;
1570 }
1571
1572 l2->l2_occupancy++;
1573 l2b->l2b_kva = ptep;
1574 l2b->l2b_l1slot = l1slot;
1575
1576 #ifdef ARM_MMU_EXTENDED
1577 /*
1578 * We know there will be a mapping here, so simply
1579 * enter this PTP into the L1 now.
1580 */
1581 pd_entry_t * const pdep = pmap_l1_kva(pm) + l1slot;
1582 pd_entry_t npde = L1_C_PROTO | l2b->l2b_pa
1583 | L1_C_DOM(pmap_domain(pm));
1584 KASSERT(*pdep == 0);
1585 l1pte_setone(pdep, npde);
1586 PDE_SYNC(pdep);
1587 #endif
1588 }
1589
1590 return l2b;
1591 }
1592
1593 /*
1594 * One or more mappings in the specified L2 descriptor table have just been
1595 * invalidated.
1596 *
1597 * Garbage collect the metadata and descriptor table itself if necessary.
1598 *
1599 * The pmap lock must be acquired when this is called (not necessary
1600 * for the kernel pmap).
1601 */
1602 static void
1603 pmap_free_l2_bucket(pmap_t pm, struct l2_bucket *l2b, u_int count)
1604 {
1605 KDASSERT(count <= l2b->l2b_occupancy);
1606
1607 /*
1608 * Update the bucket's reference count according to how many
1609 * PTEs the caller has just invalidated.
1610 */
1611 l2b->l2b_occupancy -= count;
1612
1613 /*
1614 * Note:
1615 *
1616 * Level 2 page tables allocated to the kernel pmap are never freed
1617 * as that would require checking all Level 1 page tables and
1618 * removing any references to the Level 2 page table. See also the
1619 * comment elsewhere about never freeing bootstrap L2 descriptors.
1620 *
1621 * We make do with just invalidating the mapping in the L2 table.
1622 *
1623 * This isn't really a big deal in practice and, in fact, leads
1624 * to a performance win over time as we don't need to continually
1625 * alloc/free.
1626 */
1627 if (l2b->l2b_occupancy > 0 || pm == pmap_kernel())
1628 return;
1629
1630 /*
1631 * There are no more valid mappings in this level 2 page table.
1632 * Go ahead and NULL-out the pointer in the bucket, then
1633 * free the page table.
1634 */
1635 const size_t l1slot = l2b->l2b_l1slot;
1636 pt_entry_t * const ptep = l2b->l2b_kva;
1637 l2b->l2b_kva = NULL;
1638
1639 pd_entry_t * const pdep = pmap_l1_kva(pm) + l1slot;
1640 pd_entry_t pde __diagused = *pdep;
1641
1642 #ifdef ARM_MMU_EXTENDED
1643 /*
1644 * Invalidate the L1 slot.
1645 */
1646 KASSERT((pde & L1_TYPE_MASK) == L1_TYPE_C);
1647 #else
1648 /*
1649 * If the L1 slot matches the pmap's domain number, then invalidate it.
1650 */
1651 if ((pde & (L1_C_DOM_MASK|L1_TYPE_MASK))
1652 == (L1_C_DOM(pmap_domain(pm))|L1_TYPE_C)) {
1653 #endif
1654 l1pte_setone(pdep, 0);
1655 PDE_SYNC(pdep);
1656 #ifndef ARM_MMU_EXTENDED
1657 }
1658 #endif
1659
1660 /*
1661 * Release the L2 descriptor table back to the pool cache.
1662 */
1663 #if defined(PMAP_INCLUDE_PTE_SYNC) && defined(PMAP_CACHE_VIVT)
1664 pmap_free_l2_ptp(!pmap_is_cached(pm), ptep, l2b->l2b_pa);
1665 #else
1666 pmap_free_l2_ptp(ptep, l2b->l2b_pa);
1667 #endif
1668
1669 /*
1670 * Update the reference count in the associated l2_dtable
1671 */
1672 struct l2_dtable * const l2 = pm->pm_l2[L2_IDX(l1slot)];
1673 if (--l2->l2_occupancy > 0)
1674 return;
1675
1676 /*
1677 * There are no more valid mappings in any of the Level 1
1678 * slots managed by this l2_dtable. Go ahead and NULL-out
1679 * the pointer in the parent pmap and free the l2_dtable.
1680 */
1681 pm->pm_l2[L2_IDX(l1slot)] = NULL;
1682 pmap_free_l2_dtable(l2);
1683 }
1684
1685 #if defined(ARM_MMU_EXTENDED)
1686 /*
1687 * Pool cache constructors for L1 translation tables
1688 */
1689
1690 static int
1691 pmap_l1tt_ctor(void *arg, void *v, int flags)
1692 {
1693 #ifndef PMAP_INCLUDE_PTE_SYNC
1694 #error not supported
1695 #endif
1696
1697 memset(v, 0, L1TT_SIZE);
1698 PTE_SYNC_RANGE(v, L1TT_SIZE / sizeof(pt_entry_t));
1699 return 0;
1700 }
1701 #endif
1702
1703 /*
1704 * Pool cache constructors for L2 descriptor tables, metadata and pmap
1705 * structures.
1706 */
1707 static int
1708 pmap_l2ptp_ctor(void *arg, void *v, int flags)
1709 {
1710 #ifndef PMAP_INCLUDE_PTE_SYNC
1711 vaddr_t va = (vaddr_t)v & ~PGOFSET;
1712
1713 /*
1714 * The mappings for these page tables were initially made using
1715 * pmap_kenter_pa() by the pool subsystem. Therefore, the cache-
1716 * mode will not be right for page table mappings. To avoid
1717 * polluting the pmap_kenter_pa() code with a special case for
1718 * page tables, we simply fix up the cache-mode here if it's not
1719 * correct.
1720 */
1721 if (pte_l2_s_cache_mode != pte_l2_s_cache_mode_pt) {
1722 const struct l2_bucket * const l2b =
1723 pmap_get_l2_bucket(pmap_kernel(), va);
1724 KASSERTMSG(l2b != NULL, "%#lx", va);
1725 pt_entry_t * const ptep = &l2b->l2b_kva[l2pte_index(va)];
1726 const pt_entry_t opte = *ptep;
1727
1728 if ((opte & L2_S_CACHE_MASK) != pte_l2_s_cache_mode_pt) {
1729 /*
1730 * Page tables must have the cache-mode set correctly.
1731 */
1732 const pt_entry_t npte = (opte & ~L2_S_CACHE_MASK)
1733 | pte_l2_s_cache_mode_pt;
1734 l2pte_set(ptep, npte, opte);
1735 PTE_SYNC(ptep);
1736 cpu_tlb_flushD_SE(va);
1737 cpu_cpwait();
1738 }
1739 }
1740 #endif
1741
1742 memset(v, 0, L2_TABLE_SIZE_REAL);
1743 PTE_SYNC_RANGE(v, L2_TABLE_SIZE_REAL / sizeof(pt_entry_t));
1744 return 0;
1745 }
1746
1747 static int
1748 pmap_l2dtable_ctor(void *arg, void *v, int flags)
1749 {
1750
1751 memset(v, 0, sizeof(struct l2_dtable));
1752 return 0;
1753 }
1754
1755 static int
1756 pmap_pmap_ctor(void *arg, void *v, int flags)
1757 {
1758
1759 memset(v, 0, sizeof(struct pmap));
1760 return 0;
1761 }
1762
1763 static void
1764 pmap_pinit(pmap_t pm)
1765 {
1766 #ifndef ARM_HAS_VBAR
1767 struct l2_bucket *l2b;
1768
1769 if (vector_page < KERNEL_BASE) {
1770 /*
1771 * Map the vector page.
1772 */
1773 pmap_enter(pm, vector_page, systempage.pv_pa,
1774 VM_PROT_READ | VM_PROT_EXECUTE,
1775 VM_PROT_READ | VM_PROT_EXECUTE | PMAP_WIRED);
1776 pmap_update(pm);
1777
1778 pm->pm_pl1vec = pmap_l1_kva(pm) + l1pte_index(vector_page);
1779 l2b = pmap_get_l2_bucket(pm, vector_page);
1780 KASSERTMSG(l2b != NULL, "%#lx", vector_page);
1781 pm->pm_l1vec = l2b->l2b_pa | L1_C_PROTO |
1782 L1_C_DOM(pmap_domain(pm));
1783 } else
1784 pm->pm_pl1vec = NULL;
1785 #endif
1786 }
1787
1788 #ifdef PMAP_CACHE_VIVT
1789 /*
1790 * Since we have a virtually indexed cache, we may need to inhibit caching if
1791 * there is more than one mapping and at least one of them is writable.
1792 * Since we purge the cache on every context switch, we only need to check for
1793 * other mappings within the same pmap, or kernel_pmap.
1794 * This function is also called when a page is unmapped, to possibly reenable
1795 * caching on any remaining mappings.
1796 *
1797 * The code implements the following logic, where:
1798 *
1799 * KW = # of kernel read/write pages
1800 * KR = # of kernel read only pages
1801 * UW = # of user read/write pages
1802 * UR = # of user read only pages
1803 *
1804 * KC = kernel mapping is cacheable
1805 * UC = user mapping is cacheable
1806 *
1807 * KW=0,KR=0 KW=0,KR>0 KW=1,KR=0 KW>1,KR>=0
1808 * +---------------------------------------------
1809 * UW=0,UR=0 | --- KC=1 KC=1 KC=0
1810 * UW=0,UR>0 | UC=1 KC=1,UC=1 KC=0,UC=0 KC=0,UC=0
1811 * UW=1,UR=0 | UC=1 KC=0,UC=0 KC=0,UC=0 KC=0,UC=0
1812 * UW>1,UR>=0 | UC=0 KC=0,UC=0 KC=0,UC=0 KC=0,UC=0
1813 */
1814
1815 static const int pmap_vac_flags[4][4] = {
1816 {-1, 0, 0, PVF_KNC},
1817 {0, 0, PVF_NC, PVF_NC},
1818 {0, PVF_NC, PVF_NC, PVF_NC},
1819 {PVF_UNC, PVF_NC, PVF_NC, PVF_NC}
1820 };
1821
1822 static inline int
1823 pmap_get_vac_flags(const struct vm_page_md *md)
1824 {
1825 int kidx, uidx;
1826
1827 kidx = 0;
1828 if (md->kro_mappings || md->krw_mappings > 1)
1829 kidx |= 1;
1830 if (md->krw_mappings)
1831 kidx |= 2;
1832
1833 uidx = 0;
1834 if (md->uro_mappings || md->urw_mappings > 1)
1835 uidx |= 1;
1836 if (md->urw_mappings)
1837 uidx |= 2;
1838
1839 return pmap_vac_flags[uidx][kidx];
1840 }
1841
1842 static inline void
1843 pmap_vac_me_harder(struct vm_page_md *md, paddr_t pa, pmap_t pm, vaddr_t va)
1844 {
1845 int nattr;
1846
1847 nattr = pmap_get_vac_flags(md);
1848
1849 if (nattr < 0) {
1850 md->pvh_attrs &= ~PVF_NC;
1851 return;
1852 }
1853
1854 if (nattr == 0 && (md->pvh_attrs & PVF_NC) == 0)
1855 return;
1856
1857 if (pm == pmap_kernel())
1858 pmap_vac_me_kpmap(md, pa, pm, va);
1859 else
1860 pmap_vac_me_user(md, pa, pm, va);
1861
1862 md->pvh_attrs = (md->pvh_attrs & ~PVF_NC) | nattr;
1863 }
1864
1865 static void
1866 pmap_vac_me_kpmap(struct vm_page_md *md, paddr_t pa, pmap_t pm, vaddr_t va)
1867 {
1868 u_int u_cacheable, u_entries;
1869 struct pv_entry *pv;
1870 pmap_t last_pmap = pm;
1871
1872 /*
1873 * Pass one, see if there are both kernel and user pmaps for
1874 * this page. Calculate whether there are user-writable or
1875 * kernel-writable pages.
1876 */
1877 u_cacheable = 0;
1878 SLIST_FOREACH(pv, &md->pvh_list, pv_link) {
1879 if (pv->pv_pmap != pm && (pv->pv_flags & PVF_NC) == 0)
1880 u_cacheable++;
1881 }
1882
1883 u_entries = md->urw_mappings + md->uro_mappings;
1884
1885 /*
1886 * We know we have just been updating a kernel entry, so if
1887 * all user pages are already cacheable, then there is nothing
1888 * further to do.
1889 */
1890 if (md->k_mappings == 0 && u_cacheable == u_entries)
1891 return;
1892
1893 if (u_entries) {
1894 /*
1895 * Scan over the list again, for each entry, if it
1896 * might not be set correctly, call pmap_vac_me_user
1897 * to recalculate the settings.
1898 */
1899 SLIST_FOREACH(pv, &md->pvh_list, pv_link) {
1900 /*
1901 * We know kernel mappings will get set
1902 * correctly in other calls. We also know
1903 * that if the pmap is the same as last_pmap
1904 * then we've just handled this entry.
1905 */
1906 if (pv->pv_pmap == pm || pv->pv_pmap == last_pmap)
1907 continue;
1908
1909 /*
1910 * If there are kernel entries and this page
1911 * is writable but non-cacheable, then we can
1912 * skip this entry also.
1913 */
1914 if (md->k_mappings &&
1915 (pv->pv_flags & (PVF_NC | PVF_WRITE)) ==
1916 (PVF_NC | PVF_WRITE))
1917 continue;
1918
1919 /*
1920 * Similarly if there are no kernel-writable
1921 * entries and the page is already
1922 * read-only/cacheable.
1923 */
1924 if (md->krw_mappings == 0 &&
1925 (pv->pv_flags & (PVF_NC | PVF_WRITE)) == 0)
1926 continue;
1927
1928 /*
1929 * For some of the remaining cases, we know
1930 * that we must recalculate, but for others we
1931 * can't tell if they are correct or not, so
1932 * we recalculate anyway.
1933 */
1934 pmap_vac_me_user(md, pa, (last_pmap = pv->pv_pmap), 0);
1935 }
1936
1937 if (md->k_mappings == 0)
1938 return;
1939 }
1940
1941 pmap_vac_me_user(md, pa, pm, va);
1942 }
1943
1944 static void
1945 pmap_vac_me_user(struct vm_page_md *md, paddr_t pa, pmap_t pm, vaddr_t va)
1946 {
1947 pmap_t kpmap = pmap_kernel();
1948 struct pv_entry *pv, *npv = NULL;
1949 u_int entries = 0;
1950 u_int writable = 0;
1951 u_int cacheable_entries = 0;
1952 u_int kern_cacheable = 0;
1953 u_int other_writable = 0;
1954
1955 /*
1956 * Count mappings and writable mappings in this pmap.
1957 * Include kernel mappings as part of our own.
1958 * Keep a pointer to the first one.
1959 */
1960 npv = NULL;
1961 KASSERT(pmap_page_locked_p(md));
1962 SLIST_FOREACH(pv, &md->pvh_list, pv_link) {
1963 /* Count mappings in the same pmap */
1964 if (pm == pv->pv_pmap || kpmap == pv->pv_pmap) {
1965 if (entries++ == 0)
1966 npv = pv;
1967
1968 /* Cacheable mappings */
1969 if ((pv->pv_flags & PVF_NC) == 0) {
1970 cacheable_entries++;
1971 if (kpmap == pv->pv_pmap)
1972 kern_cacheable++;
1973 }
1974
1975 /* Writable mappings */
1976 if (pv->pv_flags & PVF_WRITE)
1977 ++writable;
1978 } else if (pv->pv_flags & PVF_WRITE)
1979 other_writable = 1;
1980 }
1981
1982 /*
1983 * Enable or disable caching as necessary.
1984 * Note: the first entry might be part of the kernel pmap,
1985 * so we can't assume this is indicative of the state of the
1986 * other (maybe non-kpmap) entries.
1987 */
1988 if ((entries > 1 && writable) ||
1989 (entries > 0 && pm == kpmap && other_writable)) {
1990 if (cacheable_entries == 0) {
1991 return;
1992 }
1993
1994 for (pv = npv; pv; pv = SLIST_NEXT(pv, pv_link)) {
1995 if ((pm != pv->pv_pmap && kpmap != pv->pv_pmap) ||
1996 (pv->pv_flags & PVF_NC))
1997 continue;
1998
1999 pv->pv_flags |= PVF_NC;
2000
2001 struct l2_bucket * const l2b
2002 = pmap_get_l2_bucket(pv->pv_pmap, pv->pv_va);
2003 KASSERTMSG(l2b != NULL, "%#lx", va);
2004 pt_entry_t * const ptep
2005 = &l2b->l2b_kva[l2pte_index(pv->pv_va)];
2006 const pt_entry_t opte = *ptep;
2007 pt_entry_t npte = opte & ~L2_S_CACHE_MASK;
2008
2009 if ((va != pv->pv_va || pm != pv->pv_pmap)
2010 && l2pte_valid_p(opte)) {
2011 pmap_cache_wbinv_page(pv->pv_pmap, pv->pv_va,
2012 true, pv->pv_flags);
2013 pmap_tlb_flush_SE(pv->pv_pmap, pv->pv_va,
2014 pv->pv_flags);
2015 }
2016
2017 l2pte_set(ptep, npte, opte);
2018 PTE_SYNC_CURRENT(pv->pv_pmap, ptep);
2019 }
2020 cpu_cpwait();
2021 } else if (entries > cacheable_entries) {
2022 /*
2023 * Turn cacheing back on for some pages. If it is a kernel
2024 * page, only do so if there are no other writable pages.
2025 */
2026 for (pv = npv; pv; pv = SLIST_NEXT(pv, pv_link)) {
2027 if (!(pv->pv_flags & PVF_NC) || (pm != pv->pv_pmap &&
2028 (kpmap != pv->pv_pmap || other_writable)))
2029 continue;
2030
2031 pv->pv_flags &= ~PVF_NC;
2032
2033 struct l2_bucket * const l2b
2034 = pmap_get_l2_bucket(pv->pv_pmap, pv->pv_va);
2035 KASSERTMSG(l2b != NULL, "%#lx", va);
2036 pt_entry_t * const ptep
2037 = &l2b->l2b_kva[l2pte_index(pv->pv_va)];
2038 const pt_entry_t opte = *ptep;
2039 pt_entry_t npte = (opte & ~L2_S_CACHE_MASK)
2040 | pte_l2_s_cache_mode;
2041
2042 if (l2pte_valid_p(opte)) {
2043 pmap_tlb_flush_SE(pv->pv_pmap, pv->pv_va,
2044 pv->pv_flags);
2045 }
2046
2047 l2pte_set(ptep, npte, opte);
2048 PTE_SYNC_CURRENT(pv->pv_pmap, ptep);
2049 }
2050 }
2051 }
2052 #endif
2053
2054 #ifdef PMAP_CACHE_VIPT
2055 static void
2056 pmap_vac_me_harder(struct vm_page_md *md, paddr_t pa, pmap_t pm, vaddr_t va)
2057 {
2058 #ifndef ARM_MMU_EXTENDED
2059 struct pv_entry *pv;
2060 vaddr_t tst_mask;
2061 bool bad_alias;
2062 const u_int
2063 rw_mappings = md->urw_mappings + md->krw_mappings,
2064 ro_mappings = md->uro_mappings + md->kro_mappings;
2065
2066 /* do we need to do anything? */
2067 if (arm_cache_prefer_mask == 0)
2068 return;
2069
2070 NPDEBUG(PDB_VAC, printf("pmap_vac_me_harder: md=%p, pmap=%p va=%08lx\n",
2071 md, pm, va));
2072
2073 KASSERT(!va || pm);
2074 KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC)));
2075
2076 /* Already a conflict? */
2077 if (__predict_false(md->pvh_attrs & PVF_NC)) {
2078 /* just an add, things are already non-cached */
2079 KASSERT(!(md->pvh_attrs & PVF_DIRTY));
2080 KASSERT(!(md->pvh_attrs & PVF_MULTCLR));
2081 bad_alias = false;
2082 if (va) {
2083 PMAPCOUNT(vac_color_none);
2084 bad_alias = true;
2085 KASSERT((rw_mappings == 0) == !(md->pvh_attrs & PVF_WRITE));
2086 goto fixup;
2087 }
2088 pv = SLIST_FIRST(&md->pvh_list);
2089 /* the list can't be empty because it would be cachable */
2090 if (md->pvh_attrs & PVF_KMPAGE) {
2091 tst_mask = md->pvh_attrs;
2092 } else {
2093 KASSERT(pv);
2094 tst_mask = pv->pv_va;
2095 pv = SLIST_NEXT(pv, pv_link);
2096 }
2097 /*
2098 * Only check for a bad alias if we have writable mappings.
2099 */
2100 tst_mask &= arm_cache_prefer_mask;
2101 if (rw_mappings > 0) {
2102 for (; pv && !bad_alias; pv = SLIST_NEXT(pv, pv_link)) {
2103 /* if there's a bad alias, stop checking. */
2104 if (tst_mask != (pv->pv_va & arm_cache_prefer_mask))
2105 bad_alias = true;
2106 }
2107 md->pvh_attrs |= PVF_WRITE;
2108 if (!bad_alias)
2109 md->pvh_attrs |= PVF_DIRTY;
2110 } else {
2111 /*
2112 * We have only read-only mappings. Let's see if there
2113 * are multiple colors in use or if we mapped a KMPAGE.
2114 * If the latter, we have a bad alias. If the former,
2115 * we need to remember that.
2116 */
2117 for (; pv; pv = SLIST_NEXT(pv, pv_link)) {
2118 if (tst_mask != (pv->pv_va & arm_cache_prefer_mask)) {
2119 if (md->pvh_attrs & PVF_KMPAGE)
2120 bad_alias = true;
2121 break;
2122 }
2123 }
2124 md->pvh_attrs &= ~PVF_WRITE;
2125 /*
2126 * No KMPAGE and we exited early, so we must have
2127 * multiple color mappings.
2128 */
2129 if (!bad_alias && pv != NULL)
2130 md->pvh_attrs |= PVF_MULTCLR;
2131 }
2132
2133 /* If no conflicting colors, set everything back to cached */
2134 if (!bad_alias) {
2135 #ifdef DEBUG
2136 if ((md->pvh_attrs & PVF_WRITE)
2137 || ro_mappings < 2) {
2138 SLIST_FOREACH(pv, &md->pvh_list, pv_link)
2139 KDASSERT(((tst_mask ^ pv->pv_va) & arm_cache_prefer_mask) == 0);
2140 }
2141 #endif
2142 md->pvh_attrs &= (PAGE_SIZE - 1) & ~PVF_NC;
2143 md->pvh_attrs |= tst_mask | PVF_COLORED;
2144 /*
2145 * Restore DIRTY bit if page is modified
2146 */
2147 if (md->pvh_attrs & PVF_DMOD)
2148 md->pvh_attrs |= PVF_DIRTY;
2149 PMAPCOUNT(vac_color_restore);
2150 } else {
2151 KASSERT(SLIST_FIRST(&md->pvh_list) != NULL);
2152 KASSERT(SLIST_NEXT(SLIST_FIRST(&md->pvh_list), pv_link) != NULL);
2153 }
2154 KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC)));
2155 KASSERT((rw_mappings == 0) == !(md->pvh_attrs & PVF_WRITE));
2156 } else if (!va) {
2157 KASSERT(pmap_is_page_colored_p(md));
2158 KASSERT(!(md->pvh_attrs & PVF_WRITE)
2159 || (md->pvh_attrs & PVF_DIRTY));
2160 if (rw_mappings == 0) {
2161 md->pvh_attrs &= ~PVF_WRITE;
2162 if (ro_mappings == 1
2163 && (md->pvh_attrs & PVF_MULTCLR)) {
2164 /*
2165 * If this is the last readonly mapping
2166 * but it doesn't match the current color
2167 * for the page, change the current color
2168 * to match this last readonly mapping.
2169 */
2170 pv = SLIST_FIRST(&md->pvh_list);
2171 tst_mask = (md->pvh_attrs ^ pv->pv_va)
2172 & arm_cache_prefer_mask;
2173 if (tst_mask) {
2174 md->pvh_attrs ^= tst_mask;
2175 PMAPCOUNT(vac_color_change);
2176 }
2177 }
2178 }
2179 KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC)));
2180 KASSERT((rw_mappings == 0) == !(md->pvh_attrs & PVF_WRITE));
2181 return;
2182 } else if (!pmap_is_page_colored_p(md)) {
2183 /* not colored so we just use its color */
2184 KASSERT(md->pvh_attrs & (PVF_WRITE|PVF_DIRTY));
2185 KASSERT(!(md->pvh_attrs & PVF_MULTCLR));
2186 PMAPCOUNT(vac_color_new);
2187 md->pvh_attrs &= PAGE_SIZE - 1;
2188 md->pvh_attrs |= PVF_COLORED
2189 | (va & arm_cache_prefer_mask)
2190 | (rw_mappings > 0 ? PVF_WRITE : 0);
2191 KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC)));
2192 KASSERT((rw_mappings == 0) == !(md->pvh_attrs & PVF_WRITE));
2193 return;
2194 } else if (((md->pvh_attrs ^ va) & arm_cache_prefer_mask) == 0) {
2195 bad_alias = false;
2196 if (rw_mappings > 0) {
2197 /*
2198 * We now have writeable mappings and if we have
2199 * readonly mappings in more than once color, we have
2200 * an aliasing problem. Regardless mark the page as
2201 * writeable.
2202 */
2203 if (md->pvh_attrs & PVF_MULTCLR) {
2204 if (ro_mappings < 2) {
2205 /*
2206 * If we only have less than two
2207 * read-only mappings, just flush the
2208 * non-primary colors from the cache.
2209 */
2210 pmap_flush_page(md, pa,
2211 PMAP_FLUSH_SECONDARY);
2212 } else {
2213 bad_alias = true;
2214 }
2215 }
2216 md->pvh_attrs |= PVF_WRITE;
2217 }
2218 /* If no conflicting colors, set everything back to cached */
2219 if (!bad_alias) {
2220 #ifdef DEBUG
2221 if (rw_mappings > 0
2222 || (md->pvh_attrs & PMAP_KMPAGE)) {
2223 tst_mask = md->pvh_attrs & arm_cache_prefer_mask;
2224 SLIST_FOREACH(pv, &md->pvh_list, pv_link)
2225 KDASSERT(((tst_mask ^ pv->pv_va) & arm_cache_prefer_mask) == 0);
2226 }
2227 #endif
2228 if (SLIST_EMPTY(&md->pvh_list))
2229 PMAPCOUNT(vac_color_reuse);
2230 else
2231 PMAPCOUNT(vac_color_ok);
2232
2233 /* matching color, just return */
2234 KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC)));
2235 KASSERT((rw_mappings == 0) == !(md->pvh_attrs & PVF_WRITE));
2236 return;
2237 }
2238 KASSERT(SLIST_FIRST(&md->pvh_list) != NULL);
2239 KASSERT(SLIST_NEXT(SLIST_FIRST(&md->pvh_list), pv_link) != NULL);
2240
2241 /* color conflict. evict from cache. */
2242
2243 pmap_flush_page(md, pa, PMAP_FLUSH_PRIMARY);
2244 md->pvh_attrs &= ~PVF_COLORED;
2245 md->pvh_attrs |= PVF_NC;
2246 KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC)));
2247 KASSERT(!(md->pvh_attrs & PVF_MULTCLR));
2248 PMAPCOUNT(vac_color_erase);
2249 } else if (rw_mappings == 0
2250 && (md->pvh_attrs & PVF_KMPAGE) == 0) {
2251 KASSERT((md->pvh_attrs & PVF_WRITE) == 0);
2252
2253 /*
2254 * If the page has dirty cache lines, clean it.
2255 */
2256 if (md->pvh_attrs & PVF_DIRTY)
2257 pmap_flush_page(md, pa, PMAP_CLEAN_PRIMARY);
2258
2259 /*
2260 * If this is the first remapping (we know that there are no
2261 * writeable mappings), then this is a simple color change.
2262 * Otherwise this is a seconary r/o mapping, which means
2263 * we don't have to do anything.
2264 */
2265 if (ro_mappings == 1) {
2266 KASSERT(((md->pvh_attrs ^ va) & arm_cache_prefer_mask) != 0);
2267 md->pvh_attrs &= PAGE_SIZE - 1;
2268 md->pvh_attrs |= (va & arm_cache_prefer_mask);
2269 PMAPCOUNT(vac_color_change);
2270 } else {
2271 PMAPCOUNT(vac_color_blind);
2272 }
2273 md->pvh_attrs |= PVF_MULTCLR;
2274 KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC)));
2275 KASSERT((rw_mappings == 0) == !(md->pvh_attrs & PVF_WRITE));
2276 return;
2277 } else {
2278 if (rw_mappings > 0)
2279 md->pvh_attrs |= PVF_WRITE;
2280
2281 /* color conflict. evict from cache. */
2282 pmap_flush_page(md, pa, PMAP_FLUSH_PRIMARY);
2283
2284 /* the list can't be empty because this was a enter/modify */
2285 pv = SLIST_FIRST(&md->pvh_list);
2286 if ((md->pvh_attrs & PVF_KMPAGE) == 0) {
2287 KASSERT(pv);
2288 /*
2289 * If there's only one mapped page, change color to the
2290 * page's new color and return. Restore the DIRTY bit
2291 * that was erased by pmap_flush_page.
2292 */
2293 if (SLIST_NEXT(pv, pv_link) == NULL) {
2294 md->pvh_attrs &= PAGE_SIZE - 1;
2295 md->pvh_attrs |= (va & arm_cache_prefer_mask);
2296 if (md->pvh_attrs & PVF_DMOD)
2297 md->pvh_attrs |= PVF_DIRTY;
2298 PMAPCOUNT(vac_color_change);
2299 KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC)));
2300 KASSERT((rw_mappings == 0) == !(md->pvh_attrs & PVF_WRITE));
2301 KASSERT(!(md->pvh_attrs & PVF_MULTCLR));
2302 return;
2303 }
2304 }
2305 bad_alias = true;
2306 md->pvh_attrs &= ~PVF_COLORED;
2307 md->pvh_attrs |= PVF_NC;
2308 PMAPCOUNT(vac_color_erase);
2309 KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC)));
2310 }
2311
2312 fixup:
2313 KASSERT((rw_mappings == 0) == !(md->pvh_attrs & PVF_WRITE));
2314
2315 /*
2316 * Turn cacheing on/off for all pages.
2317 */
2318 SLIST_FOREACH(pv, &md->pvh_list, pv_link) {
2319 struct l2_bucket * const l2b = pmap_get_l2_bucket(pv->pv_pmap,
2320 pv->pv_va);
2321 KASSERTMSG(l2b != NULL, "%#lx", va);
2322 pt_entry_t * const ptep = &l2b->l2b_kva[l2pte_index(pv->pv_va)];
2323 const pt_entry_t opte = *ptep;
2324 pt_entry_t npte = opte & ~L2_S_CACHE_MASK;
2325 if (bad_alias) {
2326 pv->pv_flags |= PVF_NC;
2327 } else {
2328 pv->pv_flags &= ~PVF_NC;
2329 npte |= pte_l2_s_cache_mode;
2330 }
2331
2332 if (opte == npte) /* only update is there's a change */
2333 continue;
2334
2335 if (l2pte_valid_p(opte)) {
2336 pmap_tlb_flush_SE(pv->pv_pmap, pv->pv_va, pv->pv_flags);
2337 }
2338
2339 l2pte_set(ptep, npte, opte);
2340 PTE_SYNC_CURRENT(pv->pv_pmap, ptep);
2341 }
2342 #endif /* !ARM_MMU_EXTENDED */
2343 }
2344 #endif /* PMAP_CACHE_VIPT */
2345
2346
2347 /*
2348 * Modify pte bits for all ptes corresponding to the given physical address.
2349 * We use `maskbits' rather than `clearbits' because we're always passing
2350 * constants and the latter would require an extra inversion at run-time.
2351 */
2352 static void
2353 pmap_clearbit(struct vm_page_md *md, paddr_t pa, u_int maskbits)
2354 {
2355 struct pv_entry *pv;
2356 #ifdef PMAP_CACHE_VIPT
2357 const bool want_syncicache = PV_IS_EXEC_P(md->pvh_attrs);
2358 bool need_syncicache = false;
2359 #ifdef ARM_MMU_EXTENDED
2360 const u_int execbits = (maskbits & PVF_EXEC) ? L2_XS_XN : 0;
2361 #else
2362 const u_int execbits = 0;
2363 bool need_vac_me_harder = false;
2364 #endif
2365 #else
2366 const u_int execbits = 0;
2367 #endif
2368
2369 NPDEBUG(PDB_BITS,
2370 printf("pmap_clearbit: md %p mask 0x%x\n",
2371 md, maskbits));
2372
2373 #ifdef PMAP_CACHE_VIPT
2374 /*
2375 * If we might want to sync the I-cache and we've modified it,
2376 * then we know we definitely need to sync or discard it.
2377 */
2378 if (want_syncicache) {
2379 if (md->pvh_attrs & PVF_MOD) {
2380 need_syncicache = true;
2381 }
2382 }
2383 #endif
2384 KASSERT(pmap_page_locked_p(md));
2385
2386 /*
2387 * Clear saved attributes (modify, reference)
2388 */
2389 md->pvh_attrs &= ~(maskbits & (PVF_MOD | PVF_REF));
2390
2391 if (SLIST_EMPTY(&md->pvh_list)) {
2392 #if defined(PMAP_CACHE_VIPT)
2393 if (need_syncicache) {
2394 /*
2395 * No one has it mapped, so just discard it. The next
2396 * exec remapping will cause it to be synced.
2397 */
2398 md->pvh_attrs &= ~PVF_EXEC;
2399 PMAPCOUNT(exec_discarded_clearbit);
2400 }
2401 #endif
2402 return;
2403 }
2404
2405 /*
2406 * Loop over all current mappings setting/clearing as appropos
2407 */
2408 SLIST_FOREACH(pv, &md->pvh_list, pv_link) {
2409 pmap_t pm = pv->pv_pmap;
2410 const vaddr_t va = pv->pv_va;
2411 const u_int oflags = pv->pv_flags;
2412 #ifndef ARM_MMU_EXTENDED
2413 /*
2414 * Kernel entries are unmanaged and as such not to be changed.
2415 */
2416 if (PV_IS_KENTRY_P(oflags))
2417 continue;
2418 #endif
2419 pv->pv_flags &= ~maskbits;
2420
2421 pmap_release_page_lock(md);
2422 pmap_acquire_pmap_lock(pm);
2423
2424 struct l2_bucket * const l2b = pmap_get_l2_bucket(pm, va);
2425 if (l2b == NULL) {
2426 pmap_release_pmap_lock(pm);
2427 pmap_acquire_page_lock(md);
2428 continue;
2429 }
2430 KASSERTMSG(l2b != NULL, "%#lx", va);
2431
2432 pt_entry_t * const ptep = &l2b->l2b_kva[l2pte_index(va)];
2433 const pt_entry_t opte = *ptep;
2434 pt_entry_t npte = opte | execbits;
2435
2436 #ifdef ARM_MMU_EXTENDED
2437 KASSERT((opte & L2_XS_nG) == (pm == pmap_kernel() ? 0 : L2_XS_nG));
2438 #endif
2439
2440 NPDEBUG(PDB_BITS,
2441 printf( "%s: pv %p, pm %p, va 0x%08lx, flag 0x%x\n",
2442 __func__, pv, pm, va, oflags));
2443
2444 if (maskbits & (PVF_WRITE|PVF_MOD)) {
2445 #ifdef PMAP_CACHE_VIVT
2446 if ((oflags & PVF_NC)) {
2447 /*
2448 * Entry is not cacheable:
2449 *
2450 * Don't turn caching on again if this is a
2451 * modified emulation. This would be
2452 * inconsitent with the settings created by
2453 * pmap_vac_me_harder(). Otherwise, it's safe
2454 * to re-enable cacheing.
2455 *
2456 * There's no need to call pmap_vac_me_harder()
2457 * here: all pages are losing their write
2458 * permission.
2459 */
2460 if (maskbits & PVF_WRITE) {
2461 npte |= pte_l2_s_cache_mode;
2462 pv->pv_flags &= ~PVF_NC;
2463 }
2464 } else if (l2pte_writable_p(opte)) {
2465 /*
2466 * Entry is writable/cacheable: check if pmap
2467 * is current if it is flush it, otherwise it
2468 * won't be in the cache
2469 */
2470 pmap_cache_wbinv_page(pm, va,
2471 (maskbits & PVF_REF) != 0,
2472 oflags|PVF_WRITE);
2473 }
2474 #endif
2475
2476 /* make the pte read only */
2477 npte = l2pte_set_readonly(npte);
2478
2479 pmap_acquire_page_lock(md);
2480 #ifdef MULTIPROCESSOR
2481 pv = pmap_find_pv(md, pm, va);
2482 #endif
2483 if (pv != NULL && (maskbits & oflags & PVF_WRITE)) {
2484 /*
2485 * Keep alias accounting up to date
2486 */
2487 if (pm == pmap_kernel()) {
2488 md->krw_mappings--;
2489 md->kro_mappings++;
2490 } else {
2491 md->urw_mappings--;
2492 md->uro_mappings++;
2493 }
2494 #ifdef PMAP_CACHE_VIPT
2495 if (arm_cache_prefer_mask != 0) {
2496 if (md->urw_mappings + md->krw_mappings == 0) {
2497 md->pvh_attrs &= ~PVF_WRITE;
2498 } else {
2499 PMAP_VALIDATE_MD_PAGE(md);
2500 }
2501 }
2502 if (want_syncicache)
2503 need_syncicache = true;
2504 #ifndef ARM_MMU_EXTENDED
2505 need_vac_me_harder = true;
2506 #endif
2507 #endif /* PMAP_CACHE_VIPT */
2508 }
2509 pmap_release_page_lock(md);
2510 }
2511
2512 if (maskbits & PVF_REF) {
2513 if (true
2514 #ifndef ARM_MMU_EXTENDED
2515 && (oflags & PVF_NC) == 0
2516 #endif
2517 && (maskbits & (PVF_WRITE|PVF_MOD)) == 0
2518 && l2pte_valid_p(npte)) {
2519 #ifdef PMAP_CACHE_VIVT
2520 /*
2521 * Check npte here; we may have already
2522 * done the wbinv above, and the validity
2523 * of the PTE is the same for opte and
2524 * npte.
2525 */
2526 pmap_cache_wbinv_page(pm, va, true, oflags);
2527 #endif
2528 }
2529
2530 /*
2531 * Make the PTE invalid so that we will take a
2532 * page fault the next time the mapping is
2533 * referenced.
2534 */
2535 npte &= ~L2_TYPE_MASK;
2536 npte |= L2_TYPE_INV;
2537 }
2538
2539 if (npte != opte) {
2540 l2pte_reset(ptep);
2541 PTE_SYNC(ptep);
2542
2543 /* Flush the TLB entry if a current pmap. */
2544 pmap_tlb_flush_SE(pm, va, oflags);
2545
2546 l2pte_set(ptep, npte, 0);
2547 PTE_SYNC(ptep);
2548 }
2549
2550 pmap_release_pmap_lock(pm);
2551 pmap_acquire_page_lock(md);
2552
2553 NPDEBUG(PDB_BITS,
2554 printf("pmap_clearbit: pm %p va 0x%lx opte 0x%08x npte 0x%08x\n",
2555 pm, va, opte, npte));
2556 }
2557
2558 #if defined(PMAP_CACHE_VIPT)
2559 /*
2560 * If we need to sync the I-cache and we haven't done it yet, do it.
2561 */
2562 if (need_syncicache) {
2563 pmap_release_page_lock(md);
2564 pmap_syncicache_page(md, pa);
2565 pmap_acquire_page_lock(md);
2566 PMAPCOUNT(exec_synced_clearbit);
2567 }
2568 #ifndef ARM_MMU_EXTENDED
2569 /*
2570 * If we are changing this to read-only, we need to call vac_me_harder
2571 * so we can change all the read-only pages to cacheable. We pretend
2572 * this as a page deletion.
2573 */
2574 if (need_vac_me_harder) {
2575 if (md->pvh_attrs & PVF_NC)
2576 pmap_vac_me_harder(md, pa, NULL, 0);
2577 }
2578 #endif /* !ARM_MMU_EXTENDED */
2579 #endif /* PMAP_CACHE_VIPT */
2580 }
2581
2582 /*
2583 * pmap_clean_page()
2584 *
2585 * This is a local function used to work out the best strategy to clean
2586 * a single page referenced by its entry in the PV table. It's used by
2587 * pmap_copy_page, pmap_zero_page and maybe some others later on.
2588 *
2589 * Its policy is effectively:
2590 * o If there are no mappings, we don't bother doing anything with the cache.
2591 * o If there is one mapping, we clean just that page.
2592 * o If there are multiple mappings, we clean the entire cache.
2593 *
2594 * So that some functions can be further optimised, it returns 0 if it didn't
2595 * clean the entire cache, or 1 if it did.
2596 *
2597 * XXX One bug in this routine is that if the pv_entry has a single page
2598 * mapped at 0x00000000 a whole cache clean will be performed rather than
2599 * just the 1 page. Since this should not occur in everyday use and if it does
2600 * it will just result in not the most efficient clean for the page.
2601 */
2602 #ifdef PMAP_CACHE_VIVT
2603 static bool
2604 pmap_clean_page(struct vm_page_md *md, bool is_src)
2605 {
2606 struct pv_entry *pv;
2607 pmap_t pm_to_clean = NULL;
2608 bool cache_needs_cleaning = false;
2609 vaddr_t page_to_clean = 0;
2610 u_int flags = 0;
2611
2612 /*
2613 * Since we flush the cache each time we change to a different
2614 * user vmspace, we only need to flush the page if it is in the
2615 * current pmap.
2616 */
2617 KASSERT(pmap_page_locked_p(md));
2618 SLIST_FOREACH(pv, &md->pvh_list, pv_link) {
2619 if (pmap_is_current(pv->pv_pmap)) {
2620 flags |= pv->pv_flags;
2621 /*
2622 * The page is mapped non-cacheable in
2623 * this map. No need to flush the cache.
2624 */
2625 if (pv->pv_flags & PVF_NC) {
2626 #ifdef DIAGNOSTIC
2627 KASSERT(!cache_needs_cleaning);
2628 #endif
2629 break;
2630 } else if (is_src && (pv->pv_flags & PVF_WRITE) == 0)
2631 continue;
2632 if (cache_needs_cleaning) {
2633 page_to_clean = 0;
2634 break;
2635 } else {
2636 page_to_clean = pv->pv_va;
2637 pm_to_clean = pv->pv_pmap;
2638 }
2639 cache_needs_cleaning = true;
2640 }
2641 }
2642
2643 if (page_to_clean) {
2644 pmap_cache_wbinv_page(pm_to_clean, page_to_clean,
2645 !is_src, flags | PVF_REF);
2646 } else if (cache_needs_cleaning) {
2647 pmap_t const pm = curproc->p_vmspace->vm_map.pmap;
2648
2649 pmap_cache_wbinv_all(pm, flags);
2650 return true;
2651 }
2652 return false;
2653 }
2654 #endif
2655
2656 #ifdef PMAP_CACHE_VIPT
2657 /*
2658 * Sync a page with the I-cache. Since this is a VIPT, we must pick the
2659 * right cache alias to make sure we flush the right stuff.
2660 */
2661 void
2662 pmap_syncicache_page(struct vm_page_md *md, paddr_t pa)
2663 {
2664 pmap_t kpm = pmap_kernel();
2665 const size_t way_size = arm_pcache.icache_type == CACHE_TYPE_PIPT
2666 ? PAGE_SIZE
2667 : arm_pcache.icache_way_size;
2668
2669 NPDEBUG(PDB_EXEC, printf("pmap_syncicache_page: md=%p (attrs=%#x)\n",
2670 md, md->pvh_attrs));
2671 /*
2672 * No need to clean the page if it's non-cached.
2673 */
2674 #ifndef ARM_MMU_EXTENDED
2675 if (md->pvh_attrs & PVF_NC)
2676 return;
2677 KASSERT(arm_cache_prefer_mask == 0 || md->pvh_attrs & PVF_COLORED);
2678 #endif
2679
2680 pt_entry_t * const ptep = cpu_cdst_pte(0);
2681 const vaddr_t dstp = cpu_cdstp(0);
2682 #ifdef __HAVE_MM_MD_DIRECT_MAPPED_PHYS
2683 if (way_size <= PAGE_SIZE) {
2684 bool ok = false;
2685 vaddr_t vdstp = pmap_direct_mapped_phys(pa, &ok, dstp);
2686 if (ok) {
2687 cpu_icache_sync_range(vdstp, way_size);
2688 return;
2689 }
2690 }
2691 #endif
2692
2693 /*
2694 * We don't worry about the color of the exec page, we map the
2695 * same page to pages in the way and then do the icache_sync on
2696 * the entire way making sure we are cleaned.
2697 */
2698 const pt_entry_t npte = L2_S_PROTO | pa | pte_l2_s_cache_mode
2699 | L2_S_PROT(PTE_KERNEL, VM_PROT_READ|VM_PROT_WRITE);
2700
2701 for (size_t i = 0, j = 0; i < way_size;
2702 i += PAGE_SIZE, j += PAGE_SIZE / L2_S_SIZE) {
2703 l2pte_reset(ptep + j);
2704 PTE_SYNC(ptep + j);
2705
2706 pmap_tlb_flush_SE(kpm, dstp + i, PVF_REF | PVF_EXEC);
2707 /*
2708 * Set up a PTE with to flush these cache lines.
2709 */
2710 l2pte_set(ptep + j, npte, 0);
2711 }
2712 PTE_SYNC_RANGE(ptep, way_size / L2_S_SIZE);
2713
2714 /*
2715 * Flush it.
2716 */
2717 cpu_icache_sync_range(dstp, way_size);
2718
2719 for (size_t i = 0, j = 0; i < way_size;
2720 i += PAGE_SIZE, j += PAGE_SIZE / L2_S_SIZE) {
2721 /*
2722 * Unmap the page(s).
2723 */
2724 l2pte_reset(ptep + j);
2725 pmap_tlb_flush_SE(kpm, dstp + i, PVF_REF | PVF_EXEC);
2726 }
2727 PTE_SYNC_RANGE(ptep, way_size / L2_S_SIZE);
2728
2729 md->pvh_attrs |= PVF_EXEC;
2730 PMAPCOUNT(exec_synced);
2731 }
2732
2733 #ifndef ARM_MMU_EXTENDED
2734 void
2735 pmap_flush_page(struct vm_page_md *md, paddr_t pa, enum pmap_flush_op flush)
2736 {
2737 vsize_t va_offset, end_va;
2738 bool wbinv_p;
2739
2740 if (arm_cache_prefer_mask == 0)
2741 return;
2742
2743 switch (flush) {
2744 case PMAP_FLUSH_PRIMARY:
2745 if (md->pvh_attrs & PVF_MULTCLR) {
2746 va_offset = 0;
2747 end_va = arm_cache_prefer_mask;
2748 md->pvh_attrs &= ~PVF_MULTCLR;
2749 PMAPCOUNT(vac_flush_lots);
2750 } else {
2751 va_offset = md->pvh_attrs & arm_cache_prefer_mask;
2752 end_va = va_offset;
2753 PMAPCOUNT(vac_flush_one);
2754 }
2755 /*
2756 * Mark that the page is no longer dirty.
2757 */
2758 md->pvh_attrs &= ~PVF_DIRTY;
2759 wbinv_p = true;
2760 break;
2761 case PMAP_FLUSH_SECONDARY:
2762 va_offset = 0;
2763 end_va = arm_cache_prefer_mask;
2764 wbinv_p = true;
2765 md->pvh_attrs &= ~PVF_MULTCLR;
2766 PMAPCOUNT(vac_flush_lots);
2767 break;
2768 case PMAP_CLEAN_PRIMARY:
2769 va_offset = md->pvh_attrs & arm_cache_prefer_mask;
2770 end_va = va_offset;
2771 wbinv_p = false;
2772 /*
2773 * Mark that the page is no longer dirty.
2774 */
2775 if ((md->pvh_attrs & PVF_DMOD) == 0)
2776 md->pvh_attrs &= ~PVF_DIRTY;
2777 PMAPCOUNT(vac_clean_one);
2778 break;
2779 default:
2780 return;
2781 }
2782
2783 KASSERT(!(md->pvh_attrs & PVF_NC));
2784
2785 NPDEBUG(PDB_VAC, printf("pmap_flush_page: md=%p (attrs=%#x)\n",
2786 md, md->pvh_attrs));
2787
2788 const size_t scache_line_size = arm_scache.dcache_line_size;
2789
2790 for (; va_offset <= end_va; va_offset += PAGE_SIZE) {
2791 pt_entry_t * const ptep = cpu_cdst_pte(va_offset);
2792 const vaddr_t dstp = cpu_cdstp(va_offset);
2793 const pt_entry_t opte = *ptep;
2794
2795 if (flush == PMAP_FLUSH_SECONDARY
2796 && va_offset == (md->pvh_attrs & arm_cache_prefer_mask))
2797 continue;
2798
2799 pmap_tlb_flush_SE(pmap_kernel(), dstp, PVF_REF | PVF_EXEC);
2800 /*
2801 * Set up a PTE with the right coloring to flush
2802 * existing cache entries.
2803 */
2804 const pt_entry_t npte = L2_S_PROTO
2805 | pa
2806 | L2_S_PROT(PTE_KERNEL, VM_PROT_READ|VM_PROT_WRITE)
2807 | pte_l2_s_cache_mode;
2808 l2pte_set(ptep, npte, opte);
2809 PTE_SYNC(ptep);
2810
2811 /*
2812 * Flush it. Make sure to flush secondary cache too since
2813 * bus_dma will ignore uncached pages.
2814 */
2815 if (scache_line_size != 0) {
2816 cpu_dcache_wb_range(dstp, PAGE_SIZE);
2817 if (wbinv_p) {
2818 cpu_sdcache_wbinv_range(dstp, pa, PAGE_SIZE);
2819 cpu_dcache_inv_range(dstp, PAGE_SIZE);
2820 } else {
2821 cpu_sdcache_wb_range(dstp, pa, PAGE_SIZE);
2822 }
2823 } else {
2824 if (wbinv_p) {
2825 cpu_dcache_wbinv_range(dstp, PAGE_SIZE);
2826 } else {
2827 cpu_dcache_wb_range(dstp, PAGE_SIZE);
2828 }
2829 }
2830
2831 /*
2832 * Restore the page table entry since we might have interrupted
2833 * pmap_zero_page or pmap_copy_page which was already using
2834 * this pte.
2835 */
2836 if (opte) {
2837 l2pte_set(ptep, opte, npte);
2838 } else {
2839 l2pte_reset(ptep);
2840 }
2841 PTE_SYNC(ptep);
2842 pmap_tlb_flush_SE(pmap_kernel(), dstp, PVF_REF | PVF_EXEC);
2843 }
2844 }
2845 #endif /* ARM_MMU_EXTENDED */
2846 #endif /* PMAP_CACHE_VIPT */
2847
2848 /*
2849 * Routine: pmap_page_remove
2850 * Function:
2851 * Removes this physical page from
2852 * all physical maps in which it resides.
2853 * Reflects back modify bits to the pager.
2854 */
2855 static void
2856 pmap_page_remove(struct vm_page_md *md, paddr_t pa)
2857 {
2858 struct l2_bucket *l2b;
2859 struct pv_entry *pv;
2860 pt_entry_t *ptep;
2861 #ifndef ARM_MMU_EXTENDED
2862 bool flush = false;
2863 #endif
2864 u_int flags = 0;
2865
2866 NPDEBUG(PDB_FOLLOW,
2867 printf("pmap_page_remove: md %p (0x%08lx)\n", md,
2868 pa));
2869
2870 struct pv_entry **pvp = &SLIST_FIRST(&md->pvh_list);
2871 pmap_acquire_page_lock(md);
2872 if (*pvp == NULL) {
2873 #ifdef PMAP_CACHE_VIPT
2874 /*
2875 * We *know* the page contents are about to be replaced.
2876 * Discard the exec contents
2877 */
2878 if (PV_IS_EXEC_P(md->pvh_attrs))
2879 PMAPCOUNT(exec_discarded_page_protect);
2880 md->pvh_attrs &= ~PVF_EXEC;
2881 PMAP_VALIDATE_MD_PAGE(md);
2882 #endif
2883 pmap_release_page_lock(md);
2884 return;
2885 }
2886 #if defined(PMAP_CACHE_VIPT) && !defined(ARM_MMU_EXTENDED)
2887 KASSERT(arm_cache_prefer_mask == 0 || pmap_is_page_colored_p(md));
2888 #endif
2889
2890 /*
2891 * Clear alias counts
2892 */
2893 #ifdef PMAP_CACHE_VIVT
2894 md->k_mappings = 0;
2895 #endif
2896 md->urw_mappings = md->uro_mappings = 0;
2897
2898 #ifdef PMAP_CACHE_VIVT
2899 pmap_clean_page(md, false);
2900 #endif
2901
2902 while ((pv = *pvp) != NULL) {
2903 pmap_t pm = pv->pv_pmap;
2904 #ifndef ARM_MMU_EXTENDED
2905 if (flush == false && pmap_is_current(pm))
2906 flush = true;
2907 #endif
2908
2909 if (pm == pmap_kernel()) {
2910 #ifdef PMAP_CACHE_VIPT
2911 /*
2912 * If this was unmanaged mapping, it must be preserved.
2913 * Move it back on the list and advance the end-of-list
2914 * pointer.
2915 */
2916 if (PV_IS_KENTRY_P(pv->pv_flags)) {
2917 *pvp = pv;
2918 pvp = &SLIST_NEXT(pv, pv_link);
2919 continue;
2920 }
2921 if (pv->pv_flags & PVF_WRITE)
2922 md->krw_mappings--;
2923 else
2924 md->kro_mappings--;
2925 #endif
2926 PMAPCOUNT(kernel_unmappings);
2927 }
2928 *pvp = SLIST_NEXT(pv, pv_link); /* remove from list */
2929 PMAPCOUNT(unmappings);
2930
2931 pmap_release_page_lock(md);
2932 pmap_acquire_pmap_lock(pm);
2933
2934 l2b = pmap_get_l2_bucket(pm, pv->pv_va);
2935 KASSERTMSG(l2b != NULL, "%#lx", pv->pv_va);
2936
2937 ptep = &l2b->l2b_kva[l2pte_index(pv->pv_va)];
2938
2939 /*
2940 * Update statistics
2941 */
2942 --pm->pm_stats.resident_count;
2943
2944 /* Wired bit */
2945 if (pv->pv_flags & PVF_WIRED)
2946 --pm->pm_stats.wired_count;
2947
2948 flags |= pv->pv_flags;
2949
2950 /*
2951 * Invalidate the PTEs.
2952 */
2953 l2pte_reset(ptep);
2954 PTE_SYNC_CURRENT(pm, ptep);
2955
2956 #ifdef ARM_MMU_EXTENDED
2957 pmap_tlb_invalidate_addr(pm, pv->pv_va);
2958 #endif
2959
2960 pmap_free_l2_bucket(pm, l2b, PAGE_SIZE / L2_S_SIZE);
2961
2962 pmap_release_pmap_lock(pm);
2963
2964 pool_put(&pmap_pv_pool, pv);
2965 pmap_acquire_page_lock(md);
2966 #ifdef MULTIPROCESSOR
2967 /*
2968 * Restart of the beginning of the list.
2969 */
2970 pvp = &SLIST_FIRST(&md->pvh_list);
2971 #endif
2972 }
2973 /*
2974 * if we reach the end of the list and there are still mappings, they
2975 * might be able to be cached now. And they must be kernel mappings.
2976 */
2977 if (!SLIST_EMPTY(&md->pvh_list)) {
2978 pmap_vac_me_harder(md, pa, pmap_kernel(), 0);
2979 }
2980
2981 #ifdef PMAP_CACHE_VIPT
2982 /*
2983 * Its EXEC cache is now gone.
2984 */
2985 if (PV_IS_EXEC_P(md->pvh_attrs))
2986 PMAPCOUNT(exec_discarded_page_protect);
2987 md->pvh_attrs &= ~PVF_EXEC;
2988 KASSERT(md->urw_mappings == 0);
2989 KASSERT(md->uro_mappings == 0);
2990 #ifndef ARM_MMU_EXTENDED
2991 if (arm_cache_prefer_mask != 0) {
2992 if (md->krw_mappings == 0)
2993 md->pvh_attrs &= ~PVF_WRITE;
2994 PMAP_VALIDATE_MD_PAGE(md);
2995 }
2996 #endif /* ARM_MMU_EXTENDED */
2997 #endif /* PMAP_CACHE_VIPT */
2998 pmap_release_page_lock(md);
2999
3000 #ifndef ARM_MMU_EXTENDED
3001 if (flush) {
3002 /*
3003 * Note: We can't use pmap_tlb_flush{I,D}() here since that
3004 * would need a subsequent call to pmap_update() to ensure
3005 * curpm->pm_cstate.cs_all is reset. Our callers are not
3006 * required to do that (see pmap(9)), so we can't modify
3007 * the current pmap's state.
3008 */
3009 if (PV_BEEN_EXECD(flags))
3010 cpu_tlb_flushID();
3011 else
3012 cpu_tlb_flushD();
3013 }
3014 cpu_cpwait();
3015 #endif /* ARM_MMU_EXTENDED */
3016 }
3017
3018 /*
3019 * pmap_t pmap_create(void)
3020 *
3021 * Create a new pmap structure from scratch.
3022 */
3023 pmap_t
3024 pmap_create(void)
3025 {
3026 pmap_t pm;
3027
3028 pm = pool_cache_get(&pmap_cache, PR_WAITOK);
3029
3030 mutex_init(&pm->pm_lock, MUTEX_DEFAULT, IPL_NONE);
3031
3032 pm->pm_refs = 1;
3033 pm->pm_stats.wired_count = 0;
3034 pm->pm_stats.resident_count = 1;
3035 #ifdef ARM_MMU_EXTENDED
3036 #ifdef MULTIPROCESSOR
3037 kcpuset_create(&pm->pm_active, true);
3038 kcpuset_create(&pm->pm_onproc, true);
3039 #endif
3040 #else
3041 pm->pm_cstate.cs_all = 0;
3042 #endif
3043 pmap_alloc_l1(pm);
3044
3045 /*
3046 * Note: The pool cache ensures that the pm_l2[] array is already
3047 * initialised to zero.
3048 */
3049
3050 pmap_pinit(pm);
3051
3052 return pm;
3053 }
3054
3055 u_int
3056 arm32_mmap_flags(paddr_t pa)
3057 {
3058 /*
3059 * the upper 8 bits in pmap_enter()'s flags are reserved for MD stuff
3060 * and we're using the upper bits in page numbers to pass flags around
3061 * so we might as well use the same bits
3062 */
3063 return (u_int)pa & PMAP_MD_MASK;
3064 }
3065 /*
3066 * int pmap_enter(pmap_t pm, vaddr_t va, paddr_t pa, vm_prot_t prot,
3067 * u_int flags)
3068 *
3069 * Insert the given physical page (p) at
3070 * the specified virtual address (v) in the
3071 * target physical map with the protection requested.
3072 *
3073 * NB: This is the only routine which MAY NOT lazy-evaluate
3074 * or lose information. That is, this routine must actually
3075 * insert this page into the given map NOW.
3076 */
3077 int
3078 pmap_enter(pmap_t pm, vaddr_t va, paddr_t pa, vm_prot_t prot, u_int flags)
3079 {
3080 struct l2_bucket *l2b;
3081 struct vm_page *pg, *opg;
3082 u_int nflags;
3083 u_int oflags;
3084 const bool kpm_p = (pm == pmap_kernel());
3085 #ifdef ARM_HAS_VBAR
3086 const bool vector_page_p = false;
3087 #else
3088 const bool vector_page_p = (va == vector_page);
3089 #endif
3090 struct pmap_page *pp = pmap_pv_tracked(pa);
3091 struct pv_entry *new_pv = NULL;
3092 struct pv_entry *old_pv = NULL;
3093 int error = 0;
3094
3095 UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
3096
3097 UVMHIST_LOG(maphist, " (pm %#jx va %#jx pa %#jx prot %#jx",
3098 (uintptr_t)pm, va, pa, prot);
3099 UVMHIST_LOG(maphist, " flag %#jx", flags, 0, 0, 0);
3100
3101 KDASSERT((flags & PMAP_WIRED) == 0 || (flags & VM_PROT_ALL) != 0);
3102 KDASSERT(((va | pa) & PGOFSET) == 0);
3103
3104 /*
3105 * Get a pointer to the page. Later on in this function, we
3106 * test for a managed page by checking pg != NULL.
3107 */
3108 pg = pmap_initialized ? PHYS_TO_VM_PAGE(pa) : NULL;
3109 /*
3110 * if we may need a new pv entry allocate if now, as we can't do it
3111 * with the kernel_pmap locked
3112 */
3113 if (pg || pp)
3114 new_pv = pool_get(&pmap_pv_pool, PR_NOWAIT);
3115
3116 nflags = 0;
3117 if (prot & VM_PROT_WRITE)
3118 nflags |= PVF_WRITE;
3119 if (prot & VM_PROT_EXECUTE)
3120 nflags |= PVF_EXEC;
3121 if (flags & PMAP_WIRED)
3122 nflags |= PVF_WIRED;
3123
3124 pmap_acquire_pmap_lock(pm);
3125
3126 /*
3127 * Fetch the L2 bucket which maps this page, allocating one if
3128 * necessary for user pmaps.
3129 */
3130 if (kpm_p) {
3131 l2b = pmap_get_l2_bucket(pm, va);
3132 } else {
3133 l2b = pmap_alloc_l2_bucket(pm, va);
3134 }
3135 if (l2b == NULL) {
3136 if (flags & PMAP_CANFAIL) {
3137 pmap_release_pmap_lock(pm);
3138 error = ENOMEM;
3139 goto free_pv;
3140 }
3141 panic("pmap_enter: failed to allocate L2 bucket");
3142 }
3143 pt_entry_t *ptep = &l2b->l2b_kva[l2pte_index(va)];
3144 const pt_entry_t opte = *ptep;
3145 pt_entry_t npte = pa;
3146 oflags = 0;
3147
3148 if (opte) {
3149 /*
3150 * There is already a mapping at this address.
3151 * If the physical address is different, lookup the
3152 * vm_page.
3153 */
3154 if (l2pte_pa(opte) != pa) {
3155 KASSERT(!pmap_pv_tracked(pa));
3156 opg = PHYS_TO_VM_PAGE(l2pte_pa(opte));
3157 } else
3158 opg = pg;
3159 } else
3160 opg = NULL;
3161
3162 if (pg || pp) {
3163 KASSERT((pg != NULL) != (pp != NULL));
3164 struct vm_page_md *md = (pg != NULL) ? VM_PAGE_TO_MD(pg) :
3165 PMAP_PAGE_TO_MD(pp);
3166
3167 /*
3168 * This is to be a managed mapping.
3169 */
3170 pmap_acquire_page_lock(md);
3171 if ((flags & VM_PROT_ALL) || (md->pvh_attrs & PVF_REF)) {
3172 /*
3173 * - The access type indicates that we don't need
3174 * to do referenced emulation.
3175 * OR
3176 * - The physical page has already been referenced
3177 * so no need to re-do referenced emulation here.
3178 */
3179 npte |= l2pte_set_readonly(L2_S_PROTO);
3180
3181 nflags |= PVF_REF;
3182
3183 if ((prot & VM_PROT_WRITE) != 0 &&
3184 ((flags & VM_PROT_WRITE) != 0 ||
3185 (md->pvh_attrs & PVF_MOD) != 0)) {
3186 /*
3187 * This is a writable mapping, and the
3188 * page's mod state indicates it has
3189 * already been modified. Make it
3190 * writable from the outset.
3191 */
3192 npte = l2pte_set_writable(npte);
3193 nflags |= PVF_MOD;
3194 }
3195
3196 #ifdef ARM_MMU_EXTENDED
3197 /*
3198 * If the page has been cleaned, then the pvh_attrs
3199 * will have PVF_EXEC set, so mark it execute so we
3200 * don't get an access fault when trying to execute
3201 * from it.
3202 */
3203 if (md->pvh_attrs & nflags & PVF_EXEC) {
3204 npte &= ~L2_XS_XN;
3205 }
3206 #endif
3207 } else {
3208 /*
3209 * Need to do page referenced emulation.
3210 */
3211 npte |= L2_TYPE_INV;
3212 }
3213
3214 if (flags & ARM32_MMAP_WRITECOMBINE) {
3215 npte |= pte_l2_s_wc_mode;
3216 } else
3217 npte |= pte_l2_s_cache_mode;
3218
3219 if (pg != NULL && pg == opg) {
3220 /*
3221 * We're changing the attrs of an existing mapping.
3222 */
3223 oflags = pmap_modify_pv(md, pa, pm, va,
3224 PVF_WRITE | PVF_EXEC | PVF_WIRED |
3225 PVF_MOD | PVF_REF, nflags);
3226
3227 #ifdef PMAP_CACHE_VIVT
3228 /*
3229 * We may need to flush the cache if we're
3230 * doing rw-ro...
3231 */
3232 if (pm->pm_cstate.cs_cache_d &&
3233 (oflags & PVF_NC) == 0 &&
3234 l2pte_writable_p(opte) &&
3235 (prot & VM_PROT_WRITE) == 0)
3236 cpu_dcache_wb_range(va, PAGE_SIZE);
3237 #endif
3238 } else {
3239 struct pv_entry *pv;
3240 /*
3241 * New mapping, or changing the backing page
3242 * of an existing mapping.
3243 */
3244 if (opg) {
3245 struct vm_page_md *omd = VM_PAGE_TO_MD(opg);
3246 paddr_t opa = VM_PAGE_TO_PHYS(opg);
3247
3248 /*
3249 * Replacing an existing mapping with a new one.
3250 * It is part of our managed memory so we
3251 * must remove it from the PV list
3252 */
3253 pv = pmap_remove_pv(omd, opa, pm, va);
3254 pmap_vac_me_harder(omd, opa, pm, 0);
3255 oflags = pv->pv_flags;
3256
3257 #ifdef PMAP_CACHE_VIVT
3258 /*
3259 * If the old mapping was valid (ref/mod
3260 * emulation creates 'invalid' mappings
3261 * initially) then make sure to frob
3262 * the cache.
3263 */
3264 if (!(oflags & PVF_NC) && l2pte_valid_p(opte)) {
3265 pmap_cache_wbinv_page(pm, va, true,
3266 oflags);
3267 }
3268 #endif
3269 } else {
3270 pv = new_pv;
3271 new_pv = NULL;
3272 if (pv == NULL) {
3273 pmap_release_page_lock(md);
3274 pmap_release_pmap_lock(pm);
3275 if ((flags & PMAP_CANFAIL) == 0)
3276 panic("pmap_enter: "
3277 "no pv entries");
3278
3279 pmap_free_l2_bucket(pm, l2b, 0);
3280 UVMHIST_LOG(maphist, " <-- done (ENOMEM)",
3281 0, 0, 0, 0);
3282 return ENOMEM;
3283 }
3284 }
3285
3286 pmap_enter_pv(md, pa, pv, pm, va, nflags);
3287 }
3288 pmap_release_page_lock(md);
3289 } else {
3290 /*
3291 * We're mapping an unmanaged page.
3292 * These are always readable, and possibly writable, from
3293 * the get go as we don't need to track ref/mod status.
3294 */
3295 npte |= l2pte_set_readonly(L2_S_PROTO);
3296 if (prot & VM_PROT_WRITE)
3297 npte = l2pte_set_writable(npte);
3298
3299 /*
3300 * Make sure the vector table is mapped cacheable
3301 */
3302 if ((vector_page_p && !kpm_p)
3303 || (flags & ARM32_MMAP_CACHEABLE)) {
3304 npte |= pte_l2_s_cache_mode;
3305 #ifdef ARM_MMU_EXTENDED
3306 npte &= ~L2_XS_XN; /* and executable */
3307 #endif
3308 } else if (flags & ARM32_MMAP_WRITECOMBINE) {
3309 npte |= pte_l2_s_wc_mode;
3310 }
3311 if (opg) {
3312 /*
3313 * Looks like there's an existing 'managed' mapping
3314 * at this address.
3315 */
3316 struct vm_page_md *omd = VM_PAGE_TO_MD(opg);
3317 paddr_t opa = VM_PAGE_TO_PHYS(opg);
3318
3319 pmap_acquire_page_lock(omd);
3320 old_pv = pmap_remove_pv(omd, opa, pm, va);
3321 pmap_vac_me_harder(omd, opa, pm, 0);
3322 oflags = old_pv->pv_flags;
3323 pmap_release_page_lock(omd);
3324
3325 #ifdef PMAP_CACHE_VIVT
3326 if (!(oflags & PVF_NC) && l2pte_valid_p(opte)) {
3327 pmap_cache_wbinv_page(pm, va, true, oflags);
3328 }
3329 #endif
3330 }
3331 }
3332
3333 /*
3334 * Make sure userland mappings get the right permissions
3335 */
3336 if (!vector_page_p && !kpm_p) {
3337 npte |= L2_S_PROT_U;
3338 #ifdef ARM_MMU_EXTENDED
3339 npte |= L2_XS_nG; /* user pages are not global */
3340 #endif
3341 }
3342
3343 /*
3344 * Keep the stats up to date
3345 */
3346 if (opte == 0) {
3347 l2b->l2b_occupancy += PAGE_SIZE / L2_S_SIZE;
3348 pm->pm_stats.resident_count++;
3349 }
3350
3351 UVMHIST_LOG(maphist, " opte %#jx npte %#jx", opte, npte, 0, 0);
3352
3353 #if defined(ARM_MMU_EXTENDED)
3354 /*
3355 * If exec protection was requested but the page hasn't been synced,
3356 * sync it now and allow execution from it.
3357 */
3358 if ((nflags & PVF_EXEC) && (npte & L2_XS_XN)) {
3359 struct vm_page_md *md = VM_PAGE_TO_MD(pg);
3360 npte &= ~L2_XS_XN;
3361 pmap_syncicache_page(md, pa);
3362 PMAPCOUNT(exec_synced_map);
3363 }
3364 #endif
3365 /*
3366 * If this is just a wiring change, the two PTEs will be
3367 * identical, so there's no need to update the page table.
3368 */
3369 if (npte != opte) {
3370 l2pte_reset(ptep);
3371 PTE_SYNC(ptep);
3372 if (l2pte_valid_p(opte)) {
3373 pmap_tlb_flush_SE(pm, va, oflags);
3374 }
3375 l2pte_set(ptep, npte, 0);
3376 PTE_SYNC(ptep);
3377 #ifndef ARM_MMU_EXTENDED
3378 bool is_cached = pmap_is_cached(pm);
3379 if (is_cached) {
3380 /*
3381 * We only need to frob the cache/tlb if this pmap
3382 * is current
3383 */
3384 if (!vector_page_p && l2pte_valid_p(npte)) {
3385 /*
3386 * This mapping is likely to be accessed as
3387 * soon as we return to userland. Fix up the
3388 * L1 entry to avoid taking another
3389 * page/domain fault.
3390 */
3391 pd_entry_t *pdep = pmap_l1_kva(pm)
3392 + l1pte_index(va);
3393 pd_entry_t pde = L1_C_PROTO | l2b->l2b_pa
3394 | L1_C_DOM(pmap_domain(pm));
3395 if (*pdep != pde) {
3396 l1pte_setone(pdep, pde);
3397 PDE_SYNC(pdep);
3398 }
3399 }
3400 }
3401
3402 UVMHIST_LOG(maphist, " is_cached %jd cs 0x%08jx",
3403 is_cached, pm->pm_cstate.cs_all, 0, 0);
3404
3405 if (pg != NULL) {
3406 struct vm_page_md *md = VM_PAGE_TO_MD(pg);
3407
3408 pmap_acquire_page_lock(md);
3409 pmap_vac_me_harder(md, pa, pm, va);
3410 pmap_release_page_lock(md);
3411 }
3412 #endif
3413 }
3414 #if defined(PMAP_CACHE_VIPT) && defined(DIAGNOSTIC)
3415 if (pg) {
3416 struct vm_page_md *md = VM_PAGE_TO_MD(pg);
3417
3418 pmap_acquire_page_lock(md);
3419 #ifndef ARM_MMU_EXTENDED
3420 KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC)));
3421 #endif
3422 PMAP_VALIDATE_MD_PAGE(md);
3423 pmap_release_page_lock(md);
3424 }
3425 #endif
3426
3427 pmap_release_pmap_lock(pm);
3428
3429
3430 if (old_pv)
3431 pool_put(&pmap_pv_pool, old_pv);
3432 free_pv:
3433 if (new_pv)
3434 pool_put(&pmap_pv_pool, new_pv);
3435 return error;
3436 }
3437
3438 /*
3439 * pmap_remove()
3440 *
3441 * pmap_remove is responsible for nuking a number of mappings for a range
3442 * of virtual address space in the current pmap. To do this efficiently
3443 * is interesting, because in a number of cases a wide virtual address
3444 * range may be supplied that contains few actual mappings. So, the
3445 * optimisations are:
3446 * 1. Skip over hunks of address space for which no L1 or L2 entry exists.
3447 * 2. Build up a list of pages we've hit, up to a maximum, so we can
3448 * maybe do just a partial cache clean. This path of execution is
3449 * complicated by the fact that the cache must be flushed _before_
3450 * the PTE is nuked, being a VAC :-)
3451 * 3. If we're called after UVM calls pmap_remove_all(), we can defer
3452 * all invalidations until pmap_update(), since pmap_remove_all() has
3453 * already flushed the cache.
3454 * 4. Maybe later fast-case a single page, but I don't think this is
3455 * going to make _that_ much difference overall.
3456 */
3457
3458 #define PMAP_REMOVE_CLEAN_LIST_SIZE 3
3459
3460 void
3461 pmap_remove(pmap_t pm, vaddr_t sva, vaddr_t eva)
3462 {
3463 SLIST_HEAD(,pv_entry) opv_list;
3464 struct pv_entry *pv, *npv;
3465 UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
3466 UVMHIST_LOG(maphist, " (pm=%#jx, sva=%#jx, eva=%#jx)",
3467 (uintptr_t)pm, sva, eva, 0);
3468
3469 #ifdef PMAP_FAULTINFO
3470 curpcb->pcb_faultinfo.pfi_faultaddr = 0;
3471 curpcb->pcb_faultinfo.pfi_repeats = 0;
3472 curpcb->pcb_faultinfo.pfi_faultptep = NULL;
3473 #endif
3474
3475 SLIST_INIT(&opv_list);
3476 /*
3477 * we lock in the pmap => pv_head direction
3478 */
3479 pmap_acquire_pmap_lock(pm);
3480
3481 #ifndef ARM_MMU_EXTENDED
3482 u_int cleanlist_idx, total, cnt;
3483 struct {
3484 vaddr_t va;
3485 pt_entry_t *ptep;
3486 } cleanlist[PMAP_REMOVE_CLEAN_LIST_SIZE];
3487
3488 if (pm->pm_remove_all || !pmap_is_cached(pm)) {
3489 cleanlist_idx = PMAP_REMOVE_CLEAN_LIST_SIZE + 1;
3490 if (pm->pm_cstate.cs_tlb == 0)
3491 pm->pm_remove_all = true;
3492 } else
3493 cleanlist_idx = 0;
3494 total = 0;
3495 #endif
3496
3497 while (sva < eva) {
3498 /*
3499 * Do one L2 bucket's worth at a time.
3500 */
3501 vaddr_t next_bucket = L2_NEXT_BUCKET_VA(sva);
3502 if (next_bucket > eva)
3503 next_bucket = eva;
3504
3505 struct l2_bucket * const l2b = pmap_get_l2_bucket(pm, sva);
3506 if (l2b == NULL) {
3507 sva = next_bucket;
3508 continue;
3509 }
3510
3511 pt_entry_t *ptep = &l2b->l2b_kva[l2pte_index(sva)];
3512 u_int mappings = 0;
3513
3514 for (;sva < next_bucket;
3515 sva += PAGE_SIZE, ptep += PAGE_SIZE / L2_S_SIZE) {
3516 pt_entry_t opte = *ptep;
3517
3518 if (opte == 0) {
3519 /* Nothing here, move along */
3520 continue;
3521 }
3522
3523 u_int flags = PVF_REF;
3524 paddr_t pa = l2pte_pa(opte);
3525 struct vm_page * const pg = PHYS_TO_VM_PAGE(pa);
3526
3527 /*
3528 * Update flags. In a number of circumstances,
3529 * we could cluster a lot of these and do a
3530 * number of sequential pages in one go.
3531 */
3532 if (pg != NULL) {
3533 struct vm_page_md *md = VM_PAGE_TO_MD(pg);
3534
3535 pmap_acquire_page_lock(md);
3536 pv = pmap_remove_pv(md, pa, pm, sva);
3537 pmap_vac_me_harder(md, pa, pm, 0);
3538 pmap_release_page_lock(md);
3539 if (pv != NULL) {
3540 if (pm->pm_remove_all == false) {
3541 flags = pv->pv_flags;
3542 }
3543 SLIST_INSERT_HEAD(&opv_list,
3544 pv, pv_link);
3545 }
3546 }
3547 mappings += PAGE_SIZE / L2_S_SIZE;
3548
3549 if (!l2pte_valid_p(opte)) {
3550 /*
3551 * Ref/Mod emulation is still active for this
3552 * mapping, therefore it is has not yet been
3553 * accessed. No need to frob the cache/tlb.
3554 */
3555 l2pte_reset(ptep);
3556 PTE_SYNC_CURRENT(pm, ptep);
3557 continue;
3558 }
3559
3560 #ifdef ARM_MMU_EXTENDED
3561 l2pte_reset(ptep);
3562 PTE_SYNC(ptep);
3563 if (__predict_false(pm->pm_remove_all == false)) {
3564 pmap_tlb_flush_SE(pm, sva, flags);
3565 }
3566 #else
3567 if (cleanlist_idx < PMAP_REMOVE_CLEAN_LIST_SIZE) {
3568 /* Add to the clean list. */
3569 cleanlist[cleanlist_idx].ptep = ptep;
3570 cleanlist[cleanlist_idx].va =
3571 sva | (flags & PVF_EXEC);
3572 cleanlist_idx++;
3573 } else if (cleanlist_idx == PMAP_REMOVE_CLEAN_LIST_SIZE) {
3574 /* Nuke everything if needed. */
3575 #ifdef PMAP_CACHE_VIVT
3576 pmap_cache_wbinv_all(pm, PVF_EXEC);
3577 #endif
3578 /*
3579 * Roll back the previous PTE list,
3580 * and zero out the current PTE.
3581 */
3582 for (cnt = 0;
3583 cnt < PMAP_REMOVE_CLEAN_LIST_SIZE; cnt++) {
3584 l2pte_reset(cleanlist[cnt].ptep);
3585 PTE_SYNC(cleanlist[cnt].ptep);
3586 }
3587 l2pte_reset(ptep);
3588 PTE_SYNC(ptep);
3589 cleanlist_idx++;
3590 pm->pm_remove_all = true;
3591 } else {
3592 l2pte_reset(ptep);
3593 PTE_SYNC(ptep);
3594 if (pm->pm_remove_all == false) {
3595 pmap_tlb_flush_SE(pm, sva, flags);
3596 }
3597 }
3598 #endif
3599 }
3600
3601 #ifndef ARM_MMU_EXTENDED
3602 /*
3603 * Deal with any left overs
3604 */
3605 if (cleanlist_idx <= PMAP_REMOVE_CLEAN_LIST_SIZE) {
3606 total += cleanlist_idx;
3607 for (cnt = 0; cnt < cleanlist_idx; cnt++) {
3608 l2pte_reset(cleanlist[cnt].ptep);
3609 PTE_SYNC_CURRENT(pm, cleanlist[cnt].ptep);
3610 vaddr_t va = cleanlist[cnt].va;
3611 if (pm->pm_cstate.cs_all != 0) {
3612 vaddr_t clva = va & ~PAGE_MASK;
3613 u_int flags = va & PVF_EXEC;
3614 #ifdef PMAP_CACHE_VIVT
3615 pmap_cache_wbinv_page(pm, clva, true,
3616 PVF_REF | PVF_WRITE | flags);
3617 #endif
3618 pmap_tlb_flush_SE(pm, clva,
3619 PVF_REF | flags);
3620 }
3621 }
3622
3623 /*
3624 * If it looks like we're removing a whole bunch
3625 * of mappings, it's faster to just write-back
3626 * the whole cache now and defer TLB flushes until
3627 * pmap_update() is called.
3628 */
3629 if (total <= PMAP_REMOVE_CLEAN_LIST_SIZE)
3630 cleanlist_idx = 0;
3631 else {
3632 cleanlist_idx = PMAP_REMOVE_CLEAN_LIST_SIZE + 1;
3633 #ifdef PMAP_CACHE_VIVT
3634 pmap_cache_wbinv_all(pm, PVF_EXEC);
3635 #endif
3636 pm->pm_remove_all = true;
3637 }
3638 }
3639 #endif /* ARM_MMU_EXTENDED */
3640
3641 pmap_free_l2_bucket(pm, l2b, mappings);
3642 pm->pm_stats.resident_count -= mappings / (PAGE_SIZE/L2_S_SIZE);
3643 }
3644
3645 pmap_release_pmap_lock(pm);
3646 SLIST_FOREACH_SAFE(pv, &opv_list, pv_link, npv) {
3647 pool_put(&pmap_pv_pool, pv);
3648 }
3649 }
3650
3651 #if defined(PMAP_CACHE_VIPT) && !defined(ARM_MMU_EXTENDED)
3652 static struct pv_entry *
3653 pmap_kremove_pg(struct vm_page *pg, vaddr_t va)
3654 {
3655 struct vm_page_md *md = VM_PAGE_TO_MD(pg);
3656 paddr_t pa = VM_PAGE_TO_PHYS(pg);
3657 struct pv_entry *pv;
3658
3659 KASSERT(arm_cache_prefer_mask == 0 || md->pvh_attrs & (PVF_COLORED|PVF_NC));
3660 KASSERT((md->pvh_attrs & PVF_KMPAGE) == 0);
3661 KASSERT(pmap_page_locked_p(md));
3662
3663 pv = pmap_remove_pv(md, pa, pmap_kernel(), va);
3664 KASSERTMSG(pv, "pg %p (pa #%lx) va %#lx", pg, pa, va);
3665 KASSERT(PV_IS_KENTRY_P(pv->pv_flags));
3666
3667 /*
3668 * We are removing a writeable mapping to a cached exec page, if
3669 * it's the last mapping then clear its execness otherwise sync
3670 * the page to the icache.
3671 */
3672 if ((md->pvh_attrs & (PVF_NC|PVF_EXEC)) == PVF_EXEC
3673 && (pv->pv_flags & PVF_WRITE) != 0) {
3674 if (SLIST_EMPTY(&md->pvh_list)) {
3675 md->pvh_attrs &= ~PVF_EXEC;
3676 PMAPCOUNT(exec_discarded_kremove);
3677 } else {
3678 pmap_syncicache_page(md, pa);
3679 PMAPCOUNT(exec_synced_kremove);
3680 }
3681 }
3682 pmap_vac_me_harder(md, pa, pmap_kernel(), 0);
3683
3684 return pv;
3685 }
3686 #endif /* PMAP_CACHE_VIPT && !ARM_MMU_EXTENDED */
3687
3688 /*
3689 * pmap_kenter_pa: enter an unmanaged, wired kernel mapping
3690 *
3691 * We assume there is already sufficient KVM space available
3692 * to do this, as we can't allocate L2 descriptor tables/metadata
3693 * from here.
3694 */
3695 void
3696 pmap_kenter_pa(vaddr_t va, paddr_t pa, vm_prot_t prot, u_int flags)
3697 {
3698 #ifdef PMAP_CACHE_VIVT
3699 struct vm_page *pg = (flags & PMAP_KMPAGE) ? PHYS_TO_VM_PAGE(pa) : NULL;
3700 #endif
3701 #ifdef PMAP_CACHE_VIPT
3702 struct vm_page *pg = PHYS_TO_VM_PAGE(pa);
3703 struct vm_page *opg;
3704 #ifndef ARM_MMU_EXTENDED
3705 struct pv_entry *pv = NULL;
3706 #endif
3707 #endif
3708 struct vm_page_md *md = pg != NULL ? VM_PAGE_TO_MD(pg) : NULL;
3709
3710 UVMHIST_FUNC(__func__);
3711
3712 if (pmap_initialized) {
3713 UVMHIST_CALLED(maphist);
3714 UVMHIST_LOG(maphist, " (va=%#jx, pa=%#jx, prot=%#jx, flags=%#jx",
3715 va, pa, prot, flags);
3716 }
3717
3718 pmap_t kpm = pmap_kernel();
3719 pmap_acquire_pmap_lock(kpm);
3720 struct l2_bucket * const l2b = pmap_get_l2_bucket(kpm, va);
3721 const size_t l1slot __diagused = l1pte_index(va);
3722 KASSERTMSG(l2b != NULL,
3723 "va %#lx pa %#lx prot %d maxkvaddr %#lx: l2 %p l2b %p kva %p",
3724 va, pa, prot, pmap_curmaxkvaddr, kpm->pm_l2[L2_IDX(l1slot)],
3725 kpm->pm_l2[L2_IDX(l1slot)]
3726 ? &kpm->pm_l2[L2_IDX(l1slot)]->l2_bucket[L2_BUCKET(l1slot)]
3727 : NULL,
3728 kpm->pm_l2[L2_IDX(l1slot)]
3729 ? kpm->pm_l2[L2_IDX(l1slot)]->l2_bucket[L2_BUCKET(l1slot)].l2b_kva
3730 : NULL);
3731 KASSERT(l2b->l2b_kva != NULL);
3732
3733 pt_entry_t * const ptep = &l2b->l2b_kva[l2pte_index(va)];
3734 const pt_entry_t opte = *ptep;
3735
3736 if (opte == 0) {
3737 PMAPCOUNT(kenter_mappings);
3738 l2b->l2b_occupancy += PAGE_SIZE / L2_S_SIZE;
3739 } else {
3740 PMAPCOUNT(kenter_remappings);
3741 #ifdef PMAP_CACHE_VIPT
3742 opg = PHYS_TO_VM_PAGE(l2pte_pa(opte));
3743 #if !defined(ARM_MMU_EXTENDED) || defined(DIAGNOSTIC)
3744 struct vm_page_md *omd __diagused = VM_PAGE_TO_MD(opg);
3745 #endif
3746 if (opg && arm_cache_prefer_mask != 0) {
3747 KASSERT(opg != pg);
3748 KASSERT((omd->pvh_attrs & PVF_KMPAGE) == 0);
3749 KASSERT((flags & PMAP_KMPAGE) == 0);
3750 #ifndef ARM_MMU_EXTENDED
3751 pmap_acquire_page_lock(omd);
3752 pv = pmap_kremove_pg(opg, va);
3753 pmap_release_page_lock(omd);
3754 #endif
3755 }
3756 #endif
3757 if (l2pte_valid_p(opte)) {
3758 l2pte_reset(ptep);
3759 PTE_SYNC(ptep);
3760 #ifdef PMAP_CACHE_VIVT
3761 cpu_dcache_wbinv_range(va, PAGE_SIZE);
3762 #endif
3763 cpu_tlb_flushD_SE(va);
3764 cpu_cpwait();
3765 }
3766 }
3767 pmap_release_pmap_lock(kpm);
3768 pt_entry_t npte = L2_S_PROTO | pa | L2_S_PROT(PTE_KERNEL, prot);
3769
3770 if (flags & PMAP_PTE) {
3771 KASSERT((flags & PMAP_CACHE_MASK) == 0);
3772 if (!(flags & PMAP_NOCACHE))
3773 npte |= pte_l2_s_cache_mode_pt;
3774 } else {
3775 switch (flags & (PMAP_CACHE_MASK | PMAP_DEV_MASK)) {
3776 case PMAP_DEV ... PMAP_DEV | PMAP_CACHE_MASK:
3777 break;
3778 case PMAP_NOCACHE:
3779 npte |= pte_l2_s_nocache_mode;
3780 break;
3781 case PMAP_WRITE_COMBINE:
3782 npte |= pte_l2_s_wc_mode;
3783 break;
3784 default:
3785 npte |= pte_l2_s_cache_mode;
3786 break;
3787 }
3788 }
3789 #ifdef ARM_MMU_EXTENDED
3790 if (prot & VM_PROT_EXECUTE)
3791 npte &= ~L2_XS_XN;
3792 #endif
3793 l2pte_set(ptep, npte, 0);
3794 PTE_SYNC(ptep);
3795
3796 if (pg) {
3797 if (flags & PMAP_KMPAGE) {
3798 KASSERT(md->urw_mappings == 0);
3799 KASSERT(md->uro_mappings == 0);
3800 KASSERT(md->krw_mappings == 0);
3801 KASSERT(md->kro_mappings == 0);
3802 #if defined(PMAP_CACHE_VIPT) && !defined(ARM_MMU_EXTENDED)
3803 KASSERT(pv == NULL);
3804 KASSERT(arm_cache_prefer_mask == 0 || (va & PVF_COLORED) == 0);
3805 KASSERT((md->pvh_attrs & PVF_NC) == 0);
3806 /* if there is a color conflict, evict from cache. */
3807 if (pmap_is_page_colored_p(md)
3808 && ((va ^ md->pvh_attrs) & arm_cache_prefer_mask)) {
3809 PMAPCOUNT(vac_color_change);
3810 pmap_flush_page(md, pa, PMAP_FLUSH_PRIMARY);
3811 } else if (md->pvh_attrs & PVF_MULTCLR) {
3812 /*
3813 * If this page has multiple colors, expunge
3814 * them.
3815 */
3816 PMAPCOUNT(vac_flush_lots2);
3817 pmap_flush_page(md, pa, PMAP_FLUSH_SECONDARY);
3818 }
3819 /*
3820 * Since this is a KMPAGE, there can be no contention
3821 * for this page so don't lock it.
3822 */
3823 md->pvh_attrs &= PAGE_SIZE - 1;
3824 md->pvh_attrs |= PVF_KMPAGE | PVF_COLORED | PVF_DIRTY
3825 | (va & arm_cache_prefer_mask);
3826 #else /* !PMAP_CACHE_VIPT || ARM_MMU_EXTENDED */
3827 md->pvh_attrs |= PVF_KMPAGE;
3828 #endif
3829 atomic_inc_32(&pmap_kmpages);
3830 #if defined(PMAP_CACHE_VIPT) && !defined(ARM_MMU_EXTENDED)
3831 } else if (arm_cache_prefer_mask != 0) {
3832 if (pv == NULL) {
3833 pv = pool_get(&pmap_pv_pool, PR_NOWAIT);
3834 KASSERT(pv != NULL);
3835 }
3836 pmap_acquire_page_lock(md);
3837 pmap_enter_pv(md, pa, pv, pmap_kernel(), va,
3838 PVF_WIRED | PVF_KENTRY
3839 | (prot & VM_PROT_WRITE ? PVF_WRITE : 0));
3840 if ((prot & VM_PROT_WRITE)
3841 && !(md->pvh_attrs & PVF_NC))
3842 md->pvh_attrs |= PVF_DIRTY;
3843 KASSERT((prot & VM_PROT_WRITE) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC)));
3844 pmap_vac_me_harder(md, pa, pmap_kernel(), va);
3845 pmap_release_page_lock(md);
3846 #endif
3847 }
3848 #if defined(PMAP_CACHE_VIPT) && !defined(ARM_MMU_EXTENDED)
3849 } else {
3850 if (pv != NULL)
3851 pool_put(&pmap_pv_pool, pv);
3852 #endif
3853 }
3854 if (pmap_initialized) {
3855 UVMHIST_LOG(maphist, " <-- done (ptep %#jx: %#jx -> %#jx)",
3856 (uintptr_t)ptep, opte, npte, 0);
3857 }
3858
3859 }
3860
3861 void
3862 pmap_kremove(vaddr_t va, vsize_t len)
3863 {
3864 #ifdef UVMHIST
3865 u_int total_mappings = 0;
3866 #endif
3867
3868 PMAPCOUNT(kenter_unmappings);
3869
3870 UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
3871
3872 UVMHIST_LOG(maphist, " (va=%#jx, len=%#jx)", va, len, 0, 0);
3873
3874 const vaddr_t eva = va + len;
3875 pmap_t kpm = pmap_kernel();
3876
3877 pmap_acquire_pmap_lock(kpm);
3878
3879 while (va < eva) {
3880 vaddr_t next_bucket = L2_NEXT_BUCKET_VA(va);
3881 if (next_bucket > eva)
3882 next_bucket = eva;
3883
3884 struct l2_bucket * const l2b = pmap_get_l2_bucket(kpm, va);
3885 KDASSERT(l2b != NULL);
3886
3887 pt_entry_t * const sptep = &l2b->l2b_kva[l2pte_index(va)];
3888 pt_entry_t *ptep = sptep;
3889 u_int mappings = 0;
3890
3891 while (va < next_bucket) {
3892 const pt_entry_t opte = *ptep;
3893 struct vm_page *opg = PHYS_TO_VM_PAGE(l2pte_pa(opte));
3894 if (opg != NULL) {
3895 struct vm_page_md *omd = VM_PAGE_TO_MD(opg);
3896
3897 if (omd->pvh_attrs & PVF_KMPAGE) {
3898 KASSERT(omd->urw_mappings == 0);
3899 KASSERT(omd->uro_mappings == 0);
3900 KASSERT(omd->krw_mappings == 0);
3901 KASSERT(omd->kro_mappings == 0);
3902 omd->pvh_attrs &= ~PVF_KMPAGE;
3903 #if defined(PMAP_CACHE_VIPT) && !defined(ARM_MMU_EXTENDED)
3904 if (arm_cache_prefer_mask != 0) {
3905 omd->pvh_attrs &= ~PVF_WRITE;
3906 }
3907 #endif
3908 atomic_dec_32(&pmap_kmpages);
3909 #if defined(PMAP_CACHE_VIPT) && !defined(ARM_MMU_EXTENDED)
3910 } else if (arm_cache_prefer_mask != 0) {
3911 pmap_acquire_page_lock(omd);
3912 pool_put(&pmap_pv_pool,
3913 pmap_kremove_pg(opg, va));
3914 pmap_release_page_lock(omd);
3915 #endif
3916 }
3917 }
3918 if (l2pte_valid_p(opte)) {
3919 l2pte_reset(ptep);
3920 PTE_SYNC(ptep);
3921 #ifdef PMAP_CACHE_VIVT
3922 cpu_dcache_wbinv_range(va, PAGE_SIZE);
3923 #endif
3924 cpu_tlb_flushD_SE(va);
3925
3926 mappings += PAGE_SIZE / L2_S_SIZE;
3927 }
3928 va += PAGE_SIZE;
3929 ptep += PAGE_SIZE / L2_S_SIZE;
3930 }
3931 KDASSERTMSG(mappings <= l2b->l2b_occupancy, "%u %u",
3932 mappings, l2b->l2b_occupancy);
3933 l2b->l2b_occupancy -= mappings;
3934 //PTE_SYNC_RANGE(sptep, (u_int)(ptep - sptep));
3935 #ifdef UVMHIST
3936 total_mappings += mappings;
3937 #endif
3938 }
3939 pmap_release_pmap_lock(kpm);
3940 cpu_cpwait();
3941 UVMHIST_LOG(maphist, " <--- done (%ju mappings removed)",
3942 total_mappings, 0, 0, 0);
3943 }
3944
3945 bool
3946 pmap_extract(pmap_t pm, vaddr_t va, paddr_t *pap)
3947 {
3948
3949 return pmap_extract_coherency(pm, va, pap, NULL);
3950 }
3951
3952 bool
3953 pmap_extract_coherency(pmap_t pm, vaddr_t va, paddr_t *pap, bool *coherentp)
3954 {
3955 struct l2_dtable *l2;
3956 pd_entry_t *pdep, pde;
3957 pt_entry_t *ptep, pte;
3958 paddr_t pa;
3959 u_int l1slot;
3960 bool coherent;
3961
3962 pmap_acquire_pmap_lock(pm);
3963
3964 l1slot = l1pte_index(va);
3965 pdep = pmap_l1_kva(pm) + l1slot;
3966 pde = *pdep;
3967
3968 if (l1pte_section_p(pde)) {
3969 /*
3970 * These should only happen for pmap_kernel()
3971 */
3972 KDASSERT(pm == pmap_kernel());
3973 pmap_release_pmap_lock(pm);
3974 #if (ARM_MMU_V6 + ARM_MMU_V7) > 0
3975 if (l1pte_supersection_p(pde)) {
3976 pa = (pde & L1_SS_FRAME) | (va & L1_SS_OFFSET);
3977 } else
3978 #endif
3979 pa = (pde & L1_S_FRAME) | (va & L1_S_OFFSET);
3980 coherent = (pde & L1_S_CACHE_MASK) == 0;
3981 } else {
3982 /*
3983 * Note that we can't rely on the validity of the L1
3984 * descriptor as an indication that a mapping exists.
3985 * We have to look it up in the L2 dtable.
3986 */
3987 l2 = pm->pm_l2[L2_IDX(l1slot)];
3988
3989 if (l2 == NULL ||
3990 (ptep = l2->l2_bucket[L2_BUCKET(l1slot)].l2b_kva) == NULL) {
3991 pmap_release_pmap_lock(pm);
3992 return false;
3993 }
3994
3995 pte = ptep[l2pte_index(va)];
3996 pmap_release_pmap_lock(pm);
3997
3998 if (pte == 0)
3999 return false;
4000
4001 switch (pte & L2_TYPE_MASK) {
4002 case L2_TYPE_L:
4003 pa = (pte & L2_L_FRAME) | (va & L2_L_OFFSET);
4004 coherent = (pte & L2_L_CACHE_MASK) == 0;
4005 break;
4006
4007 default:
4008 pa = (pte & ~PAGE_MASK) | (va & PAGE_MASK);
4009 coherent = (pte & L2_S_CACHE_MASK) == 0;
4010 break;
4011 }
4012 }
4013
4014 if (pap != NULL)
4015 *pap = pa;
4016
4017 if (coherentp != NULL)
4018 *coherentp = (pm == pmap_kernel() && coherent);
4019
4020 return true;
4021 }
4022
4023 /*
4024 * pmap_pv_remove: remove an unmanaged pv-tracked page from all pmaps
4025 * that map it
4026 */
4027
4028 static void
4029 pmap_pv_remove(paddr_t pa)
4030 {
4031 struct pmap_page *pp;
4032
4033 pp = pmap_pv_tracked(pa);
4034 if (pp == NULL)
4035 panic("pmap_pv_protect: page not pv-tracked: 0x%"PRIxPADDR,
4036 pa);
4037
4038 struct vm_page_md *md = PMAP_PAGE_TO_MD(pp);
4039 pmap_page_remove(md, pa);
4040 }
4041
4042 void
4043 pmap_pv_protect(paddr_t pa, vm_prot_t prot)
4044 {
4045
4046 /* the only case is remove at the moment */
4047 KASSERT(prot == VM_PROT_NONE);
4048 pmap_pv_remove(pa);
4049 }
4050
4051 void
4052 pmap_protect(pmap_t pm, vaddr_t sva, vaddr_t eva, vm_prot_t prot)
4053 {
4054 struct l2_bucket *l2b;
4055 vaddr_t next_bucket;
4056
4057 NPDEBUG(PDB_PROTECT,
4058 printf("pmap_protect: pm %p sva 0x%lx eva 0x%lx prot 0x%x\n",
4059 pm, sva, eva, prot));
4060
4061 if ((prot & VM_PROT_READ) == 0) {
4062 pmap_remove(pm, sva, eva);
4063 return;
4064 }
4065
4066 if (prot & VM_PROT_WRITE) {
4067 /*
4068 * If this is a read->write transition, just ignore it and let
4069 * uvm_fault() take care of it later.
4070 */
4071 return;
4072 }
4073
4074 pmap_acquire_pmap_lock(pm);
4075
4076 #ifndef ARM_MMU_EXTENDED
4077 const bool flush = eva - sva >= PAGE_SIZE * 4;
4078 u_int flags = 0;
4079 #endif
4080 u_int clr_mask = PVF_WRITE | ((prot & VM_PROT_EXECUTE) ? 0 : PVF_EXEC);
4081
4082 while (sva < eva) {
4083 next_bucket = L2_NEXT_BUCKET_VA(sva);
4084 if (next_bucket > eva)
4085 next_bucket = eva;
4086
4087 l2b = pmap_get_l2_bucket(pm, sva);
4088 if (l2b == NULL) {
4089 sva = next_bucket;
4090 continue;
4091 }
4092
4093 pt_entry_t *ptep = &l2b->l2b_kva[l2pte_index(sva)];
4094
4095 while (sva < next_bucket) {
4096 const pt_entry_t opte = *ptep;
4097 if (l2pte_valid_p(opte) && l2pte_writable_p(opte)) {
4098 struct vm_page *pg;
4099 #ifndef ARM_MMU_EXTENDED
4100 u_int f;
4101 #endif
4102
4103 #ifdef PMAP_CACHE_VIVT
4104 /*
4105 * OK, at this point, we know we're doing
4106 * write-protect operation. If the pmap is
4107 * active, write-back the page.
4108 */
4109 pmap_cache_wbinv_page(pm, sva, false,
4110 PVF_REF | PVF_WRITE);
4111 #endif
4112
4113 pg = PHYS_TO_VM_PAGE(l2pte_pa(opte));
4114 pt_entry_t npte = l2pte_set_readonly(opte);
4115 l2pte_reset(ptep);
4116 PTE_SYNC(ptep);
4117 #ifdef ARM_MMU_EXTENDED
4118 pmap_tlb_flush_SE(pm, sva, PVF_REF);
4119 #endif
4120 l2pte_set(ptep, npte, 0);
4121 PTE_SYNC(ptep);
4122
4123 if (pg != NULL) {
4124 struct vm_page_md *md = VM_PAGE_TO_MD(pg);
4125 paddr_t pa = VM_PAGE_TO_PHYS(pg);
4126
4127 pmap_acquire_page_lock(md);
4128 #ifndef ARM_MMU_EXTENDED
4129 f =
4130 #endif
4131 pmap_modify_pv(md, pa, pm, sva,
4132 clr_mask, 0);
4133 pmap_vac_me_harder(md, pa, pm, sva);
4134 pmap_release_page_lock(md);
4135 #ifndef ARM_MMU_EXTENDED
4136 } else {
4137 f = PVF_REF | PVF_EXEC;
4138 }
4139
4140 if (flush) {
4141 flags |= f;
4142 } else {
4143 pmap_tlb_flush_SE(pm, sva, f);
4144 #endif
4145 }
4146 }
4147
4148 sva += PAGE_SIZE;
4149 ptep += PAGE_SIZE / L2_S_SIZE;
4150 }
4151 }
4152
4153 #ifndef ARM_MMU_EXTENDED
4154 if (flush) {
4155 if (PV_BEEN_EXECD(flags)) {
4156 pmap_tlb_flushID(pm);
4157 } else if (PV_BEEN_REFD(flags)) {
4158 pmap_tlb_flushD(pm);
4159 }
4160 }
4161 #endif
4162
4163 pmap_release_pmap_lock(pm);
4164 }
4165
4166 void
4167 pmap_icache_sync_range(pmap_t pm, vaddr_t sva, vaddr_t eva)
4168 {
4169 struct l2_bucket *l2b;
4170 pt_entry_t *ptep;
4171 vaddr_t next_bucket;
4172 vsize_t page_size = trunc_page(sva) + PAGE_SIZE - sva;
4173
4174 NPDEBUG(PDB_EXEC,
4175 printf("pmap_icache_sync_range: pm %p sva 0x%lx eva 0x%lx\n",
4176 pm, sva, eva));
4177
4178 pmap_acquire_pmap_lock(pm);
4179
4180 while (sva < eva) {
4181 next_bucket = L2_NEXT_BUCKET_VA(sva);
4182 if (next_bucket > eva)
4183 next_bucket = eva;
4184
4185 l2b = pmap_get_l2_bucket(pm, sva);
4186 if (l2b == NULL) {
4187 sva = next_bucket;
4188 continue;
4189 }
4190
4191 for (ptep = &l2b->l2b_kva[l2pte_index(sva)];
4192 sva < next_bucket;
4193 sva += page_size,
4194 ptep += PAGE_SIZE / L2_S_SIZE,
4195 page_size = PAGE_SIZE) {
4196 if (l2pte_valid_p(*ptep)) {
4197 cpu_icache_sync_range(sva,
4198 uimin(page_size, eva - sva));
4199 }
4200 }
4201 }
4202
4203 pmap_release_pmap_lock(pm);
4204 }
4205
4206 void
4207 pmap_page_protect(struct vm_page *pg, vm_prot_t prot)
4208 {
4209 struct vm_page_md *md = VM_PAGE_TO_MD(pg);
4210 paddr_t pa = VM_PAGE_TO_PHYS(pg);
4211
4212 NPDEBUG(PDB_PROTECT,
4213 printf("pmap_page_protect: md %p (0x%08lx), prot 0x%x\n",
4214 md, pa, prot));
4215
4216 switch(prot) {
4217 case VM_PROT_READ|VM_PROT_WRITE:
4218 #if defined(ARM_MMU_EXTENDED)
4219 pmap_acquire_page_lock(md);
4220 pmap_clearbit(md, pa, PVF_EXEC);
4221 pmap_release_page_lock(md);
4222 break;
4223 #endif
4224 case VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE:
4225 break;
4226
4227 case VM_PROT_READ:
4228 #if defined(ARM_MMU_EXTENDED)
4229 pmap_acquire_page_lock(md);
4230 pmap_clearbit(md, pa, PVF_WRITE|PVF_EXEC);
4231 pmap_release_page_lock(md);
4232 break;
4233 #endif
4234 case VM_PROT_READ|VM_PROT_EXECUTE:
4235 pmap_acquire_page_lock(md);
4236 pmap_clearbit(md, pa, PVF_WRITE);
4237 pmap_release_page_lock(md);
4238 break;
4239
4240 default:
4241 pmap_page_remove(md, pa);
4242 break;
4243 }
4244 }
4245
4246 /*
4247 * pmap_clear_modify:
4248 *
4249 * Clear the "modified" attribute for a page.
4250 */
4251 bool
4252 pmap_clear_modify(struct vm_page *pg)
4253 {
4254 struct vm_page_md *md = VM_PAGE_TO_MD(pg);
4255 paddr_t pa = VM_PAGE_TO_PHYS(pg);
4256 bool rv;
4257
4258 pmap_acquire_page_lock(md);
4259
4260 if (md->pvh_attrs & PVF_MOD) {
4261 rv = true;
4262 #if defined(PMAP_CACHE_VIPT) && !defined(ARM_MMU_EXTENDED)
4263 /*
4264 * If we are going to clear the modified bit and there are
4265 * no other modified bits set, flush the page to memory and
4266 * mark it clean.
4267 */
4268 if ((md->pvh_attrs & (PVF_DMOD|PVF_NC)) == PVF_MOD)
4269 pmap_flush_page(md, pa, PMAP_CLEAN_PRIMARY);
4270 #endif
4271 pmap_clearbit(md, pa, PVF_MOD);
4272 } else {
4273 rv = false;
4274 }
4275 pmap_release_page_lock(md);
4276
4277 return rv;
4278 }
4279
4280 /*
4281 * pmap_clear_reference:
4282 *
4283 * Clear the "referenced" attribute for a page.
4284 */
4285 bool
4286 pmap_clear_reference(struct vm_page *pg)
4287 {
4288 struct vm_page_md *md = VM_PAGE_TO_MD(pg);
4289 paddr_t pa = VM_PAGE_TO_PHYS(pg);
4290 bool rv;
4291
4292 pmap_acquire_page_lock(md);
4293
4294 if (md->pvh_attrs & PVF_REF) {
4295 rv = true;
4296 pmap_clearbit(md, pa, PVF_REF);
4297 } else {
4298 rv = false;
4299 }
4300 pmap_release_page_lock(md);
4301
4302 return rv;
4303 }
4304
4305 /*
4306 * pmap_is_modified:
4307 *
4308 * Test if a page has the "modified" attribute.
4309 */
4310 /* See <arm/arm32/pmap.h> */
4311
4312 /*
4313 * pmap_is_referenced:
4314 *
4315 * Test if a page has the "referenced" attribute.
4316 */
4317 /* See <arm/arm32/pmap.h> */
4318
4319 #if defined(ARM_MMU_EXTENDED) && 0
4320 int
4321 pmap_prefetchabt_fixup(void *v)
4322 {
4323 struct trapframe * const tf = v;
4324 vaddr_t va = trunc_page(tf->tf_pc);
4325 int rv = ABORT_FIXUP_FAILED;
4326
4327 if (!TRAP_USERMODE(tf) && va < VM_MAXUSER_ADDRESS)
4328 return rv;
4329
4330 kpreempt_disable();
4331 pmap_t pm = curcpu()->ci_pmap_cur;
4332 const size_t l1slot = l1pte_index(va);
4333 struct l2_dtable * const l2 = pm->pm_l2[L2_IDX(l1slot)];
4334 if (l2 == NULL)
4335 goto out;
4336
4337 struct l2_bucket * const l2b = &l2->l2_bucket[L2_BUCKET(l1slot)];
4338 if (l2b->l2b_kva == NULL)
4339 goto out;
4340
4341 /*
4342 * Check the PTE itself.
4343 */
4344 pt_entry_t * const ptep = &l2b->l2b_kva[l2pte_index(va)];
4345 const pt_entry_t opte = *ptep;
4346 if ((opte & L2_S_PROT_U) == 0 || (opte & L2_XS_XN) == 0)
4347 goto out;
4348
4349 paddr_t pa = l2pte_pa(opte);
4350 struct vm_page * const pg = PHYS_TO_VM_PAGE(pa);
4351 KASSERT(pg != NULL);
4352
4353 struct vm_page_md * const md = VM_PAGE_TO_MD(pg);
4354
4355 pmap_acquire_page_lock(md);
4356 struct pv_entry * const pv = pmap_find_pv(md, pm, va);
4357 KASSERT(pv != NULL);
4358
4359 if (PV_IS_EXEC_P(pv->pv_flags)) {
4360 l2pte_reset(ptep);
4361 PTE_SYNC(ptep);
4362 pmap_tlb_flush_SE(pm, va, PVF_EXEC | PVF_REF);
4363 if (!PV_IS_EXEC_P(md->pvh_attrs)) {
4364 pmap_syncicache_page(md, pa);
4365 }
4366 rv = ABORT_FIXUP_RETURN;
4367 l2pte_set(ptep, opte & ~L2_XS_XN, 0);
4368 PTE_SYNC(ptep);
4369 }
4370 pmap_release_page_lock(md);
4371
4372 out:
4373 kpreempt_enable();
4374 return rv;
4375 }
4376 #endif
4377
4378 int
4379 pmap_fault_fixup(pmap_t pm, vaddr_t va, vm_prot_t ftype, int user)
4380 {
4381 struct l2_dtable *l2;
4382 struct l2_bucket *l2b;
4383 paddr_t pa;
4384 const size_t l1slot = l1pte_index(va);
4385 int rv = 0;
4386
4387 UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
4388
4389 va = trunc_page(va);
4390
4391 KASSERT(!user || (pm != pmap_kernel()));
4392
4393 UVMHIST_LOG(maphist, " (pm=%#jx, va=%#jx, ftype=%#jx, user=%jd)",
4394 (uintptr_t)pm, va, ftype, user);
4395 #ifdef ARM_MMU_EXTENDED
4396 UVMHIST_LOG(maphist, " ti=%#jx pai=%#jx asid=%#jx",
4397 (uintptr_t)cpu_tlb_info(curcpu()),
4398 (uintptr_t)PMAP_PAI(pm, cpu_tlb_info(curcpu())),
4399 (uintptr_t)PMAP_PAI(pm, cpu_tlb_info(curcpu()))->pai_asid, 0);
4400 #endif
4401
4402 pmap_acquire_pmap_lock(pm);
4403
4404 /*
4405 * If there is no l2_dtable for this address, then the process
4406 * has no business accessing it.
4407 *
4408 * Note: This will catch userland processes trying to access
4409 * kernel addresses.
4410 */
4411 l2 = pm->pm_l2[L2_IDX(l1slot)];
4412 if (l2 == NULL) {
4413 UVMHIST_LOG(maphist, " no l2 for l1slot %#jx", l1slot, 0, 0, 0);
4414 goto out;
4415 }
4416
4417 /*
4418 * Likewise if there is no L2 descriptor table
4419 */
4420 l2b = &l2->l2_bucket[L2_BUCKET(l1slot)];
4421 if (l2b->l2b_kva == NULL) {
4422 UVMHIST_LOG(maphist, " <-- done (no ptep for l1slot %#jx)",
4423 l1slot, 0, 0, 0);
4424 goto out;
4425 }
4426
4427 /*
4428 * Check the PTE itself.
4429 */
4430 pt_entry_t * const ptep = &l2b->l2b_kva[l2pte_index(va)];
4431 pt_entry_t const opte = *ptep;
4432 if (opte == 0 || (opte & L2_TYPE_MASK) == L2_TYPE_L) {
4433 UVMHIST_LOG(maphist, " <-- done (empty pde for l1slot %#jx)",
4434 l1slot, 0, 0, 0);
4435 goto out;
4436 }
4437
4438 #ifndef ARM_HAS_VBAR
4439 /*
4440 * Catch a userland access to the vector page mapped at 0x0
4441 */
4442 if (user && (opte & L2_S_PROT_U) == 0) {
4443 UVMHIST_LOG(maphist, " <-- done (vector_page)", 0, 0, 0, 0);
4444 goto out;
4445 }
4446 #endif
4447
4448 pa = l2pte_pa(opte);
4449
4450 if ((ftype & VM_PROT_WRITE) && !l2pte_writable_p(opte)) {
4451 /*
4452 * This looks like a good candidate for "page modified"
4453 * emulation...
4454 */
4455 struct pv_entry *pv;
4456 struct vm_page *pg;
4457
4458 /* Extract the physical address of the page */
4459 if ((pg = PHYS_TO_VM_PAGE(pa)) == NULL) {
4460 UVMHIST_LOG(maphist, " <-- done (mod/ref unmanaged page)", 0, 0, 0, 0);
4461 goto out;
4462 }
4463
4464 struct vm_page_md *md = VM_PAGE_TO_MD(pg);
4465
4466 /* Get the current flags for this page. */
4467 pmap_acquire_page_lock(md);
4468 pv = pmap_find_pv(md, pm, va);
4469 if (pv == NULL || PV_IS_KENTRY_P(pv->pv_flags)) {
4470 pmap_release_page_lock(md);
4471 UVMHIST_LOG(maphist, " <-- done (mod/ref emul: no PV)", 0, 0, 0, 0);
4472 goto out;
4473 }
4474
4475 /*
4476 * Do the flags say this page is writable? If not then it
4477 * is a genuine write fault. If yes then the write fault is
4478 * our fault as we did not reflect the write access in the
4479 * PTE. Now we know a write has occurred we can correct this
4480 * and also set the modified bit
4481 */
4482 if ((pv->pv_flags & PVF_WRITE) == 0) {
4483 pmap_release_page_lock(md);
4484 goto out;
4485 }
4486
4487 md->pvh_attrs |= PVF_REF | PVF_MOD;
4488 pv->pv_flags |= PVF_REF | PVF_MOD;
4489 #if defined(PMAP_CACHE_VIPT) && !defined(ARM_MMU_EXTENDED)
4490 /*
4491 * If there are cacheable mappings for this page, mark it dirty.
4492 */
4493 if ((md->pvh_attrs & PVF_NC) == 0)
4494 md->pvh_attrs |= PVF_DIRTY;
4495 #endif
4496 #ifdef ARM_MMU_EXTENDED
4497 if (md->pvh_attrs & PVF_EXEC) {
4498 md->pvh_attrs &= ~PVF_EXEC;
4499 PMAPCOUNT(exec_discarded_modfixup);
4500 }
4501 #endif
4502 pmap_release_page_lock(md);
4503
4504 /*
4505 * Re-enable write permissions for the page. No need to call
4506 * pmap_vac_me_harder(), since this is just a
4507 * modified-emulation fault, and the PVF_WRITE bit isn't
4508 * changing. We've already set the cacheable bits based on
4509 * the assumption that we can write to this page.
4510 */
4511 const pt_entry_t npte =
4512 l2pte_set_writable((opte & ~L2_TYPE_MASK) | L2_S_PROTO)
4513 #ifdef ARM_MMU_EXTENDED
4514 | (pm != pmap_kernel() ? L2_XS_nG : 0)
4515 #endif
4516 | 0;
4517 l2pte_reset(ptep);
4518 PTE_SYNC(ptep);
4519 pmap_tlb_flush_SE(pm, va,
4520 (ftype & VM_PROT_EXECUTE) ? PVF_EXEC | PVF_REF : PVF_REF);
4521 l2pte_set(ptep, npte, 0);
4522 PTE_SYNC(ptep);
4523 PMAPCOUNT(fixup_mod);
4524 rv = 1;
4525 UVMHIST_LOG(maphist, " <-- done (mod/ref emul: changed pte "
4526 "from %#jx to %#jx)", opte, npte, 0, 0);
4527 } else if ((opte & L2_TYPE_MASK) == L2_TYPE_INV) {
4528 /*
4529 * This looks like a good candidate for "page referenced"
4530 * emulation.
4531 */
4532 struct vm_page *pg;
4533
4534 /* Extract the physical address of the page */
4535 if ((pg = PHYS_TO_VM_PAGE(pa)) == NULL) {
4536 UVMHIST_LOG(maphist, " <-- done (ref emul: unmanaged page)", 0, 0, 0, 0);
4537 goto out;
4538 }
4539
4540 struct vm_page_md *md = VM_PAGE_TO_MD(pg);
4541
4542 /* Get the current flags for this page. */
4543 pmap_acquire_page_lock(md);
4544 struct pv_entry *pv = pmap_find_pv(md, pm, va);
4545 if (pv == NULL || PV_IS_KENTRY_P(pv->pv_flags)) {
4546 pmap_release_page_lock(md);
4547 UVMHIST_LOG(maphist, " <-- done (ref emul no PV)", 0, 0, 0, 0);
4548 goto out;
4549 }
4550
4551 md->pvh_attrs |= PVF_REF;
4552 pv->pv_flags |= PVF_REF;
4553
4554 pt_entry_t npte =
4555 l2pte_set_readonly((opte & ~L2_TYPE_MASK) | L2_S_PROTO);
4556 #ifdef ARM_MMU_EXTENDED
4557 if (pm != pmap_kernel()) {
4558 npte |= L2_XS_nG;
4559 }
4560 /*
4561 * If we got called from prefetch abort, then ftype will have
4562 * VM_PROT_EXECUTE set. Now see if we have no-execute set in
4563 * the PTE.
4564 */
4565 if (user && (ftype & VM_PROT_EXECUTE) && (npte & L2_XS_XN)) {
4566 /*
4567 * Is this a mapping of an executable page?
4568 */
4569 if ((pv->pv_flags & PVF_EXEC) == 0) {
4570 pmap_release_page_lock(md);
4571 UVMHIST_LOG(maphist, " <-- done (ref emul: no exec)",
4572 0, 0, 0, 0);
4573 goto out;
4574 }
4575 /*
4576 * If we haven't synced the page, do so now.
4577 */
4578 if ((md->pvh_attrs & PVF_EXEC) == 0) {
4579 UVMHIST_LOG(maphist, " ref emul: syncicache "
4580 "page #%#jx", pa, 0, 0, 0);
4581 pmap_syncicache_page(md, pa);
4582 PMAPCOUNT(fixup_exec);
4583 }
4584 npte &= ~L2_XS_XN;
4585 }
4586 #endif /* ARM_MMU_EXTENDED */
4587 pmap_release_page_lock(md);
4588 l2pte_reset(ptep);
4589 PTE_SYNC(ptep);
4590 pmap_tlb_flush_SE(pm, va,
4591 (ftype & VM_PROT_EXECUTE) ? PVF_EXEC | PVF_REF : PVF_REF);
4592 l2pte_set(ptep, npte, 0);
4593 PTE_SYNC(ptep);
4594 PMAPCOUNT(fixup_ref);
4595 rv = 1;
4596 UVMHIST_LOG(maphist, " <-- done (ref emul: changed pte from "
4597 "%#jx to %#jx)", opte, npte, 0, 0);
4598 #ifdef ARM_MMU_EXTENDED
4599 } else if (user && (ftype & VM_PROT_EXECUTE) && (opte & L2_XS_XN)) {
4600 struct vm_page * const pg = PHYS_TO_VM_PAGE(pa);
4601 if (pg == NULL) {
4602 UVMHIST_LOG(maphist, " <-- done (unmanaged page)", 0, 0, 0, 0);
4603 goto out;
4604 }
4605
4606 struct vm_page_md * const md = VM_PAGE_TO_MD(pg);
4607
4608 /* Get the current flags for this page. */
4609 pmap_acquire_page_lock(md);
4610 struct pv_entry * const pv = pmap_find_pv(md, pm, va);
4611 if (pv == NULL || (pv->pv_flags & PVF_EXEC) == 0) {
4612 pmap_release_page_lock(md);
4613 UVMHIST_LOG(maphist, " <-- done (no PV or not EXEC)", 0, 0, 0, 0);
4614 goto out;
4615 }
4616
4617 /*
4618 * If we haven't synced the page, do so now.
4619 */
4620 if ((md->pvh_attrs & PVF_EXEC) == 0) {
4621 UVMHIST_LOG(maphist, "syncicache page #%#jx",
4622 pa, 0, 0, 0);
4623 pmap_syncicache_page(md, pa);
4624 }
4625 pmap_release_page_lock(md);
4626 /*
4627 * Turn off no-execute.
4628 */
4629 KASSERT(opte & L2_XS_nG);
4630 l2pte_reset(ptep);
4631 PTE_SYNC(ptep);
4632 pmap_tlb_flush_SE(pm, va, PVF_EXEC | PVF_REF);
4633 l2pte_set(ptep, opte & ~L2_XS_XN, 0);
4634 PTE_SYNC(ptep);
4635 rv = 1;
4636 PMAPCOUNT(fixup_exec);
4637 UVMHIST_LOG(maphist, "exec: changed pte from %#jx to %#jx",
4638 opte, opte & ~L2_XS_XN, 0, 0);
4639 #endif
4640 }
4641
4642 #ifndef ARM_MMU_EXTENDED
4643 /*
4644 * We know there is a valid mapping here, so simply
4645 * fix up the L1 if necessary.
4646 */
4647 pd_entry_t * const pdep = pmap_l1_kva(pm) + l1slot;
4648 pd_entry_t pde = L1_C_PROTO | l2b->l2b_pa | L1_C_DOM(pmap_domain(pm));
4649 if (*pdep != pde) {
4650 l1pte_setone(pdep, pde);
4651 PDE_SYNC(pdep);
4652 rv = 1;
4653 PMAPCOUNT(fixup_pdes);
4654 }
4655 #endif
4656
4657 #ifdef CPU_SA110
4658 /*
4659 * There are bugs in the rev K SA110. This is a check for one
4660 * of them.
4661 */
4662 if (rv == 0 && curcpu()->ci_arm_cputype == CPU_ID_SA110 &&
4663 curcpu()->ci_arm_cpurev < 3) {
4664 /* Always current pmap */
4665 if (l2pte_valid_p(opte)) {
4666 extern int kernel_debug;
4667 if (kernel_debug & 1) {
4668 struct proc *p = curlwp->l_proc;
4669 printf("prefetch_abort: page is already "
4670 "mapped - pte=%p *pte=%08x\n", ptep, opte);
4671 printf("prefetch_abort: pc=%08lx proc=%p "
4672 "process=%s\n", va, p, p->p_comm);
4673 printf("prefetch_abort: far=%08x fs=%x\n",
4674 cpu_faultaddress(), cpu_faultstatus());
4675 }
4676 #ifdef DDB
4677 if (kernel_debug & 2)
4678 Debugger();
4679 #endif
4680 rv = 1;
4681 }
4682 }
4683 #endif /* CPU_SA110 */
4684
4685 #ifndef ARM_MMU_EXTENDED
4686 /*
4687 * If 'rv == 0' at this point, it generally indicates that there is a
4688 * stale TLB entry for the faulting address. That might be due to a
4689 * wrong setting of pmap_needs_pte_sync. So set it and retry.
4690 */
4691 if (rv == 0
4692 && pm->pm_l1->l1_domain_use_count == 1
4693 && pmap_needs_pte_sync == 0) {
4694 pmap_needs_pte_sync = 1;
4695 PTE_SYNC(ptep);
4696 PMAPCOUNT(fixup_ptesync);
4697 rv = 1;
4698 }
4699 #endif
4700
4701 #ifndef MULTIPROCESSOR
4702 #if defined(DEBUG) || 1
4703 /*
4704 * If 'rv == 0' at this point, it generally indicates that there is a
4705 * stale TLB entry for the faulting address. This happens when two or
4706 * more processes are sharing an L1. Since we don't flush the TLB on
4707 * a context switch between such processes, we can take domain faults
4708 * for mappings which exist at the same VA in both processes. EVEN IF
4709 * WE'VE RECENTLY FIXED UP THE CORRESPONDING L1 in pmap_enter(), for
4710 * example.
4711 *
4712 * This is extremely likely to happen if pmap_enter() updated the L1
4713 * entry for a recently entered mapping. In this case, the TLB is
4714 * flushed for the new mapping, but there may still be TLB entries for
4715 * other mappings belonging to other processes in the 1MB range
4716 * covered by the L1 entry.
4717 *
4718 * Since 'rv == 0', we know that the L1 already contains the correct
4719 * value, so the fault must be due to a stale TLB entry.
4720 *
4721 * Since we always need to flush the TLB anyway in the case where we
4722 * fixed up the L1, or frobbed the L2 PTE, we effectively deal with
4723 * stale TLB entries dynamically.
4724 *
4725 * However, the above condition can ONLY happen if the current L1 is
4726 * being shared. If it happens when the L1 is unshared, it indicates
4727 * that other parts of the pmap are not doing their job WRT managing
4728 * the TLB.
4729 */
4730 if (rv == 0
4731 #ifndef ARM_MMU_EXTENDED
4732 && pm->pm_l1->l1_domain_use_count == 1
4733 #endif
4734 && true) {
4735 #ifdef DEBUG
4736 extern int last_fault_code;
4737 #else
4738 int last_fault_code = ftype & VM_PROT_EXECUTE
4739 ? armreg_ifsr_read()
4740 : armreg_dfsr_read();
4741 #endif
4742 printf("fixup: pm %p, va 0x%lx, ftype %d - nothing to do!\n",
4743 pm, va, ftype);
4744 printf("fixup: l2 %p, l2b %p, ptep %p, pte %#x\n",
4745 l2, l2b, ptep, opte);
4746
4747 #ifndef ARM_MMU_EXTENDED
4748 printf("fixup: pdep %p, pde %#x, fsr %#x\n",
4749 pdep, pde, last_fault_code);
4750 #else
4751 printf("fixup: pdep %p, pde %#x, ttbcr %#x\n",
4752 &pmap_l1_kva(pm)[l1slot], pmap_l1_kva(pm)[l1slot],
4753 armreg_ttbcr_read());
4754 printf("fixup: fsr %#x cpm %p casid %#x contextidr %#x dacr %#x\n",
4755 last_fault_code, curcpu()->ci_pmap_cur,
4756 curcpu()->ci_pmap_asid_cur,
4757 armreg_contextidr_read(), armreg_dacr_read());
4758 #ifdef _ARM_ARCH_7
4759 if (ftype & VM_PROT_WRITE)
4760 armreg_ats1cuw_write(va);
4761 else
4762 armreg_ats1cur_write(va);
4763 arm_isb();
4764 printf("fixup: par %#x\n", armreg_par_read());
4765 #endif
4766 #endif
4767 #ifdef DDB
4768 extern int kernel_debug;
4769
4770 if (kernel_debug & 2) {
4771 pmap_release_pmap_lock(pm);
4772 #ifdef UVMHIST
4773 KERNHIST_DUMP(maphist);
4774 #endif
4775 cpu_Debugger();
4776 pmap_acquire_pmap_lock(pm);
4777 }
4778 #endif
4779 }
4780 #endif
4781 #endif
4782
4783 #ifndef ARM_MMU_EXTENDED
4784 /* Flush the TLB in the shared L1 case - see comment above */
4785 pmap_tlb_flush_SE(pm, va,
4786 (ftype & VM_PROT_EXECUTE) ? PVF_EXEC | PVF_REF : PVF_REF);
4787 #endif
4788
4789 rv = 1;
4790
4791 out:
4792 pmap_release_pmap_lock(pm);
4793
4794 return rv;
4795 }
4796
4797 /*
4798 * Routine: pmap_procwr
4799 *
4800 * Function:
4801 * Synchronize caches corresponding to [addr, addr+len) in p.
4802 *
4803 */
4804 void
4805 pmap_procwr(struct proc *p, vaddr_t va, int len)
4806 {
4807 #ifndef ARM_MMU_EXTENDED
4808
4809 /* We only need to do anything if it is the current process. */
4810 if (p == curproc)
4811 cpu_icache_sync_range(va, len);
4812 #endif
4813 }
4814
4815 /*
4816 * Routine: pmap_unwire
4817 * Function: Clear the wired attribute for a map/virtual-address pair.
4818 *
4819 * In/out conditions:
4820 * The mapping must already exist in the pmap.
4821 */
4822 void
4823 pmap_unwire(pmap_t pm, vaddr_t va)
4824 {
4825 struct l2_bucket *l2b;
4826 pt_entry_t *ptep, pte;
4827 struct vm_page *pg;
4828 paddr_t pa;
4829
4830 NPDEBUG(PDB_WIRING, printf("pmap_unwire: pm %p, va 0x%08lx\n", pm, va));
4831
4832 pmap_acquire_pmap_lock(pm);
4833
4834 l2b = pmap_get_l2_bucket(pm, va);
4835 KDASSERT(l2b != NULL);
4836
4837 ptep = &l2b->l2b_kva[l2pte_index(va)];
4838 pte = *ptep;
4839
4840 /* Extract the physical address of the page */
4841 pa = l2pte_pa(pte);
4842
4843 if ((pg = PHYS_TO_VM_PAGE(pa)) != NULL) {
4844 /* Update the wired bit in the pv entry for this page. */
4845 struct vm_page_md *md = VM_PAGE_TO_MD(pg);
4846
4847 pmap_acquire_page_lock(md);
4848 (void) pmap_modify_pv(md, pa, pm, va, PVF_WIRED, 0);
4849 pmap_release_page_lock(md);
4850 }
4851
4852 pmap_release_pmap_lock(pm);
4853 }
4854
4855 #ifdef ARM_MMU_EXTENDED
4856 void
4857 pmap_md_pdetab_activate(pmap_t pm, struct lwp *l)
4858 {
4859 UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
4860
4861 /*
4862 * Assume that TTBR1 has only global mappings and TTBR0 only
4863 * has non-global mappings. To prevent speculation from doing
4864 * evil things we disable translation table walks using TTBR0
4865 * before setting the CONTEXTIDR (ASID) or new TTBR0 value.
4866 * Once both are set, table walks are reenabled.
4867 */
4868 const uint32_t old_ttbcr = armreg_ttbcr_read();
4869 armreg_ttbcr_write(old_ttbcr | TTBCR_S_PD0);
4870 arm_isb();
4871
4872 pmap_tlb_asid_acquire(pm, l);
4873
4874 struct cpu_info * const ci = curcpu();
4875 struct pmap_asid_info * const pai = PMAP_PAI(pm, cpu_tlb_info(ci));
4876
4877 cpu_setttb(pm->pm_l1_pa, pai->pai_asid);
4878 /*
4879 * Now we can reenable tablewalks since the CONTEXTIDR and TTRB0
4880 * have been updated.
4881 */
4882 arm_isb();
4883
4884 if (pm != pmap_kernel()) {
4885 armreg_ttbcr_write(old_ttbcr & ~TTBCR_S_PD0);
4886 }
4887 cpu_cpwait();
4888
4889 UVMHIST_LOG(maphist, " pm %#jx pm->pm_l1_pa %08jx asid %ju... done",
4890 (uintptr_t)pm, pm->pm_l1_pa, pai->pai_asid, 0);
4891
4892 KASSERTMSG(ci->ci_pmap_asid_cur == pai->pai_asid, "%u vs %u",
4893 ci->ci_pmap_asid_cur, pai->pai_asid);
4894 ci->ci_pmap_cur = pm;
4895 }
4896
4897 void
4898 pmap_md_pdetab_deactivate(pmap_t pm)
4899 {
4900
4901 UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
4902
4903 kpreempt_disable();
4904 struct cpu_info * const ci = curcpu();
4905 /*
4906 * Disable translation table walks from TTBR0 while no pmap has been
4907 * activated.
4908 */
4909 const uint32_t old_ttbcr = armreg_ttbcr_read();
4910 armreg_ttbcr_write(old_ttbcr | TTBCR_S_PD0);
4911 arm_isb();
4912 pmap_tlb_asid_deactivate(pm);
4913 cpu_setttb(pmap_kernel()->pm_l1_pa, KERNEL_PID);
4914 arm_isb();
4915
4916 ci->ci_pmap_cur = pmap_kernel();
4917 KASSERTMSG(ci->ci_pmap_asid_cur == KERNEL_PID, "ci_pmap_asid_cur %u",
4918 ci->ci_pmap_asid_cur);
4919 kpreempt_enable();
4920 }
4921 #endif
4922
4923 void
4924 pmap_activate(struct lwp *l)
4925 {
4926 extern int block_userspace_access;
4927 pmap_t npm = l->l_proc->p_vmspace->vm_map.pmap;
4928
4929 UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
4930
4931 UVMHIST_LOG(maphist, "(l=%#jx) pm=%#jx", (uintptr_t)l, (uintptr_t)npm,
4932 0, 0);
4933
4934 struct cpu_info * const ci = curcpu();
4935
4936 /*
4937 * If activating a non-current lwp or the current lwp is
4938 * already active, just return.
4939 */
4940 if (false
4941 || l != curlwp
4942 #ifdef ARM_MMU_EXTENDED
4943 || (ci->ci_pmap_cur == npm &&
4944 (npm == pmap_kernel()
4945 /* || PMAP_PAI_ASIDVALID_P(pai, cpu_tlb_info(ci)) */))
4946 #else
4947 || npm->pm_activated == true
4948 #endif
4949 || false) {
4950 UVMHIST_LOG(maphist, " <-- (same pmap)", (uintptr_t)curlwp,
4951 (uintptr_t)l, 0, 0);
4952 return;
4953 }
4954
4955 #ifndef ARM_MMU_EXTENDED
4956 const uint32_t ndacr = (DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL * 2))
4957 | (DOMAIN_CLIENT << (pmap_domain(npm) * 2));
4958
4959 /*
4960 * If TTB and DACR are unchanged, short-circuit all the
4961 * TLB/cache management stuff.
4962 */
4963 pmap_t opm = ci->ci_lastlwp
4964 ? ci->ci_lastlwp->l_proc->p_vmspace->vm_map.pmap
4965 : NULL;
4966 if (opm != NULL) {
4967 uint32_t odacr = (DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL * 2))
4968 | (DOMAIN_CLIENT << (pmap_domain(opm) * 2));
4969
4970 if (opm->pm_l1 == npm->pm_l1 && odacr == ndacr)
4971 goto all_done;
4972 }
4973 #endif /* !ARM_MMU_EXTENDED */
4974
4975 PMAPCOUNT(activations);
4976 block_userspace_access = 1;
4977
4978 #ifndef ARM_MMU_EXTENDED
4979 /*
4980 * If switching to a user vmspace which is different to the
4981 * most recent one, and the most recent one is potentially
4982 * live in the cache, we must write-back and invalidate the
4983 * entire cache.
4984 */
4985 pmap_t rpm = ci->ci_pmap_lastuser;
4986
4987 /*
4988 * XXXSCW: There's a corner case here which can leave turds in the
4989 * cache as reported in kern/41058. They're probably left over during
4990 * tear-down and switching away from an exiting process. Until the root
4991 * cause is identified and fixed, zap the cache when switching pmaps.
4992 * This will result in a few unnecessary cache flushes, but that's
4993 * better than silently corrupting data.
4994 */
4995 #if 0
4996 if (npm != pmap_kernel() && rpm && npm != rpm &&
4997 rpm->pm_cstate.cs_cache) {
4998 rpm->pm_cstate.cs_cache = 0;
4999 #ifdef PMAP_CACHE_VIVT
5000 cpu_idcache_wbinv_all();
5001 #endif
5002 }
5003 #else
5004 if (rpm) {
5005 rpm->pm_cstate.cs_cache = 0;
5006 if (npm == pmap_kernel())
5007 ci->ci_pmap_lastuser = NULL;
5008 #ifdef PMAP_CACHE_VIVT
5009 cpu_idcache_wbinv_all();
5010 #endif
5011 }
5012 #endif
5013
5014 /* No interrupts while we frob the TTB/DACR */
5015 uint32_t oldirqstate = disable_interrupts(IF32_bits);
5016 #endif /* !ARM_MMU_EXTENDED */
5017
5018 #ifndef ARM_HAS_VBAR
5019 /*
5020 * For ARM_VECTORS_LOW, we MUST, I repeat, MUST fix up the L1
5021 * entry corresponding to 'vector_page' in the incoming L1 table
5022 * before switching to it otherwise subsequent interrupts/exceptions
5023 * (including domain faults!) will jump into hyperspace.
5024 */
5025 if (npm->pm_pl1vec != NULL) {
5026 cpu_tlb_flushID_SE((u_int)vector_page);
5027 cpu_cpwait();
5028 *npm->pm_pl1vec = npm->pm_l1vec;
5029 PTE_SYNC(npm->pm_pl1vec);
5030 }
5031 #endif
5032
5033 #ifdef ARM_MMU_EXTENDED
5034 pmap_md_pdetab_activate(npm, l);
5035 #else
5036 cpu_domains(ndacr);
5037 if (npm == pmap_kernel() || npm == rpm) {
5038 /*
5039 * Switching to a kernel thread, or back to the
5040 * same user vmspace as before... Simply update
5041 * the TTB (no TLB flush required)
5042 */
5043 cpu_setttb(npm->pm_l1->l1_physaddr, false);
5044 cpu_cpwait();
5045 } else {
5046 /*
5047 * Otherwise, update TTB and flush TLB
5048 */
5049 cpu_context_switch(npm->pm_l1->l1_physaddr);
5050 if (rpm != NULL)
5051 rpm->pm_cstate.cs_tlb = 0;
5052 }
5053
5054 restore_interrupts(oldirqstate);
5055 #endif /* ARM_MMU_EXTENDED */
5056
5057 block_userspace_access = 0;
5058
5059 #ifndef ARM_MMU_EXTENDED
5060 all_done:
5061 /*
5062 * The new pmap is resident. Make sure it's marked
5063 * as resident in the cache/TLB.
5064 */
5065 npm->pm_cstate.cs_all = PMAP_CACHE_STATE_ALL;
5066 if (npm != pmap_kernel())
5067 ci->ci_pmap_lastuser = npm;
5068
5069 /* The old pmap is not longer active */
5070 if (opm != npm) {
5071 if (opm != NULL)
5072 opm->pm_activated = false;
5073
5074 /* But the new one is */
5075 npm->pm_activated = true;
5076 }
5077 ci->ci_pmap_cur = npm;
5078 #endif
5079 UVMHIST_LOG(maphist, " <-- done", 0, 0, 0, 0);
5080 }
5081
5082 void
5083 pmap_deactivate(struct lwp *l)
5084 {
5085 pmap_t pm = l->l_proc->p_vmspace->vm_map.pmap;
5086
5087 UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
5088
5089 UVMHIST_LOG(maphist, "(l=%#jx) pm=%#jx", (uintptr_t)l, (uintptr_t)pm,
5090 0, 0);
5091
5092 #ifdef ARM_MMU_EXTENDED
5093 pmap_md_pdetab_deactivate(pm);
5094 #else
5095 /*
5096 * If the process is exiting, make sure pmap_activate() does
5097 * a full MMU context-switch and cache flush, which we might
5098 * otherwise skip. See PR port-arm/38950.
5099 */
5100 if (l->l_proc->p_sflag & PS_WEXIT)
5101 curcpu()->ci_lastlwp = NULL;
5102
5103 pm->pm_activated = false;
5104 #endif
5105 UVMHIST_LOG(maphist, " <-- done", 0, 0, 0, 0);
5106 }
5107
5108 void
5109 pmap_update(pmap_t pm)
5110 {
5111
5112 UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
5113
5114 UVMHIST_LOG(maphist, "pm=%#jx remove_all %jd", (uintptr_t)pm,
5115 pm->pm_remove_all, 0, 0);
5116
5117 #ifndef ARM_MMU_EXTENDED
5118 if (pm->pm_remove_all) {
5119 /*
5120 * Finish up the pmap_remove_all() optimisation by flushing
5121 * the TLB.
5122 */
5123 pmap_tlb_flushID(pm);
5124 pm->pm_remove_all = false;
5125 }
5126
5127 if (pmap_is_current(pm)) {
5128 /*
5129 * If we're dealing with a current userland pmap, move its L1
5130 * to the end of the LRU.
5131 */
5132 if (pm != pmap_kernel())
5133 pmap_use_l1(pm);
5134
5135 /*
5136 * We can assume we're done with frobbing the cache/tlb for
5137 * now. Make sure any future pmap ops don't skip cache/tlb
5138 * flushes.
5139 */
5140 pm->pm_cstate.cs_all = PMAP_CACHE_STATE_ALL;
5141 }
5142 #else
5143
5144 kpreempt_disable();
5145 #if defined(MULTIPROCESSOR) && PMAP_TLB_MAX > 1
5146 u_int pending = atomic_swap_uint(&pmap->pm_shootdown_pending, 0);
5147 if (pending && pmap_tlb_shootdown_bystanders(pmap)) {
5148 PMAP_COUNT(shootdown_ipis);
5149 }
5150 #endif
5151
5152 /*
5153 * If pmap_remove_all was called, we deactivated ourselves and released
5154 * our ASID. Now we have to reactivate ourselves.
5155 */
5156 if (__predict_false(pm->pm_remove_all)) {
5157 pm->pm_remove_all = false;
5158
5159 KASSERT(pm != pmap_kernel());
5160 pmap_md_pdetab_activate(pm, curlwp);
5161 }
5162
5163 if (arm_has_mpext_p)
5164 armreg_bpiallis_write(0);
5165 else
5166 armreg_bpiall_write(0);
5167
5168 kpreempt_enable();
5169
5170 KASSERTMSG(pm == pmap_kernel()
5171 || curcpu()->ci_pmap_cur != pm
5172 || pm->pm_pai[0].pai_asid == curcpu()->ci_pmap_asid_cur,
5173 "pmap/asid %p/%#x != %s cur pmap/asid %p/%#x", pm,
5174 pm->pm_pai[0].pai_asid, curcpu()->ci_data.cpu_name,
5175 curcpu()->ci_pmap_cur, curcpu()->ci_pmap_asid_cur);
5176 #endif
5177
5178 PMAPCOUNT(updates);
5179
5180 /*
5181 * make sure TLB/cache operations have completed.
5182 */
5183 cpu_cpwait();
5184 UVMHIST_LOG(maphist, " <-- done", 0, 0, 0, 0);
5185 }
5186
5187 bool
5188 pmap_remove_all(pmap_t pm)
5189 {
5190
5191 /*
5192 * The vmspace described by this pmap is about to be torn down.
5193 * Until pmap_update() is called, UVM will only make calls
5194 * to pmap_remove(). We can make life much simpler by flushing
5195 * the cache now, and deferring TLB invalidation to pmap_update().
5196 */
5197 #ifdef PMAP_CACHE_VIVT
5198 pmap_cache_wbinv_all(pm, PVF_EXEC);
5199 #endif
5200 #ifdef ARM_MMU_EXTENDED
5201 #ifdef MULTIPROCESSOR
5202 struct cpu_info * const ci = curcpu();
5203 // This should be the last CPU with this pmap onproc
5204 KASSERT(!kcpuset_isotherset(pm->pm_onproc, cpu_index(ci)));
5205 if (kcpuset_isset(pm->pm_onproc, cpu_index(ci)))
5206 #endif
5207 pmap_tlb_asid_deactivate(pm);
5208 #ifdef MULTIPROCESSOR
5209 KASSERT(kcpuset_iszero(pm->pm_onproc));
5210 #endif
5211
5212 pmap_tlb_asid_release_all(pm);
5213 #endif
5214 pm->pm_remove_all = true;
5215 return false;
5216 }
5217
5218 /*
5219 * Retire the given physical map from service.
5220 * Should only be called if the map contains no valid mappings.
5221 */
5222 void
5223 pmap_destroy(pmap_t pm)
5224 {
5225 UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
5226
5227 if (pm == NULL)
5228 return;
5229
5230 UVMHIST_LOG(maphist, "pm=%#jx remove_all %jd", (uintptr_t)pm,
5231 pm->pm_remove_all, 0, 0);
5232
5233 if (pm->pm_remove_all) {
5234 #ifdef ARM_MMU_EXTENDED
5235 pmap_tlb_asid_release_all(pm);
5236 #else
5237 pmap_tlb_flushID(pm);
5238 #endif
5239 pm->pm_remove_all = false;
5240 }
5241
5242 /*
5243 * Drop reference count
5244 */
5245 if (atomic_dec_uint_nv(&pm->pm_refs) > 0) {
5246 #ifndef ARM_MMU_EXTENDED
5247 if (pmap_is_current(pm)) {
5248 if (pm != pmap_kernel())
5249 pmap_use_l1(pm);
5250 pm->pm_cstate.cs_all = PMAP_CACHE_STATE_ALL;
5251 }
5252 #endif
5253 return;
5254 }
5255
5256 /*
5257 * reference count is zero, free pmap resources and then free pmap.
5258 */
5259
5260 #ifndef ARM_HAS_VBAR
5261 if (vector_page < KERNEL_BASE) {
5262 KDASSERT(!pmap_is_current(pm));
5263
5264 /* Remove the vector page mapping */
5265 pmap_remove(pm, vector_page, vector_page + PAGE_SIZE);
5266 pmap_update(pm);
5267 }
5268 #endif
5269
5270 pmap_free_l1(pm);
5271
5272 #ifdef ARM_MMU_EXTENDED
5273 #ifdef MULTIPROCESSOR
5274 kcpuset_destroy(pm->pm_active);
5275 kcpuset_destroy(pm->pm_onproc);
5276 #endif
5277 #else
5278 struct cpu_info * const ci = curcpu();
5279 if (ci->ci_pmap_lastuser == pm)
5280 ci->ci_pmap_lastuser = NULL;
5281 #endif
5282
5283 mutex_destroy(&pm->pm_lock);
5284 pool_cache_put(&pmap_cache, pm);
5285 UVMHIST_LOG(maphist, " <-- done", 0, 0, 0, 0);
5286 }
5287
5288
5289 /*
5290 * void pmap_reference(pmap_t pm)
5291 *
5292 * Add a reference to the specified pmap.
5293 */
5294 void
5295 pmap_reference(pmap_t pm)
5296 {
5297
5298 if (pm == NULL)
5299 return;
5300
5301 #ifndef ARM_MMU_EXTENDED
5302 pmap_use_l1(pm);
5303 #endif
5304
5305 atomic_inc_uint(&pm->pm_refs);
5306 }
5307
5308 #if (ARM_MMU_V6 + ARM_MMU_V7) > 0
5309
5310 static struct evcnt pmap_prefer_nochange_ev =
5311 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap prefer", "nochange");
5312 static struct evcnt pmap_prefer_change_ev =
5313 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap prefer", "change");
5314
5315 EVCNT_ATTACH_STATIC(pmap_prefer_change_ev);
5316 EVCNT_ATTACH_STATIC(pmap_prefer_nochange_ev);
5317
5318 void
5319 pmap_prefer(vaddr_t hint, vaddr_t *vap, int td)
5320 {
5321 vsize_t mask = arm_cache_prefer_mask | (PAGE_SIZE - 1);
5322 vaddr_t va = *vap;
5323 vaddr_t diff = (hint - va) & mask;
5324 if (diff == 0) {
5325 pmap_prefer_nochange_ev.ev_count++;
5326 } else {
5327 pmap_prefer_change_ev.ev_count++;
5328 if (__predict_false(td))
5329 va -= mask + 1;
5330 *vap = va + diff;
5331 }
5332 }
5333 #endif /* ARM_MMU_V6 | ARM_MMU_V7 */
5334
5335 /*
5336 * pmap_zero_page()
5337 *
5338 * Zero a given physical page by mapping it at a page hook point.
5339 * In doing the zero page op, the page we zero is mapped cachable, as with
5340 * StrongARM accesses to non-cached pages are non-burst making writing
5341 * _any_ bulk data very slow.
5342 */
5343 #if (ARM_MMU_GENERIC + ARM_MMU_SA1 + ARM_MMU_V6 + ARM_MMU_V7) != 0
5344 void
5345 pmap_zero_page_generic(paddr_t pa)
5346 {
5347 #if defined(PMAP_CACHE_VIPT) || defined(DEBUG)
5348 struct vm_page *pg = PHYS_TO_VM_PAGE(pa);
5349 struct vm_page_md *md = VM_PAGE_TO_MD(pg);
5350 #endif
5351 #if defined(PMAP_CACHE_VIPT)
5352 /* Choose the last page color it had, if any */
5353 const vsize_t va_offset = md->pvh_attrs & arm_cache_prefer_mask;
5354 #else
5355 const vsize_t va_offset = 0;
5356 #endif
5357 #if defined(__HAVE_MM_MD_DIRECT_MAPPED_PHYS)
5358 /*
5359 * Is this page mapped at its natural color?
5360 * If we have all of memory mapped, then just convert PA to VA.
5361 */
5362 bool okcolor = arm_pcache.dcache_type == CACHE_TYPE_PIPT
5363 || va_offset == (pa & arm_cache_prefer_mask);
5364 const vaddr_t vdstp = okcolor
5365 ? pmap_direct_mapped_phys(pa, &okcolor, cpu_cdstp(va_offset))
5366 : cpu_cdstp(va_offset);
5367 #else
5368 const bool okcolor = false;
5369 const vaddr_t vdstp = cpu_cdstp(va_offset);
5370 #endif
5371 pt_entry_t * const ptep = cpu_cdst_pte(va_offset);
5372
5373
5374 #ifdef DEBUG
5375 if (!SLIST_EMPTY(&md->pvh_list))
5376 panic("pmap_zero_page: page has mappings");
5377 #endif
5378
5379 KDASSERT((pa & PGOFSET) == 0);
5380
5381 if (!okcolor) {
5382 /*
5383 * Hook in the page, zero it, and purge the cache for that
5384 * zeroed page. Invalidate the TLB as needed.
5385 */
5386 const pt_entry_t npte = L2_S_PROTO | pa | pte_l2_s_cache_mode
5387 | L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE);
5388 l2pte_set(ptep, npte, 0);
5389 PTE_SYNC(ptep);
5390 cpu_tlb_flushD_SE(vdstp);
5391 cpu_cpwait();
5392 #if defined(__HAVE_MM_MD_DIRECT_MAPPED_PHYS) && defined(PMAP_CACHE_VIPT) \
5393 && !defined(ARM_MMU_EXTENDED)
5394 /*
5395 * If we are direct-mapped and our color isn't ok, then before
5396 * we bzero the page invalidate its contents from the cache and
5397 * reset the color to its natural color.
5398 */
5399 cpu_dcache_inv_range(vdstp, PAGE_SIZE);
5400 md->pvh_attrs &= ~arm_cache_prefer_mask;
5401 md->pvh_attrs |= (pa & arm_cache_prefer_mask);
5402 #endif
5403 }
5404 bzero_page(vdstp);
5405 if (!okcolor) {
5406 /*
5407 * Unmap the page.
5408 */
5409 l2pte_reset(ptep);
5410 PTE_SYNC(ptep);
5411 cpu_tlb_flushD_SE(vdstp);
5412 #ifdef PMAP_CACHE_VIVT
5413 cpu_dcache_wbinv_range(vdstp, PAGE_SIZE);
5414 #endif
5415 }
5416 #ifdef PMAP_CACHE_VIPT
5417 /*
5418 * This page is now cache resident so it now has a page color.
5419 * Any contents have been obliterated so clear the EXEC flag.
5420 */
5421 #ifndef ARM_MMU_EXTENDED
5422 if (!pmap_is_page_colored_p(md)) {
5423 PMAPCOUNT(vac_color_new);
5424 md->pvh_attrs |= PVF_COLORED;
5425 }
5426 md->pvh_attrs |= PVF_DIRTY;
5427 #endif
5428 if (PV_IS_EXEC_P(md->pvh_attrs)) {
5429 md->pvh_attrs &= ~PVF_EXEC;
5430 PMAPCOUNT(exec_discarded_zero);
5431 }
5432 #endif
5433 }
5434 #endif /* (ARM_MMU_GENERIC + ARM_MMU_SA1 + ARM_MMU_V6) != 0 */
5435
5436 #if ARM_MMU_XSCALE == 1
5437 void
5438 pmap_zero_page_xscale(paddr_t pa)
5439 {
5440 #ifdef DEBUG
5441 struct vm_page *pg = PHYS_TO_VM_PAGE(pa);
5442 struct vm_page_md *md = VM_PAGE_TO_MD(pg);
5443
5444 if (!SLIST_EMPTY(&md->pvh_list))
5445 panic("pmap_zero_page: page has mappings");
5446 #endif
5447
5448 KDASSERT((pa & PGOFSET) == 0);
5449
5450 /*
5451 * Hook in the page, zero it, and purge the cache for that
5452 * zeroed page. Invalidate the TLB as needed.
5453 */
5454
5455 pt_entry_t npte = L2_S_PROTO | pa |
5456 L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE) |
5457 L2_C | L2_XS_T_TEX(TEX_XSCALE_X); /* mini-data */
5458 l2pte_set(cdst_pte, npte, 0);
5459 PTE_SYNC(cdst_pte);
5460 cpu_tlb_flushD_SE(cdstp);
5461 cpu_cpwait();
5462 bzero_page(cdstp);
5463 xscale_cache_clean_minidata();
5464 l2pte_reset(cdst_pte);
5465 PTE_SYNC(cdst_pte);
5466 }
5467 #endif /* ARM_MMU_XSCALE == 1 */
5468
5469 /* pmap_pageidlezero()
5470 *
5471 * The same as above, except that we assume that the page is not
5472 * mapped. This means we never have to flush the cache first. Called
5473 * from the idle loop.
5474 */
5475 bool
5476 pmap_pageidlezero(paddr_t pa)
5477 {
5478 bool rv = true;
5479 #if defined(PMAP_CACHE_VIPT) || defined(DEBUG)
5480 struct vm_page * const pg = PHYS_TO_VM_PAGE(pa);
5481 struct vm_page_md *md = VM_PAGE_TO_MD(pg);
5482 #endif
5483 #ifdef PMAP_CACHE_VIPT
5484 /* Choose the last page color it had, if any */
5485 const vsize_t va_offset = md->pvh_attrs & arm_cache_prefer_mask;
5486 #else
5487 const vsize_t va_offset = 0;
5488 #endif
5489 #ifdef __HAVE_MM_MD_DIRECT_MAPPED_PHYS
5490 bool okcolor = arm_pcache.dcache_type == CACHE_TYPE_PIPT
5491 || va_offset == (pa & arm_cache_prefer_mask);
5492 const vaddr_t vdstp = okcolor
5493 ? pmap_direct_mapped_phys(pa, &okcolor, cpu_cdstp(va_offset))
5494 : cpu_cdstp(va_offset);
5495 #else
5496 const bool okcolor = false;
5497 const vaddr_t vdstp = cpu_cdstp(va_offset);
5498 #endif
5499 pt_entry_t * const ptep = cpu_cdst_pte(va_offset);
5500
5501
5502 #ifdef DEBUG
5503 if (!SLIST_EMPTY(&md->pvh_list))
5504 panic("pmap_pageidlezero: page has mappings");
5505 #endif
5506
5507 KDASSERT((pa & PGOFSET) == 0);
5508
5509 if (!okcolor) {
5510 /*
5511 * Hook in the page, zero it, and purge the cache for that
5512 * zeroed page. Invalidate the TLB as needed.
5513 */
5514 const pt_entry_t npte = L2_S_PROTO | pa |
5515 L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE) | pte_l2_s_cache_mode;
5516 l2pte_set(ptep, npte, 0);
5517 PTE_SYNC(ptep);
5518 cpu_tlb_flushD_SE(vdstp);
5519 cpu_cpwait();
5520 }
5521
5522 uint64_t *ptr = (uint64_t *)vdstp;
5523 for (size_t i = 0; i < PAGE_SIZE / sizeof(*ptr); i++) {
5524 if (sched_curcpu_runnable_p() != 0) {
5525 /*
5526 * A process has become ready. Abort now,
5527 * so we don't keep it waiting while we
5528 * do slow memory access to finish this
5529 * page.
5530 */
5531 rv = false;
5532 break;
5533 }
5534 *ptr++ = 0;
5535 }
5536
5537 #ifdef PMAP_CACHE_VIVT
5538 if (rv)
5539 /*
5540 * if we aborted we'll rezero this page again later so don't
5541 * purge it unless we finished it
5542 */
5543 cpu_dcache_wbinv_range(vdstp, PAGE_SIZE);
5544 #elif defined(PMAP_CACHE_VIPT)
5545 /*
5546 * This page is now cache resident so it now has a page color.
5547 * Any contents have been obliterated so clear the EXEC flag.
5548 */
5549 #ifndef ARM_MMU_EXTENDED
5550 if (!pmap_is_page_colored_p(md)) {
5551 PMAPCOUNT(vac_color_new);
5552 md->pvh_attrs |= PVF_COLORED;
5553 }
5554 #endif
5555 if (PV_IS_EXEC_P(md->pvh_attrs)) {
5556 md->pvh_attrs &= ~PVF_EXEC;
5557 PMAPCOUNT(exec_discarded_zero);
5558 }
5559 #endif
5560 /*
5561 * Unmap the page.
5562 */
5563 if (!okcolor) {
5564 l2pte_reset(ptep);
5565 PTE_SYNC(ptep);
5566 cpu_tlb_flushD_SE(vdstp);
5567 }
5568
5569 return rv;
5570 }
5571
5572 /*
5573 * pmap_copy_page()
5574 *
5575 * Copy one physical page into another, by mapping the pages into
5576 * hook points. The same comment regarding cachability as in
5577 * pmap_zero_page also applies here.
5578 */
5579 #if (ARM_MMU_GENERIC + ARM_MMU_SA1 + ARM_MMU_V6 + ARM_MMU_V7) != 0
5580 void
5581 pmap_copy_page_generic(paddr_t src, paddr_t dst)
5582 {
5583 struct vm_page * const src_pg = PHYS_TO_VM_PAGE(src);
5584 struct vm_page_md *src_md = VM_PAGE_TO_MD(src_pg);
5585 #if defined(PMAP_CACHE_VIPT) || defined(DEBUG)
5586 struct vm_page * const dst_pg = PHYS_TO_VM_PAGE(dst);
5587 struct vm_page_md *dst_md = VM_PAGE_TO_MD(dst_pg);
5588 #endif
5589 #ifdef PMAP_CACHE_VIPT
5590 const vsize_t src_va_offset = src_md->pvh_attrs & arm_cache_prefer_mask;
5591 const vsize_t dst_va_offset = dst_md->pvh_attrs & arm_cache_prefer_mask;
5592 #else
5593 const vsize_t src_va_offset = 0;
5594 const vsize_t dst_va_offset = 0;
5595 #endif
5596 #if defined(__HAVE_MM_MD_DIRECT_MAPPED_PHYS)
5597 /*
5598 * Is this page mapped at its natural color?
5599 * If we have all of memory mapped, then just convert PA to VA.
5600 */
5601 bool src_okcolor = arm_pcache.dcache_type == CACHE_TYPE_PIPT
5602 || src_va_offset == (src & arm_cache_prefer_mask);
5603 bool dst_okcolor = arm_pcache.dcache_type == CACHE_TYPE_PIPT
5604 || dst_va_offset == (dst & arm_cache_prefer_mask);
5605 const vaddr_t vsrcp = src_okcolor
5606 ? pmap_direct_mapped_phys(src, &src_okcolor,
5607 cpu_csrcp(src_va_offset))
5608 : cpu_csrcp(src_va_offset);
5609 const vaddr_t vdstp = pmap_direct_mapped_phys(dst, &dst_okcolor,
5610 cpu_cdstp(dst_va_offset));
5611 #else
5612 const bool src_okcolor = false;
5613 const bool dst_okcolor = false;
5614 const vaddr_t vsrcp = cpu_csrcp(src_va_offset);
5615 const vaddr_t vdstp = cpu_cdstp(dst_va_offset);
5616 #endif
5617 pt_entry_t * const src_ptep = cpu_csrc_pte(src_va_offset);
5618 pt_entry_t * const dst_ptep = cpu_cdst_pte(dst_va_offset);
5619
5620 #ifdef DEBUG
5621 if (!SLIST_EMPTY(&dst_md->pvh_list))
5622 panic("pmap_copy_page: dst page has mappings");
5623 #endif
5624
5625 #if defined(PMAP_CACHE_VIPT) && !defined(ARM_MMU_EXTENDED)
5626 KASSERT(arm_cache_prefer_mask == 0 || src_md->pvh_attrs & (PVF_COLORED|PVF_NC));
5627 #endif
5628 KDASSERT((src & PGOFSET) == 0);
5629 KDASSERT((dst & PGOFSET) == 0);
5630
5631 /*
5632 * Clean the source page. Hold the source page's lock for
5633 * the duration of the copy so that no other mappings can
5634 * be created while we have a potentially aliased mapping.
5635 */
5636 #ifdef PMAP_CACHE_VIVT
5637 pmap_acquire_page_lock(src_md);
5638 (void) pmap_clean_page(src_md, true);
5639 pmap_release_page_lock(src_md);
5640 #endif
5641
5642 /*
5643 * Map the pages into the page hook points, copy them, and purge
5644 * the cache for the appropriate page. Invalidate the TLB
5645 * as required.
5646 */
5647 if (!src_okcolor) {
5648 const pt_entry_t nsrc_pte = L2_S_PROTO
5649 | src
5650 #if defined(PMAP_CACHE_VIPT) && !defined(ARM_MMU_EXTENDED)
5651 | ((src_md->pvh_attrs & PVF_NC) ? 0 : pte_l2_s_cache_mode)
5652 #else // defined(PMAP_CACHE_VIVT) || defined(ARM_MMU_EXTENDED)
5653 | pte_l2_s_cache_mode
5654 #endif
5655 | L2_S_PROT(PTE_KERNEL, VM_PROT_READ);
5656 l2pte_set(src_ptep, nsrc_pte, 0);
5657 PTE_SYNC(src_ptep);
5658 cpu_tlb_flushD_SE(vsrcp);
5659 cpu_cpwait();
5660 }
5661 if (!dst_okcolor) {
5662 const pt_entry_t ndst_pte = L2_S_PROTO | dst |
5663 L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE) | pte_l2_s_cache_mode;
5664 l2pte_set(dst_ptep, ndst_pte, 0);
5665 PTE_SYNC(dst_ptep);
5666 cpu_tlb_flushD_SE(vdstp);
5667 cpu_cpwait();
5668 #if defined(__HAVE_MM_MD_DIRECT_MAPPED_PHYS) && defined(PMAP_CACHE_VIPT)
5669 /*
5670 * If we are direct-mapped and our color isn't ok, then before
5671 * we bcopy to the new page invalidate its contents from the
5672 * cache and reset its color to its natural color.
5673 */
5674 cpu_dcache_inv_range(vdstp, PAGE_SIZE);
5675 dst_md->pvh_attrs &= ~arm_cache_prefer_mask;
5676 dst_md->pvh_attrs |= (dst & arm_cache_prefer_mask);
5677 #endif
5678 }
5679 bcopy_page(vsrcp, vdstp);
5680 #ifdef PMAP_CACHE_VIVT
5681 cpu_dcache_inv_range(vsrcp, PAGE_SIZE);
5682 cpu_dcache_wbinv_range(vdstp, PAGE_SIZE);
5683 #endif
5684 /*
5685 * Unmap the pages.
5686 */
5687 if (!src_okcolor) {
5688 l2pte_reset(src_ptep);
5689 PTE_SYNC(src_ptep);
5690 cpu_tlb_flushD_SE(vsrcp);
5691 cpu_cpwait();
5692 }
5693 if (!dst_okcolor) {
5694 l2pte_reset(dst_ptep);
5695 PTE_SYNC(dst_ptep);
5696 cpu_tlb_flushD_SE(vdstp);
5697 cpu_cpwait();
5698 }
5699 #ifdef PMAP_CACHE_VIPT
5700 /*
5701 * Now that the destination page is in the cache, mark it as colored.
5702 * If this was an exec page, discard it.
5703 */
5704 pmap_acquire_page_lock(dst_md);
5705 #ifndef ARM_MMU_EXTENDED
5706 if (arm_pcache.cache_type == CACHE_TYPE_PIPT) {
5707 dst_md->pvh_attrs &= ~arm_cache_prefer_mask;
5708 dst_md->pvh_attrs |= (dst & arm_cache_prefer_mask);
5709 }
5710 if (!pmap_is_page_colored_p(dst_md)) {
5711 PMAPCOUNT(vac_color_new);
5712 dst_md->pvh_attrs |= PVF_COLORED;
5713 }
5714 dst_md->pvh_attrs |= PVF_DIRTY;
5715 #endif
5716 if (PV_IS_EXEC_P(dst_md->pvh_attrs)) {
5717 dst_md->pvh_attrs &= ~PVF_EXEC;
5718 PMAPCOUNT(exec_discarded_copy);
5719 }
5720 pmap_release_page_lock(dst_md);
5721 #endif
5722 }
5723 #endif /* (ARM_MMU_GENERIC + ARM_MMU_SA1 + ARM_MMU_V6) != 0 */
5724
5725 #if ARM_MMU_XSCALE == 1
5726 void
5727 pmap_copy_page_xscale(paddr_t src, paddr_t dst)
5728 {
5729 struct vm_page *src_pg = PHYS_TO_VM_PAGE(src);
5730 struct vm_page_md *src_md = VM_PAGE_TO_MD(src_pg);
5731 #ifdef DEBUG
5732 struct vm_page_md *dst_md = VM_PAGE_TO_MD(PHYS_TO_VM_PAGE(dst));
5733
5734 if (!SLIST_EMPTY(&dst_md->pvh_list))
5735 panic("pmap_copy_page: dst page has mappings");
5736 #endif
5737
5738 KDASSERT((src & PGOFSET) == 0);
5739 KDASSERT((dst & PGOFSET) == 0);
5740
5741 /*
5742 * Clean the source page. Hold the source page's lock for
5743 * the duration of the copy so that no other mappings can
5744 * be created while we have a potentially aliased mapping.
5745 */
5746 #ifdef PMAP_CACHE_VIVT
5747 pmap_acquire_page_lock(src_md);
5748 (void) pmap_clean_page(src_md, true);
5749 pmap_release_page_lock(src_md);
5750 #endif
5751
5752 /*
5753 * Map the pages into the page hook points, copy them, and purge
5754 * the cache for the appropriate page. Invalidate the TLB
5755 * as required.
5756 */
5757 const pt_entry_t nsrc_pte = L2_S_PROTO | src
5758 | L2_S_PROT(PTE_KERNEL, VM_PROT_READ)
5759 | L2_C | L2_XS_T_TEX(TEX_XSCALE_X); /* mini-data */
5760 l2pte_set(csrc_pte, nsrc_pte, 0);
5761 PTE_SYNC(csrc_pte);
5762
5763 const pt_entry_t ndst_pte = L2_S_PROTO | dst
5764 | L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE)
5765 | L2_C | L2_XS_T_TEX(TEX_XSCALE_X); /* mini-data */
5766 l2pte_set(cdst_pte, ndst_pte, 0);
5767 PTE_SYNC(cdst_pte);
5768
5769 cpu_tlb_flushD_SE(csrcp);
5770 cpu_tlb_flushD_SE(cdstp);
5771 cpu_cpwait();
5772 bcopy_page(csrcp, cdstp);
5773 xscale_cache_clean_minidata();
5774 l2pte_reset(csrc_pte);
5775 l2pte_reset(cdst_pte);
5776 PTE_SYNC(csrc_pte);
5777 PTE_SYNC(cdst_pte);
5778 }
5779 #endif /* ARM_MMU_XSCALE == 1 */
5780
5781 /*
5782 * void pmap_virtual_space(vaddr_t *start, vaddr_t *end)
5783 *
5784 * Return the start and end addresses of the kernel's virtual space.
5785 * These values are setup in pmap_bootstrap and are updated as pages
5786 * are allocated.
5787 */
5788 void
5789 pmap_virtual_space(vaddr_t *start, vaddr_t *end)
5790 {
5791 *start = virtual_avail;
5792 *end = virtual_end;
5793 }
5794
5795 /*
5796 * Helper function for pmap_grow_l2_bucket()
5797 */
5798 static inline int
5799 pmap_grow_map(vaddr_t va, paddr_t *pap)
5800 {
5801 paddr_t pa;
5802
5803 KASSERT((va & PGOFSET) == 0);
5804
5805 if (uvm.page_init_done == false) {
5806 #ifdef PMAP_STEAL_MEMORY
5807 pv_addr_t pv;
5808 pmap_boot_pagealloc(PAGE_SIZE,
5809 #ifdef PMAP_CACHE_VIPT
5810 arm_cache_prefer_mask,
5811 va & arm_cache_prefer_mask,
5812 #else
5813 0, 0,
5814 #endif
5815 &pv);
5816 pa = pv.pv_pa;
5817 #else
5818 if (uvm_page_physget(&pa) == false)
5819 return 1;
5820 #endif /* PMAP_STEAL_MEMORY */
5821 } else {
5822 struct vm_page *pg;
5823 pg = uvm_pagealloc(NULL, 0, NULL, UVM_PGA_USERESERVE);
5824 if (pg == NULL)
5825 return 1;
5826 pa = VM_PAGE_TO_PHYS(pg);
5827 /*
5828 * This new page must not have any mappings.
5829 */
5830 struct vm_page_md *md __diagused = VM_PAGE_TO_MD(pg);
5831 KASSERT(SLIST_EMPTY(&md->pvh_list));
5832 }
5833
5834 /*
5835 * Enter it via pmap_kenter_pa and let that routine do the hard work.
5836 */
5837 pmap_kenter_pa(va, pa, VM_PROT_READ | VM_PROT_WRITE,
5838 PMAP_KMPAGE | PMAP_PTE);
5839
5840 if (pap)
5841 *pap = pa;
5842
5843 PMAPCOUNT(pt_mappings);
5844
5845 const pmap_t kpm __diagused = pmap_kernel();
5846 struct l2_bucket * const l2b __diagused = pmap_get_l2_bucket(kpm, va);
5847 KASSERT(l2b != NULL);
5848
5849 pt_entry_t * const ptep __diagused = &l2b->l2b_kva[l2pte_index(va)];
5850 const pt_entry_t pte __diagused = *ptep;
5851 KASSERT(l2pte_valid_p(pte));
5852 KASSERT((pte & L2_S_CACHE_MASK) == pte_l2_s_cache_mode_pt);
5853
5854 memset((void *)va, 0, PAGE_SIZE);
5855
5856 return 0;
5857 }
5858
5859 /*
5860 * This is the same as pmap_alloc_l2_bucket(), except that it is only
5861 * used by pmap_growkernel().
5862 */
5863 static inline struct l2_bucket *
5864 pmap_grow_l2_bucket(pmap_t pm, vaddr_t va)
5865 {
5866 const size_t l1slot = l1pte_index(va);
5867 struct l2_dtable *l2;
5868 vaddr_t nva;
5869
5870 CTASSERT((PAGE_SIZE % L2_TABLE_SIZE_REAL) == 0);
5871 if ((l2 = pm->pm_l2[L2_IDX(l1slot)]) == NULL) {
5872 /*
5873 * No mapping at this address, as there is
5874 * no entry in the L1 table.
5875 * Need to allocate a new l2_dtable.
5876 */
5877 nva = pmap_kernel_l2dtable_kva;
5878 if ((nva & PGOFSET) == 0) {
5879 /*
5880 * Need to allocate a backing page
5881 */
5882 if (pmap_grow_map(nva, NULL))
5883 return NULL;
5884 }
5885
5886 l2 = (struct l2_dtable *)nva;
5887 nva += sizeof(struct l2_dtable);
5888
5889 if ((nva & PGOFSET) < (pmap_kernel_l2dtable_kva & PGOFSET)) {
5890 /*
5891 * The new l2_dtable straddles a page boundary.
5892 * Map in another page to cover it.
5893 */
5894 if (pmap_grow_map(nva & ~PGOFSET, NULL))
5895 return NULL;
5896 }
5897
5898 pmap_kernel_l2dtable_kva = nva;
5899
5900 /*
5901 * Link it into the parent pmap
5902 */
5903 pm->pm_l2[L2_IDX(l1slot)] = l2;
5904 }
5905
5906 struct l2_bucket * const l2b = &l2->l2_bucket[L2_BUCKET(l1slot)];
5907
5908 /*
5909 * Fetch pointer to the L2 page table associated with the address.
5910 */
5911 if (l2b->l2b_kva == NULL) {
5912 pt_entry_t *ptep;
5913
5914 /*
5915 * No L2 page table has been allocated. Chances are, this
5916 * is because we just allocated the l2_dtable, above.
5917 */
5918 nva = pmap_kernel_l2ptp_kva;
5919 ptep = (pt_entry_t *)nva;
5920 if ((nva & PGOFSET) == 0) {
5921 /*
5922 * Need to allocate a backing page
5923 */
5924 if (pmap_grow_map(nva, &pmap_kernel_l2ptp_phys))
5925 return NULL;
5926 PTE_SYNC_RANGE(ptep, PAGE_SIZE / sizeof(pt_entry_t));
5927 }
5928
5929 l2->l2_occupancy++;
5930 l2b->l2b_kva = ptep;
5931 l2b->l2b_l1slot = l1slot;
5932 l2b->l2b_pa = pmap_kernel_l2ptp_phys;
5933
5934 pmap_kernel_l2ptp_kva += L2_TABLE_SIZE_REAL;
5935 pmap_kernel_l2ptp_phys += L2_TABLE_SIZE_REAL;
5936 }
5937
5938 return l2b;
5939 }
5940
5941 vaddr_t
5942 pmap_growkernel(vaddr_t maxkvaddr)
5943 {
5944 pmap_t kpm = pmap_kernel();
5945 #ifndef ARM_MMU_EXTENDED
5946 struct l1_ttable *l1;
5947 #endif
5948 int s;
5949
5950 if (maxkvaddr <= pmap_curmaxkvaddr)
5951 goto out; /* we are OK */
5952
5953 NPDEBUG(PDB_GROWKERN,
5954 printf("pmap_growkernel: growing kernel from 0x%lx to 0x%lx\n",
5955 pmap_curmaxkvaddr, maxkvaddr));
5956
5957 KDASSERT(maxkvaddr <= virtual_end);
5958
5959 /*
5960 * whoops! we need to add kernel PTPs
5961 */
5962
5963 s = splvm(); /* to be safe */
5964 mutex_enter(&kpm_lock);
5965
5966 /* Map 1MB at a time */
5967 size_t l1slot = l1pte_index(pmap_curmaxkvaddr);
5968 #ifdef ARM_MMU_EXTENDED
5969 pd_entry_t * const spdep = &kpm->pm_l1[l1slot];
5970 pd_entry_t *pdep = spdep;
5971 #endif
5972 for (;pmap_curmaxkvaddr < maxkvaddr; pmap_curmaxkvaddr += L1_S_SIZE,
5973 #ifdef ARM_MMU_EXTENDED
5974 pdep++,
5975 #endif
5976 l1slot++) {
5977 struct l2_bucket *l2b =
5978 pmap_grow_l2_bucket(kpm, pmap_curmaxkvaddr);
5979 KASSERT(l2b != NULL);
5980
5981 const pd_entry_t npde = L1_C_PROTO | l2b->l2b_pa
5982 | L1_C_DOM(PMAP_DOMAIN_KERNEL);
5983 #ifdef ARM_MMU_EXTENDED
5984 KASSERT(*pdep == 0);
5985 l1pte_setone(pdep, npde);
5986 #else
5987 /* Distribute new L1 entry to all other L1s */
5988 SLIST_FOREACH(l1, &l1_list, l1_link) {
5989 pd_entry_t * const pdep = &l1->l1_kva[l1slot];
5990 l1pte_setone(pdep, npde);
5991 PDE_SYNC(pdep);
5992 }
5993 #endif
5994 }
5995 #ifdef ARM_MMU_EXTENDED
5996 PDE_SYNC_RANGE(spdep, pdep - spdep);
5997 #endif
5998
5999 #ifdef PMAP_CACHE_VIVT
6000 /*
6001 * flush out the cache, expensive but growkernel will happen so
6002 * rarely
6003 */
6004 cpu_dcache_wbinv_all();
6005 cpu_tlb_flushD();
6006 cpu_cpwait();
6007 #endif
6008
6009 mutex_exit(&kpm_lock);
6010 splx(s);
6011
6012 out:
6013 return pmap_curmaxkvaddr;
6014 }
6015
6016 /************************ Utility routines ****************************/
6017
6018 #ifndef ARM_HAS_VBAR
6019 /*
6020 * vector_page_setprot:
6021 *
6022 * Manipulate the protection of the vector page.
6023 */
6024 void
6025 vector_page_setprot(int prot)
6026 {
6027 struct l2_bucket *l2b;
6028 pt_entry_t *ptep;
6029
6030 #if defined(CPU_ARMV7) || defined(CPU_ARM11)
6031 /*
6032 * If we are using VBAR to use the vectors in the kernel, then it's
6033 * already mapped in the kernel text so no need to anything here.
6034 */
6035 if (vector_page != ARM_VECTORS_LOW && vector_page != ARM_VECTORS_HIGH) {
6036 KASSERT((armreg_pfr1_read() & ARM_PFR1_SEC_MASK) != 0);
6037 return;
6038 }
6039 #endif
6040
6041 l2b = pmap_get_l2_bucket(pmap_kernel(), vector_page);
6042 KASSERT(l2b != NULL);
6043
6044 ptep = &l2b->l2b_kva[l2pte_index(vector_page)];
6045
6046 const pt_entry_t opte = *ptep;
6047 #ifdef ARM_MMU_EXTENDED
6048 const pt_entry_t npte = (opte & ~(L2_S_PROT_MASK|L2_XS_XN))
6049 | L2_S_PROT(PTE_KERNEL, prot);
6050 #else
6051 const pt_entry_t npte = (opte & ~L2_S_PROT_MASK)
6052 | L2_S_PROT(PTE_KERNEL, prot);
6053 #endif
6054 l2pte_set(ptep, npte, opte);
6055 PTE_SYNC(ptep);
6056 cpu_tlb_flushD_SE(vector_page);
6057 cpu_cpwait();
6058 }
6059 #endif
6060
6061 /*
6062 * Fetch pointers to the PDE/PTE for the given pmap/VA pair.
6063 * Returns true if the mapping exists, else false.
6064 *
6065 * NOTE: This function is only used by a couple of arm-specific modules.
6066 * It is not safe to take any pmap locks here, since we could be right
6067 * in the middle of debugging the pmap anyway...
6068 *
6069 * It is possible for this routine to return false even though a valid
6070 * mapping does exist. This is because we don't lock, so the metadata
6071 * state may be inconsistent.
6072 *
6073 * NOTE: We can return a NULL *ptp in the case where the L1 pde is
6074 * a "section" mapping.
6075 */
6076 bool
6077 pmap_get_pde_pte(pmap_t pm, vaddr_t va, pd_entry_t **pdp, pt_entry_t **ptp)
6078 {
6079 struct l2_dtable *l2;
6080 pd_entry_t *pdep, pde;
6081 pt_entry_t *ptep;
6082 u_short l1slot;
6083
6084 if (pm->pm_l1 == NULL)
6085 return false;
6086
6087 l1slot = l1pte_index(va);
6088 *pdp = pdep = pmap_l1_kva(pm) + l1slot;
6089 pde = *pdep;
6090
6091 if (l1pte_section_p(pde)) {
6092 *ptp = NULL;
6093 return true;
6094 }
6095
6096 l2 = pm->pm_l2[L2_IDX(l1slot)];
6097 if (l2 == NULL ||
6098 (ptep = l2->l2_bucket[L2_BUCKET(l1slot)].l2b_kva) == NULL) {
6099 return false;
6100 }
6101
6102 *ptp = &ptep[l2pte_index(va)];
6103 return true;
6104 }
6105
6106 bool
6107 pmap_get_pde(pmap_t pm, vaddr_t va, pd_entry_t **pdp)
6108 {
6109
6110 if (pm->pm_l1 == NULL)
6111 return false;
6112
6113 *pdp = pmap_l1_kva(pm) + l1pte_index(va);
6114
6115 return true;
6116 }
6117
6118 /************************ Bootstrapping routines ****************************/
6119
6120 #ifndef ARM_MMU_EXTENDED
6121 static void
6122 pmap_init_l1(struct l1_ttable *l1, pd_entry_t *l1pt)
6123 {
6124 int i;
6125
6126 l1->l1_kva = l1pt;
6127 l1->l1_domain_use_count = 0;
6128 l1->l1_domain_first = 0;
6129
6130 for (i = 0; i < PMAP_DOMAINS; i++)
6131 l1->l1_domain_free[i] = i + 1;
6132
6133 /*
6134 * Copy the kernel's L1 entries to each new L1.
6135 */
6136 if (pmap_initialized)
6137 memcpy(l1pt, pmap_l1_kva(pmap_kernel()), L1_TABLE_SIZE);
6138
6139 if (pmap_extract(pmap_kernel(), (vaddr_t)l1pt,
6140 &l1->l1_physaddr) == false)
6141 panic("pmap_init_l1: can't get PA of L1 at %p", l1pt);
6142
6143 SLIST_INSERT_HEAD(&l1_list, l1, l1_link);
6144 TAILQ_INSERT_TAIL(&l1_lru_list, l1, l1_lru);
6145 }
6146 #endif /* !ARM_MMU_EXTENDED */
6147
6148 /*
6149 * pmap_bootstrap() is called from the board-specific initarm() routine
6150 * once the kernel L1/L2 descriptors tables have been set up.
6151 *
6152 * This is a somewhat convoluted process since pmap bootstrap is, effectively,
6153 * spread over a number of disparate files/functions.
6154 *
6155 * We are passed the following parameters
6156 * - vstart
6157 * 1MB-aligned start of managed kernel virtual memory.
6158 * - vend
6159 * 1MB-aligned end of managed kernel virtual memory.
6160 *
6161 * We use 'kernel_l1pt' to build the metadata (struct l1_ttable and
6162 * struct l2_dtable) necessary to track kernel mappings.
6163 */
6164 #define PMAP_STATIC_L2_SIZE 16
6165 void
6166 pmap_bootstrap(vaddr_t vstart, vaddr_t vend)
6167 {
6168 static struct l2_dtable static_l2[PMAP_STATIC_L2_SIZE];
6169 #ifndef ARM_MMU_EXTENDED
6170 static struct l1_ttable static_l1;
6171 struct l1_ttable *l1 = &static_l1;
6172 #endif
6173 struct l2_dtable *l2;
6174 struct l2_bucket *l2b;
6175 pd_entry_t *l1pt = (pd_entry_t *) kernel_l1pt.pv_va;
6176 pmap_t pm = pmap_kernel();
6177 pt_entry_t *ptep;
6178 paddr_t pa;
6179 vsize_t size;
6180 int nptes, l2idx, l2next = 0;
6181
6182 #ifdef ARM_MMU_EXTENDED
6183 KASSERT(pte_l1_s_cache_mode == pte_l1_s_cache_mode_pt);
6184 KASSERT(pte_l2_s_cache_mode == pte_l2_s_cache_mode_pt);
6185 #endif
6186
6187 VPRINTF("kpm ");
6188 /*
6189 * Initialise the kernel pmap object
6190 */
6191 curcpu()->ci_pmap_cur = pm;
6192 #ifdef ARM_MMU_EXTENDED
6193 pm->pm_l1 = l1pt;
6194 pm->pm_l1_pa = kernel_l1pt.pv_pa;
6195 VPRINTF("tlb0 ");
6196 pmap_tlb_info_init(&pmap_tlb0_info);
6197 #ifdef MULTIPROCESSOR
6198 VPRINTF("kcpusets ");
6199 pm->pm_onproc = kcpuset_running;
6200 pm->pm_active = kcpuset_running;
6201 #endif
6202 #else
6203 pm->pm_l1 = l1;
6204 #endif
6205
6206 VPRINTF("locks ");
6207 /*
6208 * pmap_kenter_pa() and pmap_kremove() may be called from interrupt
6209 * context, so its locks have to be at IPL_VM
6210 */
6211 mutex_init(&pmap_lock, MUTEX_DEFAULT, IPL_VM);
6212 mutex_init(&kpm_lock, MUTEX_DEFAULT, IPL_NONE);
6213 mutex_init(&pm->pm_lock, MUTEX_DEFAULT, IPL_VM);
6214 pm->pm_refs = 1;
6215
6216 VPRINTF("l1pt ");
6217 /*
6218 * Scan the L1 translation table created by initarm() and create
6219 * the required metadata for all valid mappings found in it.
6220 */
6221 for (size_t l1slot = 0;
6222 l1slot < L1_TABLE_SIZE / sizeof(pd_entry_t);
6223 l1slot++) {
6224 pd_entry_t pde = l1pt[l1slot];
6225
6226 /*
6227 * We're only interested in Coarse mappings.
6228 * pmap_extract() can deal with section mappings without
6229 * recourse to checking L2 metadata.
6230 */
6231 if ((pde & L1_TYPE_MASK) != L1_TYPE_C)
6232 continue;
6233
6234 /*
6235 * Lookup the KVA of this L2 descriptor table
6236 */
6237 pa = l1pte_pa(pde);
6238 ptep = (pt_entry_t *)kernel_pt_lookup(pa);
6239 if (ptep == NULL) {
6240 panic("pmap_bootstrap: No L2 for va 0x%x, pa 0x%lx",
6241 (u_int)l1slot << L1_S_SHIFT, pa);
6242 }
6243
6244 /*
6245 * Fetch the associated L2 metadata structure.
6246 * Allocate a new one if necessary.
6247 */
6248 if ((l2 = pm->pm_l2[L2_IDX(l1slot)]) == NULL) {
6249 if (l2next == PMAP_STATIC_L2_SIZE)
6250 panic("pmap_bootstrap: out of static L2s");
6251 pm->pm_l2[L2_IDX(l1slot)] = l2 = &static_l2[l2next++];
6252 }
6253
6254 /*
6255 * One more L1 slot tracked...
6256 */
6257 l2->l2_occupancy++;
6258
6259 /*
6260 * Fill in the details of the L2 descriptor in the
6261 * appropriate bucket.
6262 */
6263 l2b = &l2->l2_bucket[L2_BUCKET(l1slot)];
6264 l2b->l2b_kva = ptep;
6265 l2b->l2b_pa = pa;
6266 l2b->l2b_l1slot = l1slot;
6267
6268 /*
6269 * Establish an initial occupancy count for this descriptor
6270 */
6271 for (l2idx = 0;
6272 l2idx < (L2_TABLE_SIZE_REAL / sizeof(pt_entry_t));
6273 l2idx++) {
6274 if ((ptep[l2idx] & L2_TYPE_MASK) != L2_TYPE_INV) {
6275 l2b->l2b_occupancy++;
6276 }
6277 }
6278
6279 /*
6280 * Make sure the descriptor itself has the correct cache mode.
6281 * If not, fix it, but whine about the problem. Port-meisters
6282 * should consider this a clue to fix up their initarm()
6283 * function. :)
6284 */
6285 if (pmap_set_pt_cache_mode(l1pt, (vaddr_t)ptep, 1)) {
6286 printf("pmap_bootstrap: WARNING! wrong cache mode for "
6287 "L2 pte @ %p\n", ptep);
6288 }
6289 }
6290
6291 VPRINTF("cache(l1pt) ");
6292 /*
6293 * Ensure the primary (kernel) L1 has the correct cache mode for
6294 * a page table. Bitch if it is not correctly set.
6295 */
6296 if (pmap_set_pt_cache_mode(l1pt, kernel_l1pt.pv_va,
6297 L1_TABLE_SIZE / L2_S_SIZE)) {
6298 printf("pmap_bootstrap: WARNING! wrong cache mode for "
6299 "primary L1 @ 0x%lx\n", kernel_l1pt.pv_va);
6300 }
6301
6302 #ifdef PMAP_CACHE_VIVT
6303 cpu_dcache_wbinv_all();
6304 cpu_tlb_flushID();
6305 cpu_cpwait();
6306 #endif
6307
6308 /*
6309 * now we allocate the "special" VAs which are used for tmp mappings
6310 * by the pmap (and other modules). we allocate the VAs by advancing
6311 * virtual_avail (note that there are no pages mapped at these VAs).
6312 *
6313 * Managed KVM space start from wherever initarm() tells us.
6314 */
6315 virtual_avail = vstart;
6316 virtual_end = vend;
6317
6318 VPRINTF("specials ");
6319 #ifdef PMAP_CACHE_VIPT
6320 /*
6321 * If we have a VIPT cache, we need one page/pte per possible alias
6322 * page so we won't violate cache aliasing rules.
6323 */
6324 virtual_avail = (virtual_avail + arm_cache_prefer_mask) & ~arm_cache_prefer_mask;
6325 nptes = (arm_cache_prefer_mask >> L2_S_SHIFT) + 1;
6326 nptes = roundup(nptes, PAGE_SIZE / L2_S_SIZE);
6327 if (arm_pcache.icache_type != CACHE_TYPE_PIPT
6328 && arm_pcache.icache_way_size > nptes * L2_S_SIZE) {
6329 nptes = arm_pcache.icache_way_size >> L2_S_SHIFT;
6330 nptes = roundup(nptes, PAGE_SIZE / L2_S_SIZE);
6331 }
6332 #else
6333 nptes = PAGE_SIZE / L2_S_SIZE;
6334 #endif
6335 #ifdef MULTIPROCESSOR
6336 cnptes = nptes;
6337 nptes *= arm_cpu_max;
6338 #endif
6339 pmap_alloc_specials(&virtual_avail, nptes, &csrcp, &csrc_pte);
6340 pmap_set_pt_cache_mode(l1pt, (vaddr_t)csrc_pte, nptes);
6341 pmap_alloc_specials(&virtual_avail, nptes, &cdstp, &cdst_pte);
6342 pmap_set_pt_cache_mode(l1pt, (vaddr_t)cdst_pte, nptes);
6343 pmap_alloc_specials(&virtual_avail, nptes, &memhook, NULL);
6344 if (msgbufaddr == NULL) {
6345 pmap_alloc_specials(&virtual_avail,
6346 round_page(MSGBUFSIZE) / PAGE_SIZE,
6347 (void *)&msgbufaddr, NULL);
6348 }
6349
6350 /*
6351 * Allocate a range of kernel virtual address space to be used
6352 * for L2 descriptor tables and metadata allocation in
6353 * pmap_growkernel().
6354 */
6355 size = ((virtual_end - pmap_curmaxkvaddr) + L1_S_OFFSET) / L1_S_SIZE;
6356 pmap_alloc_specials(&virtual_avail,
6357 round_page(size * L2_TABLE_SIZE_REAL) / PAGE_SIZE,
6358 &pmap_kernel_l2ptp_kva, NULL);
6359
6360 size = (size + (L2_BUCKET_SIZE - 1)) / L2_BUCKET_SIZE;
6361 pmap_alloc_specials(&virtual_avail,
6362 round_page(size * sizeof(struct l2_dtable)) / PAGE_SIZE,
6363 &pmap_kernel_l2dtable_kva, NULL);
6364
6365 #ifndef ARM_MMU_EXTENDED
6366 /*
6367 * init the static-global locks and global pmap list.
6368 */
6369 mutex_init(&l1_lru_lock, MUTEX_DEFAULT, IPL_VM);
6370
6371 /*
6372 * We can now initialise the first L1's metadata.
6373 */
6374 SLIST_INIT(&l1_list);
6375 TAILQ_INIT(&l1_lru_list);
6376 pmap_init_l1(l1, l1pt);
6377 #endif /* ARM_MMU_EXTENDED */
6378
6379 #ifndef ARM_HAS_VBAR
6380 /* Set up vector page L1 details, if necessary */
6381 if (vector_page < KERNEL_BASE) {
6382 pm->pm_pl1vec = pmap_l1_kva(pm) + l1pte_index(vector_page);
6383 l2b = pmap_get_l2_bucket(pm, vector_page);
6384 KDASSERT(l2b != NULL);
6385 pm->pm_l1vec = l2b->l2b_pa | L1_C_PROTO |
6386 L1_C_DOM(pmap_domain(pm));
6387 } else
6388 pm->pm_pl1vec = NULL;
6389 #endif
6390
6391 VPRINTF("pools ");
6392 /*
6393 * Initialize the pmap cache
6394 */
6395 pool_cache_bootstrap(&pmap_cache, sizeof(struct pmap), 0, 0, 0,
6396 "pmappl", NULL, IPL_NONE, pmap_pmap_ctor, NULL, NULL);
6397
6398 /*
6399 * Initialize the pv pool.
6400 */
6401 pool_init(&pmap_pv_pool, sizeof(struct pv_entry), 0, 0, 0, "pvepl",
6402 &pmap_bootstrap_pv_allocator, IPL_NONE);
6403
6404 /*
6405 * Initialize the L2 dtable pool and cache.
6406 */
6407 pool_cache_bootstrap(&pmap_l2dtable_cache, sizeof(struct l2_dtable), 0,
6408 0, 0, "l2dtblpl", NULL, IPL_NONE, pmap_l2dtable_ctor, NULL, NULL);
6409
6410 /*
6411 * Initialise the L2 descriptor table pool and cache
6412 */
6413 pool_cache_bootstrap(&pmap_l2ptp_cache, L2_TABLE_SIZE_REAL,
6414 L2_TABLE_SIZE_REAL, 0, 0, "l2ptppl", NULL, IPL_NONE,
6415 pmap_l2ptp_ctor, NULL, NULL);
6416
6417 mutex_init(&memlock, MUTEX_DEFAULT, IPL_NONE);
6418
6419 cpu_dcache_wbinv_all();
6420 }
6421
6422 static bool
6423 pmap_set_pt_cache_mode(pd_entry_t *kl1, vaddr_t va, size_t nptes)
6424 {
6425 #ifdef ARM_MMU_EXTENDED
6426 return false;
6427 #else
6428 if (pte_l1_s_cache_mode == pte_l1_s_cache_mode_pt
6429 && pte_l2_s_cache_mode == pte_l2_s_cache_mode_pt)
6430 return false;
6431
6432 const vaddr_t eva = va + nptes * PAGE_SIZE;
6433 int rv = 0;
6434
6435 while (va < eva) {
6436 /*
6437 * Make sure the descriptor itself has the correct cache mode
6438 */
6439 pd_entry_t * const pdep = &kl1[l1pte_index(va)];
6440 pd_entry_t pde = *pdep;
6441
6442 if (l1pte_section_p(pde)) {
6443 KASSERT((L1_S_CACHE_MASK & L1_S_V6_SUPER) == 0);
6444 if ((pde & L1_S_CACHE_MASK) != pte_l1_s_cache_mode_pt) {
6445 *pdep = (pde & ~L1_S_CACHE_MASK) |
6446 pte_l1_s_cache_mode_pt;
6447 PDE_SYNC(pdep);
6448 cpu_dcache_wbinv_range((vaddr_t)pdep,
6449 sizeof(*pdep));
6450 rv = 1;
6451 }
6452 return rv;
6453 }
6454 vaddr_t pa = l1pte_pa(pde);
6455 pt_entry_t *ptep = (pt_entry_t *)kernel_pt_lookup(pa);
6456 if (ptep == NULL)
6457 panic("pmap_bootstrap: No PTP for va %#lx\n", va);
6458
6459 ptep += l2pte_index(va);
6460 const pt_entry_t opte = *ptep;
6461 if ((opte & L2_S_CACHE_MASK) != pte_l2_s_cache_mode_pt) {
6462 const pt_entry_t npte = (opte & ~L2_S_CACHE_MASK)
6463 | pte_l2_s_cache_mode_pt;
6464 l2pte_set(ptep, npte, opte);
6465 PTE_SYNC(ptep);
6466 cpu_dcache_wbinv_range((vaddr_t)ptep, sizeof(*ptep));
6467 rv = 1;
6468 }
6469 va += PAGE_SIZE;
6470 }
6471
6472 return rv;
6473 #endif
6474 }
6475
6476 static void
6477 pmap_alloc_specials(vaddr_t *availp, int pages, vaddr_t *vap, pt_entry_t **ptep)
6478 {
6479 vaddr_t va = *availp;
6480 struct l2_bucket *l2b;
6481
6482 if (ptep) {
6483 l2b = pmap_get_l2_bucket(pmap_kernel(), va);
6484 if (l2b == NULL)
6485 panic("pmap_alloc_specials: no l2b for 0x%lx", va);
6486
6487 *ptep = &l2b->l2b_kva[l2pte_index(va)];
6488 }
6489
6490 *vap = va;
6491 *availp = va + (PAGE_SIZE * pages);
6492 }
6493
6494 void
6495 pmap_init(void)
6496 {
6497
6498 /*
6499 * Set the available memory vars - These do not map to real memory
6500 * addresses and cannot as the physical memory is fragmented.
6501 * They are used by ps for %mem calculations.
6502 * One could argue whether this should be the entire memory or just
6503 * the memory that is useable in a user process.
6504 */
6505 avail_start = ptoa(uvm_physseg_get_avail_start(uvm_physseg_get_first()));
6506 avail_end = ptoa(uvm_physseg_get_avail_end(uvm_physseg_get_last()));
6507
6508 /*
6509 * Now we need to free enough pv_entry structures to allow us to get
6510 * the kmem_map/kmem_object allocated and inited (done after this
6511 * function is finished). to do this we allocate one bootstrap page out
6512 * of kernel_map and use it to provide an initial pool of pv_entry
6513 * structures. we never free this page.
6514 */
6515 pool_setlowat(&pmap_pv_pool, (PAGE_SIZE / sizeof(struct pv_entry)) * 2);
6516
6517 #ifdef ARM_MMU_EXTENDED
6518 /*
6519 * Initialise the L1 pool and cache.
6520 */
6521
6522 pool_cache_bootstrap(&pmap_l1tt_cache, L1TT_SIZE, L1TT_SIZE,
6523 0, 0, "l1ttpl", &pmap_l1tt_allocator, IPL_NONE, pmap_l1tt_ctor,
6524 NULL, NULL);
6525
6526 int error __diagused = pmap_maxproc_set(maxproc);
6527 KASSERT(error == 0);
6528
6529 pmap_tlb_info_evcnt_attach(&pmap_tlb0_info);
6530 #endif
6531
6532 pmap_initialized = true;
6533 }
6534
6535 static vaddr_t last_bootstrap_page = 0;
6536 static void *free_bootstrap_pages = NULL;
6537
6538 static void *
6539 pmap_bootstrap_pv_page_alloc(struct pool *pp, int flags)
6540 {
6541 extern void *pool_page_alloc(struct pool *, int);
6542 vaddr_t new_page;
6543 void *rv;
6544
6545 if (pmap_initialized)
6546 return pool_page_alloc(pp, flags);
6547
6548 if (free_bootstrap_pages) {
6549 rv = free_bootstrap_pages;
6550 free_bootstrap_pages = *((void **)rv);
6551 return rv;
6552 }
6553
6554 KASSERT(kernel_map != NULL);
6555 new_page = uvm_km_alloc(kernel_map, PAGE_SIZE, 0,
6556 UVM_KMF_WIRED | ((flags & PR_WAITOK) ? 0 : UVM_KMF_NOWAIT));
6557
6558 KASSERT(new_page > last_bootstrap_page);
6559 last_bootstrap_page = new_page;
6560 return (void *)new_page;
6561 }
6562
6563 static void
6564 pmap_bootstrap_pv_page_free(struct pool *pp, void *v)
6565 {
6566 extern void pool_page_free(struct pool *, void *);
6567
6568 if ((vaddr_t)v <= last_bootstrap_page) {
6569 *((void **)v) = free_bootstrap_pages;
6570 free_bootstrap_pages = v;
6571 return;
6572 }
6573
6574 if (pmap_initialized) {
6575 pool_page_free(pp, v);
6576 return;
6577 }
6578 }
6579
6580
6581 #if defined(ARM_MMU_EXTENDED)
6582 static void *
6583 pmap_l1tt_alloc(struct pool *pp, int flags)
6584 {
6585 struct pglist plist;
6586 vaddr_t va;
6587
6588 const int waitok = flags & PR_WAITOK;
6589
6590 int error = uvm_pglistalloc(L1TT_SIZE, 0, -1, L1TT_SIZE, 0, &plist, 1,
6591 waitok);
6592 if (error)
6593 panic("Cannot allocate L1TT physical pages, %d", error);
6594
6595 struct vm_page *pg = TAILQ_FIRST(&plist);
6596 #if !defined( __HAVE_MM_MD_DIRECT_MAPPED_PHYS)
6597
6598 /* Allocate a L1 translation table VA */
6599 va = uvm_km_alloc(kernel_map, L1TT_SIZE, L1TT_SIZE, UVM_KMF_VAONLY);
6600 if (va == 0)
6601 panic("Cannot allocate L1TT KVA");
6602
6603 const vaddr_t eva = va + L1TT_SIZE;
6604 vaddr_t mva = va;
6605 while (pg && mva < eva) {
6606 paddr_t pa = VM_PAGE_TO_PHYS(pg);
6607
6608 pmap_kenter_pa(mva, pa,
6609 VM_PROT_READ|VM_PROT_WRITE, PMAP_KMPAGE|PMAP_PTE);
6610
6611 mva += PAGE_SIZE;
6612 pg = TAILQ_NEXT(pg, pageq.queue);
6613 }
6614 KASSERTMSG(pg == NULL && mva == eva, "pg %p mva %" PRIxVADDR
6615 " eva %" PRIxVADDR, pg, mva, eva);
6616 #else
6617 bool ok;
6618 paddr_t pa = VM_PAGE_TO_PHYS(pg);
6619 va = pmap_direct_mapped_phys(pa, &ok, 0);
6620 KASSERT(ok);
6621 KASSERT(va >= KERNEL_BASE);
6622 #endif
6623
6624 return (void *)va;
6625 }
6626
6627 static void
6628 pmap_l1tt_free(struct pool *pp, void *v)
6629 {
6630 vaddr_t va = (vaddr_t)v;
6631
6632 #if !defined( __HAVE_MM_MD_DIRECT_MAPPED_PHYS)
6633 uvm_km_free(kernel_map, va, L1TT_SIZE, UVM_KMF_WIRED);
6634 #else
6635 #if defined(KERNEL_BASE_VOFFSET)
6636 paddr_t pa = va - KERNEL_BASE_VOFFSET;
6637 #else
6638 paddr_t pa = va - KERNEL_BASE + physical_start;
6639 #endif
6640 const paddr_t epa = pa + L1TT_SIZE;
6641
6642 for (; pa < epa; pa += PAGE_SIZE) {
6643 struct vm_page *pg = PHYS_TO_VM_PAGE(pa);
6644 uvm_pagefree(pg);
6645 }
6646 #endif
6647 }
6648 #endif
6649
6650 /*
6651 * pmap_postinit()
6652 *
6653 * This routine is called after the vm and kmem subsystems have been
6654 * initialised. This allows the pmap code to perform any initialisation
6655 * that can only be done once the memory allocation is in place.
6656 */
6657 void
6658 pmap_postinit(void)
6659 {
6660 #ifndef ARM_MMU_EXTENDED
6661 extern paddr_t physical_start, physical_end;
6662 struct l1_ttable *l1;
6663 struct pglist plist;
6664 struct vm_page *m;
6665 pd_entry_t *pdep;
6666 vaddr_t va, eva;
6667 u_int loop, needed;
6668 int error;
6669 #endif
6670
6671 pool_cache_setlowat(&pmap_l2ptp_cache, (PAGE_SIZE / L2_TABLE_SIZE_REAL) * 4);
6672 pool_cache_setlowat(&pmap_l2dtable_cache,
6673 (PAGE_SIZE / sizeof(struct l2_dtable)) * 2);
6674
6675 #ifndef ARM_MMU_EXTENDED
6676 needed = (maxproc / PMAP_DOMAINS) + ((maxproc % PMAP_DOMAINS) ? 1 : 0);
6677 needed -= 1;
6678
6679 l1 = kmem_alloc(sizeof(*l1) * needed, KM_SLEEP);
6680
6681 for (loop = 0; loop < needed; loop++, l1++) {
6682 /* Allocate a L1 page table */
6683 va = uvm_km_alloc(kernel_map, L1_TABLE_SIZE, 0, UVM_KMF_VAONLY);
6684 if (va == 0)
6685 panic("Cannot allocate L1 KVM");
6686
6687 error = uvm_pglistalloc(L1_TABLE_SIZE, physical_start,
6688 physical_end, L1_TABLE_SIZE, 0, &plist, 1, 1);
6689 if (error)
6690 panic("Cannot allocate L1 physical pages");
6691
6692 m = TAILQ_FIRST(&plist);
6693 eva = va + L1_TABLE_SIZE;
6694 pdep = (pd_entry_t *)va;
6695
6696 while (m && va < eva) {
6697 paddr_t pa = VM_PAGE_TO_PHYS(m);
6698
6699 pmap_kenter_pa(va, pa,
6700 VM_PROT_READ|VM_PROT_WRITE, PMAP_KMPAGE|PMAP_PTE);
6701
6702 va += PAGE_SIZE;
6703 m = TAILQ_NEXT(m, pageq.queue);
6704 }
6705
6706 #ifdef DIAGNOSTIC
6707 if (m)
6708 panic("pmap_alloc_l1pt: pglist not empty");
6709 #endif /* DIAGNOSTIC */
6710
6711 pmap_init_l1(l1, pdep);
6712 }
6713
6714 #ifdef DEBUG
6715 printf("pmap_postinit: Allocated %d static L1 descriptor tables\n",
6716 needed);
6717 #endif
6718 #endif /* !ARM_MMU_EXTENDED */
6719 }
6720
6721 /*
6722 * Note that the following routines are used by board-specific initialisation
6723 * code to configure the initial kernel page tables.
6724 *
6725 */
6726
6727 /*
6728 * This list exists for the benefit of pmap_map_chunk(). It keeps track
6729 * of the kernel L2 tables during bootstrap, so that pmap_map_chunk() can
6730 * find them as necessary.
6731 *
6732 * Note that the data on this list MUST remain valid after initarm() returns,
6733 * as pmap_bootstrap() uses it to construct L2 table metadata.
6734 */
6735 SLIST_HEAD(, pv_addr) kernel_pt_list = SLIST_HEAD_INITIALIZER(kernel_pt_list);
6736
6737 static vaddr_t
6738 kernel_pt_lookup(paddr_t pa)
6739 {
6740 pv_addr_t *pv;
6741
6742 SLIST_FOREACH(pv, &kernel_pt_list, pv_list) {
6743 if (pv->pv_pa == (pa & ~PGOFSET))
6744 return pv->pv_va | (pa & PGOFSET);
6745 }
6746 return 0;
6747 }
6748
6749 /*
6750 * pmap_map_section:
6751 *
6752 * Create a single section mapping.
6753 */
6754 void
6755 pmap_map_section(vaddr_t l1pt, vaddr_t va, paddr_t pa, int prot, int cache)
6756 {
6757 pd_entry_t * const pdep = (pd_entry_t *) l1pt;
6758 const size_t l1slot = l1pte_index(va);
6759 pd_entry_t fl;
6760
6761 KASSERT(((va | pa) & L1_S_OFFSET) == 0);
6762
6763 switch (cache) {
6764 case PTE_NOCACHE:
6765 fl = pte_l1_s_nocache_mode;
6766 break;
6767
6768 case PTE_CACHE:
6769 fl = pte_l1_s_cache_mode;
6770 break;
6771
6772 case PTE_PAGETABLE:
6773 fl = pte_l1_s_cache_mode_pt;
6774 break;
6775
6776 case PTE_DEV:
6777 default:
6778 fl = 0;
6779 break;
6780 }
6781
6782 const pd_entry_t npde = L1_S_PROTO | pa |
6783 L1_S_PROT(PTE_KERNEL, prot) | fl | L1_S_DOM(PMAP_DOMAIN_KERNEL);
6784 l1pte_setone(pdep + l1slot, npde);
6785 PDE_SYNC(pdep + l1slot);
6786 }
6787
6788 /*
6789 * pmap_map_entry:
6790 *
6791 * Create a single page mapping.
6792 */
6793 void
6794 pmap_map_entry(vaddr_t l1pt, vaddr_t va, paddr_t pa, int prot, int cache)
6795 {
6796 pd_entry_t * const pdep = (pd_entry_t *) l1pt;
6797 const size_t l1slot = l1pte_index(va);
6798 pt_entry_t npte;
6799 pt_entry_t *ptep;
6800
6801 KASSERT(((va | pa) & PGOFSET) == 0);
6802
6803 switch (cache) {
6804 case PTE_NOCACHE:
6805 npte = pte_l2_s_nocache_mode;
6806 break;
6807
6808 case PTE_CACHE:
6809 npte = pte_l2_s_cache_mode;
6810 break;
6811
6812 case PTE_PAGETABLE:
6813 npte = pte_l2_s_cache_mode_pt;
6814 break;
6815
6816 default:
6817 npte = 0;
6818 break;
6819 }
6820
6821 if ((pdep[l1slot] & L1_TYPE_MASK) != L1_TYPE_C)
6822 panic("pmap_map_entry: no L2 table for VA 0x%08lx", va);
6823
6824 ptep = (pt_entry_t *) kernel_pt_lookup(l1pte_pa(pdep[l1slot]));
6825 if (ptep == NULL)
6826 panic("pmap_map_entry: can't find L2 table for VA 0x%08lx", va);
6827
6828 npte |= L2_S_PROTO | pa | L2_S_PROT(PTE_KERNEL, prot);
6829 #ifdef ARM_MMU_EXTENDED
6830 if (prot & VM_PROT_EXECUTE) {
6831 npte &= ~L2_XS_XN;
6832 }
6833 #endif
6834 ptep += l2pte_index(va);
6835 l2pte_set(ptep, npte, 0);
6836 PTE_SYNC(ptep);
6837 }
6838
6839 /*
6840 * pmap_link_l2pt:
6841 *
6842 * Link the L2 page table specified by "l2pv" into the L1
6843 * page table at the slot for "va".
6844 */
6845 void
6846 pmap_link_l2pt(vaddr_t l1pt, vaddr_t va, pv_addr_t *l2pv)
6847 {
6848 pd_entry_t * const pdep = (pd_entry_t *) l1pt + l1pte_index(va);
6849
6850 KASSERT((va & ((L1_S_SIZE * (PAGE_SIZE / L2_T_SIZE)) - 1)) == 0);
6851 KASSERT((l2pv->pv_pa & PGOFSET) == 0);
6852
6853 const pd_entry_t npde = L1_C_DOM(PMAP_DOMAIN_KERNEL) | L1_C_PROTO
6854 | l2pv->pv_pa;
6855
6856 l1pte_set(pdep, npde);
6857 PDE_SYNC_RANGE(pdep, PAGE_SIZE / L2_T_SIZE);
6858
6859 SLIST_INSERT_HEAD(&kernel_pt_list, l2pv, pv_list);
6860 }
6861
6862 /*
6863 * pmap_map_chunk:
6864 *
6865 * Map a chunk of memory using the most efficient mappings
6866 * possible (section, large page, small page) into the
6867 * provided L1 and L2 tables at the specified virtual address.
6868 */
6869 vsize_t
6870 pmap_map_chunk(vaddr_t l1pt, vaddr_t va, paddr_t pa, vsize_t size,
6871 int prot, int cache)
6872 {
6873 pd_entry_t * const pdep = (pd_entry_t *) l1pt;
6874 pt_entry_t f1, f2s, f2l;
6875 vsize_t resid;
6876
6877 resid = (size + (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
6878
6879 if (l1pt == 0)
6880 panic("pmap_map_chunk: no L1 table provided");
6881
6882 // VPRINTF("pmap_map_chunk: pa=0x%lx va=0x%lx size=0x%lx resid=0x%lx "
6883 // "prot=0x%x cache=%d\n", pa, va, size, resid, prot, cache);
6884
6885 switch (cache) {
6886 case PTE_NOCACHE:
6887 f1 = pte_l1_s_nocache_mode;
6888 f2l = pte_l2_l_nocache_mode;
6889 f2s = pte_l2_s_nocache_mode;
6890 break;
6891
6892 case PTE_CACHE:
6893 f1 = pte_l1_s_cache_mode;
6894 f2l = pte_l2_l_cache_mode;
6895 f2s = pte_l2_s_cache_mode;
6896 break;
6897
6898 case PTE_PAGETABLE:
6899 f1 = pte_l1_s_cache_mode_pt;
6900 f2l = pte_l2_l_cache_mode_pt;
6901 f2s = pte_l2_s_cache_mode_pt;
6902 break;
6903
6904 case PTE_DEV:
6905 default:
6906 f1 = 0;
6907 f2l = 0;
6908 f2s = 0;
6909 break;
6910 }
6911
6912 size = resid;
6913
6914 while (resid > 0) {
6915 const size_t l1slot = l1pte_index(va);
6916 #ifdef ARM_MMU_EXTENDED
6917 /* See if we can use a supersection mapping. */
6918 if (L1_SS_PROTO && L1_SS_MAPPABLE_P(va, pa, resid)) {
6919 /* Supersection are always domain 0 */
6920 const pd_entry_t npde = L1_SS_PROTO | pa
6921 | ((prot & VM_PROT_EXECUTE) ? 0 : L1_S_V6_XN)
6922 | (va & 0x80000000 ? 0 : L1_S_V6_nG)
6923 | L1_S_PROT(PTE_KERNEL, prot) | f1;
6924 VPRINTF("sS");
6925 l1pte_set(&pdep[l1slot], npde);
6926 PDE_SYNC_RANGE(&pdep[l1slot], L1_SS_SIZE / L1_S_SIZE);
6927 // VPRINTF("\npmap_map_chunk: pa=0x%lx va=0x%lx resid=0x%08lx "
6928 // "npdep=%p pde=0x%x\n", pa, va, resid, &pdep[l1slot], npde);
6929 va += L1_SS_SIZE;
6930 pa += L1_SS_SIZE;
6931 resid -= L1_SS_SIZE;
6932 continue;
6933 }
6934 #endif
6935 /* See if we can use a section mapping. */
6936 if (L1_S_MAPPABLE_P(va, pa, resid)) {
6937 const pd_entry_t npde = L1_S_PROTO | pa
6938 #ifdef ARM_MMU_EXTENDED
6939 | ((prot & VM_PROT_EXECUTE) ? 0 : L1_S_V6_XN)
6940 | (va & 0x80000000 ? 0 : L1_S_V6_nG)
6941 #endif
6942 | L1_S_PROT(PTE_KERNEL, prot) | f1
6943 | L1_S_DOM(PMAP_DOMAIN_KERNEL);
6944 VPRINTF("S");
6945 l1pte_set(&pdep[l1slot], npde);
6946 PDE_SYNC(&pdep[l1slot]);
6947 // VPRINTF("\npmap_map_chunk: pa=0x%lx va=0x%lx resid=0x%08lx "
6948 // "npdep=%p pde=0x%x\n", pa, va, resid, &pdep[l1slot], npde);
6949 va += L1_S_SIZE;
6950 pa += L1_S_SIZE;
6951 resid -= L1_S_SIZE;
6952 continue;
6953 }
6954
6955 /*
6956 * Ok, we're going to use an L2 table. Make sure
6957 * one is actually in the corresponding L1 slot
6958 * for the current VA.
6959 */
6960 if ((pdep[l1slot] & L1_TYPE_MASK) != L1_TYPE_C)
6961 panic("%s: no L2 table for VA %#lx", __func__, va);
6962
6963 pt_entry_t *ptep = (pt_entry_t *) kernel_pt_lookup(l1pte_pa(pdep[l1slot]));
6964 if (ptep == NULL)
6965 panic("%s: can't find L2 table for VA %#lx", __func__,
6966 va);
6967
6968 ptep += l2pte_index(va);
6969
6970 /* See if we can use a L2 large page mapping. */
6971 if (L2_L_MAPPABLE_P(va, pa, resid)) {
6972 const pt_entry_t npte = L2_L_PROTO | pa
6973 #ifdef ARM_MMU_EXTENDED
6974 | ((prot & VM_PROT_EXECUTE) ? 0 : L2_XS_L_XN)
6975 | (va & 0x80000000 ? 0 : L2_XS_nG)
6976 #endif
6977 | L2_L_PROT(PTE_KERNEL, prot) | f2l;
6978 VPRINTF("L");
6979 l2pte_set(ptep, npte, 0);
6980 PTE_SYNC_RANGE(ptep, L2_L_SIZE / L2_S_SIZE);
6981 va += L2_L_SIZE;
6982 pa += L2_L_SIZE;
6983 resid -= L2_L_SIZE;
6984 continue;
6985 }
6986
6987 VPRINTF("P");
6988 /* Use a small page mapping. */
6989 pt_entry_t npte = L2_S_PROTO | pa
6990 #ifdef ARM_MMU_EXTENDED
6991 | ((prot & VM_PROT_EXECUTE) ? 0 : L2_XS_XN)
6992 | (va & 0x80000000 ? 0 : L2_XS_nG)
6993 #endif
6994 | L2_S_PROT(PTE_KERNEL, prot) | f2s;
6995 #ifdef ARM_MMU_EXTENDED
6996 npte &= ((prot & VM_PROT_EXECUTE) ? ~L2_XS_XN : ~0);
6997 #endif
6998 l2pte_set(ptep, npte, 0);
6999 PTE_SYNC(ptep);
7000 va += PAGE_SIZE;
7001 pa += PAGE_SIZE;
7002 resid -= PAGE_SIZE;
7003 }
7004 VPRINTF("\n");
7005 return size;
7006 }
7007
7008 /*
7009 * pmap_unmap_chunk:
7010 *
7011 * Unmap a chunk of memory that was previously pmap_map_chunk
7012 */
7013 void
7014 pmap_unmap_chunk(vaddr_t l1pt, vaddr_t va, vsize_t size)
7015 {
7016 pd_entry_t * const pdep = (pd_entry_t *) l1pt;
7017 const size_t l1slot = l1pte_index(va);
7018
7019 KASSERT(size == L1_SS_SIZE || size == L1_S_SIZE);
7020
7021 l1pte_set(&pdep[l1slot], 0);
7022 PDE_SYNC_RANGE(&pdep[l1slot], size / L1_S_SIZE);
7023
7024 pmap_tlb_flush_SE(pmap_kernel(), va, PVF_REF);
7025 }
7026
7027
7028
7029 /********************** Static device map routines ***************************/
7030
7031 static const struct pmap_devmap *pmap_devmap_table;
7032
7033 /*
7034 * Register the devmap table. This is provided in case early console
7035 * initialization needs to register mappings created by bootstrap code
7036 * before pmap_devmap_bootstrap() is called.
7037 */
7038 void
7039 pmap_devmap_register(const struct pmap_devmap *table)
7040 {
7041
7042 pmap_devmap_table = table;
7043 }
7044
7045 /*
7046 * Map all of the static regions in the devmap table, and remember
7047 * the devmap table so other parts of the kernel can look up entries
7048 * later.
7049 */
7050 void
7051 pmap_devmap_bootstrap(vaddr_t l1pt, const struct pmap_devmap *table)
7052 {
7053 int i;
7054
7055 pmap_devmap_table = table;
7056
7057 for (i = 0; pmap_devmap_table[i].pd_size != 0; i++) {
7058 const struct pmap_devmap *pdp = &pmap_devmap_table[i];
7059
7060 KASSERTMSG(VADDR_MAX - pdp->pd_va >= pdp->pd_size - 1, "va %" PRIxVADDR
7061 " sz %" PRIxPSIZE, pdp->pd_va, pdp->pd_size);
7062 KASSERTMSG(PADDR_MAX - pdp->pd_pa >= pdp->pd_size - 1, "pa %" PRIxPADDR
7063 " sz %" PRIxPSIZE, pdp->pd_pa, pdp->pd_size);
7064 VPRINTF("devmap: %08lx -> %08lx @ %08lx\n", pdp->pd_pa,
7065 pdp->pd_pa + pdp->pd_size - 1, pdp->pd_va);
7066
7067 pmap_map_chunk(l1pt, pdp->pd_va, pdp->pd_pa, pdp->pd_size,
7068 pdp->pd_prot, pdp->pd_cache);
7069 }
7070 }
7071
7072 const struct pmap_devmap *
7073 pmap_devmap_find_pa(paddr_t pa, psize_t size)
7074 {
7075 uint64_t endpa;
7076 int i;
7077
7078 if (pmap_devmap_table == NULL)
7079 return NULL;
7080
7081 endpa = (uint64_t)pa + (uint64_t)(size - 1);
7082
7083 for (i = 0; pmap_devmap_table[i].pd_size != 0; i++) {
7084 if (pa >= pmap_devmap_table[i].pd_pa &&
7085 endpa <= (uint64_t)pmap_devmap_table[i].pd_pa +
7086 (uint64_t)(pmap_devmap_table[i].pd_size - 1))
7087 return &pmap_devmap_table[i];
7088 }
7089
7090 return NULL;
7091 }
7092
7093 const struct pmap_devmap *
7094 pmap_devmap_find_va(vaddr_t va, vsize_t size)
7095 {
7096 int i;
7097
7098 if (pmap_devmap_table == NULL)
7099 return NULL;
7100
7101 for (i = 0; pmap_devmap_table[i].pd_size != 0; i++) {
7102 if (va >= pmap_devmap_table[i].pd_va &&
7103 va + size - 1 <= pmap_devmap_table[i].pd_va +
7104 pmap_devmap_table[i].pd_size - 1)
7105 return &pmap_devmap_table[i];
7106 }
7107
7108 return NULL;
7109 }
7110
7111 /********************** PTE initialization routines **************************/
7112
7113 /*
7114 * These routines are called when the CPU type is identified to set up
7115 * the PTE prototypes, cache modes, etc.
7116 *
7117 * The variables are always here, just in case modules need to reference
7118 * them (though, they shouldn't).
7119 */
7120
7121 pt_entry_t pte_l1_s_nocache_mode;
7122 pt_entry_t pte_l1_s_cache_mode;
7123 pt_entry_t pte_l1_s_wc_mode;
7124 pt_entry_t pte_l1_s_cache_mode_pt;
7125 pt_entry_t pte_l1_s_cache_mask;
7126
7127 pt_entry_t pte_l2_l_nocache_mode;
7128 pt_entry_t pte_l2_l_cache_mode;
7129 pt_entry_t pte_l2_l_wc_mode;
7130 pt_entry_t pte_l2_l_cache_mode_pt;
7131 pt_entry_t pte_l2_l_cache_mask;
7132
7133 pt_entry_t pte_l2_s_nocache_mode;
7134 pt_entry_t pte_l2_s_cache_mode;
7135 pt_entry_t pte_l2_s_wc_mode;
7136 pt_entry_t pte_l2_s_cache_mode_pt;
7137 pt_entry_t pte_l2_s_cache_mask;
7138
7139 pt_entry_t pte_l1_s_prot_u;
7140 pt_entry_t pte_l1_s_prot_w;
7141 pt_entry_t pte_l1_s_prot_ro;
7142 pt_entry_t pte_l1_s_prot_mask;
7143
7144 pt_entry_t pte_l2_s_prot_u;
7145 pt_entry_t pte_l2_s_prot_w;
7146 pt_entry_t pte_l2_s_prot_ro;
7147 pt_entry_t pte_l2_s_prot_mask;
7148
7149 pt_entry_t pte_l2_l_prot_u;
7150 pt_entry_t pte_l2_l_prot_w;
7151 pt_entry_t pte_l2_l_prot_ro;
7152 pt_entry_t pte_l2_l_prot_mask;
7153
7154 pt_entry_t pte_l1_ss_proto;
7155 pt_entry_t pte_l1_s_proto;
7156 pt_entry_t pte_l1_c_proto;
7157 pt_entry_t pte_l2_s_proto;
7158
7159 void (*pmap_copy_page_func)(paddr_t, paddr_t);
7160 void (*pmap_zero_page_func)(paddr_t);
7161
7162 #if (ARM_MMU_GENERIC + ARM_MMU_SA1 + ARM_MMU_V6 + ARM_MMU_V7) != 0
7163 void
7164 pmap_pte_init_generic(void)
7165 {
7166
7167 pte_l1_s_nocache_mode = 0;
7168 pte_l1_s_cache_mode = L1_S_B|L1_S_C;
7169 pte_l1_s_wc_mode = L1_S_B;
7170 pte_l1_s_cache_mask = L1_S_CACHE_MASK_generic;
7171
7172 pte_l2_l_nocache_mode = 0;
7173 pte_l2_l_cache_mode = L2_B|L2_C;
7174 pte_l2_l_wc_mode = L2_B;
7175 pte_l2_l_cache_mask = L2_L_CACHE_MASK_generic;
7176
7177 pte_l2_s_nocache_mode = 0;
7178 pte_l2_s_cache_mode = L2_B|L2_C;
7179 pte_l2_s_wc_mode = L2_B;
7180 pte_l2_s_cache_mask = L2_S_CACHE_MASK_generic;
7181
7182 /*
7183 * If we have a write-through cache, set B and C. If
7184 * we have a write-back cache, then we assume setting
7185 * only C will make those pages write-through (except for those
7186 * Cortex CPUs which can read the L1 caches).
7187 */
7188 if (cpufuncs.cf_dcache_wb_range == (void *) cpufunc_nullop
7189 #if ARM_MMU_V7 > 0
7190 || CPU_ID_CORTEX_P(curcpu()->ci_arm_cpuid)
7191 #endif
7192 #if ARM_MMU_V6 > 0
7193 || CPU_ID_ARM11_P(curcpu()->ci_arm_cpuid) /* arm116 errata 399234 */
7194 #endif
7195 || false) {
7196 pte_l1_s_cache_mode_pt = L1_S_B|L1_S_C;
7197 pte_l2_l_cache_mode_pt = L2_B|L2_C;
7198 pte_l2_s_cache_mode_pt = L2_B|L2_C;
7199 } else {
7200 pte_l1_s_cache_mode_pt = L1_S_C; /* write through */
7201 pte_l2_l_cache_mode_pt = L2_C; /* write through */
7202 pte_l2_s_cache_mode_pt = L2_C; /* write through */
7203 }
7204
7205 pte_l1_s_prot_u = L1_S_PROT_U_generic;
7206 pte_l1_s_prot_w = L1_S_PROT_W_generic;
7207 pte_l1_s_prot_ro = L1_S_PROT_RO_generic;
7208 pte_l1_s_prot_mask = L1_S_PROT_MASK_generic;
7209
7210 pte_l2_s_prot_u = L2_S_PROT_U_generic;
7211 pte_l2_s_prot_w = L2_S_PROT_W_generic;
7212 pte_l2_s_prot_ro = L2_S_PROT_RO_generic;
7213 pte_l2_s_prot_mask = L2_S_PROT_MASK_generic;
7214
7215 pte_l2_l_prot_u = L2_L_PROT_U_generic;
7216 pte_l2_l_prot_w = L2_L_PROT_W_generic;
7217 pte_l2_l_prot_ro = L2_L_PROT_RO_generic;
7218 pte_l2_l_prot_mask = L2_L_PROT_MASK_generic;
7219
7220 pte_l1_ss_proto = L1_SS_PROTO_generic;
7221 pte_l1_s_proto = L1_S_PROTO_generic;
7222 pte_l1_c_proto = L1_C_PROTO_generic;
7223 pte_l2_s_proto = L2_S_PROTO_generic;
7224
7225 pmap_copy_page_func = pmap_copy_page_generic;
7226 pmap_zero_page_func = pmap_zero_page_generic;
7227 }
7228
7229 #if defined(CPU_ARM8)
7230 void
7231 pmap_pte_init_arm8(void)
7232 {
7233
7234 /*
7235 * ARM8 is compatible with generic, but we need to use
7236 * the page tables uncached.
7237 */
7238 pmap_pte_init_generic();
7239
7240 pte_l1_s_cache_mode_pt = 0;
7241 pte_l2_l_cache_mode_pt = 0;
7242 pte_l2_s_cache_mode_pt = 0;
7243 }
7244 #endif /* CPU_ARM8 */
7245
7246 #if defined(CPU_ARM9) && defined(ARM9_CACHE_WRITE_THROUGH)
7247 void
7248 pmap_pte_init_arm9(void)
7249 {
7250
7251 /*
7252 * ARM9 is compatible with generic, but we want to use
7253 * write-through caching for now.
7254 */
7255 pmap_pte_init_generic();
7256
7257 pte_l1_s_cache_mode = L1_S_C;
7258 pte_l2_l_cache_mode = L2_C;
7259 pte_l2_s_cache_mode = L2_C;
7260
7261 pte_l1_s_wc_mode = L1_S_B;
7262 pte_l2_l_wc_mode = L2_B;
7263 pte_l2_s_wc_mode = L2_B;
7264
7265 pte_l1_s_cache_mode_pt = L1_S_C;
7266 pte_l2_l_cache_mode_pt = L2_C;
7267 pte_l2_s_cache_mode_pt = L2_C;
7268 }
7269 #endif /* CPU_ARM9 && ARM9_CACHE_WRITE_THROUGH */
7270 #endif /* (ARM_MMU_GENERIC + ARM_MMU_SA1 + ARM_MMU_V6) != 0 */
7271
7272 #if defined(CPU_ARM10)
7273 void
7274 pmap_pte_init_arm10(void)
7275 {
7276
7277 /*
7278 * ARM10 is compatible with generic, but we want to use
7279 * write-through caching for now.
7280 */
7281 pmap_pte_init_generic();
7282
7283 pte_l1_s_cache_mode = L1_S_B | L1_S_C;
7284 pte_l2_l_cache_mode = L2_B | L2_C;
7285 pte_l2_s_cache_mode = L2_B | L2_C;
7286
7287 pte_l1_s_cache_mode = L1_S_B;
7288 pte_l2_l_cache_mode = L2_B;
7289 pte_l2_s_cache_mode = L2_B;
7290
7291 pte_l1_s_cache_mode_pt = L1_S_C;
7292 pte_l2_l_cache_mode_pt = L2_C;
7293 pte_l2_s_cache_mode_pt = L2_C;
7294
7295 }
7296 #endif /* CPU_ARM10 */
7297
7298 #if defined(CPU_ARM11) && defined(ARM11_CACHE_WRITE_THROUGH)
7299 void
7300 pmap_pte_init_arm11(void)
7301 {
7302
7303 /*
7304 * ARM11 is compatible with generic, but we want to use
7305 * write-through caching for now.
7306 */
7307 pmap_pte_init_generic();
7308
7309 pte_l1_s_cache_mode = L1_S_C;
7310 pte_l2_l_cache_mode = L2_C;
7311 pte_l2_s_cache_mode = L2_C;
7312
7313 pte_l1_s_wc_mode = L1_S_B;
7314 pte_l2_l_wc_mode = L2_B;
7315 pte_l2_s_wc_mode = L2_B;
7316
7317 pte_l1_s_cache_mode_pt = L1_S_C;
7318 pte_l2_l_cache_mode_pt = L2_C;
7319 pte_l2_s_cache_mode_pt = L2_C;
7320 }
7321 #endif /* CPU_ARM11 && ARM11_CACHE_WRITE_THROUGH */
7322
7323 #if ARM_MMU_SA1 == 1
7324 void
7325 pmap_pte_init_sa1(void)
7326 {
7327
7328 /*
7329 * The StrongARM SA-1 cache does not have a write-through
7330 * mode. So, do the generic initialization, then reset
7331 * the page table cache mode to B=1,C=1, and note that
7332 * the PTEs need to be sync'd.
7333 */
7334 pmap_pte_init_generic();
7335
7336 pte_l1_s_cache_mode_pt = L1_S_B|L1_S_C;
7337 pte_l2_l_cache_mode_pt = L2_B|L2_C;
7338 pte_l2_s_cache_mode_pt = L2_B|L2_C;
7339
7340 pmap_needs_pte_sync = 1;
7341 }
7342 #endif /* ARM_MMU_SA1 == 1*/
7343
7344 #if ARM_MMU_XSCALE == 1
7345 #if (ARM_NMMUS > 1)
7346 static u_int xscale_use_minidata;
7347 #endif
7348
7349 void
7350 pmap_pte_init_xscale(void)
7351 {
7352 uint32_t auxctl;
7353 int write_through = 0;
7354
7355 pte_l1_s_cache_mode = L1_S_B|L1_S_C;
7356 pte_l1_s_wc_mode = L1_S_B;
7357 pte_l1_s_cache_mask = L1_S_CACHE_MASK_xscale;
7358
7359 pte_l2_l_cache_mode = L2_B|L2_C;
7360 pte_l2_l_wc_mode = L2_B;
7361 pte_l2_l_cache_mask = L2_L_CACHE_MASK_xscale;
7362
7363 pte_l2_s_cache_mode = L2_B|L2_C;
7364 pte_l2_s_wc_mode = L2_B;
7365 pte_l2_s_cache_mask = L2_S_CACHE_MASK_xscale;
7366
7367 pte_l1_s_cache_mode_pt = L1_S_C;
7368 pte_l2_l_cache_mode_pt = L2_C;
7369 pte_l2_s_cache_mode_pt = L2_C;
7370
7371 #ifdef XSCALE_CACHE_READ_WRITE_ALLOCATE
7372 /*
7373 * The XScale core has an enhanced mode where writes that
7374 * miss the cache cause a cache line to be allocated. This
7375 * is significantly faster than the traditional, write-through
7376 * behavior of this case.
7377 */
7378 pte_l1_s_cache_mode |= L1_S_XS_TEX(TEX_XSCALE_X);
7379 pte_l2_l_cache_mode |= L2_XS_L_TEX(TEX_XSCALE_X);
7380 pte_l2_s_cache_mode |= L2_XS_T_TEX(TEX_XSCALE_X);
7381 #endif /* XSCALE_CACHE_READ_WRITE_ALLOCATE */
7382
7383 #ifdef XSCALE_CACHE_WRITE_THROUGH
7384 /*
7385 * Some versions of the XScale core have various bugs in
7386 * their cache units, the work-around for which is to run
7387 * the cache in write-through mode. Unfortunately, this
7388 * has a major (negative) impact on performance. So, we
7389 * go ahead and run fast-and-loose, in the hopes that we
7390 * don't line up the planets in a way that will trip the
7391 * bugs.
7392 *
7393 * However, we give you the option to be slow-but-correct.
7394 */
7395 write_through = 1;
7396 #elif defined(XSCALE_CACHE_WRITE_BACK)
7397 /* force write back cache mode */
7398 write_through = 0;
7399 #elif defined(CPU_XSCALE_PXA250) || defined(CPU_XSCALE_PXA270)
7400 /*
7401 * Intel PXA2[15]0 processors are known to have a bug in
7402 * write-back cache on revision 4 and earlier (stepping
7403 * A[01] and B[012]). Fixed for C0 and later.
7404 */
7405 {
7406 uint32_t id, type;
7407
7408 id = cpufunc_id();
7409 type = id & ~(CPU_ID_XSCALE_COREREV_MASK|CPU_ID_REVISION_MASK);
7410
7411 if (type == CPU_ID_PXA250 || type == CPU_ID_PXA210) {
7412 if ((id & CPU_ID_REVISION_MASK) < 5) {
7413 /* write through for stepping A0-1 and B0-2 */
7414 write_through = 1;
7415 }
7416 }
7417 }
7418 #endif /* XSCALE_CACHE_WRITE_THROUGH */
7419
7420 if (write_through) {
7421 pte_l1_s_cache_mode = L1_S_C;
7422 pte_l2_l_cache_mode = L2_C;
7423 pte_l2_s_cache_mode = L2_C;
7424 }
7425
7426 #if (ARM_NMMUS > 1)
7427 xscale_use_minidata = 1;
7428 #endif
7429
7430 pte_l1_s_prot_u = L1_S_PROT_U_xscale;
7431 pte_l1_s_prot_w = L1_S_PROT_W_xscale;
7432 pte_l1_s_prot_ro = L1_S_PROT_RO_xscale;
7433 pte_l1_s_prot_mask = L1_S_PROT_MASK_xscale;
7434
7435 pte_l2_s_prot_u = L2_S_PROT_U_xscale;
7436 pte_l2_s_prot_w = L2_S_PROT_W_xscale;
7437 pte_l2_s_prot_ro = L2_S_PROT_RO_xscale;
7438 pte_l2_s_prot_mask = L2_S_PROT_MASK_xscale;
7439
7440 pte_l2_l_prot_u = L2_L_PROT_U_xscale;
7441 pte_l2_l_prot_w = L2_L_PROT_W_xscale;
7442 pte_l2_l_prot_ro = L2_L_PROT_RO_xscale;
7443 pte_l2_l_prot_mask = L2_L_PROT_MASK_xscale;
7444
7445 pte_l1_ss_proto = L1_SS_PROTO_xscale;
7446 pte_l1_s_proto = L1_S_PROTO_xscale;
7447 pte_l1_c_proto = L1_C_PROTO_xscale;
7448 pte_l2_s_proto = L2_S_PROTO_xscale;
7449
7450 pmap_copy_page_func = pmap_copy_page_xscale;
7451 pmap_zero_page_func = pmap_zero_page_xscale;
7452
7453 /*
7454 * Disable ECC protection of page table access, for now.
7455 */
7456 auxctl = armreg_auxctl_read();
7457 auxctl &= ~XSCALE_AUXCTL_P;
7458 armreg_auxctl_write(auxctl);
7459 }
7460
7461 /*
7462 * xscale_setup_minidata:
7463 *
7464 * Set up the mini-data cache clean area. We require the
7465 * caller to allocate the right amount of physically and
7466 * virtually contiguous space.
7467 */
7468 void
7469 xscale_setup_minidata(vaddr_t l1pt, vaddr_t va, paddr_t pa)
7470 {
7471 extern vaddr_t xscale_minidata_clean_addr;
7472 extern vsize_t xscale_minidata_clean_size; /* already initialized */
7473 pd_entry_t *pde = (pd_entry_t *) l1pt;
7474 vsize_t size;
7475 uint32_t auxctl;
7476
7477 xscale_minidata_clean_addr = va;
7478
7479 /* Round it to page size. */
7480 size = (xscale_minidata_clean_size + L2_S_OFFSET) & L2_S_FRAME;
7481
7482 for (; size != 0;
7483 va += L2_S_SIZE, pa += L2_S_SIZE, size -= L2_S_SIZE) {
7484 const size_t l1slot = l1pte_index(va);
7485 pt_entry_t *ptep = (pt_entry_t *) kernel_pt_lookup(l1pte_pa(pde[l1slot]));
7486 if (ptep == NULL)
7487 panic("xscale_setup_minidata: can't find L2 table for "
7488 "VA 0x%08lx", va);
7489
7490 ptep += l2pte_index(va);
7491 pt_entry_t opte = *ptep;
7492 l2pte_set(ptep,
7493 L2_S_PROTO | pa | L2_S_PROT(PTE_KERNEL, VM_PROT_READ)
7494 | L2_C | L2_XS_T_TEX(TEX_XSCALE_X), opte);
7495 }
7496
7497 /*
7498 * Configure the mini-data cache for write-back with
7499 * read/write-allocate.
7500 *
7501 * NOTE: In order to reconfigure the mini-data cache, we must
7502 * make sure it contains no valid data! In order to do that,
7503 * we must issue a global data cache invalidate command!
7504 *
7505 * WE ASSUME WE ARE RUNNING UN-CACHED WHEN THIS ROUTINE IS CALLED!
7506 * THIS IS VERY IMPORTANT!
7507 */
7508
7509 /* Invalidate data and mini-data. */
7510 __asm volatile("mcr p15, 0, %0, c7, c6, 0" : : "r" (0));
7511 auxctl = armreg_auxctl_read();
7512 auxctl = (auxctl & ~XSCALE_AUXCTL_MD_MASK) | XSCALE_AUXCTL_MD_WB_RWA;
7513 armreg_auxctl_write(auxctl);
7514 }
7515
7516 /*
7517 * Change the PTEs for the specified kernel mappings such that they
7518 * will use the mini data cache instead of the main data cache.
7519 */
7520 void
7521 pmap_uarea(vaddr_t va)
7522 {
7523 vaddr_t next_bucket, eva;
7524
7525 #if (ARM_NMMUS > 1)
7526 if (xscale_use_minidata == 0)
7527 return;
7528 #endif
7529
7530 eva = va + USPACE;
7531
7532 while (va < eva) {
7533 next_bucket = L2_NEXT_BUCKET_VA(va);
7534 if (next_bucket > eva)
7535 next_bucket = eva;
7536
7537 struct l2_bucket *l2b = pmap_get_l2_bucket(pmap_kernel(), va);
7538 KDASSERT(l2b != NULL);
7539
7540 pt_entry_t * const sptep = &l2b->l2b_kva[l2pte_index(va)];
7541 pt_entry_t *ptep = sptep;
7542
7543 while (va < next_bucket) {
7544 const pt_entry_t opte = *ptep;
7545 if (!l2pte_minidata_p(opte)) {
7546 cpu_dcache_wbinv_range(va, PAGE_SIZE);
7547 cpu_tlb_flushD_SE(va);
7548 l2pte_set(ptep, opte & ~L2_B, opte);
7549 }
7550 ptep += PAGE_SIZE / L2_S_SIZE;
7551 va += PAGE_SIZE;
7552 }
7553 PTE_SYNC_RANGE(sptep, (u_int)(ptep - sptep));
7554 }
7555 cpu_cpwait();
7556 }
7557 #endif /* ARM_MMU_XSCALE == 1 */
7558
7559
7560 #if defined(CPU_ARM11MPCORE)
7561 void
7562 pmap_pte_init_arm11mpcore(void)
7563 {
7564
7565 /* cache mode is controlled by 5 bits (B, C, TEX[2:0]) */
7566 pte_l1_s_cache_mask = L1_S_CACHE_MASK_armv6;
7567 pte_l2_l_cache_mask = L2_L_CACHE_MASK_armv6;
7568 #if defined(ARM11MPCORE_COMPAT_MMU) || defined(ARMV6_EXTENDED_SMALL_PAGE)
7569 /* use extended small page (without APn, with TEX) */
7570 pte_l2_s_cache_mask = L2_XS_CACHE_MASK_armv6;
7571 #else
7572 pte_l2_s_cache_mask = L2_S_CACHE_MASK_armv6c;
7573 #endif
7574
7575 /* write-back, write-allocate */
7576 pte_l1_s_cache_mode = L1_S_C | L1_S_B | L1_S_V6_TEX(0x01);
7577 pte_l2_l_cache_mode = L2_C | L2_B | L2_V6_L_TEX(0x01);
7578 #if defined(ARM11MPCORE_COMPAT_MMU) || defined(ARMV6_EXTENDED_SMALL_PAGE)
7579 pte_l2_s_cache_mode = L2_C | L2_B | L2_V6_XS_TEX(0x01);
7580 #else
7581 /* no TEX. read-allocate */
7582 pte_l2_s_cache_mode = L2_C | L2_B;
7583 #endif
7584 /*
7585 * write-back, write-allocate for page tables.
7586 */
7587 pte_l1_s_cache_mode_pt = L1_S_C | L1_S_B | L1_S_V6_TEX(0x01);
7588 pte_l2_l_cache_mode_pt = L2_C | L2_B | L2_V6_L_TEX(0x01);
7589 #if defined(ARM11MPCORE_COMPAT_MMU) || defined(ARMV6_EXTENDED_SMALL_PAGE)
7590 pte_l2_s_cache_mode_pt = L2_C | L2_B | L2_V6_XS_TEX(0x01);
7591 #else
7592 pte_l2_s_cache_mode_pt = L2_C | L2_B;
7593 #endif
7594
7595 pte_l1_s_prot_u = L1_S_PROT_U_armv6;
7596 pte_l1_s_prot_w = L1_S_PROT_W_armv6;
7597 pte_l1_s_prot_ro = L1_S_PROT_RO_armv6;
7598 pte_l1_s_prot_mask = L1_S_PROT_MASK_armv6;
7599
7600 #if defined(ARM11MPCORE_COMPAT_MMU) || defined(ARMV6_EXTENDED_SMALL_PAGE)
7601 pte_l2_s_prot_u = L2_S_PROT_U_armv6n;
7602 pte_l2_s_prot_w = L2_S_PROT_W_armv6n;
7603 pte_l2_s_prot_ro = L2_S_PROT_RO_armv6n;
7604 pte_l2_s_prot_mask = L2_S_PROT_MASK_armv6n;
7605
7606 #else
7607 /* with AP[0..3] */
7608 pte_l2_s_prot_u = L2_S_PROT_U_generic;
7609 pte_l2_s_prot_w = L2_S_PROT_W_generic;
7610 pte_l2_s_prot_ro = L2_S_PROT_RO_generic;
7611 pte_l2_s_prot_mask = L2_S_PROT_MASK_generic;
7612 #endif
7613
7614 #ifdef ARM11MPCORE_COMPAT_MMU
7615 /* with AP[0..3] */
7616 pte_l2_l_prot_u = L2_L_PROT_U_generic;
7617 pte_l2_l_prot_w = L2_L_PROT_W_generic;
7618 pte_l2_l_prot_ro = L2_L_PROT_RO_generic;
7619 pte_l2_l_prot_mask = L2_L_PROT_MASK_generic;
7620
7621 pte_l1_ss_proto = L1_SS_PROTO_armv6;
7622 pte_l1_s_proto = L1_S_PROTO_armv6;
7623 pte_l1_c_proto = L1_C_PROTO_armv6;
7624 pte_l2_s_proto = L2_S_PROTO_armv6c;
7625 #else
7626 pte_l2_l_prot_u = L2_L_PROT_U_armv6n;
7627 pte_l2_l_prot_w = L2_L_PROT_W_armv6n;
7628 pte_l2_l_prot_ro = L2_L_PROT_RO_armv6n;
7629 pte_l2_l_prot_mask = L2_L_PROT_MASK_armv6n;
7630
7631 pte_l1_ss_proto = L1_SS_PROTO_armv6;
7632 pte_l1_s_proto = L1_S_PROTO_armv6;
7633 pte_l1_c_proto = L1_C_PROTO_armv6;
7634 pte_l2_s_proto = L2_S_PROTO_armv6n;
7635 #endif
7636
7637 pmap_copy_page_func = pmap_copy_page_generic;
7638 pmap_zero_page_func = pmap_zero_page_generic;
7639 pmap_needs_pte_sync = 1;
7640 }
7641 #endif /* CPU_ARM11MPCORE */
7642
7643
7644 #if ARM_MMU_V6 == 1
7645 void
7646 pmap_pte_init_armv6(void)
7647 {
7648 /*
7649 * The ARMv6-A MMU is mostly compatible with generic. If the
7650 * AP field is zero, that now means "no access" rather than
7651 * read-only. The prototypes are a little different because of
7652 * the XN bit.
7653 */
7654 pmap_pte_init_generic();
7655
7656 pte_l1_s_nocache_mode = L1_S_XS_TEX(1);
7657 pte_l2_l_nocache_mode = L2_XS_L_TEX(1);
7658 pte_l2_s_nocache_mode = L2_XS_T_TEX(1);
7659
7660 #ifdef ARM11_COMPAT_MMU
7661 /* with AP[0..3] */
7662 pte_l1_ss_proto = L1_SS_PROTO_armv6;
7663 #else
7664 pte_l1_s_cache_mask = L1_S_CACHE_MASK_armv6n;
7665 pte_l2_l_cache_mask = L2_L_CACHE_MASK_armv6n;
7666 pte_l2_s_cache_mask = L2_S_CACHE_MASK_armv6n;
7667
7668 pte_l1_ss_proto = L1_SS_PROTO_armv6;
7669 pte_l1_s_proto = L1_S_PROTO_armv6;
7670 pte_l1_c_proto = L1_C_PROTO_armv6;
7671 pte_l2_s_proto = L2_S_PROTO_armv6n;
7672
7673 pte_l1_s_prot_u = L1_S_PROT_U_armv6;
7674 pte_l1_s_prot_w = L1_S_PROT_W_armv6;
7675 pte_l1_s_prot_ro = L1_S_PROT_RO_armv6;
7676 pte_l1_s_prot_mask = L1_S_PROT_MASK_armv6;
7677
7678 pte_l2_l_prot_u = L2_L_PROT_U_armv6n;
7679 pte_l2_l_prot_w = L2_L_PROT_W_armv6n;
7680 pte_l2_l_prot_ro = L2_L_PROT_RO_armv6n;
7681 pte_l2_l_prot_mask = L2_L_PROT_MASK_armv6n;
7682
7683 pte_l2_s_prot_u = L2_S_PROT_U_armv6n;
7684 pte_l2_s_prot_w = L2_S_PROT_W_armv6n;
7685 pte_l2_s_prot_ro = L2_S_PROT_RO_armv6n;
7686 pte_l2_s_prot_mask = L2_S_PROT_MASK_armv6n;
7687
7688 #endif
7689 }
7690 #endif /* ARM_MMU_V6 */
7691
7692 #if ARM_MMU_V7 == 1
7693 void
7694 pmap_pte_init_armv7(void)
7695 {
7696 /*
7697 * The ARMv7-A MMU is mostly compatible with generic. If the
7698 * AP field is zero, that now means "no access" rather than
7699 * read-only. The prototypes are a little different because of
7700 * the XN bit.
7701 */
7702 pmap_pte_init_generic();
7703
7704 pmap_needs_pte_sync = 1;
7705
7706 pte_l1_s_nocache_mode = L1_S_XS_TEX(1);
7707 pte_l2_l_nocache_mode = L2_XS_L_TEX(1);
7708 pte_l2_s_nocache_mode = L2_XS_T_TEX(1);
7709
7710 pte_l1_s_cache_mask = L1_S_CACHE_MASK_armv7;
7711 pte_l2_l_cache_mask = L2_L_CACHE_MASK_armv7;
7712 pte_l2_s_cache_mask = L2_S_CACHE_MASK_armv7;
7713
7714 /*
7715 * If the core support coherent walk then updates to translation tables
7716 * do not require a clean to the point of unification to ensure
7717 * visibility by subsequent translation table walks. That means we can
7718 * map everything shareable and cached and the right thing will happen.
7719 */
7720 if (__SHIFTOUT(armreg_mmfr3_read(), __BITS(23,20))) {
7721 pmap_needs_pte_sync = 0;
7722
7723 /*
7724 * write-back, no write-allocate, shareable for normal pages.
7725 */
7726 pte_l1_s_cache_mode |= L1_S_V6_S;
7727 pte_l2_l_cache_mode |= L2_XS_S;
7728 pte_l2_s_cache_mode |= L2_XS_S;
7729 }
7730
7731 /*
7732 * Page tables are just all other memory. We can use write-back since
7733 * pmap_needs_pte_sync is 1 (or the MMU can read out of cache).
7734 */
7735 pte_l1_s_cache_mode_pt = pte_l1_s_cache_mode;
7736 pte_l2_l_cache_mode_pt = pte_l2_l_cache_mode;
7737 pte_l2_s_cache_mode_pt = pte_l2_s_cache_mode;
7738
7739 /*
7740 * Check the Memory Model Features to see if this CPU supports
7741 * the TLBIASID coproc op.
7742 */
7743 if (__SHIFTOUT(armreg_mmfr2_read(), __BITS(16,19)) >= 2) {
7744 arm_has_tlbiasid_p = true;
7745 } else if (__SHIFTOUT(armreg_mmfr2_read(), __BITS(12,15)) >= 2) {
7746 arm_has_tlbiasid_p = true;
7747 }
7748
7749 /*
7750 * Check the MPIDR to see if this CPU supports MP extensions.
7751 */
7752 #ifdef MULTIPROCESSOR
7753 arm_has_mpext_p = (armreg_mpidr_read() & (MPIDR_MP|MPIDR_U)) == MPIDR_MP;
7754 #else
7755 arm_has_mpext_p = false;
7756 #endif
7757
7758 pte_l1_s_prot_u = L1_S_PROT_U_armv7;
7759 pte_l1_s_prot_w = L1_S_PROT_W_armv7;
7760 pte_l1_s_prot_ro = L1_S_PROT_RO_armv7;
7761 pte_l1_s_prot_mask = L1_S_PROT_MASK_armv7;
7762
7763 pte_l2_s_prot_u = L2_S_PROT_U_armv7;
7764 pte_l2_s_prot_w = L2_S_PROT_W_armv7;
7765 pte_l2_s_prot_ro = L2_S_PROT_RO_armv7;
7766 pte_l2_s_prot_mask = L2_S_PROT_MASK_armv7;
7767
7768 pte_l2_l_prot_u = L2_L_PROT_U_armv7;
7769 pte_l2_l_prot_w = L2_L_PROT_W_armv7;
7770 pte_l2_l_prot_ro = L2_L_PROT_RO_armv7;
7771 pte_l2_l_prot_mask = L2_L_PROT_MASK_armv7;
7772
7773 pte_l1_ss_proto = L1_SS_PROTO_armv7;
7774 pte_l1_s_proto = L1_S_PROTO_armv7;
7775 pte_l1_c_proto = L1_C_PROTO_armv7;
7776 pte_l2_s_proto = L2_S_PROTO_armv7;
7777
7778 }
7779 #endif /* ARM_MMU_V7 */
7780
7781 /*
7782 * return the PA of the current L1 table, for use when handling a crash dump
7783 */
7784 uint32_t
7785 pmap_kernel_L1_addr(void)
7786 {
7787 #ifdef ARM_MMU_EXTENDED
7788 return pmap_kernel()->pm_l1_pa;
7789 #else
7790 return pmap_kernel()->pm_l1->l1_physaddr;
7791 #endif
7792 }
7793
7794 #if defined(DDB)
7795 /*
7796 * A couple of ddb-callable functions for dumping pmaps
7797 */
7798 void pmap_dump(pmap_t);
7799
7800 static pt_entry_t ncptes[64];
7801 static void pmap_dump_ncpg(pmap_t);
7802
7803 void
7804 pmap_dump(pmap_t pm)
7805 {
7806 struct l2_dtable *l2;
7807 struct l2_bucket *l2b;
7808 pt_entry_t *ptep, pte;
7809 vaddr_t l2_va, l2b_va, va;
7810 int i, j, k, occ, rows = 0;
7811
7812 if (pm == pmap_kernel())
7813 printf("pmap_kernel (%p): ", pm);
7814 else
7815 printf("user pmap (%p): ", pm);
7816
7817 #ifdef ARM_MMU_EXTENDED
7818 printf("l1 at %p\n", pmap_l1_kva(pm));
7819 #else
7820 printf("domain %d, l1 at %p\n", pmap_domain(pm), pmap_l1_kva(pm));
7821 #endif
7822
7823 l2_va = 0;
7824 for (i = 0; i < L2_SIZE; i++, l2_va += 0x01000000) {
7825 l2 = pm->pm_l2[i];
7826
7827 if (l2 == NULL || l2->l2_occupancy == 0)
7828 continue;
7829
7830 l2b_va = l2_va;
7831 for (j = 0; j < L2_BUCKET_SIZE; j++, l2b_va += 0x00100000) {
7832 l2b = &l2->l2_bucket[j];
7833
7834 if (l2b->l2b_occupancy == 0 || l2b->l2b_kva == NULL)
7835 continue;
7836
7837 ptep = l2b->l2b_kva;
7838
7839 for (k = 0; k < 256 && ptep[k] == 0; k++)
7840 ;
7841
7842 k &= ~63;
7843 occ = l2b->l2b_occupancy;
7844 va = l2b_va + (k * 4096);
7845 for (; k < 256; k++, va += 0x1000) {
7846 char ch = ' ';
7847 if ((k % 64) == 0) {
7848 if ((rows % 8) == 0) {
7849 printf(
7850 " |0000 |8000 |10000 |18000 |20000 |28000 |30000 |38000\n");
7851 }
7852 printf("%08lx: ", va);
7853 }
7854
7855 ncptes[k & 63] = 0;
7856 pte = ptep[k];
7857 if (pte == 0) {
7858 ch = '.';
7859 } else {
7860 occ--;
7861 switch (pte & 0x4c) {
7862 case 0x00:
7863 ch = 'N'; /* No cache No buff */
7864 break;
7865 case 0x04:
7866 ch = 'B'; /* No cache buff */
7867 break;
7868 case 0x08:
7869 ch = 'C'; /* Cache No buff */
7870 break;
7871 case 0x0c:
7872 ch = 'F'; /* Cache Buff */
7873 break;
7874 case 0x40:
7875 ch = 'D';
7876 break;
7877 case 0x48:
7878 ch = 'm'; /* Xscale mini-data */
7879 break;
7880 default:
7881 ch = '?';
7882 break;
7883 }
7884
7885 if ((pte & L2_S_PROT_U) == L2_S_PROT_U)
7886 ch += 0x20;
7887
7888 if ((pte & 0xc) == 0)
7889 ncptes[k & 63] = pte;
7890 }
7891
7892 if ((k % 64) == 63) {
7893 rows++;
7894 printf("%c\n", ch);
7895 pmap_dump_ncpg(pm);
7896 if (occ == 0)
7897 break;
7898 } else
7899 printf("%c", ch);
7900 }
7901 }
7902 }
7903 }
7904
7905 static void
7906 pmap_dump_ncpg(pmap_t pm)
7907 {
7908 struct vm_page *pg;
7909 struct vm_page_md *md;
7910 struct pv_entry *pv;
7911 int i;
7912
7913 for (i = 0; i < 63; i++) {
7914 if (ncptes[i] == 0)
7915 continue;
7916
7917 pg = PHYS_TO_VM_PAGE(l2pte_pa(ncptes[i]));
7918 if (pg == NULL)
7919 continue;
7920 md = VM_PAGE_TO_MD(pg);
7921
7922 printf(" pa 0x%08lx: krw %d kro %d urw %d uro %d\n",
7923 VM_PAGE_TO_PHYS(pg),
7924 md->krw_mappings, md->kro_mappings,
7925 md->urw_mappings, md->uro_mappings);
7926
7927 SLIST_FOREACH(pv, &md->pvh_list, pv_link) {
7928 printf(" %c va 0x%08lx, flags 0x%x\n",
7929 (pm == pv->pv_pmap) ? '*' : ' ',
7930 pv->pv_va, pv->pv_flags);
7931 }
7932 }
7933 }
7934 #endif
7935
7936 #ifdef PMAP_STEAL_MEMORY
7937 void
7938 pmap_boot_pageadd(pv_addr_t *newpv)
7939 {
7940 pv_addr_t *pv, *npv;
7941
7942 if ((pv = SLIST_FIRST(&pmap_boot_freeq)) != NULL) {
7943 if (newpv->pv_pa < pv->pv_va) {
7944 KASSERT(newpv->pv_pa + newpv->pv_size <= pv->pv_pa);
7945 if (newpv->pv_pa + newpv->pv_size == pv->pv_pa) {
7946 newpv->pv_size += pv->pv_size;
7947 SLIST_REMOVE_HEAD(&pmap_boot_freeq, pv_list);
7948 }
7949 pv = NULL;
7950 } else {
7951 for (; (npv = SLIST_NEXT(pv, pv_list)) != NULL;
7952 pv = npv) {
7953 KASSERT(pv->pv_pa + pv->pv_size < npv->pv_pa);
7954 KASSERT(pv->pv_pa < newpv->pv_pa);
7955 if (newpv->pv_pa > npv->pv_pa)
7956 continue;
7957 if (pv->pv_pa + pv->pv_size == newpv->pv_pa) {
7958 pv->pv_size += newpv->pv_size;
7959 return;
7960 }
7961 if (newpv->pv_pa + newpv->pv_size < npv->pv_pa)
7962 break;
7963 newpv->pv_size += npv->pv_size;
7964 SLIST_INSERT_AFTER(pv, newpv, pv_list);
7965 SLIST_REMOVE_AFTER(newpv, pv_list);
7966 return;
7967 }
7968 }
7969 }
7970
7971 if (pv) {
7972 SLIST_INSERT_AFTER(pv, newpv, pv_list);
7973 } else {
7974 SLIST_INSERT_HEAD(&pmap_boot_freeq, newpv, pv_list);
7975 }
7976 }
7977
7978 void
7979 pmap_boot_pagealloc(psize_t amount, psize_t mask, psize_t match,
7980 pv_addr_t *rpv)
7981 {
7982 pv_addr_t *pv, **pvp;
7983
7984 KASSERT(amount & PGOFSET);
7985 KASSERT((mask & PGOFSET) == 0);
7986 KASSERT((match & PGOFSET) == 0);
7987 KASSERT(amount != 0);
7988
7989 for (pvp = &SLIST_FIRST(&pmap_boot_freeq);
7990 (pv = *pvp) != NULL;
7991 pvp = &SLIST_NEXT(pv, pv_list)) {
7992 pv_addr_t *newpv;
7993 psize_t off;
7994 /*
7995 * If this entry is too small to satisfy the request...
7996 */
7997 KASSERT(pv->pv_size > 0);
7998 if (pv->pv_size < amount)
7999 continue;
8000
8001 for (off = 0; off <= mask; off += PAGE_SIZE) {
8002 if (((pv->pv_pa + off) & mask) == match
8003 && off + amount <= pv->pv_size)
8004 break;
8005 }
8006 if (off > mask)
8007 continue;
8008
8009 rpv->pv_va = pv->pv_va + off;
8010 rpv->pv_pa = pv->pv_pa + off;
8011 rpv->pv_size = amount;
8012 pv->pv_size -= amount;
8013 if (pv->pv_size == 0) {
8014 KASSERT(off == 0);
8015 KASSERT((vaddr_t) pv == rpv->pv_va);
8016 *pvp = SLIST_NEXT(pv, pv_list);
8017 } else if (off == 0) {
8018 KASSERT((vaddr_t) pv == rpv->pv_va);
8019 newpv = (pv_addr_t *) (rpv->pv_va + amount);
8020 *newpv = *pv;
8021 newpv->pv_pa += amount;
8022 newpv->pv_va += amount;
8023 *pvp = newpv;
8024 } else if (off < pv->pv_size) {
8025 newpv = (pv_addr_t *) (rpv->pv_va + amount);
8026 *newpv = *pv;
8027 newpv->pv_size -= off;
8028 newpv->pv_pa += off + amount;
8029 newpv->pv_va += off + amount;
8030
8031 SLIST_NEXT(pv, pv_list) = newpv;
8032 pv->pv_size = off;
8033 } else {
8034 KASSERT((vaddr_t) pv != rpv->pv_va);
8035 }
8036 memset((void *)rpv->pv_va, 0, amount);
8037 return;
8038 }
8039
8040 if (!uvm_physseg_valid_p(uvm_physseg_get_first()))
8041 panic("pmap_boot_pagealloc: couldn't allocate memory");
8042
8043 for (pvp = &SLIST_FIRST(&pmap_boot_freeq);
8044 (pv = *pvp) != NULL;
8045 pvp = &SLIST_NEXT(pv, pv_list)) {
8046 if (SLIST_NEXT(pv, pv_list) == NULL)
8047 break;
8048 }
8049 KASSERT(mask == 0);
8050
8051 for (uvm_physseg_t ups = uvm_physseg_get_first();
8052 uvm_physseg_valid_p(ups);
8053 ups = uvm_physseg_get_next(ups)) {
8054
8055 paddr_t spn = uvm_physseg_get_start(ups);
8056 paddr_t epn = uvm_physseg_get_end(ups);
8057 if (spn == atop(pv->pv_pa + pv->pv_size)
8058 && pv->pv_va + pv->pv_size <= ptoa(epn)) {
8059 rpv->pv_va = pv->pv_va;
8060 rpv->pv_pa = pv->pv_pa;
8061 rpv->pv_size = amount;
8062 *pvp = NULL;
8063 pmap_map_chunk(kernel_l1pt.pv_va,
8064 ptoa(spn) + (pv->pv_va - pv->pv_pa),
8065 ptoa(spn),
8066 amount - pv->pv_size,
8067 VM_PROT_READ|VM_PROT_WRITE,
8068 PTE_CACHE);
8069
8070 uvm_physseg_unplug(spn, atop(amount - pv->pv_size));
8071 memset((void *)rpv->pv_va, 0, rpv->pv_size);
8072 return;
8073 }
8074 }
8075
8076 panic("pmap_boot_pagealloc: couldn't allocate memory");
8077 }
8078
8079 vaddr_t
8080 pmap_steal_memory(vsize_t size, vaddr_t *vstartp, vaddr_t *vendp)
8081 {
8082 pv_addr_t pv;
8083
8084 pmap_boot_pagealloc(size, 0, 0, &pv);
8085
8086 return pv.pv_va;
8087 }
8088 #endif /* PMAP_STEAL_MEMORY */
8089
8090 SYSCTL_SETUP(sysctl_machdep_pmap_setup, "sysctl machdep.kmpages setup")
8091 {
8092 sysctl_createv(clog, 0, NULL, NULL,
8093 CTLFLAG_PERMANENT,
8094 CTLTYPE_NODE, "machdep", NULL,
8095 NULL, 0, NULL, 0,
8096 CTL_MACHDEP, CTL_EOL);
8097
8098 sysctl_createv(clog, 0, NULL, NULL,
8099 CTLFLAG_PERMANENT,
8100 CTLTYPE_INT, "kmpages",
8101 SYSCTL_DESCR("count of pages allocated to kernel memory allocators"),
8102 NULL, 0, &pmap_kmpages, 0,
8103 CTL_MACHDEP, CTL_CREATE, CTL_EOL);
8104 }
8105
8106 #ifdef PMAP_NEED_ALLOC_POOLPAGE
8107 struct vm_page *
8108 arm_pmap_alloc_poolpage(int flags)
8109 {
8110 /*
8111 * On some systems, only some pages may be "coherent" for dma and we
8112 * want to prefer those for pool pages (think mbufs) but fallback to
8113 * any page if none is available.
8114 */
8115 if (arm_poolpage_vmfreelist != VM_FREELIST_DEFAULT) {
8116 return uvm_pagealloc_strat(NULL, 0, NULL, flags,
8117 UVM_PGA_STRAT_FALLBACK, arm_poolpage_vmfreelist);
8118 }
8119
8120 return uvm_pagealloc(NULL, 0, NULL, flags);
8121 }
8122 #endif
8123
8124 #if defined(ARM_MMU_EXTENDED) && defined(MULTIPROCESSOR)
8125 void
8126 pmap_md_tlb_info_attach(struct pmap_tlb_info *ti, struct cpu_info *ci)
8127 {
8128 /* nothing */
8129 }
8130
8131 int
8132 pic_ipi_shootdown(void *arg)
8133 {
8134 #if PMAP_TLB_NEED_SHOOTDOWN
8135 pmap_tlb_shootdown_process();
8136 #endif
8137 return 1;
8138 }
8139 #endif /* ARM_MMU_EXTENDED && MULTIPROCESSOR */
8140
8141
8142 #ifdef __HAVE_MM_MD_DIRECT_MAPPED_PHYS
8143 vaddr_t
8144 pmap_direct_mapped_phys(paddr_t pa, bool *ok_p, vaddr_t va)
8145 {
8146 bool ok = false;
8147 if (physical_start <= pa && pa < physical_end) {
8148 #ifdef KERNEL_BASE_VOFFSET
8149 const vaddr_t newva = pa + KERNEL_BASE_VOFFSET;
8150 #else
8151 const vaddr_t newva = KERNEL_BASE + pa - physical_start;
8152 #endif
8153 #ifdef ARM_MMU_EXTENDED
8154 if (newva >= KERNEL_BASE && newva < pmap_directlimit) {
8155 #endif
8156 va = newva;
8157 ok = true;
8158 #ifdef ARM_MMU_EXTENDED
8159 }
8160 #endif
8161 }
8162 KASSERT(ok_p);
8163 *ok_p = ok;
8164 return va;
8165 }
8166
8167 vaddr_t
8168 pmap_map_poolpage(paddr_t pa)
8169 {
8170 bool ok __diagused;
8171 vaddr_t va = pmap_direct_mapped_phys(pa, &ok, 0);
8172 KASSERTMSG(ok, "pa %#lx not direct mappable", pa);
8173 #if defined(PMAP_CACHE_VIPT) && !defined(ARM_MMU_EXTENDED)
8174 if (arm_cache_prefer_mask != 0) {
8175 struct vm_page * const pg = PHYS_TO_VM_PAGE(pa);
8176 struct vm_page_md * const md = VM_PAGE_TO_MD(pg);
8177 pmap_acquire_page_lock(md);
8178 pmap_vac_me_harder(md, pa, pmap_kernel(), va);
8179 pmap_release_page_lock(md);
8180 }
8181 #endif
8182 return va;
8183 }
8184
8185 paddr_t
8186 pmap_unmap_poolpage(vaddr_t va)
8187 {
8188 KASSERT(va >= KERNEL_BASE);
8189 #ifdef PMAP_CACHE_VIVT
8190 cpu_idcache_wbinv_range(va, PAGE_SIZE);
8191 #endif
8192 #if defined(KERNEL_BASE_VOFFSET)
8193 return va - KERNEL_BASE_VOFFSET;
8194 #else
8195 return va - KERNEL_BASE + physical_start;
8196 #endif
8197 }
8198 #endif /* __HAVE_MM_MD_DIRECT_MAPPED_PHYS */
8199