pmap.c revision 1.398 1 /* $NetBSD: pmap.c,v 1.398 2020/03/13 16:25:19 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.398 2020/03/13 16:25:19 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 #endif /* !ARM_MMU_EXTENDED */
3402
3403 #ifndef ARM_MMU_EXTENDED
3404 UVMHIST_LOG(maphist, " is_cached %jd cs 0x%08jx",
3405 is_cached, pm->pm_cstate.cs_all, 0, 0);
3406
3407 if (pg != NULL) {
3408 struct vm_page_md *md = VM_PAGE_TO_MD(pg);
3409
3410 pmap_acquire_page_lock(md);
3411 pmap_vac_me_harder(md, pa, pm, va);
3412 pmap_release_page_lock(md);
3413 }
3414 #endif
3415 }
3416 #if defined(PMAP_CACHE_VIPT) && defined(DIAGNOSTIC)
3417 if (pg) {
3418 struct vm_page_md *md = VM_PAGE_TO_MD(pg);
3419
3420 pmap_acquire_page_lock(md);
3421 #ifndef ARM_MMU_EXTENDED
3422 KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC)));
3423 #endif
3424 PMAP_VALIDATE_MD_PAGE(md);
3425 pmap_release_page_lock(md);
3426 }
3427 #endif
3428
3429 pmap_release_pmap_lock(pm);
3430
3431
3432 if (old_pv)
3433 pool_put(&pmap_pv_pool, old_pv);
3434 free_pv:
3435 if (new_pv)
3436 pool_put(&pmap_pv_pool, new_pv);
3437 return error;
3438 }
3439
3440 /*
3441 * pmap_remove()
3442 *
3443 * pmap_remove is responsible for nuking a number of mappings for a range
3444 * of virtual address space in the current pmap. To do this efficiently
3445 * is interesting, because in a number of cases a wide virtual address
3446 * range may be supplied that contains few actual mappings. So, the
3447 * optimisations are:
3448 * 1. Skip over hunks of address space for which no L1 or L2 entry exists.
3449 * 2. Build up a list of pages we've hit, up to a maximum, so we can
3450 * maybe do just a partial cache clean. This path of execution is
3451 * complicated by the fact that the cache must be flushed _before_
3452 * the PTE is nuked, being a VAC :-)
3453 * 3. If we're called after UVM calls pmap_remove_all(), we can defer
3454 * all invalidations until pmap_update(), since pmap_remove_all() has
3455 * already flushed the cache.
3456 * 4. Maybe later fast-case a single page, but I don't think this is
3457 * going to make _that_ much difference overall.
3458 */
3459
3460 #define PMAP_REMOVE_CLEAN_LIST_SIZE 3
3461
3462 void
3463 pmap_remove(pmap_t pm, vaddr_t sva, vaddr_t eva)
3464 {
3465 SLIST_HEAD(,pv_entry) opv_list;
3466 struct pv_entry *pv, *npv;
3467 UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
3468 UVMHIST_LOG(maphist, " (pm=%#jx, sva=%#jx, eva=%#jx)",
3469 (uintptr_t)pm, sva, eva, 0);
3470
3471 SLIST_INIT(&opv_list);
3472 /*
3473 * we lock in the pmap => pv_head direction
3474 */
3475 pmap_acquire_pmap_lock(pm);
3476
3477 #ifndef ARM_MMU_EXTENDED
3478 u_int cleanlist_idx, total, cnt;
3479 struct {
3480 vaddr_t va;
3481 pt_entry_t *ptep;
3482 } cleanlist[PMAP_REMOVE_CLEAN_LIST_SIZE];
3483
3484 if (pm->pm_remove_all || !pmap_is_cached(pm)) {
3485 cleanlist_idx = PMAP_REMOVE_CLEAN_LIST_SIZE + 1;
3486 if (pm->pm_cstate.cs_tlb == 0)
3487 pm->pm_remove_all = true;
3488 } else
3489 cleanlist_idx = 0;
3490 total = 0;
3491 #endif
3492
3493 while (sva < eva) {
3494 /*
3495 * Do one L2 bucket's worth at a time.
3496 */
3497 vaddr_t next_bucket = L2_NEXT_BUCKET_VA(sva);
3498 if (next_bucket > eva)
3499 next_bucket = eva;
3500
3501 struct l2_bucket * const l2b = pmap_get_l2_bucket(pm, sva);
3502 if (l2b == NULL) {
3503 sva = next_bucket;
3504 continue;
3505 }
3506
3507 pt_entry_t *ptep = &l2b->l2b_kva[l2pte_index(sva)];
3508 u_int mappings = 0;
3509
3510 for (;sva < next_bucket;
3511 sva += PAGE_SIZE, ptep += PAGE_SIZE / L2_S_SIZE) {
3512 pt_entry_t opte = *ptep;
3513
3514 if (opte == 0) {
3515 /* Nothing here, move along */
3516 continue;
3517 }
3518
3519 u_int flags = PVF_REF;
3520 paddr_t pa = l2pte_pa(opte);
3521 struct vm_page * const pg = PHYS_TO_VM_PAGE(pa);
3522
3523 /*
3524 * Update flags. In a number of circumstances,
3525 * we could cluster a lot of these and do a
3526 * number of sequential pages in one go.
3527 */
3528 if (pg != NULL) {
3529 struct vm_page_md *md = VM_PAGE_TO_MD(pg);
3530
3531 pmap_acquire_page_lock(md);
3532 pv = pmap_remove_pv(md, pa, pm, sva);
3533 pmap_vac_me_harder(md, pa, pm, 0);
3534 pmap_release_page_lock(md);
3535 if (pv != NULL) {
3536 if (pm->pm_remove_all == false) {
3537 flags = pv->pv_flags;
3538 }
3539 SLIST_INSERT_HEAD(&opv_list,
3540 pv, pv_link);
3541 }
3542 }
3543 mappings += PAGE_SIZE / L2_S_SIZE;
3544
3545 if (!l2pte_valid_p(opte)) {
3546 /*
3547 * Ref/Mod emulation is still active for this
3548 * mapping, therefore it is has not yet been
3549 * accessed. No need to frob the cache/tlb.
3550 */
3551 l2pte_reset(ptep);
3552 PTE_SYNC_CURRENT(pm, ptep);
3553 continue;
3554 }
3555
3556 #ifdef ARM_MMU_EXTENDED
3557 l2pte_reset(ptep);
3558 PTE_SYNC(ptep);
3559 if (__predict_false(pm->pm_remove_all == false)) {
3560 pmap_tlb_flush_SE(pm, sva, flags);
3561 }
3562 #else
3563 if (cleanlist_idx < PMAP_REMOVE_CLEAN_LIST_SIZE) {
3564 /* Add to the clean list. */
3565 cleanlist[cleanlist_idx].ptep = ptep;
3566 cleanlist[cleanlist_idx].va =
3567 sva | (flags & PVF_EXEC);
3568 cleanlist_idx++;
3569 } else if (cleanlist_idx == PMAP_REMOVE_CLEAN_LIST_SIZE) {
3570 /* Nuke everything if needed. */
3571 #ifdef PMAP_CACHE_VIVT
3572 pmap_cache_wbinv_all(pm, PVF_EXEC);
3573 #endif
3574 /*
3575 * Roll back the previous PTE list,
3576 * and zero out the current PTE.
3577 */
3578 for (cnt = 0;
3579 cnt < PMAP_REMOVE_CLEAN_LIST_SIZE; cnt++) {
3580 l2pte_reset(cleanlist[cnt].ptep);
3581 PTE_SYNC(cleanlist[cnt].ptep);
3582 }
3583 l2pte_reset(ptep);
3584 PTE_SYNC(ptep);
3585 cleanlist_idx++;
3586 pm->pm_remove_all = true;
3587 } else {
3588 l2pte_reset(ptep);
3589 PTE_SYNC(ptep);
3590 if (pm->pm_remove_all == false) {
3591 pmap_tlb_flush_SE(pm, sva, flags);
3592 }
3593 }
3594 #endif
3595 }
3596
3597 #ifndef ARM_MMU_EXTENDED
3598 /*
3599 * Deal with any left overs
3600 */
3601 if (cleanlist_idx <= PMAP_REMOVE_CLEAN_LIST_SIZE) {
3602 total += cleanlist_idx;
3603 for (cnt = 0; cnt < cleanlist_idx; cnt++) {
3604 l2pte_reset(cleanlist[cnt].ptep);
3605 PTE_SYNC_CURRENT(pm, cleanlist[cnt].ptep);
3606 vaddr_t va = cleanlist[cnt].va;
3607 if (pm->pm_cstate.cs_all != 0) {
3608 vaddr_t clva = va & ~PAGE_MASK;
3609 u_int flags = va & PVF_EXEC;
3610 #ifdef PMAP_CACHE_VIVT
3611 pmap_cache_wbinv_page(pm, clva, true,
3612 PVF_REF | PVF_WRITE | flags);
3613 #endif
3614 pmap_tlb_flush_SE(pm, clva,
3615 PVF_REF | flags);
3616 }
3617 }
3618
3619 /*
3620 * If it looks like we're removing a whole bunch
3621 * of mappings, it's faster to just write-back
3622 * the whole cache now and defer TLB flushes until
3623 * pmap_update() is called.
3624 */
3625 if (total <= PMAP_REMOVE_CLEAN_LIST_SIZE)
3626 cleanlist_idx = 0;
3627 else {
3628 cleanlist_idx = PMAP_REMOVE_CLEAN_LIST_SIZE + 1;
3629 #ifdef PMAP_CACHE_VIVT
3630 pmap_cache_wbinv_all(pm, PVF_EXEC);
3631 #endif
3632 pm->pm_remove_all = true;
3633 }
3634 }
3635 #endif /* ARM_MMU_EXTENDED */
3636
3637 pmap_free_l2_bucket(pm, l2b, mappings);
3638 pm->pm_stats.resident_count -= mappings / (PAGE_SIZE/L2_S_SIZE);
3639 }
3640
3641 pmap_release_pmap_lock(pm);
3642 SLIST_FOREACH_SAFE(pv, &opv_list, pv_link, npv) {
3643 pool_put(&pmap_pv_pool, pv);
3644 }
3645 }
3646
3647 #if defined(PMAP_CACHE_VIPT) && !defined(ARM_MMU_EXTENDED)
3648 static struct pv_entry *
3649 pmap_kremove_pg(struct vm_page *pg, vaddr_t va)
3650 {
3651 struct vm_page_md *md = VM_PAGE_TO_MD(pg);
3652 paddr_t pa = VM_PAGE_TO_PHYS(pg);
3653 struct pv_entry *pv;
3654
3655 KASSERT(arm_cache_prefer_mask == 0 || md->pvh_attrs & (PVF_COLORED|PVF_NC));
3656 KASSERT((md->pvh_attrs & PVF_KMPAGE) == 0);
3657 KASSERT(pmap_page_locked_p(md));
3658
3659 pv = pmap_remove_pv(md, pa, pmap_kernel(), va);
3660 KASSERTMSG(pv, "pg %p (pa #%lx) va %#lx", pg, pa, va);
3661 KASSERT(PV_IS_KENTRY_P(pv->pv_flags));
3662
3663 /*
3664 * We are removing a writeable mapping to a cached exec page, if
3665 * it's the last mapping then clear its execness otherwise sync
3666 * the page to the icache.
3667 */
3668 if ((md->pvh_attrs & (PVF_NC|PVF_EXEC)) == PVF_EXEC
3669 && (pv->pv_flags & PVF_WRITE) != 0) {
3670 if (SLIST_EMPTY(&md->pvh_list)) {
3671 md->pvh_attrs &= ~PVF_EXEC;
3672 PMAPCOUNT(exec_discarded_kremove);
3673 } else {
3674 pmap_syncicache_page(md, pa);
3675 PMAPCOUNT(exec_synced_kremove);
3676 }
3677 }
3678 pmap_vac_me_harder(md, pa, pmap_kernel(), 0);
3679
3680 return pv;
3681 }
3682 #endif /* PMAP_CACHE_VIPT && !ARM_MMU_EXTENDED */
3683
3684 /*
3685 * pmap_kenter_pa: enter an unmanaged, wired kernel mapping
3686 *
3687 * We assume there is already sufficient KVM space available
3688 * to do this, as we can't allocate L2 descriptor tables/metadata
3689 * from here.
3690 */
3691 void
3692 pmap_kenter_pa(vaddr_t va, paddr_t pa, vm_prot_t prot, u_int flags)
3693 {
3694 #ifdef PMAP_CACHE_VIVT
3695 struct vm_page *pg = (flags & PMAP_KMPAGE) ? PHYS_TO_VM_PAGE(pa) : NULL;
3696 #endif
3697 #ifdef PMAP_CACHE_VIPT
3698 struct vm_page *pg = PHYS_TO_VM_PAGE(pa);
3699 struct vm_page *opg;
3700 #ifndef ARM_MMU_EXTENDED
3701 struct pv_entry *pv = NULL;
3702 #endif
3703 #endif
3704 struct vm_page_md *md = pg != NULL ? VM_PAGE_TO_MD(pg) : NULL;
3705
3706 UVMHIST_FUNC(__func__);
3707
3708 if (pmap_initialized) {
3709 UVMHIST_CALLED(maphist);
3710 UVMHIST_LOG(maphist, " (va=%#jx, pa=%#jx, prot=%#jx, flags=%#jx",
3711 va, pa, prot, flags);
3712 }
3713
3714 pmap_t kpm = pmap_kernel();
3715 pmap_acquire_pmap_lock(kpm);
3716 struct l2_bucket * const l2b = pmap_get_l2_bucket(kpm, va);
3717 const size_t l1slot __diagused = l1pte_index(va);
3718 KASSERTMSG(l2b != NULL,
3719 "va %#lx pa %#lx prot %d maxkvaddr %#lx: l2 %p l2b %p kva %p",
3720 va, pa, prot, pmap_curmaxkvaddr, kpm->pm_l2[L2_IDX(l1slot)],
3721 kpm->pm_l2[L2_IDX(l1slot)]
3722 ? &kpm->pm_l2[L2_IDX(l1slot)]->l2_bucket[L2_BUCKET(l1slot)]
3723 : NULL,
3724 kpm->pm_l2[L2_IDX(l1slot)]
3725 ? kpm->pm_l2[L2_IDX(l1slot)]->l2_bucket[L2_BUCKET(l1slot)].l2b_kva
3726 : NULL);
3727 KASSERT(l2b->l2b_kva != NULL);
3728
3729 pt_entry_t * const ptep = &l2b->l2b_kva[l2pte_index(va)];
3730 const pt_entry_t opte = *ptep;
3731
3732 if (opte == 0) {
3733 PMAPCOUNT(kenter_mappings);
3734 l2b->l2b_occupancy += PAGE_SIZE / L2_S_SIZE;
3735 } else {
3736 PMAPCOUNT(kenter_remappings);
3737 #ifdef PMAP_CACHE_VIPT
3738 opg = PHYS_TO_VM_PAGE(l2pte_pa(opte));
3739 #if !defined(ARM_MMU_EXTENDED) || defined(DIAGNOSTIC)
3740 struct vm_page_md *omd __diagused = VM_PAGE_TO_MD(opg);
3741 #endif
3742 if (opg && arm_cache_prefer_mask != 0) {
3743 KASSERT(opg != pg);
3744 KASSERT((omd->pvh_attrs & PVF_KMPAGE) == 0);
3745 KASSERT((flags & PMAP_KMPAGE) == 0);
3746 #ifndef ARM_MMU_EXTENDED
3747 pmap_acquire_page_lock(omd);
3748 pv = pmap_kremove_pg(opg, va);
3749 pmap_release_page_lock(omd);
3750 #endif
3751 }
3752 #endif
3753 if (l2pte_valid_p(opte)) {
3754 l2pte_reset(ptep);
3755 PTE_SYNC(ptep);
3756 #ifdef PMAP_CACHE_VIVT
3757 cpu_dcache_wbinv_range(va, PAGE_SIZE);
3758 #endif
3759 cpu_tlb_flushD_SE(va);
3760 cpu_cpwait();
3761 }
3762 }
3763 pmap_release_pmap_lock(kpm);
3764 pt_entry_t npte = L2_S_PROTO | pa | L2_S_PROT(PTE_KERNEL, prot);
3765
3766 if (flags & PMAP_PTE) {
3767 KASSERT((flags & PMAP_CACHE_MASK) == 0);
3768 if (!(flags & PMAP_NOCACHE))
3769 npte |= pte_l2_s_cache_mode_pt;
3770 } else {
3771 switch (flags & (PMAP_CACHE_MASK | PMAP_DEV_MASK)) {
3772 case PMAP_DEV ... PMAP_DEV | PMAP_CACHE_MASK:
3773 break;
3774 case PMAP_NOCACHE:
3775 npte |= pte_l2_s_nocache_mode;
3776 break;
3777 case PMAP_WRITE_COMBINE:
3778 npte |= pte_l2_s_wc_mode;
3779 break;
3780 default:
3781 npte |= pte_l2_s_cache_mode;
3782 break;
3783 }
3784 }
3785 #ifdef ARM_MMU_EXTENDED
3786 if (prot & VM_PROT_EXECUTE)
3787 npte &= ~L2_XS_XN;
3788 #endif
3789 l2pte_set(ptep, npte, 0);
3790 PTE_SYNC(ptep);
3791
3792 if (pg) {
3793 if (flags & PMAP_KMPAGE) {
3794 KASSERT(md->urw_mappings == 0);
3795 KASSERT(md->uro_mappings == 0);
3796 KASSERT(md->krw_mappings == 0);
3797 KASSERT(md->kro_mappings == 0);
3798 #if defined(PMAP_CACHE_VIPT) && !defined(ARM_MMU_EXTENDED)
3799 KASSERT(pv == NULL);
3800 KASSERT(arm_cache_prefer_mask == 0 || (va & PVF_COLORED) == 0);
3801 KASSERT((md->pvh_attrs & PVF_NC) == 0);
3802 /* if there is a color conflict, evict from cache. */
3803 if (pmap_is_page_colored_p(md)
3804 && ((va ^ md->pvh_attrs) & arm_cache_prefer_mask)) {
3805 PMAPCOUNT(vac_color_change);
3806 pmap_flush_page(md, pa, PMAP_FLUSH_PRIMARY);
3807 } else if (md->pvh_attrs & PVF_MULTCLR) {
3808 /*
3809 * If this page has multiple colors, expunge
3810 * them.
3811 */
3812 PMAPCOUNT(vac_flush_lots2);
3813 pmap_flush_page(md, pa, PMAP_FLUSH_SECONDARY);
3814 }
3815 /*
3816 * Since this is a KMPAGE, there can be no contention
3817 * for this page so don't lock it.
3818 */
3819 md->pvh_attrs &= PAGE_SIZE - 1;
3820 md->pvh_attrs |= PVF_KMPAGE | PVF_COLORED | PVF_DIRTY
3821 | (va & arm_cache_prefer_mask);
3822 #else /* !PMAP_CACHE_VIPT || ARM_MMU_EXTENDED */
3823 md->pvh_attrs |= PVF_KMPAGE;
3824 #endif
3825 atomic_inc_32(&pmap_kmpages);
3826 #if defined(PMAP_CACHE_VIPT) && !defined(ARM_MMU_EXTENDED)
3827 } else if (arm_cache_prefer_mask != 0) {
3828 if (pv == NULL) {
3829 pv = pool_get(&pmap_pv_pool, PR_NOWAIT);
3830 KASSERT(pv != NULL);
3831 }
3832 pmap_acquire_page_lock(md);
3833 pmap_enter_pv(md, pa, pv, pmap_kernel(), va,
3834 PVF_WIRED | PVF_KENTRY
3835 | (prot & VM_PROT_WRITE ? PVF_WRITE : 0));
3836 if ((prot & VM_PROT_WRITE)
3837 && !(md->pvh_attrs & PVF_NC))
3838 md->pvh_attrs |= PVF_DIRTY;
3839 KASSERT((prot & VM_PROT_WRITE) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC)));
3840 pmap_vac_me_harder(md, pa, pmap_kernel(), va);
3841 pmap_release_page_lock(md);
3842 #endif
3843 }
3844 #if defined(PMAP_CACHE_VIPT) && !defined(ARM_MMU_EXTENDED)
3845 } else {
3846 if (pv != NULL)
3847 pool_put(&pmap_pv_pool, pv);
3848 #endif
3849 }
3850 if (pmap_initialized) {
3851 UVMHIST_LOG(maphist, " <-- done (ptep %#jx: %#jx -> %#jx)",
3852 (uintptr_t)ptep, opte, npte, 0);
3853 }
3854
3855 }
3856
3857 void
3858 pmap_kremove(vaddr_t va, vsize_t len)
3859 {
3860 #ifdef UVMHIST
3861 u_int total_mappings = 0;
3862 #endif
3863
3864 PMAPCOUNT(kenter_unmappings);
3865
3866 UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
3867
3868 UVMHIST_LOG(maphist, " (va=%#jx, len=%#jx)", va, len, 0, 0);
3869
3870 const vaddr_t eva = va + len;
3871 pmap_t kpm = pmap_kernel();
3872
3873 pmap_acquire_pmap_lock(kpm);
3874
3875 while (va < eva) {
3876 vaddr_t next_bucket = L2_NEXT_BUCKET_VA(va);
3877 if (next_bucket > eva)
3878 next_bucket = eva;
3879
3880 struct l2_bucket * const l2b = pmap_get_l2_bucket(kpm, va);
3881 KDASSERT(l2b != NULL);
3882
3883 pt_entry_t * const sptep = &l2b->l2b_kva[l2pte_index(va)];
3884 pt_entry_t *ptep = sptep;
3885 u_int mappings = 0;
3886
3887 while (va < next_bucket) {
3888 const pt_entry_t opte = *ptep;
3889 struct vm_page *opg = PHYS_TO_VM_PAGE(l2pte_pa(opte));
3890 if (opg != NULL) {
3891 struct vm_page_md *omd = VM_PAGE_TO_MD(opg);
3892
3893 if (omd->pvh_attrs & PVF_KMPAGE) {
3894 KASSERT(omd->urw_mappings == 0);
3895 KASSERT(omd->uro_mappings == 0);
3896 KASSERT(omd->krw_mappings == 0);
3897 KASSERT(omd->kro_mappings == 0);
3898 omd->pvh_attrs &= ~PVF_KMPAGE;
3899 #if defined(PMAP_CACHE_VIPT) && !defined(ARM_MMU_EXTENDED)
3900 if (arm_cache_prefer_mask != 0) {
3901 omd->pvh_attrs &= ~PVF_WRITE;
3902 }
3903 #endif
3904 atomic_dec_32(&pmap_kmpages);
3905 #if defined(PMAP_CACHE_VIPT) && !defined(ARM_MMU_EXTENDED)
3906 } else if (arm_cache_prefer_mask != 0) {
3907 pmap_acquire_page_lock(omd);
3908 pool_put(&pmap_pv_pool,
3909 pmap_kremove_pg(opg, va));
3910 pmap_release_page_lock(omd);
3911 #endif
3912 }
3913 }
3914 if (l2pte_valid_p(opte)) {
3915 l2pte_reset(ptep);
3916 PTE_SYNC(ptep);
3917 #ifdef PMAP_CACHE_VIVT
3918 cpu_dcache_wbinv_range(va, PAGE_SIZE);
3919 #endif
3920 cpu_tlb_flushD_SE(va);
3921
3922 mappings += PAGE_SIZE / L2_S_SIZE;
3923 }
3924 va += PAGE_SIZE;
3925 ptep += PAGE_SIZE / L2_S_SIZE;
3926 }
3927 KDASSERTMSG(mappings <= l2b->l2b_occupancy, "%u %u",
3928 mappings, l2b->l2b_occupancy);
3929 l2b->l2b_occupancy -= mappings;
3930 //PTE_SYNC_RANGE(sptep, (u_int)(ptep - sptep));
3931 #ifdef UVMHIST
3932 total_mappings += mappings;
3933 #endif
3934 }
3935 pmap_release_pmap_lock(kpm);
3936 cpu_cpwait();
3937 UVMHIST_LOG(maphist, " <--- done (%ju mappings removed)",
3938 total_mappings, 0, 0, 0);
3939 }
3940
3941 bool
3942 pmap_extract(pmap_t pm, vaddr_t va, paddr_t *pap)
3943 {
3944
3945 return pmap_extract_coherency(pm, va, pap, NULL);
3946 }
3947
3948 bool
3949 pmap_extract_coherency(pmap_t pm, vaddr_t va, paddr_t *pap, bool *coherentp)
3950 {
3951 struct l2_dtable *l2;
3952 pd_entry_t *pdep, pde;
3953 pt_entry_t *ptep, pte;
3954 paddr_t pa;
3955 u_int l1slot;
3956 bool coherent;
3957
3958 pmap_acquire_pmap_lock(pm);
3959
3960 l1slot = l1pte_index(va);
3961 pdep = pmap_l1_kva(pm) + l1slot;
3962 pde = *pdep;
3963
3964 if (l1pte_section_p(pde)) {
3965 /*
3966 * These should only happen for pmap_kernel()
3967 */
3968 KDASSERT(pm == pmap_kernel());
3969 pmap_release_pmap_lock(pm);
3970 #if (ARM_MMU_V6 + ARM_MMU_V7) > 0
3971 if (l1pte_supersection_p(pde)) {
3972 pa = (pde & L1_SS_FRAME) | (va & L1_SS_OFFSET);
3973 } else
3974 #endif
3975 pa = (pde & L1_S_FRAME) | (va & L1_S_OFFSET);
3976 coherent = (pde & L1_S_CACHE_MASK) == 0;
3977 } else {
3978 /*
3979 * Note that we can't rely on the validity of the L1
3980 * descriptor as an indication that a mapping exists.
3981 * We have to look it up in the L2 dtable.
3982 */
3983 l2 = pm->pm_l2[L2_IDX(l1slot)];
3984
3985 if (l2 == NULL ||
3986 (ptep = l2->l2_bucket[L2_BUCKET(l1slot)].l2b_kva) == NULL) {
3987 pmap_release_pmap_lock(pm);
3988 return false;
3989 }
3990
3991 pte = ptep[l2pte_index(va)];
3992 pmap_release_pmap_lock(pm);
3993
3994 if (pte == 0)
3995 return false;
3996
3997 switch (pte & L2_TYPE_MASK) {
3998 case L2_TYPE_L:
3999 pa = (pte & L2_L_FRAME) | (va & L2_L_OFFSET);
4000 coherent = (pte & L2_L_CACHE_MASK) == 0;
4001 break;
4002
4003 default:
4004 pa = (pte & ~PAGE_MASK) | (va & PAGE_MASK);
4005 coherent = (pte & L2_S_CACHE_MASK) == 0;
4006 break;
4007 }
4008 }
4009
4010 if (pap != NULL)
4011 *pap = pa;
4012
4013 if (coherentp != NULL)
4014 *coherentp = (pm == pmap_kernel() && coherent);
4015
4016 return true;
4017 }
4018
4019 /*
4020 * pmap_pv_remove: remove an unmanaged pv-tracked page from all pmaps
4021 * that map it
4022 */
4023
4024 static void
4025 pmap_pv_remove(paddr_t pa)
4026 {
4027 struct pmap_page *pp;
4028
4029 pp = pmap_pv_tracked(pa);
4030 if (pp == NULL)
4031 panic("pmap_pv_protect: page not pv-tracked: 0x%"PRIxPADDR,
4032 pa);
4033
4034 struct vm_page_md *md = PMAP_PAGE_TO_MD(pp);
4035 pmap_page_remove(md, pa);
4036 }
4037
4038 void
4039 pmap_pv_protect(paddr_t pa, vm_prot_t prot)
4040 {
4041
4042 /* the only case is remove at the moment */
4043 KASSERT(prot == VM_PROT_NONE);
4044 pmap_pv_remove(pa);
4045 }
4046
4047 void
4048 pmap_protect(pmap_t pm, vaddr_t sva, vaddr_t eva, vm_prot_t prot)
4049 {
4050 struct l2_bucket *l2b;
4051 vaddr_t next_bucket;
4052
4053 NPDEBUG(PDB_PROTECT,
4054 printf("pmap_protect: pm %p sva 0x%lx eva 0x%lx prot 0x%x\n",
4055 pm, sva, eva, prot));
4056
4057 if ((prot & VM_PROT_READ) == 0) {
4058 pmap_remove(pm, sva, eva);
4059 return;
4060 }
4061
4062 if (prot & VM_PROT_WRITE) {
4063 /*
4064 * If this is a read->write transition, just ignore it and let
4065 * uvm_fault() take care of it later.
4066 */
4067 return;
4068 }
4069
4070 pmap_acquire_pmap_lock(pm);
4071
4072 #ifndef ARM_MMU_EXTENDED
4073 const bool flush = eva - sva >= PAGE_SIZE * 4;
4074 u_int flags = 0;
4075 #endif
4076 u_int clr_mask = PVF_WRITE | ((prot & VM_PROT_EXECUTE) ? 0 : PVF_EXEC);
4077
4078 while (sva < eva) {
4079 next_bucket = L2_NEXT_BUCKET_VA(sva);
4080 if (next_bucket > eva)
4081 next_bucket = eva;
4082
4083 l2b = pmap_get_l2_bucket(pm, sva);
4084 if (l2b == NULL) {
4085 sva = next_bucket;
4086 continue;
4087 }
4088
4089 pt_entry_t *ptep = &l2b->l2b_kva[l2pte_index(sva)];
4090
4091 while (sva < next_bucket) {
4092 const pt_entry_t opte = *ptep;
4093 if (l2pte_valid_p(opte) && l2pte_writable_p(opte)) {
4094 struct vm_page *pg;
4095 #ifndef ARM_MMU_EXTENDED
4096 u_int f;
4097 #endif
4098
4099 #ifdef PMAP_CACHE_VIVT
4100 /*
4101 * OK, at this point, we know we're doing
4102 * write-protect operation. If the pmap is
4103 * active, write-back the page.
4104 */
4105 pmap_cache_wbinv_page(pm, sva, false,
4106 PVF_REF | PVF_WRITE);
4107 #endif
4108
4109 pg = PHYS_TO_VM_PAGE(l2pte_pa(opte));
4110 pt_entry_t npte = l2pte_set_readonly(opte);
4111 l2pte_reset(ptep);
4112 PTE_SYNC(ptep);
4113 #ifdef ARM_MMU_EXTENDED
4114 pmap_tlb_flush_SE(pm, sva, PVF_REF);
4115 #endif
4116 l2pte_set(ptep, npte, 0);
4117 PTE_SYNC(ptep);
4118
4119 if (pg != NULL) {
4120 struct vm_page_md *md = VM_PAGE_TO_MD(pg);
4121 paddr_t pa = VM_PAGE_TO_PHYS(pg);
4122
4123 pmap_acquire_page_lock(md);
4124 #ifndef ARM_MMU_EXTENDED
4125 f =
4126 #endif
4127 pmap_modify_pv(md, pa, pm, sva,
4128 clr_mask, 0);
4129 pmap_vac_me_harder(md, pa, pm, sva);
4130 pmap_release_page_lock(md);
4131 #ifndef ARM_MMU_EXTENDED
4132 } else {
4133 f = PVF_REF | PVF_EXEC;
4134 }
4135
4136 if (flush) {
4137 flags |= f;
4138 } else {
4139 pmap_tlb_flush_SE(pm, sva, f);
4140 #endif
4141 }
4142 }
4143
4144 sva += PAGE_SIZE;
4145 ptep += PAGE_SIZE / L2_S_SIZE;
4146 }
4147 }
4148
4149 #ifndef ARM_MMU_EXTENDED
4150 if (flush) {
4151 if (PV_BEEN_EXECD(flags)) {
4152 pmap_tlb_flushID(pm);
4153 } else if (PV_BEEN_REFD(flags)) {
4154 pmap_tlb_flushD(pm);
4155 }
4156 }
4157 #endif
4158
4159 pmap_release_pmap_lock(pm);
4160 }
4161
4162 void
4163 pmap_icache_sync_range(pmap_t pm, vaddr_t sva, vaddr_t eva)
4164 {
4165 struct l2_bucket *l2b;
4166 pt_entry_t *ptep;
4167 vaddr_t next_bucket;
4168 vsize_t page_size = trunc_page(sva) + PAGE_SIZE - sva;
4169
4170 NPDEBUG(PDB_EXEC,
4171 printf("pmap_icache_sync_range: pm %p sva 0x%lx eva 0x%lx\n",
4172 pm, sva, eva));
4173
4174 pmap_acquire_pmap_lock(pm);
4175
4176 while (sva < eva) {
4177 next_bucket = L2_NEXT_BUCKET_VA(sva);
4178 if (next_bucket > eva)
4179 next_bucket = eva;
4180
4181 l2b = pmap_get_l2_bucket(pm, sva);
4182 if (l2b == NULL) {
4183 sva = next_bucket;
4184 continue;
4185 }
4186
4187 for (ptep = &l2b->l2b_kva[l2pte_index(sva)];
4188 sva < next_bucket;
4189 sva += page_size,
4190 ptep += PAGE_SIZE / L2_S_SIZE,
4191 page_size = PAGE_SIZE) {
4192 if (l2pte_valid_p(*ptep)) {
4193 cpu_icache_sync_range(sva,
4194 uimin(page_size, eva - sva));
4195 }
4196 }
4197 }
4198
4199 pmap_release_pmap_lock(pm);
4200 }
4201
4202 void
4203 pmap_page_protect(struct vm_page *pg, vm_prot_t prot)
4204 {
4205 struct vm_page_md *md = VM_PAGE_TO_MD(pg);
4206 paddr_t pa = VM_PAGE_TO_PHYS(pg);
4207
4208 NPDEBUG(PDB_PROTECT,
4209 printf("pmap_page_protect: md %p (0x%08lx), prot 0x%x\n",
4210 md, pa, prot));
4211
4212 switch(prot) {
4213 case VM_PROT_READ|VM_PROT_WRITE:
4214 #if defined(ARM_MMU_EXTENDED)
4215 pmap_acquire_page_lock(md);
4216 pmap_clearbit(md, pa, PVF_EXEC);
4217 pmap_release_page_lock(md);
4218 break;
4219 #endif
4220 case VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE:
4221 break;
4222
4223 case VM_PROT_READ:
4224 #if defined(ARM_MMU_EXTENDED)
4225 pmap_acquire_page_lock(md);
4226 pmap_clearbit(md, pa, PVF_WRITE|PVF_EXEC);
4227 pmap_release_page_lock(md);
4228 break;
4229 #endif
4230 case VM_PROT_READ|VM_PROT_EXECUTE:
4231 pmap_acquire_page_lock(md);
4232 pmap_clearbit(md, pa, PVF_WRITE);
4233 pmap_release_page_lock(md);
4234 break;
4235
4236 default:
4237 pmap_page_remove(md, pa);
4238 break;
4239 }
4240 }
4241
4242 /*
4243 * pmap_clear_modify:
4244 *
4245 * Clear the "modified" attribute for a page.
4246 */
4247 bool
4248 pmap_clear_modify(struct vm_page *pg)
4249 {
4250 struct vm_page_md *md = VM_PAGE_TO_MD(pg);
4251 paddr_t pa = VM_PAGE_TO_PHYS(pg);
4252 bool rv;
4253
4254 pmap_acquire_page_lock(md);
4255
4256 if (md->pvh_attrs & PVF_MOD) {
4257 rv = true;
4258 #if defined(PMAP_CACHE_VIPT) && !defined(ARM_MMU_EXTENDED)
4259 /*
4260 * If we are going to clear the modified bit and there are
4261 * no other modified bits set, flush the page to memory and
4262 * mark it clean.
4263 */
4264 if ((md->pvh_attrs & (PVF_DMOD|PVF_NC)) == PVF_MOD)
4265 pmap_flush_page(md, pa, PMAP_CLEAN_PRIMARY);
4266 #endif
4267 pmap_clearbit(md, pa, PVF_MOD);
4268 } else {
4269 rv = false;
4270 }
4271 pmap_release_page_lock(md);
4272
4273 return rv;
4274 }
4275
4276 /*
4277 * pmap_clear_reference:
4278 *
4279 * Clear the "referenced" attribute for a page.
4280 */
4281 bool
4282 pmap_clear_reference(struct vm_page *pg)
4283 {
4284 struct vm_page_md *md = VM_PAGE_TO_MD(pg);
4285 paddr_t pa = VM_PAGE_TO_PHYS(pg);
4286 bool rv;
4287
4288 pmap_acquire_page_lock(md);
4289
4290 if (md->pvh_attrs & PVF_REF) {
4291 rv = true;
4292 pmap_clearbit(md, pa, PVF_REF);
4293 } else {
4294 rv = false;
4295 }
4296 pmap_release_page_lock(md);
4297
4298 return rv;
4299 }
4300
4301 /*
4302 * pmap_is_modified:
4303 *
4304 * Test if a page has the "modified" attribute.
4305 */
4306 /* See <arm/arm32/pmap.h> */
4307
4308 /*
4309 * pmap_is_referenced:
4310 *
4311 * Test if a page has the "referenced" attribute.
4312 */
4313 /* See <arm/arm32/pmap.h> */
4314
4315 #if defined(ARM_MMU_EXTENDED) && 0
4316 int
4317 pmap_prefetchabt_fixup(void *v)
4318 {
4319 struct trapframe * const tf = v;
4320 vaddr_t va = trunc_page(tf->tf_pc);
4321 int rv = ABORT_FIXUP_FAILED;
4322
4323 if (!TRAP_USERMODE(tf) && va < VM_MAXUSER_ADDRESS)
4324 return rv;
4325
4326 kpreempt_disable();
4327 pmap_t pm = curcpu()->ci_pmap_cur;
4328 const size_t l1slot = l1pte_index(va);
4329 struct l2_dtable * const l2 = pm->pm_l2[L2_IDX(l1slot)];
4330 if (l2 == NULL)
4331 goto out;
4332
4333 struct l2_bucket * const l2b = &l2->l2_bucket[L2_BUCKET(l1slot)];
4334 if (l2b->l2b_kva == NULL)
4335 goto out;
4336
4337 /*
4338 * Check the PTE itself.
4339 */
4340 pt_entry_t * const ptep = &l2b->l2b_kva[l2pte_index(va)];
4341 const pt_entry_t opte = *ptep;
4342 if ((opte & L2_S_PROT_U) == 0 || (opte & L2_XS_XN) == 0)
4343 goto out;
4344
4345 paddr_t pa = l2pte_pa(opte);
4346 struct vm_page * const pg = PHYS_TO_VM_PAGE(pa);
4347 KASSERT(pg != NULL);
4348
4349 struct vm_page_md * const md = VM_PAGE_TO_MD(pg);
4350
4351 pmap_acquire_page_lock(md);
4352 struct pv_entry * const pv = pmap_find_pv(md, pm, va);
4353 KASSERT(pv != NULL);
4354
4355 if (PV_IS_EXEC_P(pv->pv_flags)) {
4356 l2pte_reset(ptep);
4357 PTE_SYNC(ptep);
4358 pmap_tlb_flush_SE(pm, va, PVF_EXEC | PVF_REF);
4359 if (!PV_IS_EXEC_P(md->pvh_attrs)) {
4360 pmap_syncicache_page(md, pa);
4361 }
4362 rv = ABORT_FIXUP_RETURN;
4363 l2pte_set(ptep, opte & ~L2_XS_XN, 0);
4364 PTE_SYNC(ptep);
4365 }
4366 pmap_release_page_lock(md);
4367
4368 out:
4369 kpreempt_enable();
4370 return rv;
4371 }
4372 #endif
4373
4374 int
4375 pmap_fault_fixup(pmap_t pm, vaddr_t va, vm_prot_t ftype, int user)
4376 {
4377 struct l2_dtable *l2;
4378 struct l2_bucket *l2b;
4379 paddr_t pa;
4380 const size_t l1slot = l1pte_index(va);
4381 int rv = 0;
4382
4383 UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
4384
4385 va = trunc_page(va);
4386
4387 KASSERT(!user || (pm != pmap_kernel()));
4388
4389 UVMHIST_LOG(maphist, " (pm=%#jx, va=%#jx, ftype=%#jx, user=%jd)",
4390 (uintptr_t)pm, va, ftype, user);
4391 #ifdef ARM_MMU_EXTENDED
4392 UVMHIST_LOG(maphist, " ti=%#jx pai=%#jx asid=%#jx",
4393 (uintptr_t)cpu_tlb_info(curcpu()),
4394 (uintptr_t)PMAP_PAI(pm, cpu_tlb_info(curcpu())),
4395 (uintptr_t)PMAP_PAI(pm, cpu_tlb_info(curcpu()))->pai_asid, 0);
4396 #endif
4397
4398 pmap_acquire_pmap_lock(pm);
4399
4400 /*
4401 * If there is no l2_dtable for this address, then the process
4402 * has no business accessing it.
4403 *
4404 * Note: This will catch userland processes trying to access
4405 * kernel addresses.
4406 */
4407 l2 = pm->pm_l2[L2_IDX(l1slot)];
4408 if (l2 == NULL) {
4409 UVMHIST_LOG(maphist, " no l2 for l1slot %#jx", l1slot, 0, 0, 0);
4410 goto out;
4411 }
4412
4413 /*
4414 * Likewise if there is no L2 descriptor table
4415 */
4416 l2b = &l2->l2_bucket[L2_BUCKET(l1slot)];
4417 if (l2b->l2b_kva == NULL) {
4418 UVMHIST_LOG(maphist, " <-- done (no ptep for l1slot %#jx)",
4419 l1slot, 0, 0, 0);
4420 goto out;
4421 }
4422
4423 /*
4424 * Check the PTE itself.
4425 */
4426 pt_entry_t * const ptep = &l2b->l2b_kva[l2pte_index(va)];
4427 pt_entry_t const opte = *ptep;
4428 if (opte == 0 || (opte & L2_TYPE_MASK) == L2_TYPE_L) {
4429 UVMHIST_LOG(maphist, " <-- done (empty pde for l1slot %#jx)",
4430 l1slot, 0, 0, 0);
4431 goto out;
4432 }
4433
4434 #ifndef ARM_HAS_VBAR
4435 /*
4436 * Catch a userland access to the vector page mapped at 0x0
4437 */
4438 if (user && (opte & L2_S_PROT_U) == 0) {
4439 UVMHIST_LOG(maphist, " <-- done (vector_page)", 0, 0, 0, 0);
4440 goto out;
4441 }
4442 #endif
4443
4444 pa = l2pte_pa(opte);
4445
4446 if ((ftype & VM_PROT_WRITE) && !l2pte_writable_p(opte)) {
4447 /*
4448 * This looks like a good candidate for "page modified"
4449 * emulation...
4450 */
4451 struct pv_entry *pv;
4452 struct vm_page *pg;
4453
4454 /* Extract the physical address of the page */
4455 if ((pg = PHYS_TO_VM_PAGE(pa)) == NULL) {
4456 UVMHIST_LOG(maphist, " <-- done (mod/ref unmanaged page)", 0, 0, 0, 0);
4457 goto out;
4458 }
4459
4460 struct vm_page_md *md = VM_PAGE_TO_MD(pg);
4461
4462 /* Get the current flags for this page. */
4463 pmap_acquire_page_lock(md);
4464 pv = pmap_find_pv(md, pm, va);
4465 if (pv == NULL || PV_IS_KENTRY_P(pv->pv_flags)) {
4466 pmap_release_page_lock(md);
4467 UVMHIST_LOG(maphist, " <-- done (mod/ref emul: no PV)", 0, 0, 0, 0);
4468 goto out;
4469 }
4470
4471 /*
4472 * Do the flags say this page is writable? If not then it
4473 * is a genuine write fault. If yes then the write fault is
4474 * our fault as we did not reflect the write access in the
4475 * PTE. Now we know a write has occurred we can correct this
4476 * and also set the modified bit
4477 */
4478 if ((pv->pv_flags & PVF_WRITE) == 0) {
4479 pmap_release_page_lock(md);
4480 goto out;
4481 }
4482
4483 md->pvh_attrs |= PVF_REF | PVF_MOD;
4484 pv->pv_flags |= PVF_REF | PVF_MOD;
4485 #if defined(PMAP_CACHE_VIPT) && !defined(ARM_MMU_EXTENDED)
4486 /*
4487 * If there are cacheable mappings for this page, mark it dirty.
4488 */
4489 if ((md->pvh_attrs & PVF_NC) == 0)
4490 md->pvh_attrs |= PVF_DIRTY;
4491 #endif
4492 #ifdef ARM_MMU_EXTENDED
4493 if (md->pvh_attrs & PVF_EXEC) {
4494 md->pvh_attrs &= ~PVF_EXEC;
4495 PMAPCOUNT(exec_discarded_modfixup);
4496 }
4497 #endif
4498 pmap_release_page_lock(md);
4499
4500 /*
4501 * Re-enable write permissions for the page. No need to call
4502 * pmap_vac_me_harder(), since this is just a
4503 * modified-emulation fault, and the PVF_WRITE bit isn't
4504 * changing. We've already set the cacheable bits based on
4505 * the assumption that we can write to this page.
4506 */
4507 const pt_entry_t npte =
4508 l2pte_set_writable((opte & ~L2_TYPE_MASK) | L2_S_PROTO)
4509 #ifdef ARM_MMU_EXTENDED
4510 | (pm != pmap_kernel() ? L2_XS_nG : 0)
4511 #endif
4512 | 0;
4513 l2pte_reset(ptep);
4514 PTE_SYNC(ptep);
4515 pmap_tlb_flush_SE(pm, va,
4516 (ftype & VM_PROT_EXECUTE) ? PVF_EXEC | PVF_REF : PVF_REF);
4517 l2pte_set(ptep, npte, 0);
4518 PTE_SYNC(ptep);
4519 PMAPCOUNT(fixup_mod);
4520 rv = 1;
4521 UVMHIST_LOG(maphist, " <-- done (mod/ref emul: changed pte "
4522 "from %#jx to %#jx)", opte, npte, 0, 0);
4523 } else if ((opte & L2_TYPE_MASK) == L2_TYPE_INV) {
4524 /*
4525 * This looks like a good candidate for "page referenced"
4526 * emulation.
4527 */
4528 struct vm_page *pg;
4529
4530 /* Extract the physical address of the page */
4531 if ((pg = PHYS_TO_VM_PAGE(pa)) == NULL) {
4532 UVMHIST_LOG(maphist, " <-- done (ref emul: unmanaged page)", 0, 0, 0, 0);
4533 goto out;
4534 }
4535
4536 struct vm_page_md *md = VM_PAGE_TO_MD(pg);
4537
4538 /* Get the current flags for this page. */
4539 pmap_acquire_page_lock(md);
4540 struct pv_entry *pv = pmap_find_pv(md, pm, va);
4541 if (pv == NULL || PV_IS_KENTRY_P(pv->pv_flags)) {
4542 pmap_release_page_lock(md);
4543 UVMHIST_LOG(maphist, " <-- done (ref emul no PV)", 0, 0, 0, 0);
4544 goto out;
4545 }
4546
4547 md->pvh_attrs |= PVF_REF;
4548 pv->pv_flags |= PVF_REF;
4549
4550 pt_entry_t npte =
4551 l2pte_set_readonly((opte & ~L2_TYPE_MASK) | L2_S_PROTO);
4552 #ifdef ARM_MMU_EXTENDED
4553 if (pm != pmap_kernel()) {
4554 npte |= L2_XS_nG;
4555 }
4556 /*
4557 * If we got called from prefetch abort, then ftype will have
4558 * VM_PROT_EXECUTE set. Now see if we have no-execute set in
4559 * the PTE.
4560 */
4561 if (user && (ftype & VM_PROT_EXECUTE) && (npte & L2_XS_XN)) {
4562 /*
4563 * Is this a mapping of an executable page?
4564 */
4565 if ((pv->pv_flags & PVF_EXEC) == 0) {
4566 pmap_release_page_lock(md);
4567 UVMHIST_LOG(maphist, " <-- done (ref emul: no exec)",
4568 0, 0, 0, 0);
4569 goto out;
4570 }
4571 /*
4572 * If we haven't synced the page, do so now.
4573 */
4574 if ((md->pvh_attrs & PVF_EXEC) == 0) {
4575 UVMHIST_LOG(maphist, " ref emul: syncicache "
4576 "page #%#jx", pa, 0, 0, 0);
4577 pmap_syncicache_page(md, pa);
4578 PMAPCOUNT(fixup_exec);
4579 }
4580 npte &= ~L2_XS_XN;
4581 }
4582 #endif /* ARM_MMU_EXTENDED */
4583 pmap_release_page_lock(md);
4584 l2pte_reset(ptep);
4585 PTE_SYNC(ptep);
4586 pmap_tlb_flush_SE(pm, va,
4587 (ftype & VM_PROT_EXECUTE) ? PVF_EXEC | PVF_REF : PVF_REF);
4588 l2pte_set(ptep, npte, 0);
4589 PTE_SYNC(ptep);
4590 PMAPCOUNT(fixup_ref);
4591 rv = 1;
4592 UVMHIST_LOG(maphist, " <-- done (ref emul: changed pte from "
4593 "%#jx to %#jx)", opte, npte, 0, 0);
4594 #ifdef ARM_MMU_EXTENDED
4595 } else if (user && (ftype & VM_PROT_EXECUTE) && (opte & L2_XS_XN)) {
4596 struct vm_page * const pg = PHYS_TO_VM_PAGE(pa);
4597 if (pg == NULL) {
4598 UVMHIST_LOG(maphist, " <-- done (unmanaged page)", 0, 0, 0, 0);
4599 goto out;
4600 }
4601
4602 struct vm_page_md * const md = VM_PAGE_TO_MD(pg);
4603
4604 /* Get the current flags for this page. */
4605 pmap_acquire_page_lock(md);
4606 struct pv_entry * const pv = pmap_find_pv(md, pm, va);
4607 if (pv == NULL || (pv->pv_flags & PVF_EXEC) == 0) {
4608 pmap_release_page_lock(md);
4609 UVMHIST_LOG(maphist, " <-- done (no PV or not EXEC)", 0, 0, 0, 0);
4610 goto out;
4611 }
4612
4613 /*
4614 * If we haven't synced the page, do so now.
4615 */
4616 if ((md->pvh_attrs & PVF_EXEC) == 0) {
4617 UVMHIST_LOG(maphist, "syncicache page #%#jx",
4618 pa, 0, 0, 0);
4619 pmap_syncicache_page(md, pa);
4620 }
4621 pmap_release_page_lock(md);
4622 /*
4623 * Turn off no-execute.
4624 */
4625 KASSERT(opte & L2_XS_nG);
4626 l2pte_reset(ptep);
4627 PTE_SYNC(ptep);
4628 pmap_tlb_flush_SE(pm, va, PVF_EXEC | PVF_REF);
4629 l2pte_set(ptep, opte & ~L2_XS_XN, 0);
4630 PTE_SYNC(ptep);
4631 rv = 1;
4632 PMAPCOUNT(fixup_exec);
4633 UVMHIST_LOG(maphist, "exec: changed pte from %#jx to %#jx",
4634 opte, opte & ~L2_XS_XN, 0, 0);
4635 #endif
4636 }
4637
4638 #ifndef ARM_MMU_EXTENDED
4639 /*
4640 * We know there is a valid mapping here, so simply
4641 * fix up the L1 if necessary.
4642 */
4643 pd_entry_t * const pdep = pmap_l1_kva(pm) + l1slot;
4644 pd_entry_t pde = L1_C_PROTO | l2b->l2b_pa | L1_C_DOM(pmap_domain(pm));
4645 if (*pdep != pde) {
4646 l1pte_setone(pdep, pde);
4647 PDE_SYNC(pdep);
4648 rv = 1;
4649 PMAPCOUNT(fixup_pdes);
4650 }
4651 #endif
4652
4653 #ifdef CPU_SA110
4654 /*
4655 * There are bugs in the rev K SA110. This is a check for one
4656 * of them.
4657 */
4658 if (rv == 0 && curcpu()->ci_arm_cputype == CPU_ID_SA110 &&
4659 curcpu()->ci_arm_cpurev < 3) {
4660 /* Always current pmap */
4661 if (l2pte_valid_p(opte)) {
4662 extern int kernel_debug;
4663 if (kernel_debug & 1) {
4664 struct proc *p = curlwp->l_proc;
4665 printf("prefetch_abort: page is already "
4666 "mapped - pte=%p *pte=%08x\n", ptep, opte);
4667 printf("prefetch_abort: pc=%08lx proc=%p "
4668 "process=%s\n", va, p, p->p_comm);
4669 printf("prefetch_abort: far=%08x fs=%x\n",
4670 cpu_faultaddress(), cpu_faultstatus());
4671 }
4672 #ifdef DDB
4673 if (kernel_debug & 2)
4674 Debugger();
4675 #endif
4676 rv = 1;
4677 }
4678 }
4679 #endif /* CPU_SA110 */
4680
4681 #ifndef ARM_MMU_EXTENDED
4682 /*
4683 * If 'rv == 0' at this point, it generally indicates that there is a
4684 * stale TLB entry for the faulting address. That might be due to a
4685 * wrong setting of pmap_needs_pte_sync. So set it and retry.
4686 */
4687 if (rv == 0
4688 && pm->pm_l1->l1_domain_use_count == 1
4689 && pmap_needs_pte_sync == 0) {
4690 pmap_needs_pte_sync = 1;
4691 PTE_SYNC(ptep);
4692 PMAPCOUNT(fixup_ptesync);
4693 rv = 1;
4694 }
4695 #endif
4696
4697 #ifndef MULTIPROCESSOR
4698 #if defined(DEBUG) || 1
4699 /*
4700 * If 'rv == 0' at this point, it generally indicates that there is a
4701 * stale TLB entry for the faulting address. This happens when two or
4702 * more processes are sharing an L1. Since we don't flush the TLB on
4703 * a context switch between such processes, we can take domain faults
4704 * for mappings which exist at the same VA in both processes. EVEN IF
4705 * WE'VE RECENTLY FIXED UP THE CORRESPONDING L1 in pmap_enter(), for
4706 * example.
4707 *
4708 * This is extremely likely to happen if pmap_enter() updated the L1
4709 * entry for a recently entered mapping. In this case, the TLB is
4710 * flushed for the new mapping, but there may still be TLB entries for
4711 * other mappings belonging to other processes in the 1MB range
4712 * covered by the L1 entry.
4713 *
4714 * Since 'rv == 0', we know that the L1 already contains the correct
4715 * value, so the fault must be due to a stale TLB entry.
4716 *
4717 * Since we always need to flush the TLB anyway in the case where we
4718 * fixed up the L1, or frobbed the L2 PTE, we effectively deal with
4719 * stale TLB entries dynamically.
4720 *
4721 * However, the above condition can ONLY happen if the current L1 is
4722 * being shared. If it happens when the L1 is unshared, it indicates
4723 * that other parts of the pmap are not doing their job WRT managing
4724 * the TLB.
4725 */
4726 if (rv == 0
4727 #ifndef ARM_MMU_EXTENDED
4728 && pm->pm_l1->l1_domain_use_count == 1
4729 #endif
4730 && true) {
4731 #ifdef DEBUG
4732 extern int last_fault_code;
4733 #else
4734 int last_fault_code = ftype & VM_PROT_EXECUTE
4735 ? armreg_ifsr_read()
4736 : armreg_dfsr_read();
4737 #endif
4738 printf("fixup: pm %p, va 0x%lx, ftype %d - nothing to do!\n",
4739 pm, va, ftype);
4740 printf("fixup: l2 %p, l2b %p, ptep %p, pte %#x\n",
4741 l2, l2b, ptep, opte);
4742
4743 #ifndef ARM_MMU_EXTENDED
4744 printf("fixup: pdep %p, pde %#x, fsr %#x\n",
4745 pdep, pde, last_fault_code);
4746 #else
4747 printf("fixup: pdep %p, pde %#x, ttbcr %#x\n",
4748 &pmap_l1_kva(pm)[l1slot], pmap_l1_kva(pm)[l1slot],
4749 armreg_ttbcr_read());
4750 printf("fixup: fsr %#x cpm %p casid %#x contextidr %#x dacr %#x\n",
4751 last_fault_code, curcpu()->ci_pmap_cur,
4752 curcpu()->ci_pmap_asid_cur,
4753 armreg_contextidr_read(), armreg_dacr_read());
4754 #ifdef _ARM_ARCH_7
4755 if (ftype & VM_PROT_WRITE)
4756 armreg_ats1cuw_write(va);
4757 else
4758 armreg_ats1cur_write(va);
4759 arm_isb();
4760 printf("fixup: par %#x\n", armreg_par_read());
4761 #endif
4762 #endif
4763 #ifdef DDB
4764 extern int kernel_debug;
4765
4766 if (kernel_debug & 2) {
4767 pmap_release_pmap_lock(pm);
4768 #ifdef UVMHIST
4769 KERNHIST_DUMP(maphist);
4770 #endif
4771 cpu_Debugger();
4772 pmap_acquire_pmap_lock(pm);
4773 }
4774 #endif
4775 }
4776 #endif
4777 #endif
4778
4779 #ifndef ARM_MMU_EXTENDED
4780 /* Flush the TLB in the shared L1 case - see comment above */
4781 pmap_tlb_flush_SE(pm, va,
4782 (ftype & VM_PROT_EXECUTE) ? PVF_EXEC | PVF_REF : PVF_REF);
4783 #endif
4784
4785 rv = 1;
4786
4787 out:
4788 pmap_release_pmap_lock(pm);
4789
4790 return rv;
4791 }
4792
4793 /*
4794 * Routine: pmap_procwr
4795 *
4796 * Function:
4797 * Synchronize caches corresponding to [addr, addr+len) in p.
4798 *
4799 */
4800 void
4801 pmap_procwr(struct proc *p, vaddr_t va, int len)
4802 {
4803 #ifndef ARM_MMU_EXTENDED
4804
4805 /* We only need to do anything if it is the current process. */
4806 if (p == curproc)
4807 cpu_icache_sync_range(va, len);
4808 #endif
4809 }
4810
4811 /*
4812 * Routine: pmap_unwire
4813 * Function: Clear the wired attribute for a map/virtual-address pair.
4814 *
4815 * In/out conditions:
4816 * The mapping must already exist in the pmap.
4817 */
4818 void
4819 pmap_unwire(pmap_t pm, vaddr_t va)
4820 {
4821 struct l2_bucket *l2b;
4822 pt_entry_t *ptep, pte;
4823 struct vm_page *pg;
4824 paddr_t pa;
4825
4826 NPDEBUG(PDB_WIRING, printf("pmap_unwire: pm %p, va 0x%08lx\n", pm, va));
4827
4828 pmap_acquire_pmap_lock(pm);
4829
4830 l2b = pmap_get_l2_bucket(pm, va);
4831 KDASSERT(l2b != NULL);
4832
4833 ptep = &l2b->l2b_kva[l2pte_index(va)];
4834 pte = *ptep;
4835
4836 /* Extract the physical address of the page */
4837 pa = l2pte_pa(pte);
4838
4839 if ((pg = PHYS_TO_VM_PAGE(pa)) != NULL) {
4840 /* Update the wired bit in the pv entry for this page. */
4841 struct vm_page_md *md = VM_PAGE_TO_MD(pg);
4842
4843 pmap_acquire_page_lock(md);
4844 (void) pmap_modify_pv(md, pa, pm, va, PVF_WIRED, 0);
4845 pmap_release_page_lock(md);
4846 }
4847
4848 pmap_release_pmap_lock(pm);
4849 }
4850
4851 #ifdef ARM_MMU_EXTENDED
4852 void
4853 pmap_md_pdetab_activate(pmap_t pm, struct lwp *l)
4854 {
4855 UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
4856
4857 /*
4858 * Assume that TTBR1 has only global mappings and TTBR0 only
4859 * has non-global mappings. To prevent speculation from doing
4860 * evil things we disable translation table walks using TTBR0
4861 * before setting the CONTEXTIDR (ASID) or new TTBR0 value.
4862 * Once both are set, table walks are reenabled.
4863 */
4864 const uint32_t old_ttbcr = armreg_ttbcr_read();
4865 armreg_ttbcr_write(old_ttbcr | TTBCR_S_PD0);
4866 arm_isb();
4867
4868 pmap_tlb_asid_acquire(pm, l);
4869
4870 struct cpu_info * const ci = curcpu();
4871 struct pmap_asid_info * const pai = PMAP_PAI(pm, cpu_tlb_info(ci));
4872
4873 cpu_setttb(pm->pm_l1_pa, pai->pai_asid);
4874 /*
4875 * Now we can reenable tablewalks since the CONTEXTIDR and TTRB0
4876 * have been updated.
4877 */
4878 arm_isb();
4879
4880 if (pm != pmap_kernel()) {
4881 armreg_ttbcr_write(old_ttbcr & ~TTBCR_S_PD0);
4882 }
4883 cpu_cpwait();
4884
4885 UVMHIST_LOG(maphist, " pm %#jx pm->pm_l1_pa %08jx asid %ju... done",
4886 (uintptr_t)pm, pm->pm_l1_pa, pai->pai_asid, 0);
4887
4888 KASSERTMSG(ci->ci_pmap_asid_cur == pai->pai_asid, "%u vs %u",
4889 ci->ci_pmap_asid_cur, pai->pai_asid);
4890 ci->ci_pmap_cur = pm;
4891 }
4892
4893 void
4894 pmap_md_pdetab_deactivate(pmap_t pm)
4895 {
4896
4897 UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
4898
4899 kpreempt_disable();
4900 struct cpu_info * const ci = curcpu();
4901 /*
4902 * Disable translation table walks from TTBR0 while no pmap has been
4903 * activated.
4904 */
4905 const uint32_t old_ttbcr = armreg_ttbcr_read();
4906 armreg_ttbcr_write(old_ttbcr | TTBCR_S_PD0);
4907 arm_isb();
4908 pmap_tlb_asid_deactivate(pm);
4909 cpu_setttb(pmap_kernel()->pm_l1_pa, KERNEL_PID);
4910 arm_isb();
4911
4912 ci->ci_pmap_cur = pmap_kernel();
4913 KASSERTMSG(ci->ci_pmap_asid_cur == KERNEL_PID, "ci_pmap_asid_cur %u",
4914 ci->ci_pmap_asid_cur);
4915 kpreempt_enable();
4916 }
4917 #endif
4918
4919 void
4920 pmap_activate(struct lwp *l)
4921 {
4922 extern int block_userspace_access;
4923 pmap_t npm = l->l_proc->p_vmspace->vm_map.pmap;
4924
4925 UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
4926
4927 UVMHIST_LOG(maphist, "(l=%#jx) pm=%#jx", (uintptr_t)l, (uintptr_t)npm,
4928 0, 0);
4929
4930 struct cpu_info * const ci = curcpu();
4931
4932 /*
4933 * If activating a non-current lwp or the current lwp is
4934 * already active, just return.
4935 */
4936 if (false
4937 || l != curlwp
4938 #ifdef ARM_MMU_EXTENDED
4939 || (ci->ci_pmap_cur == npm &&
4940 (npm == pmap_kernel()
4941 /* || PMAP_PAI_ASIDVALID_P(pai, cpu_tlb_info(ci)) */))
4942 #else
4943 || npm->pm_activated == true
4944 #endif
4945 || false) {
4946 UVMHIST_LOG(maphist, " <-- (same pmap)", (uintptr_t)curlwp,
4947 (uintptr_t)l, 0, 0);
4948 return;
4949 }
4950
4951 #ifndef ARM_MMU_EXTENDED
4952 const uint32_t ndacr = (DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL * 2))
4953 | (DOMAIN_CLIENT << (pmap_domain(npm) * 2));
4954
4955 /*
4956 * If TTB and DACR are unchanged, short-circuit all the
4957 * TLB/cache management stuff.
4958 */
4959 pmap_t opm = ci->ci_lastlwp
4960 ? ci->ci_lastlwp->l_proc->p_vmspace->vm_map.pmap
4961 : NULL;
4962 if (opm != NULL) {
4963 uint32_t odacr = (DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL * 2))
4964 | (DOMAIN_CLIENT << (pmap_domain(opm) * 2));
4965
4966 if (opm->pm_l1 == npm->pm_l1 && odacr == ndacr)
4967 goto all_done;
4968 }
4969 #endif /* !ARM_MMU_EXTENDED */
4970
4971 PMAPCOUNT(activations);
4972 block_userspace_access = 1;
4973
4974 #ifndef ARM_MMU_EXTENDED
4975 /*
4976 * If switching to a user vmspace which is different to the
4977 * most recent one, and the most recent one is potentially
4978 * live in the cache, we must write-back and invalidate the
4979 * entire cache.
4980 */
4981 pmap_t rpm = ci->ci_pmap_lastuser;
4982
4983 /*
4984 * XXXSCW: There's a corner case here which can leave turds in the
4985 * cache as reported in kern/41058. They're probably left over during
4986 * tear-down and switching away from an exiting process. Until the root
4987 * cause is identified and fixed, zap the cache when switching pmaps.
4988 * This will result in a few unnecessary cache flushes, but that's
4989 * better than silently corrupting data.
4990 */
4991 #if 0
4992 if (npm != pmap_kernel() && rpm && npm != rpm &&
4993 rpm->pm_cstate.cs_cache) {
4994 rpm->pm_cstate.cs_cache = 0;
4995 #ifdef PMAP_CACHE_VIVT
4996 cpu_idcache_wbinv_all();
4997 #endif
4998 }
4999 #else
5000 if (rpm) {
5001 rpm->pm_cstate.cs_cache = 0;
5002 if (npm == pmap_kernel())
5003 ci->ci_pmap_lastuser = NULL;
5004 #ifdef PMAP_CACHE_VIVT
5005 cpu_idcache_wbinv_all();
5006 #endif
5007 }
5008 #endif
5009
5010 /* No interrupts while we frob the TTB/DACR */
5011 uint32_t oldirqstate = disable_interrupts(IF32_bits);
5012 #endif /* !ARM_MMU_EXTENDED */
5013
5014 #ifndef ARM_HAS_VBAR
5015 /*
5016 * For ARM_VECTORS_LOW, we MUST, I repeat, MUST fix up the L1
5017 * entry corresponding to 'vector_page' in the incoming L1 table
5018 * before switching to it otherwise subsequent interrupts/exceptions
5019 * (including domain faults!) will jump into hyperspace.
5020 */
5021 if (npm->pm_pl1vec != NULL) {
5022 cpu_tlb_flushID_SE((u_int)vector_page);
5023 cpu_cpwait();
5024 *npm->pm_pl1vec = npm->pm_l1vec;
5025 PTE_SYNC(npm->pm_pl1vec);
5026 }
5027 #endif
5028
5029 #ifdef ARM_MMU_EXTENDED
5030 pmap_md_pdetab_activate(npm, l);
5031 #else
5032 cpu_domains(ndacr);
5033 if (npm == pmap_kernel() || npm == rpm) {
5034 /*
5035 * Switching to a kernel thread, or back to the
5036 * same user vmspace as before... Simply update
5037 * the TTB (no TLB flush required)
5038 */
5039 cpu_setttb(npm->pm_l1->l1_physaddr, false);
5040 cpu_cpwait();
5041 } else {
5042 /*
5043 * Otherwise, update TTB and flush TLB
5044 */
5045 cpu_context_switch(npm->pm_l1->l1_physaddr);
5046 if (rpm != NULL)
5047 rpm->pm_cstate.cs_tlb = 0;
5048 }
5049
5050 restore_interrupts(oldirqstate);
5051 #endif /* ARM_MMU_EXTENDED */
5052
5053 block_userspace_access = 0;
5054
5055 #ifndef ARM_MMU_EXTENDED
5056 all_done:
5057 /*
5058 * The new pmap is resident. Make sure it's marked
5059 * as resident in the cache/TLB.
5060 */
5061 npm->pm_cstate.cs_all = PMAP_CACHE_STATE_ALL;
5062 if (npm != pmap_kernel())
5063 ci->ci_pmap_lastuser = npm;
5064
5065 /* The old pmap is not longer active */
5066 if (opm != npm) {
5067 if (opm != NULL)
5068 opm->pm_activated = false;
5069
5070 /* But the new one is */
5071 npm->pm_activated = true;
5072 }
5073 ci->ci_pmap_cur = npm;
5074 #endif
5075 UVMHIST_LOG(maphist, " <-- done", 0, 0, 0, 0);
5076 }
5077
5078 void
5079 pmap_deactivate(struct lwp *l)
5080 {
5081 pmap_t pm = l->l_proc->p_vmspace->vm_map.pmap;
5082
5083 UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
5084
5085 UVMHIST_LOG(maphist, "(l=%#jx) pm=%#jx", (uintptr_t)l, (uintptr_t)pm,
5086 0, 0);
5087
5088 #ifdef ARM_MMU_EXTENDED
5089 pmap_md_pdetab_deactivate(pm);
5090 #else
5091 /*
5092 * If the process is exiting, make sure pmap_activate() does
5093 * a full MMU context-switch and cache flush, which we might
5094 * otherwise skip. See PR port-arm/38950.
5095 */
5096 if (l->l_proc->p_sflag & PS_WEXIT)
5097 curcpu()->ci_lastlwp = NULL;
5098
5099 pm->pm_activated = false;
5100 #endif
5101 UVMHIST_LOG(maphist, " <-- done", 0, 0, 0, 0);
5102 }
5103
5104 void
5105 pmap_update(pmap_t pm)
5106 {
5107
5108 UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
5109
5110 UVMHIST_LOG(maphist, "pm=%#jx remove_all %jd", (uintptr_t)pm,
5111 pm->pm_remove_all, 0, 0);
5112
5113 #ifndef ARM_MMU_EXTENDED
5114 if (pm->pm_remove_all) {
5115 /*
5116 * Finish up the pmap_remove_all() optimisation by flushing
5117 * the TLB.
5118 */
5119 pmap_tlb_flushID(pm);
5120 pm->pm_remove_all = false;
5121 }
5122
5123 if (pmap_is_current(pm)) {
5124 /*
5125 * If we're dealing with a current userland pmap, move its L1
5126 * to the end of the LRU.
5127 */
5128 if (pm != pmap_kernel())
5129 pmap_use_l1(pm);
5130
5131 /*
5132 * We can assume we're done with frobbing the cache/tlb for
5133 * now. Make sure any future pmap ops don't skip cache/tlb
5134 * flushes.
5135 */
5136 pm->pm_cstate.cs_all = PMAP_CACHE_STATE_ALL;
5137 }
5138 #else
5139
5140 kpreempt_disable();
5141 #if defined(MULTIPROCESSOR) && PMAP_TLB_MAX > 1
5142 u_int pending = atomic_swap_uint(&pmap->pm_shootdown_pending, 0);
5143 if (pending && pmap_tlb_shootdown_bystanders(pmap)) {
5144 PMAP_COUNT(shootdown_ipis);
5145 }
5146 #endif
5147
5148 /*
5149 * If pmap_remove_all was called, we deactivated ourselves and released
5150 * our ASID. Now we have to reactivate ourselves.
5151 */
5152 if (__predict_false(pm->pm_remove_all)) {
5153 pm->pm_remove_all = false;
5154
5155 KASSERT(pm != pmap_kernel());
5156 pmap_md_pdetab_activate(pm, curlwp);
5157 }
5158
5159 if (arm_has_mpext_p)
5160 armreg_bpiallis_write(0);
5161 else
5162 armreg_bpiall_write(0);
5163
5164 kpreempt_enable();
5165
5166 KASSERTMSG(pm == pmap_kernel()
5167 || curcpu()->ci_pmap_cur != pm
5168 || pm->pm_pai[0].pai_asid == curcpu()->ci_pmap_asid_cur,
5169 "pmap/asid %p/%#x != %s cur pmap/asid %p/%#x", pm,
5170 pm->pm_pai[0].pai_asid, curcpu()->ci_data.cpu_name,
5171 curcpu()->ci_pmap_cur, curcpu()->ci_pmap_asid_cur);
5172 #endif
5173
5174 PMAPCOUNT(updates);
5175
5176 /*
5177 * make sure TLB/cache operations have completed.
5178 */
5179 cpu_cpwait();
5180 UVMHIST_LOG(maphist, " <-- done", 0, 0, 0, 0);
5181 }
5182
5183 void
5184 pmap_remove_all(pmap_t pm)
5185 {
5186
5187 /*
5188 * The vmspace described by this pmap is about to be torn down.
5189 * Until pmap_update() is called, UVM will only make calls
5190 * to pmap_remove(). We can make life much simpler by flushing
5191 * the cache now, and deferring TLB invalidation to pmap_update().
5192 */
5193 #ifdef PMAP_CACHE_VIVT
5194 pmap_cache_wbinv_all(pm, PVF_EXEC);
5195 #endif
5196 #ifdef ARM_MMU_EXTENDED
5197 #ifdef MULTIPROCESSOR
5198 struct cpu_info * const ci = curcpu();
5199 // This should be the last CPU with this pmap onproc
5200 KASSERT(!kcpuset_isotherset(pm->pm_onproc, cpu_index(ci)));
5201 if (kcpuset_isset(pm->pm_onproc, cpu_index(ci)))
5202 #endif
5203 pmap_tlb_asid_deactivate(pm);
5204 #ifdef MULTIPROCESSOR
5205 KASSERT(kcpuset_iszero(pm->pm_onproc));
5206 #endif
5207
5208 pmap_tlb_asid_release_all(pm);
5209 #endif
5210 pm->pm_remove_all = true;
5211 }
5212
5213 /*
5214 * Retire the given physical map from service.
5215 * Should only be called if the map contains no valid mappings.
5216 */
5217 void
5218 pmap_destroy(pmap_t pm)
5219 {
5220 UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
5221
5222 if (pm == NULL)
5223 return;
5224
5225 UVMHIST_LOG(maphist, "pm=%#jx remove_all %jd", (uintptr_t)pm,
5226 pm->pm_remove_all, 0, 0);
5227
5228 if (pm->pm_remove_all) {
5229 #ifdef ARM_MMU_EXTENDED
5230 pmap_tlb_asid_release_all(pm);
5231 #else
5232 pmap_tlb_flushID(pm);
5233 #endif
5234 pm->pm_remove_all = false;
5235 }
5236
5237 /*
5238 * Drop reference count
5239 */
5240 if (atomic_dec_uint_nv(&pm->pm_refs) > 0) {
5241 #ifndef ARM_MMU_EXTENDED
5242 if (pmap_is_current(pm)) {
5243 if (pm != pmap_kernel())
5244 pmap_use_l1(pm);
5245 pm->pm_cstate.cs_all = PMAP_CACHE_STATE_ALL;
5246 }
5247 #endif
5248 return;
5249 }
5250
5251 /*
5252 * reference count is zero, free pmap resources and then free pmap.
5253 */
5254
5255 #ifndef ARM_HAS_VBAR
5256 if (vector_page < KERNEL_BASE) {
5257 KDASSERT(!pmap_is_current(pm));
5258
5259 /* Remove the vector page mapping */
5260 pmap_remove(pm, vector_page, vector_page + PAGE_SIZE);
5261 pmap_update(pm);
5262 }
5263 #endif
5264
5265 pmap_free_l1(pm);
5266
5267 #ifdef ARM_MMU_EXTENDED
5268 #ifdef MULTIPROCESSOR
5269 kcpuset_destroy(pm->pm_active);
5270 kcpuset_destroy(pm->pm_onproc);
5271 #endif
5272 #else
5273 struct cpu_info * const ci = curcpu();
5274 if (ci->ci_pmap_lastuser == pm)
5275 ci->ci_pmap_lastuser = NULL;
5276 #endif
5277
5278 mutex_destroy(&pm->pm_lock);
5279 pool_cache_put(&pmap_cache, pm);
5280 UVMHIST_LOG(maphist, " <-- done", 0, 0, 0, 0);
5281 }
5282
5283
5284 /*
5285 * void pmap_reference(pmap_t pm)
5286 *
5287 * Add a reference to the specified pmap.
5288 */
5289 void
5290 pmap_reference(pmap_t pm)
5291 {
5292
5293 if (pm == NULL)
5294 return;
5295
5296 #ifndef ARM_MMU_EXTENDED
5297 pmap_use_l1(pm);
5298 #endif
5299
5300 atomic_inc_uint(&pm->pm_refs);
5301 }
5302
5303 #if (ARM_MMU_V6 + ARM_MMU_V7) > 0
5304
5305 static struct evcnt pmap_prefer_nochange_ev =
5306 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap prefer", "nochange");
5307 static struct evcnt pmap_prefer_change_ev =
5308 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap prefer", "change");
5309
5310 EVCNT_ATTACH_STATIC(pmap_prefer_change_ev);
5311 EVCNT_ATTACH_STATIC(pmap_prefer_nochange_ev);
5312
5313 void
5314 pmap_prefer(vaddr_t hint, vaddr_t *vap, int td)
5315 {
5316 vsize_t mask = arm_cache_prefer_mask | (PAGE_SIZE - 1);
5317 vaddr_t va = *vap;
5318 vaddr_t diff = (hint - va) & mask;
5319 if (diff == 0) {
5320 pmap_prefer_nochange_ev.ev_count++;
5321 } else {
5322 pmap_prefer_change_ev.ev_count++;
5323 if (__predict_false(td))
5324 va -= mask + 1;
5325 *vap = va + diff;
5326 }
5327 }
5328 #endif /* ARM_MMU_V6 | ARM_MMU_V7 */
5329
5330 /*
5331 * pmap_zero_page()
5332 *
5333 * Zero a given physical page by mapping it at a page hook point.
5334 * In doing the zero page op, the page we zero is mapped cachable, as with
5335 * StrongARM accesses to non-cached pages are non-burst making writing
5336 * _any_ bulk data very slow.
5337 */
5338 #if (ARM_MMU_GENERIC + ARM_MMU_SA1 + ARM_MMU_V6 + ARM_MMU_V7) != 0
5339 void
5340 pmap_zero_page_generic(paddr_t pa)
5341 {
5342 #if defined(PMAP_CACHE_VIPT) || defined(DEBUG)
5343 struct vm_page *pg = PHYS_TO_VM_PAGE(pa);
5344 struct vm_page_md *md = VM_PAGE_TO_MD(pg);
5345 #endif
5346 #if defined(PMAP_CACHE_VIPT)
5347 /* Choose the last page color it had, if any */
5348 const vsize_t va_offset = md->pvh_attrs & arm_cache_prefer_mask;
5349 #else
5350 const vsize_t va_offset = 0;
5351 #endif
5352 #if defined(__HAVE_MM_MD_DIRECT_MAPPED_PHYS)
5353 /*
5354 * Is this page mapped at its natural color?
5355 * If we have all of memory mapped, then just convert PA to VA.
5356 */
5357 bool okcolor = arm_pcache.dcache_type == CACHE_TYPE_PIPT
5358 || va_offset == (pa & arm_cache_prefer_mask);
5359 const vaddr_t vdstp = okcolor
5360 ? pmap_direct_mapped_phys(pa, &okcolor, cpu_cdstp(va_offset))
5361 : cpu_cdstp(va_offset);
5362 #else
5363 const bool okcolor = false;
5364 const vaddr_t vdstp = cpu_cdstp(va_offset);
5365 #endif
5366 pt_entry_t * const ptep = cpu_cdst_pte(va_offset);
5367
5368
5369 #ifdef DEBUG
5370 if (!SLIST_EMPTY(&md->pvh_list))
5371 panic("pmap_zero_page: page has mappings");
5372 #endif
5373
5374 KDASSERT((pa & PGOFSET) == 0);
5375
5376 if (!okcolor) {
5377 /*
5378 * Hook in the page, zero it, and purge the cache for that
5379 * zeroed page. Invalidate the TLB as needed.
5380 */
5381 const pt_entry_t npte = L2_S_PROTO | pa | pte_l2_s_cache_mode
5382 | L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE);
5383 l2pte_set(ptep, npte, 0);
5384 PTE_SYNC(ptep);
5385 cpu_tlb_flushD_SE(vdstp);
5386 cpu_cpwait();
5387 #if defined(__HAVE_MM_MD_DIRECT_MAPPED_PHYS) && defined(PMAP_CACHE_VIPT) \
5388 && !defined(ARM_MMU_EXTENDED)
5389 /*
5390 * If we are direct-mapped and our color isn't ok, then before
5391 * we bzero the page invalidate its contents from the cache and
5392 * reset the color to its natural color.
5393 */
5394 cpu_dcache_inv_range(vdstp, PAGE_SIZE);
5395 md->pvh_attrs &= ~arm_cache_prefer_mask;
5396 md->pvh_attrs |= (pa & arm_cache_prefer_mask);
5397 #endif
5398 }
5399 bzero_page(vdstp);
5400 if (!okcolor) {
5401 /*
5402 * Unmap the page.
5403 */
5404 l2pte_reset(ptep);
5405 PTE_SYNC(ptep);
5406 cpu_tlb_flushD_SE(vdstp);
5407 #ifdef PMAP_CACHE_VIVT
5408 cpu_dcache_wbinv_range(vdstp, PAGE_SIZE);
5409 #endif
5410 }
5411 #ifdef PMAP_CACHE_VIPT
5412 /*
5413 * This page is now cache resident so it now has a page color.
5414 * Any contents have been obliterated so clear the EXEC flag.
5415 */
5416 #ifndef ARM_MMU_EXTENDED
5417 if (!pmap_is_page_colored_p(md)) {
5418 PMAPCOUNT(vac_color_new);
5419 md->pvh_attrs |= PVF_COLORED;
5420 }
5421 md->pvh_attrs |= PVF_DIRTY;
5422 #endif
5423 if (PV_IS_EXEC_P(md->pvh_attrs)) {
5424 md->pvh_attrs &= ~PVF_EXEC;
5425 PMAPCOUNT(exec_discarded_zero);
5426 }
5427 #endif
5428 }
5429 #endif /* (ARM_MMU_GENERIC + ARM_MMU_SA1 + ARM_MMU_V6) != 0 */
5430
5431 #if ARM_MMU_XSCALE == 1
5432 void
5433 pmap_zero_page_xscale(paddr_t pa)
5434 {
5435 #ifdef DEBUG
5436 struct vm_page *pg = PHYS_TO_VM_PAGE(pa);
5437 struct vm_page_md *md = VM_PAGE_TO_MD(pg);
5438
5439 if (!SLIST_EMPTY(&md->pvh_list))
5440 panic("pmap_zero_page: page has mappings");
5441 #endif
5442
5443 KDASSERT((pa & PGOFSET) == 0);
5444
5445 /*
5446 * Hook in the page, zero it, and purge the cache for that
5447 * zeroed page. Invalidate the TLB as needed.
5448 */
5449
5450 pt_entry_t npte = L2_S_PROTO | pa |
5451 L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE) |
5452 L2_C | L2_XS_T_TEX(TEX_XSCALE_X); /* mini-data */
5453 l2pte_set(cdst_pte, npte, 0);
5454 PTE_SYNC(cdst_pte);
5455 cpu_tlb_flushD_SE(cdstp);
5456 cpu_cpwait();
5457 bzero_page(cdstp);
5458 xscale_cache_clean_minidata();
5459 l2pte_reset(cdst_pte);
5460 PTE_SYNC(cdst_pte);
5461 }
5462 #endif /* ARM_MMU_XSCALE == 1 */
5463
5464 /* pmap_pageidlezero()
5465 *
5466 * The same as above, except that we assume that the page is not
5467 * mapped. This means we never have to flush the cache first. Called
5468 * from the idle loop.
5469 */
5470 bool
5471 pmap_pageidlezero(paddr_t pa)
5472 {
5473 bool rv = true;
5474 #if defined(PMAP_CACHE_VIPT) || defined(DEBUG)
5475 struct vm_page * const pg = PHYS_TO_VM_PAGE(pa);
5476 struct vm_page_md *md = VM_PAGE_TO_MD(pg);
5477 #endif
5478 #ifdef PMAP_CACHE_VIPT
5479 /* Choose the last page color it had, if any */
5480 const vsize_t va_offset = md->pvh_attrs & arm_cache_prefer_mask;
5481 #else
5482 const vsize_t va_offset = 0;
5483 #endif
5484 #ifdef __HAVE_MM_MD_DIRECT_MAPPED_PHYS
5485 bool okcolor = arm_pcache.dcache_type == CACHE_TYPE_PIPT
5486 || va_offset == (pa & arm_cache_prefer_mask);
5487 const vaddr_t vdstp = okcolor
5488 ? pmap_direct_mapped_phys(pa, &okcolor, cpu_cdstp(va_offset))
5489 : cpu_cdstp(va_offset);
5490 #else
5491 const bool okcolor = false;
5492 const vaddr_t vdstp = cpu_cdstp(va_offset);
5493 #endif
5494 pt_entry_t * const ptep = cpu_cdst_pte(va_offset);
5495
5496
5497 #ifdef DEBUG
5498 if (!SLIST_EMPTY(&md->pvh_list))
5499 panic("pmap_pageidlezero: page has mappings");
5500 #endif
5501
5502 KDASSERT((pa & PGOFSET) == 0);
5503
5504 if (!okcolor) {
5505 /*
5506 * Hook in the page, zero it, and purge the cache for that
5507 * zeroed page. Invalidate the TLB as needed.
5508 */
5509 const pt_entry_t npte = L2_S_PROTO | pa |
5510 L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE) | pte_l2_s_cache_mode;
5511 l2pte_set(ptep, npte, 0);
5512 PTE_SYNC(ptep);
5513 cpu_tlb_flushD_SE(vdstp);
5514 cpu_cpwait();
5515 }
5516
5517 uint64_t *ptr = (uint64_t *)vdstp;
5518 for (size_t i = 0; i < PAGE_SIZE / sizeof(*ptr); i++) {
5519 if (sched_curcpu_runnable_p() != 0) {
5520 /*
5521 * A process has become ready. Abort now,
5522 * so we don't keep it waiting while we
5523 * do slow memory access to finish this
5524 * page.
5525 */
5526 rv = false;
5527 break;
5528 }
5529 *ptr++ = 0;
5530 }
5531
5532 #ifdef PMAP_CACHE_VIVT
5533 if (rv)
5534 /*
5535 * if we aborted we'll rezero this page again later so don't
5536 * purge it unless we finished it
5537 */
5538 cpu_dcache_wbinv_range(vdstp, PAGE_SIZE);
5539 #elif defined(PMAP_CACHE_VIPT)
5540 /*
5541 * This page is now cache resident so it now has a page color.
5542 * Any contents have been obliterated so clear the EXEC flag.
5543 */
5544 #ifndef ARM_MMU_EXTENDED
5545 if (!pmap_is_page_colored_p(md)) {
5546 PMAPCOUNT(vac_color_new);
5547 md->pvh_attrs |= PVF_COLORED;
5548 }
5549 #endif
5550 if (PV_IS_EXEC_P(md->pvh_attrs)) {
5551 md->pvh_attrs &= ~PVF_EXEC;
5552 PMAPCOUNT(exec_discarded_zero);
5553 }
5554 #endif
5555 /*
5556 * Unmap the page.
5557 */
5558 if (!okcolor) {
5559 l2pte_reset(ptep);
5560 PTE_SYNC(ptep);
5561 cpu_tlb_flushD_SE(vdstp);
5562 }
5563
5564 return rv;
5565 }
5566
5567 /*
5568 * pmap_copy_page()
5569 *
5570 * Copy one physical page into another, by mapping the pages into
5571 * hook points. The same comment regarding cachability as in
5572 * pmap_zero_page also applies here.
5573 */
5574 #if (ARM_MMU_GENERIC + ARM_MMU_SA1 + ARM_MMU_V6 + ARM_MMU_V7) != 0
5575 void
5576 pmap_copy_page_generic(paddr_t src, paddr_t dst)
5577 {
5578 struct vm_page * const src_pg = PHYS_TO_VM_PAGE(src);
5579 struct vm_page_md *src_md = VM_PAGE_TO_MD(src_pg);
5580 #if defined(PMAP_CACHE_VIPT) || defined(DEBUG)
5581 struct vm_page * const dst_pg = PHYS_TO_VM_PAGE(dst);
5582 struct vm_page_md *dst_md = VM_PAGE_TO_MD(dst_pg);
5583 #endif
5584 #ifdef PMAP_CACHE_VIPT
5585 const vsize_t src_va_offset = src_md->pvh_attrs & arm_cache_prefer_mask;
5586 const vsize_t dst_va_offset = dst_md->pvh_attrs & arm_cache_prefer_mask;
5587 #else
5588 const vsize_t src_va_offset = 0;
5589 const vsize_t dst_va_offset = 0;
5590 #endif
5591 #if defined(__HAVE_MM_MD_DIRECT_MAPPED_PHYS)
5592 /*
5593 * Is this page mapped at its natural color?
5594 * If we have all of memory mapped, then just convert PA to VA.
5595 */
5596 bool src_okcolor = arm_pcache.dcache_type == CACHE_TYPE_PIPT
5597 || src_va_offset == (src & arm_cache_prefer_mask);
5598 bool dst_okcolor = arm_pcache.dcache_type == CACHE_TYPE_PIPT
5599 || dst_va_offset == (dst & arm_cache_prefer_mask);
5600 const vaddr_t vsrcp = src_okcolor
5601 ? pmap_direct_mapped_phys(src, &src_okcolor,
5602 cpu_csrcp(src_va_offset))
5603 : cpu_csrcp(src_va_offset);
5604 const vaddr_t vdstp = pmap_direct_mapped_phys(dst, &dst_okcolor,
5605 cpu_cdstp(dst_va_offset));
5606 #else
5607 const bool src_okcolor = false;
5608 const bool dst_okcolor = false;
5609 const vaddr_t vsrcp = cpu_csrcp(src_va_offset);
5610 const vaddr_t vdstp = cpu_cdstp(dst_va_offset);
5611 #endif
5612 pt_entry_t * const src_ptep = cpu_csrc_pte(src_va_offset);
5613 pt_entry_t * const dst_ptep = cpu_cdst_pte(dst_va_offset);
5614
5615 #ifdef DEBUG
5616 if (!SLIST_EMPTY(&dst_md->pvh_list))
5617 panic("pmap_copy_page: dst page has mappings");
5618 #endif
5619
5620 #if defined(PMAP_CACHE_VIPT) && !defined(ARM_MMU_EXTENDED)
5621 KASSERT(arm_cache_prefer_mask == 0 || src_md->pvh_attrs & (PVF_COLORED|PVF_NC));
5622 #endif
5623 KDASSERT((src & PGOFSET) == 0);
5624 KDASSERT((dst & PGOFSET) == 0);
5625
5626 /*
5627 * Clean the source page. Hold the source page's lock for
5628 * the duration of the copy so that no other mappings can
5629 * be created while we have a potentially aliased mapping.
5630 */
5631 #ifdef PMAP_CACHE_VIVT
5632 pmap_acquire_page_lock(src_md);
5633 (void) pmap_clean_page(src_md, true);
5634 pmap_release_page_lock(src_md);
5635 #endif
5636
5637 /*
5638 * Map the pages into the page hook points, copy them, and purge
5639 * the cache for the appropriate page. Invalidate the TLB
5640 * as required.
5641 */
5642 if (!src_okcolor) {
5643 const pt_entry_t nsrc_pte = L2_S_PROTO
5644 | src
5645 #if defined(PMAP_CACHE_VIPT) && !defined(ARM_MMU_EXTENDED)
5646 | ((src_md->pvh_attrs & PVF_NC) ? 0 : pte_l2_s_cache_mode)
5647 #else // defined(PMAP_CACHE_VIVT) || defined(ARM_MMU_EXTENDED)
5648 | pte_l2_s_cache_mode
5649 #endif
5650 | L2_S_PROT(PTE_KERNEL, VM_PROT_READ);
5651 l2pte_set(src_ptep, nsrc_pte, 0);
5652 PTE_SYNC(src_ptep);
5653 cpu_tlb_flushD_SE(vsrcp);
5654 cpu_cpwait();
5655 }
5656 if (!dst_okcolor) {
5657 const pt_entry_t ndst_pte = L2_S_PROTO | dst |
5658 L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE) | pte_l2_s_cache_mode;
5659 l2pte_set(dst_ptep, ndst_pte, 0);
5660 PTE_SYNC(dst_ptep);
5661 cpu_tlb_flushD_SE(vdstp);
5662 cpu_cpwait();
5663 #if defined(__HAVE_MM_MD_DIRECT_MAPPED_PHYS) && defined(PMAP_CACHE_VIPT)
5664 /*
5665 * If we are direct-mapped and our color isn't ok, then before
5666 * we bcopy to the new page invalidate its contents from the
5667 * cache and reset its color to its natural color.
5668 */
5669 cpu_dcache_inv_range(vdstp, PAGE_SIZE);
5670 dst_md->pvh_attrs &= ~arm_cache_prefer_mask;
5671 dst_md->pvh_attrs |= (dst & arm_cache_prefer_mask);
5672 #endif
5673 }
5674 bcopy_page(vsrcp, vdstp);
5675 #ifdef PMAP_CACHE_VIVT
5676 cpu_dcache_inv_range(vsrcp, PAGE_SIZE);
5677 cpu_dcache_wbinv_range(vdstp, PAGE_SIZE);
5678 #endif
5679 /*
5680 * Unmap the pages.
5681 */
5682 if (!src_okcolor) {
5683 l2pte_reset(src_ptep);
5684 PTE_SYNC(src_ptep);
5685 cpu_tlb_flushD_SE(vsrcp);
5686 cpu_cpwait();
5687 }
5688 if (!dst_okcolor) {
5689 l2pte_reset(dst_ptep);
5690 PTE_SYNC(dst_ptep);
5691 cpu_tlb_flushD_SE(vdstp);
5692 cpu_cpwait();
5693 }
5694 #ifdef PMAP_CACHE_VIPT
5695 /*
5696 * Now that the destination page is in the cache, mark it as colored.
5697 * If this was an exec page, discard it.
5698 */
5699 pmap_acquire_page_lock(dst_md);
5700 #ifndef ARM_MMU_EXTENDED
5701 if (arm_pcache.cache_type == CACHE_TYPE_PIPT) {
5702 dst_md->pvh_attrs &= ~arm_cache_prefer_mask;
5703 dst_md->pvh_attrs |= (dst & arm_cache_prefer_mask);
5704 }
5705 if (!pmap_is_page_colored_p(dst_md)) {
5706 PMAPCOUNT(vac_color_new);
5707 dst_md->pvh_attrs |= PVF_COLORED;
5708 }
5709 dst_md->pvh_attrs |= PVF_DIRTY;
5710 #endif
5711 if (PV_IS_EXEC_P(dst_md->pvh_attrs)) {
5712 dst_md->pvh_attrs &= ~PVF_EXEC;
5713 PMAPCOUNT(exec_discarded_copy);
5714 }
5715 pmap_release_page_lock(dst_md);
5716 #endif
5717 }
5718 #endif /* (ARM_MMU_GENERIC + ARM_MMU_SA1 + ARM_MMU_V6) != 0 */
5719
5720 #if ARM_MMU_XSCALE == 1
5721 void
5722 pmap_copy_page_xscale(paddr_t src, paddr_t dst)
5723 {
5724 struct vm_page *src_pg = PHYS_TO_VM_PAGE(src);
5725 struct vm_page_md *src_md = VM_PAGE_TO_MD(src_pg);
5726 #ifdef DEBUG
5727 struct vm_page_md *dst_md = VM_PAGE_TO_MD(PHYS_TO_VM_PAGE(dst));
5728
5729 if (!SLIST_EMPTY(&dst_md->pvh_list))
5730 panic("pmap_copy_page: dst page has mappings");
5731 #endif
5732
5733 KDASSERT((src & PGOFSET) == 0);
5734 KDASSERT((dst & PGOFSET) == 0);
5735
5736 /*
5737 * Clean the source page. Hold the source page's lock for
5738 * the duration of the copy so that no other mappings can
5739 * be created while we have a potentially aliased mapping.
5740 */
5741 #ifdef PMAP_CACHE_VIVT
5742 pmap_acquire_page_lock(src_md);
5743 (void) pmap_clean_page(src_md, true);
5744 pmap_release_page_lock(src_md);
5745 #endif
5746
5747 /*
5748 * Map the pages into the page hook points, copy them, and purge
5749 * the cache for the appropriate page. Invalidate the TLB
5750 * as required.
5751 */
5752 const pt_entry_t nsrc_pte = L2_S_PROTO | src
5753 | L2_S_PROT(PTE_KERNEL, VM_PROT_READ)
5754 | L2_C | L2_XS_T_TEX(TEX_XSCALE_X); /* mini-data */
5755 l2pte_set(csrc_pte, nsrc_pte, 0);
5756 PTE_SYNC(csrc_pte);
5757
5758 const pt_entry_t ndst_pte = L2_S_PROTO | dst
5759 | L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE)
5760 | L2_C | L2_XS_T_TEX(TEX_XSCALE_X); /* mini-data */
5761 l2pte_set(cdst_pte, ndst_pte, 0);
5762 PTE_SYNC(cdst_pte);
5763
5764 cpu_tlb_flushD_SE(csrcp);
5765 cpu_tlb_flushD_SE(cdstp);
5766 cpu_cpwait();
5767 bcopy_page(csrcp, cdstp);
5768 xscale_cache_clean_minidata();
5769 l2pte_reset(csrc_pte);
5770 l2pte_reset(cdst_pte);
5771 PTE_SYNC(csrc_pte);
5772 PTE_SYNC(cdst_pte);
5773 }
5774 #endif /* ARM_MMU_XSCALE == 1 */
5775
5776 /*
5777 * void pmap_virtual_space(vaddr_t *start, vaddr_t *end)
5778 *
5779 * Return the start and end addresses of the kernel's virtual space.
5780 * These values are setup in pmap_bootstrap and are updated as pages
5781 * are allocated.
5782 */
5783 void
5784 pmap_virtual_space(vaddr_t *start, vaddr_t *end)
5785 {
5786 *start = virtual_avail;
5787 *end = virtual_end;
5788 }
5789
5790 /*
5791 * Helper function for pmap_grow_l2_bucket()
5792 */
5793 static inline int
5794 pmap_grow_map(vaddr_t va, paddr_t *pap)
5795 {
5796 paddr_t pa;
5797
5798 KASSERT((va & PGOFSET) == 0);
5799
5800 if (uvm.page_init_done == false) {
5801 #ifdef PMAP_STEAL_MEMORY
5802 pv_addr_t pv;
5803 pmap_boot_pagealloc(PAGE_SIZE,
5804 #ifdef PMAP_CACHE_VIPT
5805 arm_cache_prefer_mask,
5806 va & arm_cache_prefer_mask,
5807 #else
5808 0, 0,
5809 #endif
5810 &pv);
5811 pa = pv.pv_pa;
5812 #else
5813 if (uvm_page_physget(&pa) == false)
5814 return 1;
5815 #endif /* PMAP_STEAL_MEMORY */
5816 } else {
5817 struct vm_page *pg;
5818 pg = uvm_pagealloc(NULL, 0, NULL, UVM_PGA_USERESERVE);
5819 if (pg == NULL)
5820 return 1;
5821 pa = VM_PAGE_TO_PHYS(pg);
5822 /*
5823 * This new page must not have any mappings.
5824 */
5825 struct vm_page_md *md __diagused = VM_PAGE_TO_MD(pg);
5826 KASSERT(SLIST_EMPTY(&md->pvh_list));
5827 }
5828
5829 /*
5830 * Enter it via pmap_kenter_pa and let that routine do the hard work.
5831 */
5832 pmap_kenter_pa(va, pa, VM_PROT_READ | VM_PROT_WRITE,
5833 PMAP_KMPAGE | PMAP_PTE);
5834
5835 if (pap)
5836 *pap = pa;
5837
5838 PMAPCOUNT(pt_mappings);
5839
5840 const pmap_t kpm __diagused = pmap_kernel();
5841 struct l2_bucket * const l2b __diagused = pmap_get_l2_bucket(kpm, va);
5842 KASSERT(l2b != NULL);
5843
5844 pt_entry_t * const ptep __diagused = &l2b->l2b_kva[l2pte_index(va)];
5845 const pt_entry_t pte __diagused = *ptep;
5846 KASSERT(l2pte_valid_p(pte));
5847 KASSERT((pte & L2_S_CACHE_MASK) == pte_l2_s_cache_mode_pt);
5848
5849 memset((void *)va, 0, PAGE_SIZE);
5850
5851 return 0;
5852 }
5853
5854 /*
5855 * This is the same as pmap_alloc_l2_bucket(), except that it is only
5856 * used by pmap_growkernel().
5857 */
5858 static inline struct l2_bucket *
5859 pmap_grow_l2_bucket(pmap_t pm, vaddr_t va)
5860 {
5861 const size_t l1slot = l1pte_index(va);
5862 struct l2_dtable *l2;
5863 vaddr_t nva;
5864
5865 CTASSERT((PAGE_SIZE % L2_TABLE_SIZE_REAL) == 0);
5866 if ((l2 = pm->pm_l2[L2_IDX(l1slot)]) == NULL) {
5867 /*
5868 * No mapping at this address, as there is
5869 * no entry in the L1 table.
5870 * Need to allocate a new l2_dtable.
5871 */
5872 nva = pmap_kernel_l2dtable_kva;
5873 if ((nva & PGOFSET) == 0) {
5874 /*
5875 * Need to allocate a backing page
5876 */
5877 if (pmap_grow_map(nva, NULL))
5878 return NULL;
5879 }
5880
5881 l2 = (struct l2_dtable *)nva;
5882 nva += sizeof(struct l2_dtable);
5883
5884 if ((nva & PGOFSET) < (pmap_kernel_l2dtable_kva & PGOFSET)) {
5885 /*
5886 * The new l2_dtable straddles a page boundary.
5887 * Map in another page to cover it.
5888 */
5889 if (pmap_grow_map(nva & ~PGOFSET, NULL))
5890 return NULL;
5891 }
5892
5893 pmap_kernel_l2dtable_kva = nva;
5894
5895 /*
5896 * Link it into the parent pmap
5897 */
5898 pm->pm_l2[L2_IDX(l1slot)] = l2;
5899 }
5900
5901 struct l2_bucket * const l2b = &l2->l2_bucket[L2_BUCKET(l1slot)];
5902
5903 /*
5904 * Fetch pointer to the L2 page table associated with the address.
5905 */
5906 if (l2b->l2b_kva == NULL) {
5907 pt_entry_t *ptep;
5908
5909 /*
5910 * No L2 page table has been allocated. Chances are, this
5911 * is because we just allocated the l2_dtable, above.
5912 */
5913 nva = pmap_kernel_l2ptp_kva;
5914 ptep = (pt_entry_t *)nva;
5915 if ((nva & PGOFSET) == 0) {
5916 /*
5917 * Need to allocate a backing page
5918 */
5919 if (pmap_grow_map(nva, &pmap_kernel_l2ptp_phys))
5920 return NULL;
5921 PTE_SYNC_RANGE(ptep, PAGE_SIZE / sizeof(pt_entry_t));
5922 }
5923
5924 l2->l2_occupancy++;
5925 l2b->l2b_kva = ptep;
5926 l2b->l2b_l1slot = l1slot;
5927 l2b->l2b_pa = pmap_kernel_l2ptp_phys;
5928
5929 pmap_kernel_l2ptp_kva += L2_TABLE_SIZE_REAL;
5930 pmap_kernel_l2ptp_phys += L2_TABLE_SIZE_REAL;
5931 }
5932
5933 return l2b;
5934 }
5935
5936 vaddr_t
5937 pmap_growkernel(vaddr_t maxkvaddr)
5938 {
5939 pmap_t kpm = pmap_kernel();
5940 #ifndef ARM_MMU_EXTENDED
5941 struct l1_ttable *l1;
5942 #endif
5943 int s;
5944
5945 if (maxkvaddr <= pmap_curmaxkvaddr)
5946 goto out; /* we are OK */
5947
5948 NPDEBUG(PDB_GROWKERN,
5949 printf("pmap_growkernel: growing kernel from 0x%lx to 0x%lx\n",
5950 pmap_curmaxkvaddr, maxkvaddr));
5951
5952 KDASSERT(maxkvaddr <= virtual_end);
5953
5954 /*
5955 * whoops! we need to add kernel PTPs
5956 */
5957
5958 s = splvm(); /* to be safe */
5959 mutex_enter(&kpm_lock);
5960
5961 /* Map 1MB at a time */
5962 size_t l1slot = l1pte_index(pmap_curmaxkvaddr);
5963 #ifdef ARM_MMU_EXTENDED
5964 pd_entry_t * const spdep = &kpm->pm_l1[l1slot];
5965 pd_entry_t *pdep = spdep;
5966 #endif
5967 for (;pmap_curmaxkvaddr < maxkvaddr; pmap_curmaxkvaddr += L1_S_SIZE,
5968 #ifdef ARM_MMU_EXTENDED
5969 pdep++,
5970 #endif
5971 l1slot++) {
5972 struct l2_bucket *l2b =
5973 pmap_grow_l2_bucket(kpm, pmap_curmaxkvaddr);
5974 KASSERT(l2b != NULL);
5975
5976 const pd_entry_t npde = L1_C_PROTO | l2b->l2b_pa
5977 | L1_C_DOM(PMAP_DOMAIN_KERNEL);
5978 #ifdef ARM_MMU_EXTENDED
5979 KASSERT(*pdep == 0);
5980 l1pte_setone(pdep, npde);
5981 #else
5982 /* Distribute new L1 entry to all other L1s */
5983 SLIST_FOREACH(l1, &l1_list, l1_link) {
5984 pd_entry_t * const pdep = &l1->l1_kva[l1slot];
5985 l1pte_setone(pdep, npde);
5986 PDE_SYNC(pdep);
5987 }
5988 #endif
5989 }
5990 #ifdef ARM_MMU_EXTENDED
5991 PDE_SYNC_RANGE(spdep, pdep - spdep);
5992 #endif
5993
5994 #ifdef PMAP_CACHE_VIVT
5995 /*
5996 * flush out the cache, expensive but growkernel will happen so
5997 * rarely
5998 */
5999 cpu_dcache_wbinv_all();
6000 cpu_tlb_flushD();
6001 cpu_cpwait();
6002 #endif
6003
6004 mutex_exit(&kpm_lock);
6005 splx(s);
6006
6007 out:
6008 return pmap_curmaxkvaddr;
6009 }
6010
6011 /************************ Utility routines ****************************/
6012
6013 #ifndef ARM_HAS_VBAR
6014 /*
6015 * vector_page_setprot:
6016 *
6017 * Manipulate the protection of the vector page.
6018 */
6019 void
6020 vector_page_setprot(int prot)
6021 {
6022 struct l2_bucket *l2b;
6023 pt_entry_t *ptep;
6024
6025 #if defined(CPU_ARMV7) || defined(CPU_ARM11)
6026 /*
6027 * If we are using VBAR to use the vectors in the kernel, then it's
6028 * already mapped in the kernel text so no need to anything here.
6029 */
6030 if (vector_page != ARM_VECTORS_LOW && vector_page != ARM_VECTORS_HIGH) {
6031 KASSERT((armreg_pfr1_read() & ARM_PFR1_SEC_MASK) != 0);
6032 return;
6033 }
6034 #endif
6035
6036 l2b = pmap_get_l2_bucket(pmap_kernel(), vector_page);
6037 KASSERT(l2b != NULL);
6038
6039 ptep = &l2b->l2b_kva[l2pte_index(vector_page)];
6040
6041 const pt_entry_t opte = *ptep;
6042 #ifdef ARM_MMU_EXTENDED
6043 const pt_entry_t npte = (opte & ~(L2_S_PROT_MASK|L2_XS_XN))
6044 | L2_S_PROT(PTE_KERNEL, prot);
6045 #else
6046 const pt_entry_t npte = (opte & ~L2_S_PROT_MASK)
6047 | L2_S_PROT(PTE_KERNEL, prot);
6048 #endif
6049 l2pte_set(ptep, npte, opte);
6050 PTE_SYNC(ptep);
6051 cpu_tlb_flushD_SE(vector_page);
6052 cpu_cpwait();
6053 }
6054 #endif
6055
6056 /*
6057 * Fetch pointers to the PDE/PTE for the given pmap/VA pair.
6058 * Returns true if the mapping exists, else false.
6059 *
6060 * NOTE: This function is only used by a couple of arm-specific modules.
6061 * It is not safe to take any pmap locks here, since we could be right
6062 * in the middle of debugging the pmap anyway...
6063 *
6064 * It is possible for this routine to return false even though a valid
6065 * mapping does exist. This is because we don't lock, so the metadata
6066 * state may be inconsistent.
6067 *
6068 * NOTE: We can return a NULL *ptp in the case where the L1 pde is
6069 * a "section" mapping.
6070 */
6071 bool
6072 pmap_get_pde_pte(pmap_t pm, vaddr_t va, pd_entry_t **pdp, pt_entry_t **ptp)
6073 {
6074 struct l2_dtable *l2;
6075 pd_entry_t *pdep, pde;
6076 pt_entry_t *ptep;
6077 u_short l1slot;
6078
6079 if (pm->pm_l1 == NULL)
6080 return false;
6081
6082 l1slot = l1pte_index(va);
6083 *pdp = pdep = pmap_l1_kva(pm) + l1slot;
6084 pde = *pdep;
6085
6086 if (l1pte_section_p(pde)) {
6087 *ptp = NULL;
6088 return true;
6089 }
6090
6091 l2 = pm->pm_l2[L2_IDX(l1slot)];
6092 if (l2 == NULL ||
6093 (ptep = l2->l2_bucket[L2_BUCKET(l1slot)].l2b_kva) == NULL) {
6094 return false;
6095 }
6096
6097 *ptp = &ptep[l2pte_index(va)];
6098 return true;
6099 }
6100
6101 bool
6102 pmap_get_pde(pmap_t pm, vaddr_t va, pd_entry_t **pdp)
6103 {
6104
6105 if (pm->pm_l1 == NULL)
6106 return false;
6107
6108 *pdp = pmap_l1_kva(pm) + l1pte_index(va);
6109
6110 return true;
6111 }
6112
6113 /************************ Bootstrapping routines ****************************/
6114
6115 #ifndef ARM_MMU_EXTENDED
6116 static void
6117 pmap_init_l1(struct l1_ttable *l1, pd_entry_t *l1pt)
6118 {
6119 int i;
6120
6121 l1->l1_kva = l1pt;
6122 l1->l1_domain_use_count = 0;
6123 l1->l1_domain_first = 0;
6124
6125 for (i = 0; i < PMAP_DOMAINS; i++)
6126 l1->l1_domain_free[i] = i + 1;
6127
6128 /*
6129 * Copy the kernel's L1 entries to each new L1.
6130 */
6131 if (pmap_initialized)
6132 memcpy(l1pt, pmap_l1_kva(pmap_kernel()), L1_TABLE_SIZE);
6133
6134 if (pmap_extract(pmap_kernel(), (vaddr_t)l1pt,
6135 &l1->l1_physaddr) == false)
6136 panic("pmap_init_l1: can't get PA of L1 at %p", l1pt);
6137
6138 SLIST_INSERT_HEAD(&l1_list, l1, l1_link);
6139 TAILQ_INSERT_TAIL(&l1_lru_list, l1, l1_lru);
6140 }
6141 #endif /* !ARM_MMU_EXTENDED */
6142
6143 /*
6144 * pmap_bootstrap() is called from the board-specific initarm() routine
6145 * once the kernel L1/L2 descriptors tables have been set up.
6146 *
6147 * This is a somewhat convoluted process since pmap bootstrap is, effectively,
6148 * spread over a number of disparate files/functions.
6149 *
6150 * We are passed the following parameters
6151 * - vstart
6152 * 1MB-aligned start of managed kernel virtual memory.
6153 * - vend
6154 * 1MB-aligned end of managed kernel virtual memory.
6155 *
6156 * We use 'kernel_l1pt' to build the metadata (struct l1_ttable and
6157 * struct l2_dtable) necessary to track kernel mappings.
6158 */
6159 #define PMAP_STATIC_L2_SIZE 16
6160 void
6161 pmap_bootstrap(vaddr_t vstart, vaddr_t vend)
6162 {
6163 static struct l2_dtable static_l2[PMAP_STATIC_L2_SIZE];
6164 #ifndef ARM_MMU_EXTENDED
6165 static struct l1_ttable static_l1;
6166 struct l1_ttable *l1 = &static_l1;
6167 #endif
6168 struct l2_dtable *l2;
6169 struct l2_bucket *l2b;
6170 pd_entry_t *l1pt = (pd_entry_t *) kernel_l1pt.pv_va;
6171 pmap_t pm = pmap_kernel();
6172 pt_entry_t *ptep;
6173 paddr_t pa;
6174 vsize_t size;
6175 int nptes, l2idx, l2next = 0;
6176
6177 #ifdef ARM_MMU_EXTENDED
6178 KASSERT(pte_l1_s_cache_mode == pte_l1_s_cache_mode_pt);
6179 KASSERT(pte_l2_s_cache_mode == pte_l2_s_cache_mode_pt);
6180 #endif
6181
6182 VPRINTF("kpm ");
6183 /*
6184 * Initialise the kernel pmap object
6185 */
6186 curcpu()->ci_pmap_cur = pm;
6187 #ifdef ARM_MMU_EXTENDED
6188 pm->pm_l1 = l1pt;
6189 pm->pm_l1_pa = kernel_l1pt.pv_pa;
6190 VPRINTF("tlb0 ");
6191 pmap_tlb_info_init(&pmap_tlb0_info);
6192 #ifdef MULTIPROCESSOR
6193 VPRINTF("kcpusets ");
6194 pm->pm_onproc = kcpuset_running;
6195 pm->pm_active = kcpuset_running;
6196 #endif
6197 #else
6198 pm->pm_l1 = l1;
6199 #endif
6200
6201 VPRINTF("locks ");
6202 /*
6203 * pmap_kenter_pa() and pmap_kremove() may be called from interrupt
6204 * context, so its locks have to be at IPL_VM
6205 */
6206 mutex_init(&pmap_lock, MUTEX_DEFAULT, IPL_VM);
6207 mutex_init(&kpm_lock, MUTEX_DEFAULT, IPL_NONE);
6208 mutex_init(&pm->pm_lock, MUTEX_DEFAULT, IPL_VM);
6209 pm->pm_refs = 1;
6210
6211 VPRINTF("l1pt ");
6212 /*
6213 * Scan the L1 translation table created by initarm() and create
6214 * the required metadata for all valid mappings found in it.
6215 */
6216 for (size_t l1slot = 0;
6217 l1slot < L1_TABLE_SIZE / sizeof(pd_entry_t);
6218 l1slot++) {
6219 pd_entry_t pde = l1pt[l1slot];
6220
6221 /*
6222 * We're only interested in Coarse mappings.
6223 * pmap_extract() can deal with section mappings without
6224 * recourse to checking L2 metadata.
6225 */
6226 if ((pde & L1_TYPE_MASK) != L1_TYPE_C)
6227 continue;
6228
6229 /*
6230 * Lookup the KVA of this L2 descriptor table
6231 */
6232 pa = l1pte_pa(pde);
6233 ptep = (pt_entry_t *)kernel_pt_lookup(pa);
6234 if (ptep == NULL) {
6235 panic("pmap_bootstrap: No L2 for va 0x%x, pa 0x%lx",
6236 (u_int)l1slot << L1_S_SHIFT, pa);
6237 }
6238
6239 /*
6240 * Fetch the associated L2 metadata structure.
6241 * Allocate a new one if necessary.
6242 */
6243 if ((l2 = pm->pm_l2[L2_IDX(l1slot)]) == NULL) {
6244 if (l2next == PMAP_STATIC_L2_SIZE)
6245 panic("pmap_bootstrap: out of static L2s");
6246 pm->pm_l2[L2_IDX(l1slot)] = l2 = &static_l2[l2next++];
6247 }
6248
6249 /*
6250 * One more L1 slot tracked...
6251 */
6252 l2->l2_occupancy++;
6253
6254 /*
6255 * Fill in the details of the L2 descriptor in the
6256 * appropriate bucket.
6257 */
6258 l2b = &l2->l2_bucket[L2_BUCKET(l1slot)];
6259 l2b->l2b_kva = ptep;
6260 l2b->l2b_pa = pa;
6261 l2b->l2b_l1slot = l1slot;
6262
6263 /*
6264 * Establish an initial occupancy count for this descriptor
6265 */
6266 for (l2idx = 0;
6267 l2idx < (L2_TABLE_SIZE_REAL / sizeof(pt_entry_t));
6268 l2idx++) {
6269 if ((ptep[l2idx] & L2_TYPE_MASK) != L2_TYPE_INV) {
6270 l2b->l2b_occupancy++;
6271 }
6272 }
6273
6274 /*
6275 * Make sure the descriptor itself has the correct cache mode.
6276 * If not, fix it, but whine about the problem. Port-meisters
6277 * should consider this a clue to fix up their initarm()
6278 * function. :)
6279 */
6280 if (pmap_set_pt_cache_mode(l1pt, (vaddr_t)ptep, 1)) {
6281 printf("pmap_bootstrap: WARNING! wrong cache mode for "
6282 "L2 pte @ %p\n", ptep);
6283 }
6284 }
6285
6286 VPRINTF("cache(l1pt) ");
6287 /*
6288 * Ensure the primary (kernel) L1 has the correct cache mode for
6289 * a page table. Bitch if it is not correctly set.
6290 */
6291 if (pmap_set_pt_cache_mode(l1pt, kernel_l1pt.pv_va,
6292 L1_TABLE_SIZE / L2_S_SIZE)) {
6293 printf("pmap_bootstrap: WARNING! wrong cache mode for "
6294 "primary L1 @ 0x%lx\n", kernel_l1pt.pv_va);
6295 }
6296
6297 #ifdef PMAP_CACHE_VIVT
6298 cpu_dcache_wbinv_all();
6299 cpu_tlb_flushID();
6300 cpu_cpwait();
6301 #endif
6302
6303 /*
6304 * now we allocate the "special" VAs which are used for tmp mappings
6305 * by the pmap (and other modules). we allocate the VAs by advancing
6306 * virtual_avail (note that there are no pages mapped at these VAs).
6307 *
6308 * Managed KVM space start from wherever initarm() tells us.
6309 */
6310 virtual_avail = vstart;
6311 virtual_end = vend;
6312
6313 VPRINTF("specials ");
6314 #ifdef PMAP_CACHE_VIPT
6315 /*
6316 * If we have a VIPT cache, we need one page/pte per possible alias
6317 * page so we won't violate cache aliasing rules.
6318 */
6319 virtual_avail = (virtual_avail + arm_cache_prefer_mask) & ~arm_cache_prefer_mask;
6320 nptes = (arm_cache_prefer_mask >> L2_S_SHIFT) + 1;
6321 nptes = roundup(nptes, PAGE_SIZE / L2_S_SIZE);
6322 if (arm_pcache.icache_type != CACHE_TYPE_PIPT
6323 && arm_pcache.icache_way_size > nptes * L2_S_SIZE) {
6324 nptes = arm_pcache.icache_way_size >> L2_S_SHIFT;
6325 nptes = roundup(nptes, PAGE_SIZE / L2_S_SIZE);
6326 }
6327 #else
6328 nptes = PAGE_SIZE / L2_S_SIZE;
6329 #endif
6330 #ifdef MULTIPROCESSOR
6331 cnptes = nptes;
6332 nptes *= arm_cpu_max;
6333 #endif
6334 pmap_alloc_specials(&virtual_avail, nptes, &csrcp, &csrc_pte);
6335 pmap_set_pt_cache_mode(l1pt, (vaddr_t)csrc_pte, nptes);
6336 pmap_alloc_specials(&virtual_avail, nptes, &cdstp, &cdst_pte);
6337 pmap_set_pt_cache_mode(l1pt, (vaddr_t)cdst_pte, nptes);
6338 pmap_alloc_specials(&virtual_avail, nptes, &memhook, NULL);
6339 if (msgbufaddr == NULL) {
6340 pmap_alloc_specials(&virtual_avail,
6341 round_page(MSGBUFSIZE) / PAGE_SIZE,
6342 (void *)&msgbufaddr, NULL);
6343 }
6344
6345 /*
6346 * Allocate a range of kernel virtual address space to be used
6347 * for L2 descriptor tables and metadata allocation in
6348 * pmap_growkernel().
6349 */
6350 size = ((virtual_end - pmap_curmaxkvaddr) + L1_S_OFFSET) / L1_S_SIZE;
6351 pmap_alloc_specials(&virtual_avail,
6352 round_page(size * L2_TABLE_SIZE_REAL) / PAGE_SIZE,
6353 &pmap_kernel_l2ptp_kva, NULL);
6354
6355 size = (size + (L2_BUCKET_SIZE - 1)) / L2_BUCKET_SIZE;
6356 pmap_alloc_specials(&virtual_avail,
6357 round_page(size * sizeof(struct l2_dtable)) / PAGE_SIZE,
6358 &pmap_kernel_l2dtable_kva, NULL);
6359
6360 #ifndef ARM_MMU_EXTENDED
6361 /*
6362 * init the static-global locks and global pmap list.
6363 */
6364 mutex_init(&l1_lru_lock, MUTEX_DEFAULT, IPL_VM);
6365
6366 /*
6367 * We can now initialise the first L1's metadata.
6368 */
6369 SLIST_INIT(&l1_list);
6370 TAILQ_INIT(&l1_lru_list);
6371 pmap_init_l1(l1, l1pt);
6372 #endif /* ARM_MMU_EXTENDED */
6373
6374 #ifndef ARM_HAS_VBAR
6375 /* Set up vector page L1 details, if necessary */
6376 if (vector_page < KERNEL_BASE) {
6377 pm->pm_pl1vec = pmap_l1_kva(pm) + l1pte_index(vector_page);
6378 l2b = pmap_get_l2_bucket(pm, vector_page);
6379 KDASSERT(l2b != NULL);
6380 pm->pm_l1vec = l2b->l2b_pa | L1_C_PROTO |
6381 L1_C_DOM(pmap_domain(pm));
6382 } else
6383 pm->pm_pl1vec = NULL;
6384 #endif
6385
6386 VPRINTF("pools ");
6387 /*
6388 * Initialize the pmap cache
6389 */
6390 pool_cache_bootstrap(&pmap_cache, sizeof(struct pmap), 0, 0, 0,
6391 "pmappl", NULL, IPL_NONE, pmap_pmap_ctor, NULL, NULL);
6392
6393 /*
6394 * Initialize the pv pool.
6395 */
6396 pool_init(&pmap_pv_pool, sizeof(struct pv_entry), 0, 0, 0, "pvepl",
6397 &pmap_bootstrap_pv_allocator, IPL_NONE);
6398
6399 /*
6400 * Initialize the L2 dtable pool and cache.
6401 */
6402 pool_cache_bootstrap(&pmap_l2dtable_cache, sizeof(struct l2_dtable), 0,
6403 0, 0, "l2dtblpl", NULL, IPL_NONE, pmap_l2dtable_ctor, NULL, NULL);
6404
6405 /*
6406 * Initialise the L2 descriptor table pool and cache
6407 */
6408 pool_cache_bootstrap(&pmap_l2ptp_cache, L2_TABLE_SIZE_REAL,
6409 L2_TABLE_SIZE_REAL, 0, 0, "l2ptppl", NULL, IPL_NONE,
6410 pmap_l2ptp_ctor, NULL, NULL);
6411
6412 mutex_init(&memlock, MUTEX_DEFAULT, IPL_NONE);
6413
6414 cpu_dcache_wbinv_all();
6415 }
6416
6417 static bool
6418 pmap_set_pt_cache_mode(pd_entry_t *kl1, vaddr_t va, size_t nptes)
6419 {
6420 #ifdef ARM_MMU_EXTENDED
6421 return false;
6422 #else
6423 if (pte_l1_s_cache_mode == pte_l1_s_cache_mode_pt
6424 && pte_l2_s_cache_mode == pte_l2_s_cache_mode_pt)
6425 return false;
6426
6427 const vaddr_t eva = va + nptes * PAGE_SIZE;
6428 int rv = 0;
6429
6430 while (va < eva) {
6431 /*
6432 * Make sure the descriptor itself has the correct cache mode
6433 */
6434 pd_entry_t * const pdep = &kl1[l1pte_index(va)];
6435 pd_entry_t pde = *pdep;
6436
6437 if (l1pte_section_p(pde)) {
6438 KASSERT((L1_S_CACHE_MASK & L1_S_V6_SUPER) == 0);
6439 if ((pde & L1_S_CACHE_MASK) != pte_l1_s_cache_mode_pt) {
6440 *pdep = (pde & ~L1_S_CACHE_MASK) |
6441 pte_l1_s_cache_mode_pt;
6442 PDE_SYNC(pdep);
6443 cpu_dcache_wbinv_range((vaddr_t)pdep,
6444 sizeof(*pdep));
6445 rv = 1;
6446 }
6447 return rv;
6448 }
6449 vaddr_t pa = l1pte_pa(pde);
6450 pt_entry_t *ptep = (pt_entry_t *)kernel_pt_lookup(pa);
6451 if (ptep == NULL)
6452 panic("pmap_bootstrap: No PTP for va %#lx\n", va);
6453
6454 ptep += l2pte_index(va);
6455 const pt_entry_t opte = *ptep;
6456 if ((opte & L2_S_CACHE_MASK) != pte_l2_s_cache_mode_pt) {
6457 const pt_entry_t npte = (opte & ~L2_S_CACHE_MASK)
6458 | pte_l2_s_cache_mode_pt;
6459 l2pte_set(ptep, npte, opte);
6460 PTE_SYNC(ptep);
6461 cpu_dcache_wbinv_range((vaddr_t)ptep, sizeof(*ptep));
6462 rv = 1;
6463 }
6464 va += PAGE_SIZE;
6465 }
6466
6467 return rv;
6468 #endif
6469 }
6470
6471 static void
6472 pmap_alloc_specials(vaddr_t *availp, int pages, vaddr_t *vap, pt_entry_t **ptep)
6473 {
6474 vaddr_t va = *availp;
6475 struct l2_bucket *l2b;
6476
6477 if (ptep) {
6478 l2b = pmap_get_l2_bucket(pmap_kernel(), va);
6479 if (l2b == NULL)
6480 panic("pmap_alloc_specials: no l2b for 0x%lx", va);
6481
6482 *ptep = &l2b->l2b_kva[l2pte_index(va)];
6483 }
6484
6485 *vap = va;
6486 *availp = va + (PAGE_SIZE * pages);
6487 }
6488
6489 void
6490 pmap_init(void)
6491 {
6492
6493 /*
6494 * Set the available memory vars - These do not map to real memory
6495 * addresses and cannot as the physical memory is fragmented.
6496 * They are used by ps for %mem calculations.
6497 * One could argue whether this should be the entire memory or just
6498 * the memory that is useable in a user process.
6499 */
6500 avail_start = ptoa(uvm_physseg_get_avail_start(uvm_physseg_get_first()));
6501 avail_end = ptoa(uvm_physseg_get_avail_end(uvm_physseg_get_last()));
6502
6503 /*
6504 * Now we need to free enough pv_entry structures to allow us to get
6505 * the kmem_map/kmem_object allocated and inited (done after this
6506 * function is finished). to do this we allocate one bootstrap page out
6507 * of kernel_map and use it to provide an initial pool of pv_entry
6508 * structures. we never free this page.
6509 */
6510 pool_setlowat(&pmap_pv_pool, (PAGE_SIZE / sizeof(struct pv_entry)) * 2);
6511
6512 #ifdef ARM_MMU_EXTENDED
6513 /*
6514 * Initialise the L1 pool and cache.
6515 */
6516
6517 pool_cache_bootstrap(&pmap_l1tt_cache, L1TT_SIZE, L1TT_SIZE,
6518 0, 0, "l1ttpl", &pmap_l1tt_allocator, IPL_NONE, pmap_l1tt_ctor,
6519 NULL, NULL);
6520
6521 int error __diagused = pmap_maxproc_set(maxproc);
6522 KASSERT(error == 0);
6523
6524 pmap_tlb_info_evcnt_attach(&pmap_tlb0_info);
6525 #endif
6526
6527 pmap_initialized = true;
6528 }
6529
6530 static vaddr_t last_bootstrap_page = 0;
6531 static void *free_bootstrap_pages = NULL;
6532
6533 static void *
6534 pmap_bootstrap_pv_page_alloc(struct pool *pp, int flags)
6535 {
6536 extern void *pool_page_alloc(struct pool *, int);
6537 vaddr_t new_page;
6538 void *rv;
6539
6540 if (pmap_initialized)
6541 return pool_page_alloc(pp, flags);
6542
6543 if (free_bootstrap_pages) {
6544 rv = free_bootstrap_pages;
6545 free_bootstrap_pages = *((void **)rv);
6546 return rv;
6547 }
6548
6549 KASSERT(kernel_map != NULL);
6550 new_page = uvm_km_alloc(kernel_map, PAGE_SIZE, 0,
6551 UVM_KMF_WIRED | ((flags & PR_WAITOK) ? 0 : UVM_KMF_NOWAIT));
6552
6553 KASSERT(new_page > last_bootstrap_page);
6554 last_bootstrap_page = new_page;
6555 return (void *)new_page;
6556 }
6557
6558 static void
6559 pmap_bootstrap_pv_page_free(struct pool *pp, void *v)
6560 {
6561 extern void pool_page_free(struct pool *, void *);
6562
6563 if ((vaddr_t)v <= last_bootstrap_page) {
6564 *((void **)v) = free_bootstrap_pages;
6565 free_bootstrap_pages = v;
6566 return;
6567 }
6568
6569 if (pmap_initialized) {
6570 pool_page_free(pp, v);
6571 return;
6572 }
6573 }
6574
6575
6576 #if defined(ARM_MMU_EXTENDED)
6577 static void *
6578 pmap_l1tt_alloc(struct pool *pp, int flags)
6579 {
6580 struct pglist plist;
6581 vaddr_t va;
6582
6583 const int waitok = flags & PR_WAITOK;
6584
6585 int error = uvm_pglistalloc(L1TT_SIZE, 0, -1, L1TT_SIZE, 0, &plist, 1,
6586 waitok);
6587 if (error)
6588 panic("Cannot allocate L1TT physical pages, %d", error);
6589
6590 struct vm_page *pg = TAILQ_FIRST(&plist);
6591 #if !defined( __HAVE_MM_MD_DIRECT_MAPPED_PHYS)
6592
6593 /* Allocate a L1 translation table VA */
6594 va = uvm_km_alloc(kernel_map, L1TT_SIZE, L1TT_SIZE, UVM_KMF_VAONLY);
6595 if (va == 0)
6596 panic("Cannot allocate L1TT KVA");
6597
6598 const vaddr_t eva = va + L1TT_SIZE;
6599 vaddr_t mva = va;
6600 while (pg && mva < eva) {
6601 paddr_t pa = VM_PAGE_TO_PHYS(pg);
6602
6603 pmap_kenter_pa(mva, pa,
6604 VM_PROT_READ|VM_PROT_WRITE, PMAP_KMPAGE|PMAP_PTE);
6605
6606 mva += PAGE_SIZE;
6607 pg = TAILQ_NEXT(pg, pageq.queue);
6608 }
6609 KASSERTMSG(pg == NULL && mva == eva, "pg %p mva %" PRIxVADDR
6610 " eva %" PRIxVADDR, pg, mva, eva);
6611 #else
6612 bool ok;
6613 paddr_t pa = VM_PAGE_TO_PHYS(pg);
6614 va = pmap_direct_mapped_phys(pa, &ok, 0);
6615 KASSERT(ok);
6616 KASSERT(va >= KERNEL_BASE);
6617 #endif
6618
6619 return (void *)va;
6620 }
6621
6622 static void
6623 pmap_l1tt_free(struct pool *pp, void *v)
6624 {
6625 vaddr_t va = (vaddr_t)v;
6626
6627 #if !defined( __HAVE_MM_MD_DIRECT_MAPPED_PHYS)
6628 uvm_km_free(kernel_map, va, L1TT_SIZE, UVM_KMF_WIRED);
6629 #else
6630 #if defined(KERNEL_BASE_VOFFSET)
6631 paddr_t pa = va - KERNEL_BASE_VOFFSET;
6632 #else
6633 paddr_t pa = va - KERNEL_BASE + physical_start;
6634 #endif
6635 const paddr_t epa = pa + L1TT_SIZE;
6636
6637 for (; pa < epa; pa += PAGE_SIZE) {
6638 struct vm_page *pg = PHYS_TO_VM_PAGE(pa);
6639 uvm_pagefree(pg);
6640 }
6641 #endif
6642 }
6643 #endif
6644
6645 /*
6646 * pmap_postinit()
6647 *
6648 * This routine is called after the vm and kmem subsystems have been
6649 * initialised. This allows the pmap code to perform any initialisation
6650 * that can only be done once the memory allocation is in place.
6651 */
6652 void
6653 pmap_postinit(void)
6654 {
6655 #ifndef ARM_MMU_EXTENDED
6656 extern paddr_t physical_start, physical_end;
6657 struct l1_ttable *l1;
6658 struct pglist plist;
6659 struct vm_page *m;
6660 pd_entry_t *pdep;
6661 vaddr_t va, eva;
6662 u_int loop, needed;
6663 int error;
6664 #endif
6665
6666 pool_cache_setlowat(&pmap_l2ptp_cache, (PAGE_SIZE / L2_TABLE_SIZE_REAL) * 4);
6667 pool_cache_setlowat(&pmap_l2dtable_cache,
6668 (PAGE_SIZE / sizeof(struct l2_dtable)) * 2);
6669
6670 #ifndef ARM_MMU_EXTENDED
6671 needed = (maxproc / PMAP_DOMAINS) + ((maxproc % PMAP_DOMAINS) ? 1 : 0);
6672 needed -= 1;
6673
6674 l1 = kmem_alloc(sizeof(*l1) * needed, KM_SLEEP);
6675
6676 for (loop = 0; loop < needed; loop++, l1++) {
6677 /* Allocate a L1 page table */
6678 va = uvm_km_alloc(kernel_map, L1_TABLE_SIZE, 0, UVM_KMF_VAONLY);
6679 if (va == 0)
6680 panic("Cannot allocate L1 KVM");
6681
6682 error = uvm_pglistalloc(L1_TABLE_SIZE, physical_start,
6683 physical_end, L1_TABLE_SIZE, 0, &plist, 1, 1);
6684 if (error)
6685 panic("Cannot allocate L1 physical pages");
6686
6687 m = TAILQ_FIRST(&plist);
6688 eva = va + L1_TABLE_SIZE;
6689 pdep = (pd_entry_t *)va;
6690
6691 while (m && va < eva) {
6692 paddr_t pa = VM_PAGE_TO_PHYS(m);
6693
6694 pmap_kenter_pa(va, pa,
6695 VM_PROT_READ|VM_PROT_WRITE, PMAP_KMPAGE|PMAP_PTE);
6696
6697 va += PAGE_SIZE;
6698 m = TAILQ_NEXT(m, pageq.queue);
6699 }
6700
6701 #ifdef DIAGNOSTIC
6702 if (m)
6703 panic("pmap_alloc_l1pt: pglist not empty");
6704 #endif /* DIAGNOSTIC */
6705
6706 pmap_init_l1(l1, pdep);
6707 }
6708
6709 #ifdef DEBUG
6710 printf("pmap_postinit: Allocated %d static L1 descriptor tables\n",
6711 needed);
6712 #endif
6713 #endif /* !ARM_MMU_EXTENDED */
6714 }
6715
6716 /*
6717 * Note that the following routines are used by board-specific initialisation
6718 * code to configure the initial kernel page tables.
6719 *
6720 */
6721
6722 /*
6723 * This list exists for the benefit of pmap_map_chunk(). It keeps track
6724 * of the kernel L2 tables during bootstrap, so that pmap_map_chunk() can
6725 * find them as necessary.
6726 *
6727 * Note that the data on this list MUST remain valid after initarm() returns,
6728 * as pmap_bootstrap() uses it to construct L2 table metadata.
6729 */
6730 SLIST_HEAD(, pv_addr) kernel_pt_list = SLIST_HEAD_INITIALIZER(kernel_pt_list);
6731
6732 static vaddr_t
6733 kernel_pt_lookup(paddr_t pa)
6734 {
6735 pv_addr_t *pv;
6736
6737 SLIST_FOREACH(pv, &kernel_pt_list, pv_list) {
6738 if (pv->pv_pa == (pa & ~PGOFSET))
6739 return pv->pv_va | (pa & PGOFSET);
6740 }
6741 return 0;
6742 }
6743
6744 /*
6745 * pmap_map_section:
6746 *
6747 * Create a single section mapping.
6748 */
6749 void
6750 pmap_map_section(vaddr_t l1pt, vaddr_t va, paddr_t pa, int prot, int cache)
6751 {
6752 pd_entry_t * const pdep = (pd_entry_t *) l1pt;
6753 const size_t l1slot = l1pte_index(va);
6754 pd_entry_t fl;
6755
6756 KASSERT(((va | pa) & L1_S_OFFSET) == 0);
6757
6758 switch (cache) {
6759 case PTE_NOCACHE:
6760 fl = pte_l1_s_nocache_mode;
6761 break;
6762
6763 case PTE_CACHE:
6764 fl = pte_l1_s_cache_mode;
6765 break;
6766
6767 case PTE_PAGETABLE:
6768 fl = pte_l1_s_cache_mode_pt;
6769 break;
6770
6771 case PTE_DEV:
6772 default:
6773 fl = 0;
6774 break;
6775 }
6776
6777 const pd_entry_t npde = L1_S_PROTO | pa |
6778 L1_S_PROT(PTE_KERNEL, prot) | fl | L1_S_DOM(PMAP_DOMAIN_KERNEL);
6779 l1pte_setone(pdep + l1slot, npde);
6780 PDE_SYNC(pdep + l1slot);
6781 }
6782
6783 /*
6784 * pmap_map_entry:
6785 *
6786 * Create a single page mapping.
6787 */
6788 void
6789 pmap_map_entry(vaddr_t l1pt, vaddr_t va, paddr_t pa, int prot, int cache)
6790 {
6791 pd_entry_t * const pdep = (pd_entry_t *) l1pt;
6792 const size_t l1slot = l1pte_index(va);
6793 pt_entry_t npte;
6794 pt_entry_t *ptep;
6795
6796 KASSERT(((va | pa) & PGOFSET) == 0);
6797
6798 switch (cache) {
6799 case PTE_NOCACHE:
6800 npte = pte_l2_s_nocache_mode;
6801 break;
6802
6803 case PTE_CACHE:
6804 npte = pte_l2_s_cache_mode;
6805 break;
6806
6807 case PTE_PAGETABLE:
6808 npte = pte_l2_s_cache_mode_pt;
6809 break;
6810
6811 default:
6812 npte = 0;
6813 break;
6814 }
6815
6816 if ((pdep[l1slot] & L1_TYPE_MASK) != L1_TYPE_C)
6817 panic("pmap_map_entry: no L2 table for VA 0x%08lx", va);
6818
6819 ptep = (pt_entry_t *) kernel_pt_lookup(l1pte_pa(pdep[l1slot]));
6820 if (ptep == NULL)
6821 panic("pmap_map_entry: can't find L2 table for VA 0x%08lx", va);
6822
6823 npte |= L2_S_PROTO | pa | L2_S_PROT(PTE_KERNEL, prot);
6824 #ifdef ARM_MMU_EXTENDED
6825 if (prot & VM_PROT_EXECUTE) {
6826 npte &= ~L2_XS_XN;
6827 }
6828 #endif
6829 ptep += l2pte_index(va);
6830 l2pte_set(ptep, npte, 0);
6831 PTE_SYNC(ptep);
6832 }
6833
6834 /*
6835 * pmap_link_l2pt:
6836 *
6837 * Link the L2 page table specified by "l2pv" into the L1
6838 * page table at the slot for "va".
6839 */
6840 void
6841 pmap_link_l2pt(vaddr_t l1pt, vaddr_t va, pv_addr_t *l2pv)
6842 {
6843 pd_entry_t * const pdep = (pd_entry_t *) l1pt + l1pte_index(va);
6844
6845 KASSERT((va & ((L1_S_SIZE * (PAGE_SIZE / L2_T_SIZE)) - 1)) == 0);
6846 KASSERT((l2pv->pv_pa & PGOFSET) == 0);
6847
6848 const pd_entry_t npde = L1_C_DOM(PMAP_DOMAIN_KERNEL) | L1_C_PROTO
6849 | l2pv->pv_pa;
6850
6851 l1pte_set(pdep, npde);
6852 PDE_SYNC_RANGE(pdep, PAGE_SIZE / L2_T_SIZE);
6853
6854 SLIST_INSERT_HEAD(&kernel_pt_list, l2pv, pv_list);
6855 }
6856
6857 /*
6858 * pmap_map_chunk:
6859 *
6860 * Map a chunk of memory using the most efficient mappings
6861 * possible (section, large page, small page) into the
6862 * provided L1 and L2 tables at the specified virtual address.
6863 */
6864 vsize_t
6865 pmap_map_chunk(vaddr_t l1pt, vaddr_t va, paddr_t pa, vsize_t size,
6866 int prot, int cache)
6867 {
6868 pd_entry_t * const pdep = (pd_entry_t *) l1pt;
6869 pt_entry_t f1, f2s, f2l;
6870 vsize_t resid;
6871
6872 resid = (size + (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
6873
6874 if (l1pt == 0)
6875 panic("pmap_map_chunk: no L1 table provided");
6876
6877 // VPRINTF("pmap_map_chunk: pa=0x%lx va=0x%lx size=0x%lx resid=0x%lx "
6878 // "prot=0x%x cache=%d\n", pa, va, size, resid, prot, cache);
6879
6880 switch (cache) {
6881 case PTE_NOCACHE:
6882 f1 = pte_l1_s_nocache_mode;
6883 f2l = pte_l2_l_nocache_mode;
6884 f2s = pte_l2_s_nocache_mode;
6885 break;
6886
6887 case PTE_CACHE:
6888 f1 = pte_l1_s_cache_mode;
6889 f2l = pte_l2_l_cache_mode;
6890 f2s = pte_l2_s_cache_mode;
6891 break;
6892
6893 case PTE_PAGETABLE:
6894 f1 = pte_l1_s_cache_mode_pt;
6895 f2l = pte_l2_l_cache_mode_pt;
6896 f2s = pte_l2_s_cache_mode_pt;
6897 break;
6898
6899 case PTE_DEV:
6900 default:
6901 f1 = 0;
6902 f2l = 0;
6903 f2s = 0;
6904 break;
6905 }
6906
6907 size = resid;
6908
6909 while (resid > 0) {
6910 const size_t l1slot = l1pte_index(va);
6911 #ifdef ARM_MMU_EXTENDED
6912 /* See if we can use a supersection mapping. */
6913 if (L1_SS_PROTO && L1_SS_MAPPABLE_P(va, pa, resid)) {
6914 /* Supersection are always domain 0 */
6915 const pd_entry_t npde = L1_SS_PROTO | pa
6916 | ((prot & VM_PROT_EXECUTE) ? 0 : L1_S_V6_XN)
6917 | (va & 0x80000000 ? 0 : L1_S_V6_nG)
6918 | L1_S_PROT(PTE_KERNEL, prot) | f1;
6919 VPRINTF("sS");
6920 l1pte_set(&pdep[l1slot], npde);
6921 PDE_SYNC_RANGE(&pdep[l1slot], L1_SS_SIZE / L1_S_SIZE);
6922 // VPRINTF("\npmap_map_chunk: pa=0x%lx va=0x%lx resid=0x%08lx "
6923 // "npdep=%p pde=0x%x\n", pa, va, resid, &pdep[l1slot], npde);
6924 va += L1_SS_SIZE;
6925 pa += L1_SS_SIZE;
6926 resid -= L1_SS_SIZE;
6927 continue;
6928 }
6929 #endif
6930 /* See if we can use a section mapping. */
6931 if (L1_S_MAPPABLE_P(va, pa, resid)) {
6932 const pd_entry_t npde = L1_S_PROTO | pa
6933 #ifdef ARM_MMU_EXTENDED
6934 | ((prot & VM_PROT_EXECUTE) ? 0 : L1_S_V6_XN)
6935 | (va & 0x80000000 ? 0 : L1_S_V6_nG)
6936 #endif
6937 | L1_S_PROT(PTE_KERNEL, prot) | f1
6938 | L1_S_DOM(PMAP_DOMAIN_KERNEL);
6939 VPRINTF("S");
6940 l1pte_set(&pdep[l1slot], npde);
6941 PDE_SYNC(&pdep[l1slot]);
6942 // VPRINTF("\npmap_map_chunk: pa=0x%lx va=0x%lx resid=0x%08lx "
6943 // "npdep=%p pde=0x%x\n", pa, va, resid, &pdep[l1slot], npde);
6944 va += L1_S_SIZE;
6945 pa += L1_S_SIZE;
6946 resid -= L1_S_SIZE;
6947 continue;
6948 }
6949
6950 /*
6951 * Ok, we're going to use an L2 table. Make sure
6952 * one is actually in the corresponding L1 slot
6953 * for the current VA.
6954 */
6955 if ((pdep[l1slot] & L1_TYPE_MASK) != L1_TYPE_C)
6956 panic("%s: no L2 table for VA %#lx", __func__, va);
6957
6958 pt_entry_t *ptep = (pt_entry_t *) kernel_pt_lookup(l1pte_pa(pdep[l1slot]));
6959 if (ptep == NULL)
6960 panic("%s: can't find L2 table for VA %#lx", __func__,
6961 va);
6962
6963 ptep += l2pte_index(va);
6964
6965 /* See if we can use a L2 large page mapping. */
6966 if (L2_L_MAPPABLE_P(va, pa, resid)) {
6967 const pt_entry_t npte = L2_L_PROTO | pa
6968 #ifdef ARM_MMU_EXTENDED
6969 | ((prot & VM_PROT_EXECUTE) ? 0 : L2_XS_L_XN)
6970 | (va & 0x80000000 ? 0 : L2_XS_nG)
6971 #endif
6972 | L2_L_PROT(PTE_KERNEL, prot) | f2l;
6973 VPRINTF("L");
6974 l2pte_set(ptep, npte, 0);
6975 PTE_SYNC_RANGE(ptep, L2_L_SIZE / L2_S_SIZE);
6976 va += L2_L_SIZE;
6977 pa += L2_L_SIZE;
6978 resid -= L2_L_SIZE;
6979 continue;
6980 }
6981
6982 VPRINTF("P");
6983 /* Use a small page mapping. */
6984 pt_entry_t npte = L2_S_PROTO | pa
6985 #ifdef ARM_MMU_EXTENDED
6986 | ((prot & VM_PROT_EXECUTE) ? 0 : L2_XS_XN)
6987 | (va & 0x80000000 ? 0 : L2_XS_nG)
6988 #endif
6989 | L2_S_PROT(PTE_KERNEL, prot) | f2s;
6990 #ifdef ARM_MMU_EXTENDED
6991 npte &= ((prot & VM_PROT_EXECUTE) ? ~L2_XS_XN : ~0);
6992 #endif
6993 l2pte_set(ptep, npte, 0);
6994 PTE_SYNC(ptep);
6995 va += PAGE_SIZE;
6996 pa += PAGE_SIZE;
6997 resid -= PAGE_SIZE;
6998 }
6999 VPRINTF("\n");
7000 return size;
7001 }
7002
7003 /*
7004 * pmap_unmap_chunk:
7005 *
7006 * Unmap a chunk of memory that was previously pmap_map_chunk
7007 */
7008 void
7009 pmap_unmap_chunk(vaddr_t l1pt, vaddr_t va, vsize_t size)
7010 {
7011 pd_entry_t * const pdep = (pd_entry_t *) l1pt;
7012 const size_t l1slot = l1pte_index(va);
7013
7014 KASSERT(size == L1_SS_SIZE || size == L1_S_SIZE);
7015
7016 l1pte_set(&pdep[l1slot], 0);
7017 PDE_SYNC_RANGE(&pdep[l1slot], size / L1_S_SIZE);
7018
7019 pmap_tlb_flush_SE(pmap_kernel(), va, PVF_REF);
7020 }
7021
7022
7023
7024 /********************** Static device map routines ***************************/
7025
7026 static const struct pmap_devmap *pmap_devmap_table;
7027
7028 /*
7029 * Register the devmap table. This is provided in case early console
7030 * initialization needs to register mappings created by bootstrap code
7031 * before pmap_devmap_bootstrap() is called.
7032 */
7033 void
7034 pmap_devmap_register(const struct pmap_devmap *table)
7035 {
7036
7037 pmap_devmap_table = table;
7038 }
7039
7040 /*
7041 * Map all of the static regions in the devmap table, and remember
7042 * the devmap table so other parts of the kernel can look up entries
7043 * later.
7044 */
7045 void
7046 pmap_devmap_bootstrap(vaddr_t l1pt, const struct pmap_devmap *table)
7047 {
7048 int i;
7049
7050 pmap_devmap_table = table;
7051
7052 for (i = 0; pmap_devmap_table[i].pd_size != 0; i++) {
7053 const struct pmap_devmap *pdp = &pmap_devmap_table[i];
7054
7055 KASSERTMSG(VADDR_MAX - pdp->pd_va >= pdp->pd_size - 1, "va %" PRIxVADDR
7056 " sz %" PRIxPSIZE, pdp->pd_va, pdp->pd_size);
7057 KASSERTMSG(PADDR_MAX - pdp->pd_pa >= pdp->pd_size - 1, "pa %" PRIxPADDR
7058 " sz %" PRIxPSIZE, pdp->pd_pa, pdp->pd_size);
7059 VPRINTF("devmap: %08lx -> %08lx @ %08lx\n", pdp->pd_pa,
7060 pdp->pd_pa + pdp->pd_size - 1, pdp->pd_va);
7061
7062 pmap_map_chunk(l1pt, pdp->pd_va, pdp->pd_pa, pdp->pd_size,
7063 pdp->pd_prot, pdp->pd_cache);
7064 }
7065 }
7066
7067 const struct pmap_devmap *
7068 pmap_devmap_find_pa(paddr_t pa, psize_t size)
7069 {
7070 uint64_t endpa;
7071 int i;
7072
7073 if (pmap_devmap_table == NULL)
7074 return NULL;
7075
7076 endpa = (uint64_t)pa + (uint64_t)(size - 1);
7077
7078 for (i = 0; pmap_devmap_table[i].pd_size != 0; i++) {
7079 if (pa >= pmap_devmap_table[i].pd_pa &&
7080 endpa <= (uint64_t)pmap_devmap_table[i].pd_pa +
7081 (uint64_t)(pmap_devmap_table[i].pd_size - 1))
7082 return &pmap_devmap_table[i];
7083 }
7084
7085 return NULL;
7086 }
7087
7088 const struct pmap_devmap *
7089 pmap_devmap_find_va(vaddr_t va, vsize_t size)
7090 {
7091 int i;
7092
7093 if (pmap_devmap_table == NULL)
7094 return NULL;
7095
7096 for (i = 0; pmap_devmap_table[i].pd_size != 0; i++) {
7097 if (va >= pmap_devmap_table[i].pd_va &&
7098 va + size - 1 <= pmap_devmap_table[i].pd_va +
7099 pmap_devmap_table[i].pd_size - 1)
7100 return &pmap_devmap_table[i];
7101 }
7102
7103 return NULL;
7104 }
7105
7106 /********************** PTE initialization routines **************************/
7107
7108 /*
7109 * These routines are called when the CPU type is identified to set up
7110 * the PTE prototypes, cache modes, etc.
7111 *
7112 * The variables are always here, just in case modules need to reference
7113 * them (though, they shouldn't).
7114 */
7115
7116 pt_entry_t pte_l1_s_nocache_mode;
7117 pt_entry_t pte_l1_s_cache_mode;
7118 pt_entry_t pte_l1_s_wc_mode;
7119 pt_entry_t pte_l1_s_cache_mode_pt;
7120 pt_entry_t pte_l1_s_cache_mask;
7121
7122 pt_entry_t pte_l2_l_nocache_mode;
7123 pt_entry_t pte_l2_l_cache_mode;
7124 pt_entry_t pte_l2_l_wc_mode;
7125 pt_entry_t pte_l2_l_cache_mode_pt;
7126 pt_entry_t pte_l2_l_cache_mask;
7127
7128 pt_entry_t pte_l2_s_nocache_mode;
7129 pt_entry_t pte_l2_s_cache_mode;
7130 pt_entry_t pte_l2_s_wc_mode;
7131 pt_entry_t pte_l2_s_cache_mode_pt;
7132 pt_entry_t pte_l2_s_cache_mask;
7133
7134 pt_entry_t pte_l1_s_prot_u;
7135 pt_entry_t pte_l1_s_prot_w;
7136 pt_entry_t pte_l1_s_prot_ro;
7137 pt_entry_t pte_l1_s_prot_mask;
7138
7139 pt_entry_t pte_l2_s_prot_u;
7140 pt_entry_t pte_l2_s_prot_w;
7141 pt_entry_t pte_l2_s_prot_ro;
7142 pt_entry_t pte_l2_s_prot_mask;
7143
7144 pt_entry_t pte_l2_l_prot_u;
7145 pt_entry_t pte_l2_l_prot_w;
7146 pt_entry_t pte_l2_l_prot_ro;
7147 pt_entry_t pte_l2_l_prot_mask;
7148
7149 pt_entry_t pte_l1_ss_proto;
7150 pt_entry_t pte_l1_s_proto;
7151 pt_entry_t pte_l1_c_proto;
7152 pt_entry_t pte_l2_s_proto;
7153
7154 void (*pmap_copy_page_func)(paddr_t, paddr_t);
7155 void (*pmap_zero_page_func)(paddr_t);
7156
7157 #if (ARM_MMU_GENERIC + ARM_MMU_SA1 + ARM_MMU_V6 + ARM_MMU_V7) != 0
7158 void
7159 pmap_pte_init_generic(void)
7160 {
7161
7162 pte_l1_s_nocache_mode = 0;
7163 pte_l1_s_cache_mode = L1_S_B|L1_S_C;
7164 pte_l1_s_wc_mode = L1_S_B;
7165 pte_l1_s_cache_mask = L1_S_CACHE_MASK_generic;
7166
7167 pte_l2_l_nocache_mode = 0;
7168 pte_l2_l_cache_mode = L2_B|L2_C;
7169 pte_l2_l_wc_mode = L2_B;
7170 pte_l2_l_cache_mask = L2_L_CACHE_MASK_generic;
7171
7172 pte_l2_s_nocache_mode = 0;
7173 pte_l2_s_cache_mode = L2_B|L2_C;
7174 pte_l2_s_wc_mode = L2_B;
7175 pte_l2_s_cache_mask = L2_S_CACHE_MASK_generic;
7176
7177 /*
7178 * If we have a write-through cache, set B and C. If
7179 * we have a write-back cache, then we assume setting
7180 * only C will make those pages write-through (except for those
7181 * Cortex CPUs which can read the L1 caches).
7182 */
7183 if (cpufuncs.cf_dcache_wb_range == (void *) cpufunc_nullop
7184 #if ARM_MMU_V7 > 0
7185 || CPU_ID_CORTEX_P(curcpu()->ci_arm_cpuid)
7186 #endif
7187 #if ARM_MMU_V6 > 0
7188 || CPU_ID_ARM11_P(curcpu()->ci_arm_cpuid) /* arm116 errata 399234 */
7189 #endif
7190 || false) {
7191 pte_l1_s_cache_mode_pt = L1_S_B|L1_S_C;
7192 pte_l2_l_cache_mode_pt = L2_B|L2_C;
7193 pte_l2_s_cache_mode_pt = L2_B|L2_C;
7194 } else {
7195 pte_l1_s_cache_mode_pt = L1_S_C; /* write through */
7196 pte_l2_l_cache_mode_pt = L2_C; /* write through */
7197 pte_l2_s_cache_mode_pt = L2_C; /* write through */
7198 }
7199
7200 pte_l1_s_prot_u = L1_S_PROT_U_generic;
7201 pte_l1_s_prot_w = L1_S_PROT_W_generic;
7202 pte_l1_s_prot_ro = L1_S_PROT_RO_generic;
7203 pte_l1_s_prot_mask = L1_S_PROT_MASK_generic;
7204
7205 pte_l2_s_prot_u = L2_S_PROT_U_generic;
7206 pte_l2_s_prot_w = L2_S_PROT_W_generic;
7207 pte_l2_s_prot_ro = L2_S_PROT_RO_generic;
7208 pte_l2_s_prot_mask = L2_S_PROT_MASK_generic;
7209
7210 pte_l2_l_prot_u = L2_L_PROT_U_generic;
7211 pte_l2_l_prot_w = L2_L_PROT_W_generic;
7212 pte_l2_l_prot_ro = L2_L_PROT_RO_generic;
7213 pte_l2_l_prot_mask = L2_L_PROT_MASK_generic;
7214
7215 pte_l1_ss_proto = L1_SS_PROTO_generic;
7216 pte_l1_s_proto = L1_S_PROTO_generic;
7217 pte_l1_c_proto = L1_C_PROTO_generic;
7218 pte_l2_s_proto = L2_S_PROTO_generic;
7219
7220 pmap_copy_page_func = pmap_copy_page_generic;
7221 pmap_zero_page_func = pmap_zero_page_generic;
7222 }
7223
7224 #if defined(CPU_ARM8)
7225 void
7226 pmap_pte_init_arm8(void)
7227 {
7228
7229 /*
7230 * ARM8 is compatible with generic, but we need to use
7231 * the page tables uncached.
7232 */
7233 pmap_pte_init_generic();
7234
7235 pte_l1_s_cache_mode_pt = 0;
7236 pte_l2_l_cache_mode_pt = 0;
7237 pte_l2_s_cache_mode_pt = 0;
7238 }
7239 #endif /* CPU_ARM8 */
7240
7241 #if defined(CPU_ARM9) && defined(ARM9_CACHE_WRITE_THROUGH)
7242 void
7243 pmap_pte_init_arm9(void)
7244 {
7245
7246 /*
7247 * ARM9 is compatible with generic, but we want to use
7248 * write-through caching for now.
7249 */
7250 pmap_pte_init_generic();
7251
7252 pte_l1_s_cache_mode = L1_S_C;
7253 pte_l2_l_cache_mode = L2_C;
7254 pte_l2_s_cache_mode = L2_C;
7255
7256 pte_l1_s_wc_mode = L1_S_B;
7257 pte_l2_l_wc_mode = L2_B;
7258 pte_l2_s_wc_mode = L2_B;
7259
7260 pte_l1_s_cache_mode_pt = L1_S_C;
7261 pte_l2_l_cache_mode_pt = L2_C;
7262 pte_l2_s_cache_mode_pt = L2_C;
7263 }
7264 #endif /* CPU_ARM9 && ARM9_CACHE_WRITE_THROUGH */
7265 #endif /* (ARM_MMU_GENERIC + ARM_MMU_SA1 + ARM_MMU_V6) != 0 */
7266
7267 #if defined(CPU_ARM10)
7268 void
7269 pmap_pte_init_arm10(void)
7270 {
7271
7272 /*
7273 * ARM10 is compatible with generic, but we want to use
7274 * write-through caching for now.
7275 */
7276 pmap_pte_init_generic();
7277
7278 pte_l1_s_cache_mode = L1_S_B | L1_S_C;
7279 pte_l2_l_cache_mode = L2_B | L2_C;
7280 pte_l2_s_cache_mode = L2_B | L2_C;
7281
7282 pte_l1_s_cache_mode = L1_S_B;
7283 pte_l2_l_cache_mode = L2_B;
7284 pte_l2_s_cache_mode = L2_B;
7285
7286 pte_l1_s_cache_mode_pt = L1_S_C;
7287 pte_l2_l_cache_mode_pt = L2_C;
7288 pte_l2_s_cache_mode_pt = L2_C;
7289
7290 }
7291 #endif /* CPU_ARM10 */
7292
7293 #if defined(CPU_ARM11) && defined(ARM11_CACHE_WRITE_THROUGH)
7294 void
7295 pmap_pte_init_arm11(void)
7296 {
7297
7298 /*
7299 * ARM11 is compatible with generic, but we want to use
7300 * write-through caching for now.
7301 */
7302 pmap_pte_init_generic();
7303
7304 pte_l1_s_cache_mode = L1_S_C;
7305 pte_l2_l_cache_mode = L2_C;
7306 pte_l2_s_cache_mode = L2_C;
7307
7308 pte_l1_s_wc_mode = L1_S_B;
7309 pte_l2_l_wc_mode = L2_B;
7310 pte_l2_s_wc_mode = L2_B;
7311
7312 pte_l1_s_cache_mode_pt = L1_S_C;
7313 pte_l2_l_cache_mode_pt = L2_C;
7314 pte_l2_s_cache_mode_pt = L2_C;
7315 }
7316 #endif /* CPU_ARM11 && ARM11_CACHE_WRITE_THROUGH */
7317
7318 #if ARM_MMU_SA1 == 1
7319 void
7320 pmap_pte_init_sa1(void)
7321 {
7322
7323 /*
7324 * The StrongARM SA-1 cache does not have a write-through
7325 * mode. So, do the generic initialization, then reset
7326 * the page table cache mode to B=1,C=1, and note that
7327 * the PTEs need to be sync'd.
7328 */
7329 pmap_pte_init_generic();
7330
7331 pte_l1_s_cache_mode_pt = L1_S_B|L1_S_C;
7332 pte_l2_l_cache_mode_pt = L2_B|L2_C;
7333 pte_l2_s_cache_mode_pt = L2_B|L2_C;
7334
7335 pmap_needs_pte_sync = 1;
7336 }
7337 #endif /* ARM_MMU_SA1 == 1*/
7338
7339 #if ARM_MMU_XSCALE == 1
7340 #if (ARM_NMMUS > 1)
7341 static u_int xscale_use_minidata;
7342 #endif
7343
7344 void
7345 pmap_pte_init_xscale(void)
7346 {
7347 uint32_t auxctl;
7348 int write_through = 0;
7349
7350 pte_l1_s_cache_mode = L1_S_B|L1_S_C;
7351 pte_l1_s_wc_mode = L1_S_B;
7352 pte_l1_s_cache_mask = L1_S_CACHE_MASK_xscale;
7353
7354 pte_l2_l_cache_mode = L2_B|L2_C;
7355 pte_l2_l_wc_mode = L2_B;
7356 pte_l2_l_cache_mask = L2_L_CACHE_MASK_xscale;
7357
7358 pte_l2_s_cache_mode = L2_B|L2_C;
7359 pte_l2_s_wc_mode = L2_B;
7360 pte_l2_s_cache_mask = L2_S_CACHE_MASK_xscale;
7361
7362 pte_l1_s_cache_mode_pt = L1_S_C;
7363 pte_l2_l_cache_mode_pt = L2_C;
7364 pte_l2_s_cache_mode_pt = L2_C;
7365
7366 #ifdef XSCALE_CACHE_READ_WRITE_ALLOCATE
7367 /*
7368 * The XScale core has an enhanced mode where writes that
7369 * miss the cache cause a cache line to be allocated. This
7370 * is significantly faster than the traditional, write-through
7371 * behavior of this case.
7372 */
7373 pte_l1_s_cache_mode |= L1_S_XS_TEX(TEX_XSCALE_X);
7374 pte_l2_l_cache_mode |= L2_XS_L_TEX(TEX_XSCALE_X);
7375 pte_l2_s_cache_mode |= L2_XS_T_TEX(TEX_XSCALE_X);
7376 #endif /* XSCALE_CACHE_READ_WRITE_ALLOCATE */
7377
7378 #ifdef XSCALE_CACHE_WRITE_THROUGH
7379 /*
7380 * Some versions of the XScale core have various bugs in
7381 * their cache units, the work-around for which is to run
7382 * the cache in write-through mode. Unfortunately, this
7383 * has a major (negative) impact on performance. So, we
7384 * go ahead and run fast-and-loose, in the hopes that we
7385 * don't line up the planets in a way that will trip the
7386 * bugs.
7387 *
7388 * However, we give you the option to be slow-but-correct.
7389 */
7390 write_through = 1;
7391 #elif defined(XSCALE_CACHE_WRITE_BACK)
7392 /* force write back cache mode */
7393 write_through = 0;
7394 #elif defined(CPU_XSCALE_PXA250) || defined(CPU_XSCALE_PXA270)
7395 /*
7396 * Intel PXA2[15]0 processors are known to have a bug in
7397 * write-back cache on revision 4 and earlier (stepping
7398 * A[01] and B[012]). Fixed for C0 and later.
7399 */
7400 {
7401 uint32_t id, type;
7402
7403 id = cpufunc_id();
7404 type = id & ~(CPU_ID_XSCALE_COREREV_MASK|CPU_ID_REVISION_MASK);
7405
7406 if (type == CPU_ID_PXA250 || type == CPU_ID_PXA210) {
7407 if ((id & CPU_ID_REVISION_MASK) < 5) {
7408 /* write through for stepping A0-1 and B0-2 */
7409 write_through = 1;
7410 }
7411 }
7412 }
7413 #endif /* XSCALE_CACHE_WRITE_THROUGH */
7414
7415 if (write_through) {
7416 pte_l1_s_cache_mode = L1_S_C;
7417 pte_l2_l_cache_mode = L2_C;
7418 pte_l2_s_cache_mode = L2_C;
7419 }
7420
7421 #if (ARM_NMMUS > 1)
7422 xscale_use_minidata = 1;
7423 #endif
7424
7425 pte_l1_s_prot_u = L1_S_PROT_U_xscale;
7426 pte_l1_s_prot_w = L1_S_PROT_W_xscale;
7427 pte_l1_s_prot_ro = L1_S_PROT_RO_xscale;
7428 pte_l1_s_prot_mask = L1_S_PROT_MASK_xscale;
7429
7430 pte_l2_s_prot_u = L2_S_PROT_U_xscale;
7431 pte_l2_s_prot_w = L2_S_PROT_W_xscale;
7432 pte_l2_s_prot_ro = L2_S_PROT_RO_xscale;
7433 pte_l2_s_prot_mask = L2_S_PROT_MASK_xscale;
7434
7435 pte_l2_l_prot_u = L2_L_PROT_U_xscale;
7436 pte_l2_l_prot_w = L2_L_PROT_W_xscale;
7437 pte_l2_l_prot_ro = L2_L_PROT_RO_xscale;
7438 pte_l2_l_prot_mask = L2_L_PROT_MASK_xscale;
7439
7440 pte_l1_ss_proto = L1_SS_PROTO_xscale;
7441 pte_l1_s_proto = L1_S_PROTO_xscale;
7442 pte_l1_c_proto = L1_C_PROTO_xscale;
7443 pte_l2_s_proto = L2_S_PROTO_xscale;
7444
7445 pmap_copy_page_func = pmap_copy_page_xscale;
7446 pmap_zero_page_func = pmap_zero_page_xscale;
7447
7448 /*
7449 * Disable ECC protection of page table access, for now.
7450 */
7451 auxctl = armreg_auxctl_read();
7452 auxctl &= ~XSCALE_AUXCTL_P;
7453 armreg_auxctl_write(auxctl);
7454 }
7455
7456 /*
7457 * xscale_setup_minidata:
7458 *
7459 * Set up the mini-data cache clean area. We require the
7460 * caller to allocate the right amount of physically and
7461 * virtually contiguous space.
7462 */
7463 void
7464 xscale_setup_minidata(vaddr_t l1pt, vaddr_t va, paddr_t pa)
7465 {
7466 extern vaddr_t xscale_minidata_clean_addr;
7467 extern vsize_t xscale_minidata_clean_size; /* already initialized */
7468 pd_entry_t *pde = (pd_entry_t *) l1pt;
7469 vsize_t size;
7470 uint32_t auxctl;
7471
7472 xscale_minidata_clean_addr = va;
7473
7474 /* Round it to page size. */
7475 size = (xscale_minidata_clean_size + L2_S_OFFSET) & L2_S_FRAME;
7476
7477 for (; size != 0;
7478 va += L2_S_SIZE, pa += L2_S_SIZE, size -= L2_S_SIZE) {
7479 const size_t l1slot = l1pte_index(va);
7480 pt_entry_t *ptep = (pt_entry_t *) kernel_pt_lookup(l1pte_pa(pde[l1slot]));
7481 if (ptep == NULL)
7482 panic("xscale_setup_minidata: can't find L2 table for "
7483 "VA 0x%08lx", va);
7484
7485 ptep += l2pte_index(va);
7486 pt_entry_t opte = *ptep;
7487 l2pte_set(ptep,
7488 L2_S_PROTO | pa | L2_S_PROT(PTE_KERNEL, VM_PROT_READ)
7489 | L2_C | L2_XS_T_TEX(TEX_XSCALE_X), opte);
7490 }
7491
7492 /*
7493 * Configure the mini-data cache for write-back with
7494 * read/write-allocate.
7495 *
7496 * NOTE: In order to reconfigure the mini-data cache, we must
7497 * make sure it contains no valid data! In order to do that,
7498 * we must issue a global data cache invalidate command!
7499 *
7500 * WE ASSUME WE ARE RUNNING UN-CACHED WHEN THIS ROUTINE IS CALLED!
7501 * THIS IS VERY IMPORTANT!
7502 */
7503
7504 /* Invalidate data and mini-data. */
7505 __asm volatile("mcr p15, 0, %0, c7, c6, 0" : : "r" (0));
7506 auxctl = armreg_auxctl_read();
7507 auxctl = (auxctl & ~XSCALE_AUXCTL_MD_MASK) | XSCALE_AUXCTL_MD_WB_RWA;
7508 armreg_auxctl_write(auxctl);
7509 }
7510
7511 /*
7512 * Change the PTEs for the specified kernel mappings such that they
7513 * will use the mini data cache instead of the main data cache.
7514 */
7515 void
7516 pmap_uarea(vaddr_t va)
7517 {
7518 vaddr_t next_bucket, eva;
7519
7520 #if (ARM_NMMUS > 1)
7521 if (xscale_use_minidata == 0)
7522 return;
7523 #endif
7524
7525 eva = va + USPACE;
7526
7527 while (va < eva) {
7528 next_bucket = L2_NEXT_BUCKET_VA(va);
7529 if (next_bucket > eva)
7530 next_bucket = eva;
7531
7532 struct l2_bucket *l2b = pmap_get_l2_bucket(pmap_kernel(), va);
7533 KDASSERT(l2b != NULL);
7534
7535 pt_entry_t * const sptep = &l2b->l2b_kva[l2pte_index(va)];
7536 pt_entry_t *ptep = sptep;
7537
7538 while (va < next_bucket) {
7539 const pt_entry_t opte = *ptep;
7540 if (!l2pte_minidata_p(opte)) {
7541 cpu_dcache_wbinv_range(va, PAGE_SIZE);
7542 cpu_tlb_flushD_SE(va);
7543 l2pte_set(ptep, opte & ~L2_B, opte);
7544 }
7545 ptep += PAGE_SIZE / L2_S_SIZE;
7546 va += PAGE_SIZE;
7547 }
7548 PTE_SYNC_RANGE(sptep, (u_int)(ptep - sptep));
7549 }
7550 cpu_cpwait();
7551 }
7552 #endif /* ARM_MMU_XSCALE == 1 */
7553
7554
7555 #if defined(CPU_ARM11MPCORE)
7556 void
7557 pmap_pte_init_arm11mpcore(void)
7558 {
7559
7560 /* cache mode is controlled by 5 bits (B, C, TEX[2:0]) */
7561 pte_l1_s_cache_mask = L1_S_CACHE_MASK_armv6;
7562 pte_l2_l_cache_mask = L2_L_CACHE_MASK_armv6;
7563 #if defined(ARM11MPCORE_COMPAT_MMU) || defined(ARMV6_EXTENDED_SMALL_PAGE)
7564 /* use extended small page (without APn, with TEX) */
7565 pte_l2_s_cache_mask = L2_XS_CACHE_MASK_armv6;
7566 #else
7567 pte_l2_s_cache_mask = L2_S_CACHE_MASK_armv6c;
7568 #endif
7569
7570 /* write-back, write-allocate */
7571 pte_l1_s_cache_mode = L1_S_C | L1_S_B | L1_S_V6_TEX(0x01);
7572 pte_l2_l_cache_mode = L2_C | L2_B | L2_V6_L_TEX(0x01);
7573 #if defined(ARM11MPCORE_COMPAT_MMU) || defined(ARMV6_EXTENDED_SMALL_PAGE)
7574 pte_l2_s_cache_mode = L2_C | L2_B | L2_V6_XS_TEX(0x01);
7575 #else
7576 /* no TEX. read-allocate */
7577 pte_l2_s_cache_mode = L2_C | L2_B;
7578 #endif
7579 /*
7580 * write-back, write-allocate for page tables.
7581 */
7582 pte_l1_s_cache_mode_pt = L1_S_C | L1_S_B | L1_S_V6_TEX(0x01);
7583 pte_l2_l_cache_mode_pt = L2_C | L2_B | L2_V6_L_TEX(0x01);
7584 #if defined(ARM11MPCORE_COMPAT_MMU) || defined(ARMV6_EXTENDED_SMALL_PAGE)
7585 pte_l2_s_cache_mode_pt = L2_C | L2_B | L2_V6_XS_TEX(0x01);
7586 #else
7587 pte_l2_s_cache_mode_pt = L2_C | L2_B;
7588 #endif
7589
7590 pte_l1_s_prot_u = L1_S_PROT_U_armv6;
7591 pte_l1_s_prot_w = L1_S_PROT_W_armv6;
7592 pte_l1_s_prot_ro = L1_S_PROT_RO_armv6;
7593 pte_l1_s_prot_mask = L1_S_PROT_MASK_armv6;
7594
7595 #if defined(ARM11MPCORE_COMPAT_MMU) || defined(ARMV6_EXTENDED_SMALL_PAGE)
7596 pte_l2_s_prot_u = L2_S_PROT_U_armv6n;
7597 pte_l2_s_prot_w = L2_S_PROT_W_armv6n;
7598 pte_l2_s_prot_ro = L2_S_PROT_RO_armv6n;
7599 pte_l2_s_prot_mask = L2_S_PROT_MASK_armv6n;
7600
7601 #else
7602 /* with AP[0..3] */
7603 pte_l2_s_prot_u = L2_S_PROT_U_generic;
7604 pte_l2_s_prot_w = L2_S_PROT_W_generic;
7605 pte_l2_s_prot_ro = L2_S_PROT_RO_generic;
7606 pte_l2_s_prot_mask = L2_S_PROT_MASK_generic;
7607 #endif
7608
7609 #ifdef ARM11MPCORE_COMPAT_MMU
7610 /* with AP[0..3] */
7611 pte_l2_l_prot_u = L2_L_PROT_U_generic;
7612 pte_l2_l_prot_w = L2_L_PROT_W_generic;
7613 pte_l2_l_prot_ro = L2_L_PROT_RO_generic;
7614 pte_l2_l_prot_mask = L2_L_PROT_MASK_generic;
7615
7616 pte_l1_ss_proto = L1_SS_PROTO_armv6;
7617 pte_l1_s_proto = L1_S_PROTO_armv6;
7618 pte_l1_c_proto = L1_C_PROTO_armv6;
7619 pte_l2_s_proto = L2_S_PROTO_armv6c;
7620 #else
7621 pte_l2_l_prot_u = L2_L_PROT_U_armv6n;
7622 pte_l2_l_prot_w = L2_L_PROT_W_armv6n;
7623 pte_l2_l_prot_ro = L2_L_PROT_RO_armv6n;
7624 pte_l2_l_prot_mask = L2_L_PROT_MASK_armv6n;
7625
7626 pte_l1_ss_proto = L1_SS_PROTO_armv6;
7627 pte_l1_s_proto = L1_S_PROTO_armv6;
7628 pte_l1_c_proto = L1_C_PROTO_armv6;
7629 pte_l2_s_proto = L2_S_PROTO_armv6n;
7630 #endif
7631
7632 pmap_copy_page_func = pmap_copy_page_generic;
7633 pmap_zero_page_func = pmap_zero_page_generic;
7634 pmap_needs_pte_sync = 1;
7635 }
7636 #endif /* CPU_ARM11MPCORE */
7637
7638
7639 #if ARM_MMU_V6 == 1
7640 void
7641 pmap_pte_init_armv6(void)
7642 {
7643 /*
7644 * The ARMv6-A MMU is mostly compatible with generic. If the
7645 * AP field is zero, that now means "no access" rather than
7646 * read-only. The prototypes are a little different because of
7647 * the XN bit.
7648 */
7649 pmap_pte_init_generic();
7650
7651 pte_l1_s_nocache_mode = L1_S_XS_TEX(1);
7652 pte_l2_l_nocache_mode = L2_XS_L_TEX(1);
7653 pte_l2_s_nocache_mode = L2_XS_T_TEX(1);
7654
7655 #ifdef ARM11_COMPAT_MMU
7656 /* with AP[0..3] */
7657 pte_l1_ss_proto = L1_SS_PROTO_armv6;
7658 #else
7659 pte_l1_s_cache_mask = L1_S_CACHE_MASK_armv6n;
7660 pte_l2_l_cache_mask = L2_L_CACHE_MASK_armv6n;
7661 pte_l2_s_cache_mask = L2_S_CACHE_MASK_armv6n;
7662
7663 pte_l1_ss_proto = L1_SS_PROTO_armv6;
7664 pte_l1_s_proto = L1_S_PROTO_armv6;
7665 pte_l1_c_proto = L1_C_PROTO_armv6;
7666 pte_l2_s_proto = L2_S_PROTO_armv6n;
7667
7668 pte_l1_s_prot_u = L1_S_PROT_U_armv6;
7669 pte_l1_s_prot_w = L1_S_PROT_W_armv6;
7670 pte_l1_s_prot_ro = L1_S_PROT_RO_armv6;
7671 pte_l1_s_prot_mask = L1_S_PROT_MASK_armv6;
7672
7673 pte_l2_l_prot_u = L2_L_PROT_U_armv6n;
7674 pte_l2_l_prot_w = L2_L_PROT_W_armv6n;
7675 pte_l2_l_prot_ro = L2_L_PROT_RO_armv6n;
7676 pte_l2_l_prot_mask = L2_L_PROT_MASK_armv6n;
7677
7678 pte_l2_s_prot_u = L2_S_PROT_U_armv6n;
7679 pte_l2_s_prot_w = L2_S_PROT_W_armv6n;
7680 pte_l2_s_prot_ro = L2_S_PROT_RO_armv6n;
7681 pte_l2_s_prot_mask = L2_S_PROT_MASK_armv6n;
7682
7683 #endif
7684 }
7685 #endif /* ARM_MMU_V6 */
7686
7687 #if ARM_MMU_V7 == 1
7688 void
7689 pmap_pte_init_armv7(void)
7690 {
7691 /*
7692 * The ARMv7-A MMU is mostly compatible with generic. If the
7693 * AP field is zero, that now means "no access" rather than
7694 * read-only. The prototypes are a little different because of
7695 * the XN bit.
7696 */
7697 pmap_pte_init_generic();
7698
7699 pmap_needs_pte_sync = 1;
7700
7701 pte_l1_s_nocache_mode = L1_S_XS_TEX(1);
7702 pte_l2_l_nocache_mode = L2_XS_L_TEX(1);
7703 pte_l2_s_nocache_mode = L2_XS_T_TEX(1);
7704
7705 pte_l1_s_cache_mask = L1_S_CACHE_MASK_armv7;
7706 pte_l2_l_cache_mask = L2_L_CACHE_MASK_armv7;
7707 pte_l2_s_cache_mask = L2_S_CACHE_MASK_armv7;
7708
7709 /*
7710 * If the core support coherent walk then updates to translation tables
7711 * do not require a clean to the point of unification to ensure
7712 * visibility by subsequent translation table walks. That means we can
7713 * map everything shareable and cached and the right thing will happen.
7714 */
7715 if (__SHIFTOUT(armreg_mmfr3_read(), __BITS(23,20))) {
7716 pmap_needs_pte_sync = 0;
7717
7718 /*
7719 * write-back, no write-allocate, shareable for normal pages.
7720 */
7721 pte_l1_s_cache_mode |= L1_S_V6_S;
7722 pte_l2_l_cache_mode |= L2_XS_S;
7723 pte_l2_s_cache_mode |= L2_XS_S;
7724 }
7725
7726 /*
7727 * Page tables are just all other memory. We can use write-back since
7728 * pmap_needs_pte_sync is 1 (or the MMU can read out of cache).
7729 */
7730 pte_l1_s_cache_mode_pt = pte_l1_s_cache_mode;
7731 pte_l2_l_cache_mode_pt = pte_l2_l_cache_mode;
7732 pte_l2_s_cache_mode_pt = pte_l2_s_cache_mode;
7733
7734 /*
7735 * Check the Memory Model Features to see if this CPU supports
7736 * the TLBIASID coproc op.
7737 */
7738 if (__SHIFTOUT(armreg_mmfr2_read(), __BITS(16,19)) >= 2) {
7739 arm_has_tlbiasid_p = true;
7740 } else if (__SHIFTOUT(armreg_mmfr2_read(), __BITS(12,15)) >= 2) {
7741 arm_has_tlbiasid_p = true;
7742 }
7743
7744 /*
7745 * Check the MPIDR to see if this CPU supports MP extensions.
7746 */
7747 #ifdef MULTIPROCESSOR
7748 arm_has_mpext_p = (armreg_mpidr_read() & (MPIDR_MP|MPIDR_U)) == MPIDR_MP;
7749 #else
7750 arm_has_mpext_p = false;
7751 #endif
7752
7753 pte_l1_s_prot_u = L1_S_PROT_U_armv7;
7754 pte_l1_s_prot_w = L1_S_PROT_W_armv7;
7755 pte_l1_s_prot_ro = L1_S_PROT_RO_armv7;
7756 pte_l1_s_prot_mask = L1_S_PROT_MASK_armv7;
7757
7758 pte_l2_s_prot_u = L2_S_PROT_U_armv7;
7759 pte_l2_s_prot_w = L2_S_PROT_W_armv7;
7760 pte_l2_s_prot_ro = L2_S_PROT_RO_armv7;
7761 pte_l2_s_prot_mask = L2_S_PROT_MASK_armv7;
7762
7763 pte_l2_l_prot_u = L2_L_PROT_U_armv7;
7764 pte_l2_l_prot_w = L2_L_PROT_W_armv7;
7765 pte_l2_l_prot_ro = L2_L_PROT_RO_armv7;
7766 pte_l2_l_prot_mask = L2_L_PROT_MASK_armv7;
7767
7768 pte_l1_ss_proto = L1_SS_PROTO_armv7;
7769 pte_l1_s_proto = L1_S_PROTO_armv7;
7770 pte_l1_c_proto = L1_C_PROTO_armv7;
7771 pte_l2_s_proto = L2_S_PROTO_armv7;
7772
7773 }
7774 #endif /* ARM_MMU_V7 */
7775
7776 /*
7777 * return the PA of the current L1 table, for use when handling a crash dump
7778 */
7779 uint32_t
7780 pmap_kernel_L1_addr(void)
7781 {
7782 #ifdef ARM_MMU_EXTENDED
7783 return pmap_kernel()->pm_l1_pa;
7784 #else
7785 return pmap_kernel()->pm_l1->l1_physaddr;
7786 #endif
7787 }
7788
7789 #if defined(DDB)
7790 /*
7791 * A couple of ddb-callable functions for dumping pmaps
7792 */
7793 void pmap_dump(pmap_t);
7794
7795 static pt_entry_t ncptes[64];
7796 static void pmap_dump_ncpg(pmap_t);
7797
7798 void
7799 pmap_dump(pmap_t pm)
7800 {
7801 struct l2_dtable *l2;
7802 struct l2_bucket *l2b;
7803 pt_entry_t *ptep, pte;
7804 vaddr_t l2_va, l2b_va, va;
7805 int i, j, k, occ, rows = 0;
7806
7807 if (pm == pmap_kernel())
7808 printf("pmap_kernel (%p): ", pm);
7809 else
7810 printf("user pmap (%p): ", pm);
7811
7812 #ifdef ARM_MMU_EXTENDED
7813 printf("l1 at %p\n", pmap_l1_kva(pm));
7814 #else
7815 printf("domain %d, l1 at %p\n", pmap_domain(pm), pmap_l1_kva(pm));
7816 #endif
7817
7818 l2_va = 0;
7819 for (i = 0; i < L2_SIZE; i++, l2_va += 0x01000000) {
7820 l2 = pm->pm_l2[i];
7821
7822 if (l2 == NULL || l2->l2_occupancy == 0)
7823 continue;
7824
7825 l2b_va = l2_va;
7826 for (j = 0; j < L2_BUCKET_SIZE; j++, l2b_va += 0x00100000) {
7827 l2b = &l2->l2_bucket[j];
7828
7829 if (l2b->l2b_occupancy == 0 || l2b->l2b_kva == NULL)
7830 continue;
7831
7832 ptep = l2b->l2b_kva;
7833
7834 for (k = 0; k < 256 && ptep[k] == 0; k++)
7835 ;
7836
7837 k &= ~63;
7838 occ = l2b->l2b_occupancy;
7839 va = l2b_va + (k * 4096);
7840 for (; k < 256; k++, va += 0x1000) {
7841 char ch = ' ';
7842 if ((k % 64) == 0) {
7843 if ((rows % 8) == 0) {
7844 printf(
7845 " |0000 |8000 |10000 |18000 |20000 |28000 |30000 |38000\n");
7846 }
7847 printf("%08lx: ", va);
7848 }
7849
7850 ncptes[k & 63] = 0;
7851 pte = ptep[k];
7852 if (pte == 0) {
7853 ch = '.';
7854 } else {
7855 occ--;
7856 switch (pte & 0x4c) {
7857 case 0x00:
7858 ch = 'N'; /* No cache No buff */
7859 break;
7860 case 0x04:
7861 ch = 'B'; /* No cache buff */
7862 break;
7863 case 0x08:
7864 ch = 'C'; /* Cache No buff */
7865 break;
7866 case 0x0c:
7867 ch = 'F'; /* Cache Buff */
7868 break;
7869 case 0x40:
7870 ch = 'D';
7871 break;
7872 case 0x48:
7873 ch = 'm'; /* Xscale mini-data */
7874 break;
7875 default:
7876 ch = '?';
7877 break;
7878 }
7879
7880 if ((pte & L2_S_PROT_U) == L2_S_PROT_U)
7881 ch += 0x20;
7882
7883 if ((pte & 0xc) == 0)
7884 ncptes[k & 63] = pte;
7885 }
7886
7887 if ((k % 64) == 63) {
7888 rows++;
7889 printf("%c\n", ch);
7890 pmap_dump_ncpg(pm);
7891 if (occ == 0)
7892 break;
7893 } else
7894 printf("%c", ch);
7895 }
7896 }
7897 }
7898 }
7899
7900 static void
7901 pmap_dump_ncpg(pmap_t pm)
7902 {
7903 struct vm_page *pg;
7904 struct vm_page_md *md;
7905 struct pv_entry *pv;
7906 int i;
7907
7908 for (i = 0; i < 63; i++) {
7909 if (ncptes[i] == 0)
7910 continue;
7911
7912 pg = PHYS_TO_VM_PAGE(l2pte_pa(ncptes[i]));
7913 if (pg == NULL)
7914 continue;
7915 md = VM_PAGE_TO_MD(pg);
7916
7917 printf(" pa 0x%08lx: krw %d kro %d urw %d uro %d\n",
7918 VM_PAGE_TO_PHYS(pg),
7919 md->krw_mappings, md->kro_mappings,
7920 md->urw_mappings, md->uro_mappings);
7921
7922 SLIST_FOREACH(pv, &md->pvh_list, pv_link) {
7923 printf(" %c va 0x%08lx, flags 0x%x\n",
7924 (pm == pv->pv_pmap) ? '*' : ' ',
7925 pv->pv_va, pv->pv_flags);
7926 }
7927 }
7928 }
7929 #endif
7930
7931 #ifdef PMAP_STEAL_MEMORY
7932 void
7933 pmap_boot_pageadd(pv_addr_t *newpv)
7934 {
7935 pv_addr_t *pv, *npv;
7936
7937 if ((pv = SLIST_FIRST(&pmap_boot_freeq)) != NULL) {
7938 if (newpv->pv_pa < pv->pv_va) {
7939 KASSERT(newpv->pv_pa + newpv->pv_size <= pv->pv_pa);
7940 if (newpv->pv_pa + newpv->pv_size == pv->pv_pa) {
7941 newpv->pv_size += pv->pv_size;
7942 SLIST_REMOVE_HEAD(&pmap_boot_freeq, pv_list);
7943 }
7944 pv = NULL;
7945 } else {
7946 for (; (npv = SLIST_NEXT(pv, pv_list)) != NULL;
7947 pv = npv) {
7948 KASSERT(pv->pv_pa + pv->pv_size < npv->pv_pa);
7949 KASSERT(pv->pv_pa < newpv->pv_pa);
7950 if (newpv->pv_pa > npv->pv_pa)
7951 continue;
7952 if (pv->pv_pa + pv->pv_size == newpv->pv_pa) {
7953 pv->pv_size += newpv->pv_size;
7954 return;
7955 }
7956 if (newpv->pv_pa + newpv->pv_size < npv->pv_pa)
7957 break;
7958 newpv->pv_size += npv->pv_size;
7959 SLIST_INSERT_AFTER(pv, newpv, pv_list);
7960 SLIST_REMOVE_AFTER(newpv, pv_list);
7961 return;
7962 }
7963 }
7964 }
7965
7966 if (pv) {
7967 SLIST_INSERT_AFTER(pv, newpv, pv_list);
7968 } else {
7969 SLIST_INSERT_HEAD(&pmap_boot_freeq, newpv, pv_list);
7970 }
7971 }
7972
7973 void
7974 pmap_boot_pagealloc(psize_t amount, psize_t mask, psize_t match,
7975 pv_addr_t *rpv)
7976 {
7977 pv_addr_t *pv, **pvp;
7978
7979 KASSERT(amount & PGOFSET);
7980 KASSERT((mask & PGOFSET) == 0);
7981 KASSERT((match & PGOFSET) == 0);
7982 KASSERT(amount != 0);
7983
7984 for (pvp = &SLIST_FIRST(&pmap_boot_freeq);
7985 (pv = *pvp) != NULL;
7986 pvp = &SLIST_NEXT(pv, pv_list)) {
7987 pv_addr_t *newpv;
7988 psize_t off;
7989 /*
7990 * If this entry is too small to satisfy the request...
7991 */
7992 KASSERT(pv->pv_size > 0);
7993 if (pv->pv_size < amount)
7994 continue;
7995
7996 for (off = 0; off <= mask; off += PAGE_SIZE) {
7997 if (((pv->pv_pa + off) & mask) == match
7998 && off + amount <= pv->pv_size)
7999 break;
8000 }
8001 if (off > mask)
8002 continue;
8003
8004 rpv->pv_va = pv->pv_va + off;
8005 rpv->pv_pa = pv->pv_pa + off;
8006 rpv->pv_size = amount;
8007 pv->pv_size -= amount;
8008 if (pv->pv_size == 0) {
8009 KASSERT(off == 0);
8010 KASSERT((vaddr_t) pv == rpv->pv_va);
8011 *pvp = SLIST_NEXT(pv, pv_list);
8012 } else if (off == 0) {
8013 KASSERT((vaddr_t) pv == rpv->pv_va);
8014 newpv = (pv_addr_t *) (rpv->pv_va + amount);
8015 *newpv = *pv;
8016 newpv->pv_pa += amount;
8017 newpv->pv_va += amount;
8018 *pvp = newpv;
8019 } else if (off < pv->pv_size) {
8020 newpv = (pv_addr_t *) (rpv->pv_va + amount);
8021 *newpv = *pv;
8022 newpv->pv_size -= off;
8023 newpv->pv_pa += off + amount;
8024 newpv->pv_va += off + amount;
8025
8026 SLIST_NEXT(pv, pv_list) = newpv;
8027 pv->pv_size = off;
8028 } else {
8029 KASSERT((vaddr_t) pv != rpv->pv_va);
8030 }
8031 memset((void *)rpv->pv_va, 0, amount);
8032 return;
8033 }
8034
8035 if (!uvm_physseg_valid_p(uvm_physseg_get_first()))
8036 panic("pmap_boot_pagealloc: couldn't allocate memory");
8037
8038 for (pvp = &SLIST_FIRST(&pmap_boot_freeq);
8039 (pv = *pvp) != NULL;
8040 pvp = &SLIST_NEXT(pv, pv_list)) {
8041 if (SLIST_NEXT(pv, pv_list) == NULL)
8042 break;
8043 }
8044 KASSERT(mask == 0);
8045
8046 for (uvm_physseg_t ups = uvm_physseg_get_first();
8047 uvm_physseg_valid_p(ups);
8048 ups = uvm_physseg_get_next(ups)) {
8049
8050 paddr_t spn = uvm_physseg_get_start(ups);
8051 paddr_t epn = uvm_physseg_get_end(ups);
8052 if (spn == atop(pv->pv_pa + pv->pv_size)
8053 && pv->pv_va + pv->pv_size <= ptoa(epn)) {
8054 rpv->pv_va = pv->pv_va;
8055 rpv->pv_pa = pv->pv_pa;
8056 rpv->pv_size = amount;
8057 *pvp = NULL;
8058 pmap_map_chunk(kernel_l1pt.pv_va,
8059 ptoa(spn) + (pv->pv_va - pv->pv_pa),
8060 ptoa(spn),
8061 amount - pv->pv_size,
8062 VM_PROT_READ|VM_PROT_WRITE,
8063 PTE_CACHE);
8064
8065 uvm_physseg_unplug(spn, atop(amount - pv->pv_size));
8066 memset((void *)rpv->pv_va, 0, rpv->pv_size);
8067 return;
8068 }
8069 }
8070
8071 panic("pmap_boot_pagealloc: couldn't allocate memory");
8072 }
8073
8074 vaddr_t
8075 pmap_steal_memory(vsize_t size, vaddr_t *vstartp, vaddr_t *vendp)
8076 {
8077 pv_addr_t pv;
8078
8079 pmap_boot_pagealloc(size, 0, 0, &pv);
8080
8081 return pv.pv_va;
8082 }
8083 #endif /* PMAP_STEAL_MEMORY */
8084
8085 SYSCTL_SETUP(sysctl_machdep_pmap_setup, "sysctl machdep.kmpages setup")
8086 {
8087 sysctl_createv(clog, 0, NULL, NULL,
8088 CTLFLAG_PERMANENT,
8089 CTLTYPE_NODE, "machdep", NULL,
8090 NULL, 0, NULL, 0,
8091 CTL_MACHDEP, CTL_EOL);
8092
8093 sysctl_createv(clog, 0, NULL, NULL,
8094 CTLFLAG_PERMANENT,
8095 CTLTYPE_INT, "kmpages",
8096 SYSCTL_DESCR("count of pages allocated to kernel memory allocators"),
8097 NULL, 0, &pmap_kmpages, 0,
8098 CTL_MACHDEP, CTL_CREATE, CTL_EOL);
8099 }
8100
8101 #ifdef PMAP_NEED_ALLOC_POOLPAGE
8102 struct vm_page *
8103 arm_pmap_alloc_poolpage(int flags)
8104 {
8105 /*
8106 * On some systems, only some pages may be "coherent" for dma and we
8107 * want to prefer those for pool pages (think mbufs) but fallback to
8108 * any page if none is available.
8109 */
8110 if (arm_poolpage_vmfreelist != VM_FREELIST_DEFAULT) {
8111 return uvm_pagealloc_strat(NULL, 0, NULL, flags,
8112 UVM_PGA_STRAT_FALLBACK, arm_poolpage_vmfreelist);
8113 }
8114
8115 return uvm_pagealloc(NULL, 0, NULL, flags);
8116 }
8117 #endif
8118
8119 #if defined(ARM_MMU_EXTENDED) && defined(MULTIPROCESSOR)
8120 void
8121 pmap_md_tlb_info_attach(struct pmap_tlb_info *ti, struct cpu_info *ci)
8122 {
8123 /* nothing */
8124 }
8125
8126 int
8127 pic_ipi_shootdown(void *arg)
8128 {
8129 #if PMAP_TLB_NEED_SHOOTDOWN
8130 pmap_tlb_shootdown_process();
8131 #endif
8132 return 1;
8133 }
8134 #endif /* ARM_MMU_EXTENDED && MULTIPROCESSOR */
8135
8136
8137 #ifdef __HAVE_MM_MD_DIRECT_MAPPED_PHYS
8138 vaddr_t
8139 pmap_direct_mapped_phys(paddr_t pa, bool *ok_p, vaddr_t va)
8140 {
8141 bool ok = false;
8142 if (physical_start <= pa && pa < physical_end) {
8143 #ifdef KERNEL_BASE_VOFFSET
8144 const vaddr_t newva = pa + KERNEL_BASE_VOFFSET;
8145 #else
8146 const vaddr_t newva = KERNEL_BASE + pa - physical_start;
8147 #endif
8148 #ifdef ARM_MMU_EXTENDED
8149 if (newva >= KERNEL_BASE && newva < pmap_directlimit) {
8150 #endif
8151 va = newva;
8152 ok = true;
8153 #ifdef ARM_MMU_EXTENDED
8154 }
8155 #endif
8156 }
8157 KASSERT(ok_p);
8158 *ok_p = ok;
8159 return va;
8160 }
8161
8162 vaddr_t
8163 pmap_map_poolpage(paddr_t pa)
8164 {
8165 bool ok __diagused;
8166 vaddr_t va = pmap_direct_mapped_phys(pa, &ok, 0);
8167 KASSERTMSG(ok, "pa %#lx not direct mappable", pa);
8168 #if defined(PMAP_CACHE_VIPT) && !defined(ARM_MMU_EXTENDED)
8169 if (arm_cache_prefer_mask != 0) {
8170 struct vm_page * const pg = PHYS_TO_VM_PAGE(pa);
8171 struct vm_page_md * const md = VM_PAGE_TO_MD(pg);
8172 pmap_acquire_page_lock(md);
8173 pmap_vac_me_harder(md, pa, pmap_kernel(), va);
8174 pmap_release_page_lock(md);
8175 }
8176 #endif
8177 return va;
8178 }
8179
8180 paddr_t
8181 pmap_unmap_poolpage(vaddr_t va)
8182 {
8183 KASSERT(va >= KERNEL_BASE);
8184 #ifdef PMAP_CACHE_VIVT
8185 cpu_idcache_wbinv_range(va, PAGE_SIZE);
8186 #endif
8187 #if defined(KERNEL_BASE_VOFFSET)
8188 return va - KERNEL_BASE_VOFFSET;
8189 #else
8190 return va - KERNEL_BASE + physical_start;
8191 #endif
8192 }
8193 #endif /* __HAVE_MM_MD_DIRECT_MAPPED_PHYS */
8194