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