pmap.c revision 1.385 1 /* $NetBSD: pmap.c,v 1.385 2020/02/02 08:16:40 skrll Exp $ */
2
3 /*
4 * Copyright 2003 Wasabi Systems, Inc.
5 * All rights reserved.
6 *
7 * Written by Steve C. Woodford for Wasabi Systems, Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed for the NetBSD Project by
20 * Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 * or promote products derived from this software without specific prior
23 * written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 /*
39 * Copyright (c) 2002-2003 Wasabi Systems, Inc.
40 * Copyright (c) 2001 Richard Earnshaw
41 * Copyright (c) 2001-2002 Christopher Gilbert
42 * All rights reserved.
43 *
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 * 3. The name of the company nor the name of the author may be used to
50 * endorse or promote products derived from this software without specific
51 * prior written permission.
52 *
53 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
54 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
55 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
56 * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
57 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
58 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
59 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * SUCH DAMAGE.
64 */
65
66 /*-
67 * Copyright (c) 1999 The NetBSD Foundation, Inc.
68 * All rights reserved.
69 *
70 * This code is derived from software contributed to The NetBSD Foundation
71 * by Charles M. Hannum.
72 *
73 * Redistribution and use in source and binary forms, with or without
74 * modification, are permitted provided that the following conditions
75 * are met:
76 * 1. Redistributions of source code must retain the above copyright
77 * notice, this list of conditions and the following disclaimer.
78 * 2. Redistributions in binary form must reproduce the above copyright
79 * notice, this list of conditions and the following disclaimer in the
80 * documentation and/or other materials provided with the distribution.
81 *
82 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
83 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
84 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
85 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
86 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
87 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
88 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
89 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
90 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
91 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
92 * POSSIBILITY OF SUCH DAMAGE.
93 */
94
95 /*
96 * Copyright (c) 1994-1998 Mark Brinicombe.
97 * Copyright (c) 1994 Brini.
98 * All rights reserved.
99 *
100 * This code is derived from software written for Brini by Mark Brinicombe
101 *
102 * Redistribution and use in source and binary forms, with or without
103 * modification, are permitted provided that the following conditions
104 * are met:
105 * 1. Redistributions of source code must retain the above copyright
106 * notice, this list of conditions and the following disclaimer.
107 * 2. Redistributions in binary form must reproduce the above copyright
108 * notice, this list of conditions and the following disclaimer in the
109 * documentation and/or other materials provided with the distribution.
110 * 3. All advertising materials mentioning features or use of this software
111 * must display the following acknowledgement:
112 * This product includes software developed by Mark Brinicombe.
113 * 4. The name of the author may not be used to endorse or promote products
114 * derived from this software without specific prior written permission.
115 *
116 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
117 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
118 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
119 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
120 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
121 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
122 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
123 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
124 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
125 *
126 * RiscBSD kernel project
127 *
128 * pmap.c
129 *
130 * Machine dependent vm stuff
131 *
132 * Created : 20/09/94
133 */
134
135 /*
136 * armv6 and VIPT cache support by 3am Software Foundry,
137 * Copyright (c) 2007 Microsoft
138 */
139
140 /*
141 * Performance improvements, UVM changes, overhauls and part-rewrites
142 * were contributed by Neil A. Carson <neil (at) causality.com>.
143 */
144
145 /*
146 * Overhauled again to speedup the pmap, use MMU Domains so that L1 tables
147 * can be shared, and re-work the KVM layout, by Steve Woodford of Wasabi
148 * Systems, Inc.
149 *
150 * There are still a few things outstanding at this time:
151 *
152 * - There are some unresolved issues for MP systems:
153 *
154 * o The L1 metadata needs a lock, or more specifically, some places
155 * need to acquire an exclusive lock when modifying L1 translation
156 * table entries.
157 *
158 * o When one cpu modifies an L1 entry, and that L1 table is also
159 * being used by another cpu, then the latter will need to be told
160 * that a tlb invalidation may be necessary. (But only if the old
161 * domain number in the L1 entry being over-written is currently
162 * the active domain on that cpu). I guess there are lots more tlb
163 * shootdown issues too...
164 *
165 * o If the vector_page is at 0x00000000 instead of in kernel VA space,
166 * then MP systems will lose big-time because of the MMU domain hack.
167 * The only way this can be solved (apart from moving the vector
168 * page to 0xffff0000) is to reserve the first 1MB of user address
169 * space for kernel use only. This would require re-linking all
170 * applications so that the text section starts above this 1MB
171 * boundary.
172 *
173 * o Tracking which VM space is resident in the cache/tlb has not yet
174 * been implemented for MP systems.
175 *
176 * o Finally, there is a pathological condition where two cpus running
177 * two separate processes (not lwps) which happen to share an L1
178 * can get into a fight over one or more L1 entries. This will result
179 * in a significant slow-down if both processes are in tight loops.
180 */
181
182 /*
183 * Special compilation symbols
184 * PMAP_DEBUG - Build in pmap_debug_level code
185 */
186
187 /* Include header files */
188
189 #include "opt_arm_debug.h"
190 #include "opt_cpuoptions.h"
191 #include "opt_pmap_debug.h"
192 #include "opt_ddb.h"
193 #include "opt_lockdebug.h"
194 #include "opt_multiprocessor.h"
195
196 #ifdef MULTIPROCESSOR
197 #define _INTR_PRIVATE
198 #endif
199
200 #include <sys/cdefs.h>
201 __KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.385 2020/02/02 08:16:40 skrll Exp $");
202
203 #include <sys/param.h>
204 #include <sys/types.h>
205 #include <sys/atomic.h>
206 #include <sys/bus.h>
207 #include <sys/cpu.h>
208 #include <sys/intr.h>
209 #include <sys/kernel.h>
210 #include <sys/kernhist.h>
211 #include <sys/kmem.h>
212 #include <sys/pool.h>
213 #include <sys/proc.h>
214 #include <sys/sysctl.h>
215 #include <sys/systm.h>
216
217 #include <uvm/uvm.h>
218 #include <uvm/pmap/pmap_pvt.h>
219
220 #include <arm/locore.h>
221
222 #ifdef DDB
223 #include <arm/db_machdep.h>
224 #endif
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 #endif /* PMAP_STEAL_MEMORY */
5819 } else {
5820 struct vm_page *pg;
5821 pg = uvm_pagealloc(NULL, 0, NULL, UVM_PGA_USERESERVE);
5822 if (pg == NULL)
5823 return (1);
5824 pa = VM_PAGE_TO_PHYS(pg);
5825 /*
5826 * This new page must not have any mappings. Enter it via
5827 * pmap_kenter_pa and let that routine do the hard work.
5828 */
5829 struct vm_page_md *md __diagused = VM_PAGE_TO_MD(pg);
5830 KASSERT(SLIST_EMPTY(&md->pvh_list));
5831 }
5832
5833 pmap_kenter_pa(va, pa,
5834 VM_PROT_READ|VM_PROT_WRITE, PMAP_KMPAGE|PMAP_PTE);
5835
5836 if (pap)
5837 *pap = pa;
5838
5839 PMAPCOUNT(pt_mappings);
5840 #ifdef DEBUG
5841 struct l2_bucket * const l2b = pmap_get_l2_bucket(pmap_kernel(), va);
5842 KDASSERT(l2b != NULL);
5843
5844 pt_entry_t * const ptep = &l2b->l2b_kva[l2pte_index(va)];
5845 const pt_entry_t opte = *ptep;
5846 KDASSERT((opte & L2_S_CACHE_MASK) == pte_l2_s_cache_mode_pt);
5847 #endif
5848 memset((void *)va, 0, PAGE_SIZE);
5849 return (0);
5850 }
5851
5852 /*
5853 * This is the same as pmap_alloc_l2_bucket(), except that it is only
5854 * used by pmap_growkernel().
5855 */
5856 static inline struct l2_bucket *
5857 pmap_grow_l2_bucket(pmap_t pm, vaddr_t va)
5858 {
5859 struct l2_dtable *l2;
5860 struct l2_bucket *l2b;
5861 u_short l1slot;
5862 vaddr_t nva;
5863
5864 l1slot = l1pte_index(va);
5865
5866 if ((l2 = pm->pm_l2[L2_IDX(l1slot)]) == NULL) {
5867 /*
5868 * No mapping at this address, as there is
5869 * no entry in the L1 table.
5870 * Need to allocate a new l2_dtable.
5871 */
5872 nva = pmap_kernel_l2dtable_kva;
5873 if ((nva & PGOFSET) == 0) {
5874 /*
5875 * Need to allocate a backing page
5876 */
5877 if (pmap_grow_map(nva, NULL))
5878 return (NULL);
5879 }
5880
5881 l2 = (struct l2_dtable *)nva;
5882 nva += sizeof(struct l2_dtable);
5883
5884 if ((nva & PGOFSET) < (pmap_kernel_l2dtable_kva & PGOFSET)) {
5885 /*
5886 * The new l2_dtable straddles a page boundary.
5887 * Map in another page to cover it.
5888 */
5889 if (pmap_grow_map(nva, NULL))
5890 return (NULL);
5891 }
5892
5893 pmap_kernel_l2dtable_kva = nva;
5894
5895 /*
5896 * Link it into the parent pmap
5897 */
5898 pm->pm_l2[L2_IDX(l1slot)] = l2;
5899 }
5900
5901 l2b = &l2->l2_bucket[L2_BUCKET(l1slot)];
5902
5903 /*
5904 * Fetch pointer to the L2 page table associated with the address.
5905 */
5906 if (l2b->l2b_kva == NULL) {
5907 pt_entry_t *ptep;
5908
5909 /*
5910 * No L2 page table has been allocated. Chances are, this
5911 * is because we just allocated the l2_dtable, above.
5912 */
5913 nva = pmap_kernel_l2ptp_kva;
5914 ptep = (pt_entry_t *)nva;
5915 if ((nva & PGOFSET) == 0) {
5916 /*
5917 * Need to allocate a backing page
5918 */
5919 if (pmap_grow_map(nva, &pmap_kernel_l2ptp_phys))
5920 return (NULL);
5921 PTE_SYNC_RANGE(ptep, PAGE_SIZE / sizeof(pt_entry_t));
5922 }
5923
5924 l2->l2_occupancy++;
5925 l2b->l2b_kva = ptep;
5926 l2b->l2b_l1slot = l1slot;
5927 l2b->l2b_pa = pmap_kernel_l2ptp_phys;
5928
5929 pmap_kernel_l2ptp_kva += L2_TABLE_SIZE_REAL;
5930 pmap_kernel_l2ptp_phys += L2_TABLE_SIZE_REAL;
5931 }
5932
5933 return (l2b);
5934 }
5935
5936 vaddr_t
5937 pmap_growkernel(vaddr_t maxkvaddr)
5938 {
5939 pmap_t kpm = pmap_kernel();
5940 #ifndef ARM_MMU_EXTENDED
5941 struct l1_ttable *l1;
5942 #endif
5943 int s;
5944
5945 if (maxkvaddr <= pmap_curmaxkvaddr)
5946 goto out; /* we are OK */
5947
5948 NPDEBUG(PDB_GROWKERN,
5949 printf("pmap_growkernel: growing kernel from 0x%lx to 0x%lx\n",
5950 pmap_curmaxkvaddr, maxkvaddr));
5951
5952 KDASSERT(maxkvaddr <= virtual_end);
5953
5954 /*
5955 * whoops! we need to add kernel PTPs
5956 */
5957
5958 s = splvm(); /* to be safe */
5959 mutex_enter(&kpm_lock);
5960
5961 /* Map 1MB at a time */
5962 size_t l1slot = l1pte_index(pmap_curmaxkvaddr);
5963 #ifdef ARM_MMU_EXTENDED
5964 pd_entry_t * const spdep = &kpm->pm_l1[l1slot];
5965 pd_entry_t *pdep = spdep;
5966 #endif
5967 for (;pmap_curmaxkvaddr < maxkvaddr; pmap_curmaxkvaddr += L1_S_SIZE,
5968 #ifdef ARM_MMU_EXTENDED
5969 pdep++,
5970 #endif
5971 l1slot++) {
5972 struct l2_bucket *l2b =
5973 pmap_grow_l2_bucket(kpm, pmap_curmaxkvaddr);
5974 KASSERT(l2b != NULL);
5975
5976 const pd_entry_t npde = L1_C_PROTO | l2b->l2b_pa
5977 | L1_C_DOM(PMAP_DOMAIN_KERNEL);
5978 #ifdef ARM_MMU_EXTENDED
5979 l1pte_setone(pdep, npde);
5980 #else
5981 /* Distribute new L1 entry to all other L1s */
5982 SLIST_FOREACH(l1, &l1_list, l1_link) {
5983 pd_entry_t * const pdep = &l1->l1_kva[l1slot];
5984 l1pte_setone(pdep, npde);
5985 PDE_SYNC(pdep);
5986 }
5987 #endif
5988 }
5989 #ifdef ARM_MMU_EXTENDED
5990 PDE_SYNC_RANGE(spdep, pdep - spdep);
5991 #endif
5992
5993 #ifdef PMAP_CACHE_VIVT
5994 /*
5995 * flush out the cache, expensive but growkernel will happen so
5996 * rarely
5997 */
5998 cpu_dcache_wbinv_all();
5999 cpu_tlb_flushD();
6000 cpu_cpwait();
6001 #endif
6002
6003 mutex_exit(&kpm_lock);
6004 splx(s);
6005
6006 out:
6007 return (pmap_curmaxkvaddr);
6008 }
6009
6010 /************************ Utility routines ****************************/
6011
6012 #ifndef ARM_HAS_VBAR
6013 /*
6014 * vector_page_setprot:
6015 *
6016 * Manipulate the protection of the vector page.
6017 */
6018 void
6019 vector_page_setprot(int prot)
6020 {
6021 struct l2_bucket *l2b;
6022 pt_entry_t *ptep;
6023
6024 #if defined(CPU_ARMV7) || defined(CPU_ARM11)
6025 /*
6026 * If we are using VBAR to use the vectors in the kernel, then it's
6027 * already mapped in the kernel text so no need to anything here.
6028 */
6029 if (vector_page != ARM_VECTORS_LOW && vector_page != ARM_VECTORS_HIGH) {
6030 KASSERT((armreg_pfr1_read() & ARM_PFR1_SEC_MASK) != 0);
6031 return;
6032 }
6033 #endif
6034
6035 l2b = pmap_get_l2_bucket(pmap_kernel(), vector_page);
6036 KASSERT(l2b != NULL);
6037
6038 ptep = &l2b->l2b_kva[l2pte_index(vector_page)];
6039
6040 const pt_entry_t opte = *ptep;
6041 #ifdef ARM_MMU_EXTENDED
6042 const pt_entry_t npte = (opte & ~(L2_S_PROT_MASK|L2_XS_XN))
6043 | L2_S_PROT(PTE_KERNEL, prot);
6044 #else
6045 const pt_entry_t npte = (opte & ~L2_S_PROT_MASK)
6046 | L2_S_PROT(PTE_KERNEL, prot);
6047 #endif
6048 l2pte_set(ptep, npte, opte);
6049 PTE_SYNC(ptep);
6050 cpu_tlb_flushD_SE(vector_page);
6051 cpu_cpwait();
6052 }
6053 #endif
6054
6055 /*
6056 * Fetch pointers to the PDE/PTE for the given pmap/VA pair.
6057 * Returns true if the mapping exists, else false.
6058 *
6059 * NOTE: This function is only used by a couple of arm-specific modules.
6060 * It is not safe to take any pmap locks here, since we could be right
6061 * in the middle of debugging the pmap anyway...
6062 *
6063 * It is possible for this routine to return false even though a valid
6064 * mapping does exist. This is because we don't lock, so the metadata
6065 * state may be inconsistent.
6066 *
6067 * NOTE: We can return a NULL *ptp in the case where the L1 pde is
6068 * a "section" mapping.
6069 */
6070 bool
6071 pmap_get_pde_pte(pmap_t pm, vaddr_t va, pd_entry_t **pdp, pt_entry_t **ptp)
6072 {
6073 struct l2_dtable *l2;
6074 pd_entry_t *pdep, pde;
6075 pt_entry_t *ptep;
6076 u_short l1slot;
6077
6078 if (pm->pm_l1 == NULL)
6079 return false;
6080
6081 l1slot = l1pte_index(va);
6082 *pdp = pdep = pmap_l1_kva(pm) + l1slot;
6083 pde = *pdep;
6084
6085 if (l1pte_section_p(pde)) {
6086 *ptp = NULL;
6087 return true;
6088 }
6089
6090 l2 = pm->pm_l2[L2_IDX(l1slot)];
6091 if (l2 == NULL ||
6092 (ptep = l2->l2_bucket[L2_BUCKET(l1slot)].l2b_kva) == NULL) {
6093 return false;
6094 }
6095
6096 *ptp = &ptep[l2pte_index(va)];
6097 return true;
6098 }
6099
6100 bool
6101 pmap_get_pde(pmap_t pm, vaddr_t va, pd_entry_t **pdp)
6102 {
6103
6104 if (pm->pm_l1 == NULL)
6105 return false;
6106
6107 *pdp = pmap_l1_kva(pm) + l1pte_index(va);
6108
6109 return true;
6110 }
6111
6112 /************************ Bootstrapping routines ****************************/
6113
6114 #ifndef ARM_MMU_EXTENDED
6115 static void
6116 pmap_init_l1(struct l1_ttable *l1, pd_entry_t *l1pt)
6117 {
6118 int i;
6119
6120 l1->l1_kva = l1pt;
6121 l1->l1_domain_use_count = 0;
6122 l1->l1_domain_first = 0;
6123
6124 for (i = 0; i < PMAP_DOMAINS; i++)
6125 l1->l1_domain_free[i] = i + 1;
6126
6127 /*
6128 * Copy the kernel's L1 entries to each new L1.
6129 */
6130 if (pmap_initialized)
6131 memcpy(l1pt, pmap_l1_kva(pmap_kernel()), L1_TABLE_SIZE);
6132
6133 if (pmap_extract(pmap_kernel(), (vaddr_t)l1pt,
6134 &l1->l1_physaddr) == false)
6135 panic("pmap_init_l1: can't get PA of L1 at %p", l1pt);
6136
6137 SLIST_INSERT_HEAD(&l1_list, l1, l1_link);
6138 TAILQ_INSERT_TAIL(&l1_lru_list, l1, l1_lru);
6139 }
6140 #endif /* !ARM_MMU_EXTENDED */
6141
6142 /*
6143 * pmap_bootstrap() is called from the board-specific initarm() routine
6144 * once the kernel L1/L2 descriptors tables have been set up.
6145 *
6146 * This is a somewhat convoluted process since pmap bootstrap is, effectively,
6147 * spread over a number of disparate files/functions.
6148 *
6149 * We are passed the following parameters
6150 * - vstart
6151 * 1MB-aligned start of managed kernel virtual memory.
6152 * - vend
6153 * 1MB-aligned end of managed kernel virtual memory.
6154 *
6155 * We use 'kernel_l1pt' to build the metadata (struct l1_ttable and
6156 * struct l2_dtable) necessary to track kernel mappings.
6157 */
6158 #define PMAP_STATIC_L2_SIZE 16
6159 void
6160 pmap_bootstrap(vaddr_t vstart, vaddr_t vend)
6161 {
6162 static struct l2_dtable static_l2[PMAP_STATIC_L2_SIZE];
6163 #ifndef ARM_MMU_EXTENDED
6164 static struct l1_ttable static_l1;
6165 struct l1_ttable *l1 = &static_l1;
6166 #endif
6167 struct l2_dtable *l2;
6168 struct l2_bucket *l2b;
6169 pd_entry_t *l1pt = (pd_entry_t *) kernel_l1pt.pv_va;
6170 pmap_t pm = pmap_kernel();
6171 pt_entry_t *ptep;
6172 paddr_t pa;
6173 vsize_t size;
6174 int nptes, l2idx, l2next = 0;
6175
6176 #ifdef ARM_MMU_EXTENDED
6177 KASSERT(pte_l1_s_cache_mode == pte_l1_s_cache_mode_pt);
6178 KASSERT(pte_l2_s_cache_mode == pte_l2_s_cache_mode_pt);
6179 #endif
6180
6181 VPRINTF("kpm ");
6182 /*
6183 * Initialise the kernel pmap object
6184 */
6185 curcpu()->ci_pmap_cur = pm;
6186 #ifdef ARM_MMU_EXTENDED
6187 pm->pm_l1 = l1pt;
6188 pm->pm_l1_pa = kernel_l1pt.pv_pa;
6189 VPRINTF("tlb0 ");
6190 pmap_tlb_info_init(&pmap_tlb0_info);
6191 #ifdef MULTIPROCESSOR
6192 VPRINTF("kcpusets ");
6193 pm->pm_onproc = kcpuset_running;
6194 pm->pm_active = kcpuset_running;
6195 #endif
6196 #else
6197 pm->pm_l1 = l1;
6198 #endif
6199
6200 VPRINTF("locks ");
6201 /*
6202 * pmap_kenter_pa() and pmap_kremove() may be called from interrupt
6203 * context, so its locks have to be at IPL_VM
6204 */
6205 mutex_init(&pmap_lock, MUTEX_DEFAULT, IPL_VM);
6206 mutex_init(&kpm_lock, MUTEX_DEFAULT, IPL_NONE);
6207 mutex_init(&pm->pm_obj_lock, MUTEX_DEFAULT, IPL_VM);
6208 uvm_obj_init(&pm->pm_obj, NULL, false, 1);
6209 uvm_obj_setlock(&pm->pm_obj, &pm->pm_obj_lock);
6210
6211 VPRINTF("l1pt ");
6212 /*
6213 * Scan the L1 translation table created by initarm() and create
6214 * the required metadata for all valid mappings found in it.
6215 */
6216 for (size_t l1slot = 0;
6217 l1slot < L1_TABLE_SIZE / sizeof(pd_entry_t);
6218 l1slot++) {
6219 pd_entry_t pde = l1pt[l1slot];
6220
6221 /*
6222 * We're only interested in Coarse mappings.
6223 * pmap_extract() can deal with section mappings without
6224 * recourse to checking L2 metadata.
6225 */
6226 if ((pde & L1_TYPE_MASK) != L1_TYPE_C)
6227 continue;
6228
6229 /*
6230 * Lookup the KVA of this L2 descriptor table
6231 */
6232 pa = l1pte_pa(pde);
6233 ptep = (pt_entry_t *)kernel_pt_lookup(pa);
6234 if (ptep == NULL) {
6235 panic("pmap_bootstrap: No L2 for va 0x%x, pa 0x%lx",
6236 (u_int)l1slot << L1_S_SHIFT, pa);
6237 }
6238
6239 /*
6240 * Fetch the associated L2 metadata structure.
6241 * Allocate a new one if necessary.
6242 */
6243 if ((l2 = pm->pm_l2[L2_IDX(l1slot)]) == NULL) {
6244 if (l2next == PMAP_STATIC_L2_SIZE)
6245 panic("pmap_bootstrap: out of static L2s");
6246 pm->pm_l2[L2_IDX(l1slot)] = l2 = &static_l2[l2next++];
6247 }
6248
6249 /*
6250 * One more L1 slot tracked...
6251 */
6252 l2->l2_occupancy++;
6253
6254 /*
6255 * Fill in the details of the L2 descriptor in the
6256 * appropriate bucket.
6257 */
6258 l2b = &l2->l2_bucket[L2_BUCKET(l1slot)];
6259 l2b->l2b_kva = ptep;
6260 l2b->l2b_pa = pa;
6261 l2b->l2b_l1slot = l1slot;
6262
6263 /*
6264 * Establish an initial occupancy count for this descriptor
6265 */
6266 for (l2idx = 0;
6267 l2idx < (L2_TABLE_SIZE_REAL / sizeof(pt_entry_t));
6268 l2idx++) {
6269 if ((ptep[l2idx] & L2_TYPE_MASK) != L2_TYPE_INV) {
6270 l2b->l2b_occupancy++;
6271 }
6272 }
6273
6274 /*
6275 * Make sure the descriptor itself has the correct cache mode.
6276 * If not, fix it, but whine about the problem. Port-meisters
6277 * should consider this a clue to fix up their initarm()
6278 * function. :)
6279 */
6280 if (pmap_set_pt_cache_mode(l1pt, (vaddr_t)ptep, 1)) {
6281 printf("pmap_bootstrap: WARNING! wrong cache mode for "
6282 "L2 pte @ %p\n", ptep);
6283 }
6284 }
6285
6286 VPRINTF("cache(l1pt) ");
6287 /*
6288 * Ensure the primary (kernel) L1 has the correct cache mode for
6289 * a page table. Bitch if it is not correctly set.
6290 */
6291 if (pmap_set_pt_cache_mode(l1pt, kernel_l1pt.pv_va,
6292 L1_TABLE_SIZE / L2_S_SIZE)) {
6293 printf("pmap_bootstrap: WARNING! wrong cache mode for "
6294 "primary L1 @ 0x%lx\n", kernel_l1pt.pv_va);
6295 }
6296
6297 #ifdef PMAP_CACHE_VIVT
6298 cpu_dcache_wbinv_all();
6299 cpu_tlb_flushID();
6300 cpu_cpwait();
6301 #endif
6302
6303 /*
6304 * now we allocate the "special" VAs which are used for tmp mappings
6305 * by the pmap (and other modules). we allocate the VAs by advancing
6306 * virtual_avail (note that there are no pages mapped at these VAs).
6307 *
6308 * Managed KVM space start from wherever initarm() tells us.
6309 */
6310 virtual_avail = vstart;
6311 virtual_end = vend;
6312
6313 VPRINTF("specials ");
6314 #ifdef PMAP_CACHE_VIPT
6315 /*
6316 * If we have a VIPT cache, we need one page/pte per possible alias
6317 * page so we won't violate cache aliasing rules.
6318 */
6319 virtual_avail = (virtual_avail + arm_cache_prefer_mask) & ~arm_cache_prefer_mask;
6320 nptes = (arm_cache_prefer_mask >> L2_S_SHIFT) + 1;
6321 nptes = roundup(nptes, PAGE_SIZE / L2_S_SIZE);
6322 if (arm_pcache.icache_type != CACHE_TYPE_PIPT
6323 && arm_pcache.icache_way_size > nptes * L2_S_SIZE) {
6324 nptes = arm_pcache.icache_way_size >> L2_S_SHIFT;
6325 nptes = roundup(nptes, PAGE_SIZE / L2_S_SIZE);
6326 }
6327 #else
6328 nptes = PAGE_SIZE / L2_S_SIZE;
6329 #endif
6330 #ifdef MULTIPROCESSOR
6331 cnptes = nptes;
6332 nptes *= arm_cpu_max;
6333 #endif
6334 pmap_alloc_specials(&virtual_avail, nptes, &csrcp, &csrc_pte);
6335 pmap_set_pt_cache_mode(l1pt, (vaddr_t)csrc_pte, nptes);
6336 pmap_alloc_specials(&virtual_avail, nptes, &cdstp, &cdst_pte);
6337 pmap_set_pt_cache_mode(l1pt, (vaddr_t)cdst_pte, nptes);
6338 pmap_alloc_specials(&virtual_avail, nptes, &memhook, NULL);
6339 if (msgbufaddr == NULL) {
6340 pmap_alloc_specials(&virtual_avail,
6341 round_page(MSGBUFSIZE) / PAGE_SIZE,
6342 (void *)&msgbufaddr, NULL);
6343 }
6344
6345 /*
6346 * Allocate a range of kernel virtual address space to be used
6347 * for L2 descriptor tables and metadata allocation in
6348 * pmap_growkernel().
6349 */
6350 size = ((virtual_end - pmap_curmaxkvaddr) + L1_S_OFFSET) / L1_S_SIZE;
6351 pmap_alloc_specials(&virtual_avail,
6352 round_page(size * L2_TABLE_SIZE_REAL) / PAGE_SIZE,
6353 &pmap_kernel_l2ptp_kva, NULL);
6354
6355 size = (size + (L2_BUCKET_SIZE - 1)) / L2_BUCKET_SIZE;
6356 pmap_alloc_specials(&virtual_avail,
6357 round_page(size * sizeof(struct l2_dtable)) / PAGE_SIZE,
6358 &pmap_kernel_l2dtable_kva, NULL);
6359
6360 #ifndef ARM_MMU_EXTENDED
6361 /*
6362 * init the static-global locks and global pmap list.
6363 */
6364 mutex_init(&l1_lru_lock, MUTEX_DEFAULT, IPL_VM);
6365
6366 /*
6367 * We can now initialise the first L1's metadata.
6368 */
6369 SLIST_INIT(&l1_list);
6370 TAILQ_INIT(&l1_lru_list);
6371 pmap_init_l1(l1, l1pt);
6372 #endif /* ARM_MMU_EXTENDED */
6373
6374 #ifndef ARM_HAS_VBAR
6375 /* Set up vector page L1 details, if necessary */
6376 if (vector_page < KERNEL_BASE) {
6377 pm->pm_pl1vec = pmap_l1_kva(pm) + l1pte_index(vector_page);
6378 l2b = pmap_get_l2_bucket(pm, vector_page);
6379 KDASSERT(l2b != NULL);
6380 pm->pm_l1vec = l2b->l2b_pa | L1_C_PROTO |
6381 L1_C_DOM(pmap_domain(pm));
6382 } else
6383 pm->pm_pl1vec = NULL;
6384 #endif
6385
6386 VPRINTF("pools ");
6387 /*
6388 * Initialize the pmap cache
6389 */
6390 pool_cache_bootstrap(&pmap_cache, sizeof(struct pmap), 0, 0, 0,
6391 "pmappl", NULL, IPL_NONE, pmap_pmap_ctor, NULL, NULL);
6392
6393 /*
6394 * Initialize the pv pool.
6395 */
6396 pool_init(&pmap_pv_pool, sizeof(struct pv_entry), 0, 0, 0, "pvepl",
6397 &pmap_bootstrap_pv_allocator, IPL_NONE);
6398
6399 /*
6400 * Initialize the L2 dtable pool and cache.
6401 */
6402 pool_cache_bootstrap(&pmap_l2dtable_cache, sizeof(struct l2_dtable), 0,
6403 0, 0, "l2dtblpl", NULL, IPL_NONE, pmap_l2dtable_ctor, NULL, NULL);
6404
6405 /*
6406 * Initialise the L2 descriptor table pool and cache
6407 */
6408 pool_cache_bootstrap(&pmap_l2ptp_cache, L2_TABLE_SIZE_REAL,
6409 L2_TABLE_SIZE_REAL, 0, 0, "l2ptppl", NULL, IPL_NONE,
6410 pmap_l2ptp_ctor, NULL, NULL);
6411
6412 mutex_init(&memlock, MUTEX_DEFAULT, IPL_NONE);
6413
6414 cpu_dcache_wbinv_all();
6415 }
6416
6417 static bool
6418 pmap_set_pt_cache_mode(pd_entry_t *kl1, vaddr_t va, size_t nptes)
6419 {
6420 #ifdef ARM_MMU_EXTENDED
6421 return false;
6422 #else
6423 if (pte_l1_s_cache_mode == pte_l1_s_cache_mode_pt
6424 && pte_l2_s_cache_mode == pte_l2_s_cache_mode_pt)
6425 return false;
6426
6427 const vaddr_t eva = va + nptes * PAGE_SIZE;
6428 int rv = 0;
6429
6430 while (va < eva) {
6431 /*
6432 * Make sure the descriptor itself has the correct cache mode
6433 */
6434 pd_entry_t * const pdep = &kl1[l1pte_index(va)];
6435 pd_entry_t pde = *pdep;
6436
6437 if (l1pte_section_p(pde)) {
6438 KASSERT((L1_S_CACHE_MASK & L1_S_V6_SUPER) == 0);
6439 if ((pde & L1_S_CACHE_MASK) != pte_l1_s_cache_mode_pt) {
6440 *pdep = (pde & ~L1_S_CACHE_MASK) |
6441 pte_l1_s_cache_mode_pt;
6442 PDE_SYNC(pdep);
6443 cpu_dcache_wbinv_range((vaddr_t)pdep,
6444 sizeof(*pdep));
6445 rv = 1;
6446 }
6447 return rv;
6448 }
6449 vaddr_t pa = l1pte_pa(pde);
6450 pt_entry_t *ptep = (pt_entry_t *)kernel_pt_lookup(pa);
6451 if (ptep == NULL)
6452 panic("pmap_bootstrap: No PTP for va %#lx\n", va);
6453
6454 ptep += l2pte_index(va);
6455 const pt_entry_t opte = *ptep;
6456 if ((opte & L2_S_CACHE_MASK) != pte_l2_s_cache_mode_pt) {
6457 const pt_entry_t npte = (opte & ~L2_S_CACHE_MASK)
6458 | pte_l2_s_cache_mode_pt;
6459 l2pte_set(ptep, npte, opte);
6460 PTE_SYNC(ptep);
6461 cpu_dcache_wbinv_range((vaddr_t)ptep, sizeof(*ptep));
6462 rv = 1;
6463 }
6464 va += PAGE_SIZE;
6465 }
6466
6467 return (rv);
6468 #endif
6469 }
6470
6471 static void
6472 pmap_alloc_specials(vaddr_t *availp, int pages, vaddr_t *vap, pt_entry_t **ptep)
6473 {
6474 vaddr_t va = *availp;
6475 struct l2_bucket *l2b;
6476
6477 if (ptep) {
6478 l2b = pmap_get_l2_bucket(pmap_kernel(), va);
6479 if (l2b == NULL)
6480 panic("pmap_alloc_specials: no l2b for 0x%lx", va);
6481
6482 *ptep = &l2b->l2b_kva[l2pte_index(va)];
6483 }
6484
6485 *vap = va;
6486 *availp = va + (PAGE_SIZE * pages);
6487 }
6488
6489 void
6490 pmap_init(void)
6491 {
6492
6493 /*
6494 * Set the available memory vars - These do not map to real memory
6495 * addresses and cannot as the physical memory is fragmented.
6496 * They are used by ps for %mem calculations.
6497 * One could argue whether this should be the entire memory or just
6498 * the memory that is useable in a user process.
6499 */
6500 avail_start = ptoa(uvm_physseg_get_avail_start(uvm_physseg_get_first()));
6501 avail_end = ptoa(uvm_physseg_get_avail_end(uvm_physseg_get_last()));
6502
6503 /*
6504 * Now we need to free enough pv_entry structures to allow us to get
6505 * the kmem_map/kmem_object allocated and inited (done after this
6506 * function is finished). to do this we allocate one bootstrap page out
6507 * of kernel_map and use it to provide an initial pool of pv_entry
6508 * structures. we never free this page.
6509 */
6510 pool_setlowat(&pmap_pv_pool, (PAGE_SIZE / sizeof(struct pv_entry)) * 2);
6511
6512 #ifdef ARM_MMU_EXTENDED
6513 /*
6514 * Initialise the L1 pool and cache.
6515 */
6516
6517 pool_cache_bootstrap(&pmap_l1tt_cache, L1TT_SIZE, L1TT_SIZE,
6518 0, 0, "l1ttpl", &pmap_l1tt_allocator, IPL_NONE, pmap_l1tt_ctor,
6519 NULL, NULL);
6520
6521 int error __diagused = pmap_maxproc_set(maxproc);
6522 KASSERT(error == 0);
6523
6524 pmap_tlb_info_evcnt_attach(&pmap_tlb0_info);
6525 #endif
6526
6527 pmap_initialized = true;
6528 }
6529
6530 static vaddr_t last_bootstrap_page = 0;
6531 static void *free_bootstrap_pages = NULL;
6532
6533 static void *
6534 pmap_bootstrap_pv_page_alloc(struct pool *pp, int flags)
6535 {
6536 extern void *pool_page_alloc(struct pool *, int);
6537 vaddr_t new_page;
6538 void *rv;
6539
6540 if (pmap_initialized)
6541 return (pool_page_alloc(pp, flags));
6542
6543 if (free_bootstrap_pages) {
6544 rv = free_bootstrap_pages;
6545 free_bootstrap_pages = *((void **)rv);
6546 return (rv);
6547 }
6548
6549 KASSERT(kernel_map != NULL);
6550 new_page = uvm_km_alloc(kernel_map, PAGE_SIZE, 0,
6551 UVM_KMF_WIRED | ((flags & PR_WAITOK) ? 0 : UVM_KMF_NOWAIT));
6552
6553 KASSERT(new_page > last_bootstrap_page);
6554 last_bootstrap_page = new_page;
6555 return ((void *)new_page);
6556 }
6557
6558 static void
6559 pmap_bootstrap_pv_page_free(struct pool *pp, void *v)
6560 {
6561 extern void pool_page_free(struct pool *, void *);
6562
6563 if ((vaddr_t)v <= last_bootstrap_page) {
6564 *((void **)v) = free_bootstrap_pages;
6565 free_bootstrap_pages = v;
6566 return;
6567 }
6568
6569 if (pmap_initialized) {
6570 pool_page_free(pp, v);
6571 return;
6572 }
6573 }
6574
6575
6576 #if defined(ARM_MMU_EXTENDED)
6577 static void *
6578 pmap_l1tt_alloc(struct pool *pp, int flags)
6579 {
6580 struct pglist plist;
6581 vaddr_t va;
6582
6583 const int waitok = flags & PR_WAITOK;
6584
6585 int error = uvm_pglistalloc(L1TT_SIZE, 0, -1, L1TT_SIZE, 0, &plist, 1,
6586 waitok);
6587 if (error)
6588 panic("Cannot allocate L1TT physical pages, %d", error);
6589
6590 struct vm_page *pg = TAILQ_FIRST(&plist);
6591 #if !defined( __HAVE_MM_MD_DIRECT_MAPPED_PHYS)
6592
6593 /* Allocate a L1 translation table VA */
6594 va = uvm_km_alloc(kernel_map, L1TT_SIZE, L1TT_SIZE, UVM_KMF_VAONLY);
6595 if (va == 0)
6596 panic("Cannot allocate L1TT KVA");
6597
6598 const vaddr_t eva = va + L1TT_SIZE;
6599 vaddr_t mva = va;
6600 while (pg && mva < eva) {
6601 paddr_t pa = VM_PAGE_TO_PHYS(pg);
6602
6603 pmap_kenter_pa(mva, pa,
6604 VM_PROT_READ|VM_PROT_WRITE, PMAP_KMPAGE|PMAP_PTE);
6605
6606 mva += PAGE_SIZE;
6607 pg = TAILQ_NEXT(pg, pageq.queue);
6608 }
6609 KASSERTMSG(pg == NULL && mva == eva, "pg %p mva %" PRIxVADDR
6610 " eva %" PRIxVADDR, pg, mva, eva);
6611 #else
6612 bool ok;
6613 paddr_t pa = VM_PAGE_TO_PHYS(pg);
6614 va = pmap_direct_mapped_phys(pa, &ok, 0);
6615 KASSERT(ok);
6616 KASSERT(va >= KERNEL_BASE);
6617 #endif
6618
6619 return (void *)va;
6620 }
6621
6622 static void
6623 pmap_l1tt_free(struct pool *pp, void *v)
6624 {
6625 vaddr_t va = (vaddr_t)v;
6626
6627 #if !defined( __HAVE_MM_MD_DIRECT_MAPPED_PHYS)
6628 uvm_km_free(kernel_map, va, L1TT_SIZE, UVM_KMF_WIRED);
6629 #else
6630 #if defined(KERNEL_BASE_VOFFSET)
6631 paddr_t pa = va - KERNEL_BASE_VOFFSET;
6632 #else
6633 paddr_t pa = va - KERNEL_BASE + physical_start;
6634 #endif
6635 const paddr_t epa = pa + L1TT_SIZE;
6636
6637 for (; pa < epa; pa += PAGE_SIZE) {
6638 struct vm_page *pg = PHYS_TO_VM_PAGE(pa);
6639 uvm_pagefree(pg);
6640 }
6641 #endif
6642 }
6643 #endif
6644
6645 /*
6646 * pmap_postinit()
6647 *
6648 * This routine is called after the vm and kmem subsystems have been
6649 * initialised. This allows the pmap code to perform any initialisation
6650 * that can only be done once the memory allocation is in place.
6651 */
6652 void
6653 pmap_postinit(void)
6654 {
6655 #ifndef ARM_MMU_EXTENDED
6656 extern paddr_t physical_start, physical_end;
6657 struct l1_ttable *l1;
6658 struct pglist plist;
6659 struct vm_page *m;
6660 pd_entry_t *pdep;
6661 vaddr_t va, eva;
6662 u_int loop, needed;
6663 int error;
6664 #endif
6665
6666 pool_cache_setlowat(&pmap_l2ptp_cache, (PAGE_SIZE / L2_TABLE_SIZE_REAL) * 4);
6667 pool_cache_setlowat(&pmap_l2dtable_cache,
6668 (PAGE_SIZE / sizeof(struct l2_dtable)) * 2);
6669
6670 #ifndef ARM_MMU_EXTENDED
6671 needed = (maxproc / PMAP_DOMAINS) + ((maxproc % PMAP_DOMAINS) ? 1 : 0);
6672 needed -= 1;
6673
6674 l1 = kmem_alloc(sizeof(*l1) * needed, KM_SLEEP);
6675
6676 for (loop = 0; loop < needed; loop++, l1++) {
6677 /* Allocate a L1 page table */
6678 va = uvm_km_alloc(kernel_map, L1_TABLE_SIZE, 0, UVM_KMF_VAONLY);
6679 if (va == 0)
6680 panic("Cannot allocate L1 KVM");
6681
6682 error = uvm_pglistalloc(L1_TABLE_SIZE, physical_start,
6683 physical_end, L1_TABLE_SIZE, 0, &plist, 1, 1);
6684 if (error)
6685 panic("Cannot allocate L1 physical pages");
6686
6687 m = TAILQ_FIRST(&plist);
6688 eva = va + L1_TABLE_SIZE;
6689 pdep = (pd_entry_t *)va;
6690
6691 while (m && va < eva) {
6692 paddr_t pa = VM_PAGE_TO_PHYS(m);
6693
6694 pmap_kenter_pa(va, pa,
6695 VM_PROT_READ|VM_PROT_WRITE, PMAP_KMPAGE|PMAP_PTE);
6696
6697 va += PAGE_SIZE;
6698 m = TAILQ_NEXT(m, pageq.queue);
6699 }
6700
6701 #ifdef DIAGNOSTIC
6702 if (m)
6703 panic("pmap_alloc_l1pt: pglist not empty");
6704 #endif /* DIAGNOSTIC */
6705
6706 pmap_init_l1(l1, pdep);
6707 }
6708
6709 #ifdef DEBUG
6710 printf("pmap_postinit: Allocated %d static L1 descriptor tables\n",
6711 needed);
6712 #endif
6713 #endif /* !ARM_MMU_EXTENDED */
6714 }
6715
6716 /*
6717 * Note that the following routines are used by board-specific initialisation
6718 * code to configure the initial kernel page tables.
6719 *
6720 */
6721
6722 /*
6723 * This list exists for the benefit of pmap_map_chunk(). It keeps track
6724 * of the kernel L2 tables during bootstrap, so that pmap_map_chunk() can
6725 * find them as necessary.
6726 *
6727 * Note that the data on this list MUST remain valid after initarm() returns,
6728 * as pmap_bootstrap() uses it to contruct L2 table metadata.
6729 */
6730 SLIST_HEAD(, pv_addr) kernel_pt_list = SLIST_HEAD_INITIALIZER(kernel_pt_list);
6731
6732 static vaddr_t
6733 kernel_pt_lookup(paddr_t pa)
6734 {
6735 pv_addr_t *pv;
6736
6737 SLIST_FOREACH(pv, &kernel_pt_list, pv_list) {
6738 if (pv->pv_pa == (pa & ~PGOFSET))
6739 return (pv->pv_va | (pa & PGOFSET));
6740 }
6741 return (0);
6742 }
6743
6744 /*
6745 * pmap_map_section:
6746 *
6747 * Create a single section mapping.
6748 */
6749 void
6750 pmap_map_section(vaddr_t l1pt, vaddr_t va, paddr_t pa, int prot, int cache)
6751 {
6752 pd_entry_t * const pdep = (pd_entry_t *) l1pt;
6753 const size_t l1slot = l1pte_index(va);
6754 pd_entry_t fl;
6755
6756 KASSERT(((va | pa) & L1_S_OFFSET) == 0);
6757
6758 switch (cache) {
6759 case PTE_NOCACHE:
6760 default:
6761 fl = 0;
6762 break;
6763
6764 case PTE_CACHE:
6765 fl = pte_l1_s_cache_mode;
6766 break;
6767
6768 case PTE_PAGETABLE:
6769 fl = pte_l1_s_cache_mode_pt;
6770 break;
6771 }
6772
6773 const pd_entry_t npde = L1_S_PROTO | pa |
6774 L1_S_PROT(PTE_KERNEL, prot) | fl | L1_S_DOM(PMAP_DOMAIN_KERNEL);
6775 l1pte_setone(pdep + l1slot, npde);
6776 PDE_SYNC(pdep + l1slot);
6777 }
6778
6779 /*
6780 * pmap_map_entry:
6781 *
6782 * Create a single page mapping.
6783 */
6784 void
6785 pmap_map_entry(vaddr_t l1pt, vaddr_t va, paddr_t pa, int prot, int cache)
6786 {
6787 pd_entry_t * const pdep = (pd_entry_t *) l1pt;
6788 const size_t l1slot = l1pte_index(va);
6789 pt_entry_t npte;
6790 pt_entry_t *ptep;
6791
6792 KASSERT(((va | pa) & PGOFSET) == 0);
6793
6794 switch (cache) {
6795 case PTE_NOCACHE:
6796 default:
6797 npte = 0;
6798 break;
6799
6800 case PTE_CACHE:
6801 npte = pte_l2_s_cache_mode;
6802 break;
6803
6804 case PTE_PAGETABLE:
6805 npte = pte_l2_s_cache_mode_pt;
6806 break;
6807 }
6808
6809 if ((pdep[l1slot] & L1_TYPE_MASK) != L1_TYPE_C)
6810 panic("pmap_map_entry: no L2 table for VA 0x%08lx", va);
6811
6812 ptep = (pt_entry_t *) kernel_pt_lookup(l1pte_pa(pdep[l1slot]));
6813 if (ptep == NULL)
6814 panic("pmap_map_entry: can't find L2 table for VA 0x%08lx", va);
6815
6816 npte |= L2_S_PROTO | pa | L2_S_PROT(PTE_KERNEL, prot);
6817 #ifdef ARM_MMU_EXTENDED
6818 if (prot & VM_PROT_EXECUTE) {
6819 npte &= ~L2_XS_XN;
6820 }
6821 #endif
6822 ptep += l2pte_index(va);
6823 l2pte_set(ptep, npte, 0);
6824 PTE_SYNC(ptep);
6825 }
6826
6827 /*
6828 * pmap_link_l2pt:
6829 *
6830 * Link the L2 page table specified by "l2pv" into the L1
6831 * page table at the slot for "va".
6832 */
6833 void
6834 pmap_link_l2pt(vaddr_t l1pt, vaddr_t va, pv_addr_t *l2pv)
6835 {
6836 pd_entry_t * const pdep = (pd_entry_t *) l1pt + l1pte_index(va);
6837
6838 KASSERT((va & ((L1_S_SIZE * (PAGE_SIZE / L2_T_SIZE)) - 1)) == 0);
6839 KASSERT((l2pv->pv_pa & PGOFSET) == 0);
6840
6841 const pd_entry_t npde = L1_C_DOM(PMAP_DOMAIN_KERNEL) | L1_C_PROTO
6842 | l2pv->pv_pa;
6843
6844 l1pte_set(pdep, npde);
6845 PDE_SYNC_RANGE(pdep, PAGE_SIZE / L2_T_SIZE);
6846
6847 SLIST_INSERT_HEAD(&kernel_pt_list, l2pv, pv_list);
6848 }
6849
6850 /*
6851 * pmap_map_chunk:
6852 *
6853 * Map a chunk of memory using the most efficient mappings
6854 * possible (section, large page, small page) into the
6855 * provided L1 and L2 tables at the specified virtual address.
6856 */
6857 vsize_t
6858 pmap_map_chunk(vaddr_t l1pt, vaddr_t va, paddr_t pa, vsize_t size,
6859 int prot, int cache)
6860 {
6861 pd_entry_t * const pdep = (pd_entry_t *) l1pt;
6862 pt_entry_t f1, f2s, f2l;
6863 vsize_t resid;
6864
6865 resid = (size + (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
6866
6867 if (l1pt == 0)
6868 panic("pmap_map_chunk: no L1 table provided");
6869
6870 // VPRINTF("pmap_map_chunk: pa=0x%lx va=0x%lx size=0x%lx resid=0x%lx "
6871 // "prot=0x%x cache=%d\n", pa, va, size, resid, prot, cache);
6872
6873 switch (cache) {
6874 case PTE_NOCACHE:
6875 default:
6876 f1 = 0;
6877 f2l = 0;
6878 f2s = 0;
6879 break;
6880
6881 case PTE_CACHE:
6882 f1 = pte_l1_s_cache_mode;
6883 f2l = pte_l2_l_cache_mode;
6884 f2s = pte_l2_s_cache_mode;
6885 break;
6886
6887 case PTE_PAGETABLE:
6888 f1 = pte_l1_s_cache_mode_pt;
6889 f2l = pte_l2_l_cache_mode_pt;
6890 f2s = pte_l2_s_cache_mode_pt;
6891 break;
6892 }
6893
6894 size = resid;
6895
6896 while (resid > 0) {
6897 const size_t l1slot = l1pte_index(va);
6898 #ifdef ARM_MMU_EXTENDED
6899 /* See if we can use a supersection mapping. */
6900 if (L1_SS_PROTO && L1_SS_MAPPABLE_P(va, pa, resid)) {
6901 /* Supersection are always domain 0 */
6902 const pd_entry_t npde = L1_SS_PROTO | pa
6903 | ((prot & VM_PROT_EXECUTE) ? 0 : L1_S_V6_XN)
6904 | (va & 0x80000000 ? 0 : L1_S_V6_nG)
6905 | L1_S_PROT(PTE_KERNEL, prot) | f1;
6906 VPRINTF("sS");
6907 l1pte_set(&pdep[l1slot], npde);
6908 PDE_SYNC_RANGE(&pdep[l1slot], L1_SS_SIZE / L1_S_SIZE);
6909 // VPRINTF("\npmap_map_chunk: pa=0x%lx va=0x%lx resid=0x%08lx "
6910 // "npdep=%p pde=0x%x\n", pa, va, resid, &pdep[l1slot], npde);
6911 va += L1_SS_SIZE;
6912 pa += L1_SS_SIZE;
6913 resid -= L1_SS_SIZE;
6914 continue;
6915 }
6916 #endif
6917 /* See if we can use a section mapping. */
6918 if (L1_S_MAPPABLE_P(va, pa, resid)) {
6919 const pd_entry_t npde = L1_S_PROTO | pa
6920 #ifdef ARM_MMU_EXTENDED
6921 | ((prot & VM_PROT_EXECUTE) ? 0 : L1_S_V6_XN)
6922 | (va & 0x80000000 ? 0 : L1_S_V6_nG)
6923 #endif
6924 | L1_S_PROT(PTE_KERNEL, prot) | f1
6925 | L1_S_DOM(PMAP_DOMAIN_KERNEL);
6926 VPRINTF("S");
6927 l1pte_set(&pdep[l1slot], npde);
6928 PDE_SYNC(&pdep[l1slot]);
6929 // VPRINTF("\npmap_map_chunk: pa=0x%lx va=0x%lx resid=0x%08lx "
6930 // "npdep=%p pde=0x%x\n", pa, va, resid, &pdep[l1slot], npde);
6931 va += L1_S_SIZE;
6932 pa += L1_S_SIZE;
6933 resid -= L1_S_SIZE;
6934 continue;
6935 }
6936
6937 /*
6938 * Ok, we're going to use an L2 table. Make sure
6939 * one is actually in the corresponding L1 slot
6940 * for the current VA.
6941 */
6942 if ((pdep[l1slot] & L1_TYPE_MASK) != L1_TYPE_C)
6943 panic("%s: no L2 table for VA %#lx", __func__, va);
6944
6945 pt_entry_t *ptep = (pt_entry_t *) kernel_pt_lookup(l1pte_pa(pdep[l1slot]));
6946 if (ptep == NULL)
6947 panic("%s: can't find L2 table for VA %#lx", __func__,
6948 va);
6949
6950 ptep += l2pte_index(va);
6951
6952 /* See if we can use a L2 large page mapping. */
6953 if (L2_L_MAPPABLE_P(va, pa, resid)) {
6954 const pt_entry_t npte = L2_L_PROTO | pa
6955 #ifdef ARM_MMU_EXTENDED
6956 | ((prot & VM_PROT_EXECUTE) ? 0 : L2_XS_L_XN)
6957 | (va & 0x80000000 ? 0 : L2_XS_nG)
6958 #endif
6959 | L2_L_PROT(PTE_KERNEL, prot) | f2l;
6960 VPRINTF("L");
6961 l2pte_set(ptep, npte, 0);
6962 PTE_SYNC_RANGE(ptep, L2_L_SIZE / L2_S_SIZE);
6963 va += L2_L_SIZE;
6964 pa += L2_L_SIZE;
6965 resid -= L2_L_SIZE;
6966 continue;
6967 }
6968
6969 VPRINTF("P");
6970 /* Use a small page mapping. */
6971 pt_entry_t npte = L2_S_PROTO | pa
6972 #ifdef ARM_MMU_EXTENDED
6973 | ((prot & VM_PROT_EXECUTE) ? 0 : L2_XS_XN)
6974 | (va & 0x80000000 ? 0 : L2_XS_nG)
6975 #endif
6976 | L2_S_PROT(PTE_KERNEL, prot) | f2s;
6977 #ifdef ARM_MMU_EXTENDED
6978 npte &= ((prot & VM_PROT_EXECUTE) ? ~L2_XS_XN : ~0);
6979 #endif
6980 l2pte_set(ptep, npte, 0);
6981 PTE_SYNC(ptep);
6982 va += PAGE_SIZE;
6983 pa += PAGE_SIZE;
6984 resid -= PAGE_SIZE;
6985 }
6986 VPRINTF("\n");
6987 return (size);
6988 }
6989
6990 /*
6991 * pmap_unmap_chunk:
6992 *
6993 * Unmap a chunk of memory that was previously pmap_map_chunk
6994 */
6995 void
6996 pmap_unmap_chunk(vaddr_t l1pt, vaddr_t va, vsize_t size)
6997 {
6998 pd_entry_t * const pdep = (pd_entry_t *) l1pt;
6999 const size_t l1slot = l1pte_index(va);
7000
7001 KASSERT(size == L1_SS_SIZE || size == L1_S_SIZE);
7002
7003 l1pte_set(&pdep[l1slot], 0);
7004 PDE_SYNC_RANGE(&pdep[l1slot], size / L1_S_SIZE);
7005
7006 pmap_tlb_flush_SE(pmap_kernel(), va, PVF_REF);
7007 }
7008
7009
7010
7011 /********************** Static device map routines ***************************/
7012
7013 static const struct pmap_devmap *pmap_devmap_table;
7014
7015 /*
7016 * Register the devmap table. This is provided in case early console
7017 * initialization needs to register mappings created by bootstrap code
7018 * before pmap_devmap_bootstrap() is called.
7019 */
7020 void
7021 pmap_devmap_register(const struct pmap_devmap *table)
7022 {
7023
7024 pmap_devmap_table = table;
7025 }
7026
7027 /*
7028 * Map all of the static regions in the devmap table, and remember
7029 * the devmap table so other parts of the kernel can look up entries
7030 * later.
7031 */
7032 void
7033 pmap_devmap_bootstrap(vaddr_t l1pt, const struct pmap_devmap *table)
7034 {
7035 int i;
7036
7037 pmap_devmap_table = table;
7038
7039 for (i = 0; pmap_devmap_table[i].pd_size != 0; i++) {
7040 const struct pmap_devmap *pdp = &pmap_devmap_table[i];
7041
7042 KASSERTMSG(VADDR_MAX - pdp->pd_va >= pdp->pd_size - 1, "va %" PRIxVADDR
7043 " sz %" PRIxPSIZE, pdp->pd_va, pdp->pd_size);
7044 KASSERTMSG(PADDR_MAX - pdp->pd_pa >= pdp->pd_size - 1, "pa %" PRIxPADDR
7045 " sz %" PRIxPSIZE, pdp->pd_pa, pdp->pd_size);
7046 VPRINTF("devmap: %08lx -> %08lx @ %08lx\n", pdp->pd_pa,
7047 pdp->pd_pa + pdp->pd_size - 1, pdp->pd_va);
7048
7049 pmap_map_chunk(l1pt, pdp->pd_va, pdp->pd_pa, pdp->pd_size,
7050 pdp->pd_prot, pdp->pd_cache);
7051 }
7052 }
7053
7054 const struct pmap_devmap *
7055 pmap_devmap_find_pa(paddr_t pa, psize_t size)
7056 {
7057 uint64_t endpa;
7058 int i;
7059
7060 if (pmap_devmap_table == NULL)
7061 return (NULL);
7062
7063 endpa = (uint64_t)pa + (uint64_t)(size - 1);
7064
7065 for (i = 0; pmap_devmap_table[i].pd_size != 0; i++) {
7066 if (pa >= pmap_devmap_table[i].pd_pa &&
7067 endpa <= (uint64_t)pmap_devmap_table[i].pd_pa +
7068 (uint64_t)(pmap_devmap_table[i].pd_size - 1))
7069 return (&pmap_devmap_table[i]);
7070 }
7071
7072 return (NULL);
7073 }
7074
7075 const struct pmap_devmap *
7076 pmap_devmap_find_va(vaddr_t va, vsize_t size)
7077 {
7078 int i;
7079
7080 if (pmap_devmap_table == NULL)
7081 return (NULL);
7082
7083 for (i = 0; pmap_devmap_table[i].pd_size != 0; i++) {
7084 if (va >= pmap_devmap_table[i].pd_va &&
7085 va + size - 1 <= pmap_devmap_table[i].pd_va +
7086 pmap_devmap_table[i].pd_size - 1)
7087 return (&pmap_devmap_table[i]);
7088 }
7089
7090 return (NULL);
7091 }
7092
7093 /********************** PTE initialization routines **************************/
7094
7095 /*
7096 * These routines are called when the CPU type is identified to set up
7097 * the PTE prototypes, cache modes, etc.
7098 *
7099 * The variables are always here, just in case modules need to reference
7100 * them (though, they shouldn't).
7101 */
7102
7103 pt_entry_t pte_l1_s_cache_mode;
7104 pt_entry_t pte_l1_s_wc_mode;
7105 pt_entry_t pte_l1_s_cache_mode_pt;
7106 pt_entry_t pte_l1_s_cache_mask;
7107
7108 pt_entry_t pte_l2_l_cache_mode;
7109 pt_entry_t pte_l2_l_wc_mode;
7110 pt_entry_t pte_l2_l_cache_mode_pt;
7111 pt_entry_t pte_l2_l_cache_mask;
7112
7113 pt_entry_t pte_l2_s_cache_mode;
7114 pt_entry_t pte_l2_s_wc_mode;
7115 pt_entry_t pte_l2_s_cache_mode_pt;
7116 pt_entry_t pte_l2_s_cache_mask;
7117
7118 pt_entry_t pte_l1_s_prot_u;
7119 pt_entry_t pte_l1_s_prot_w;
7120 pt_entry_t pte_l1_s_prot_ro;
7121 pt_entry_t pte_l1_s_prot_mask;
7122
7123 pt_entry_t pte_l2_s_prot_u;
7124 pt_entry_t pte_l2_s_prot_w;
7125 pt_entry_t pte_l2_s_prot_ro;
7126 pt_entry_t pte_l2_s_prot_mask;
7127
7128 pt_entry_t pte_l2_l_prot_u;
7129 pt_entry_t pte_l2_l_prot_w;
7130 pt_entry_t pte_l2_l_prot_ro;
7131 pt_entry_t pte_l2_l_prot_mask;
7132
7133 pt_entry_t pte_l1_ss_proto;
7134 pt_entry_t pte_l1_s_proto;
7135 pt_entry_t pte_l1_c_proto;
7136 pt_entry_t pte_l2_s_proto;
7137
7138 void (*pmap_copy_page_func)(paddr_t, paddr_t);
7139 void (*pmap_zero_page_func)(paddr_t);
7140
7141 #if (ARM_MMU_GENERIC + ARM_MMU_SA1 + ARM_MMU_V6 + ARM_MMU_V7) != 0
7142 void
7143 pmap_pte_init_generic(void)
7144 {
7145
7146 pte_l1_s_cache_mode = L1_S_B|L1_S_C;
7147 pte_l1_s_wc_mode = L1_S_B;
7148 pte_l1_s_cache_mask = L1_S_CACHE_MASK_generic;
7149
7150 pte_l2_l_cache_mode = L2_B|L2_C;
7151 pte_l2_l_wc_mode = L2_B;
7152 pte_l2_l_cache_mask = L2_L_CACHE_MASK_generic;
7153
7154 pte_l2_s_cache_mode = L2_B|L2_C;
7155 pte_l2_s_wc_mode = L2_B;
7156 pte_l2_s_cache_mask = L2_S_CACHE_MASK_generic;
7157
7158 /*
7159 * If we have a write-through cache, set B and C. If
7160 * we have a write-back cache, then we assume setting
7161 * only C will make those pages write-through (except for those
7162 * Cortex CPUs which can read the L1 caches).
7163 */
7164 if (cpufuncs.cf_dcache_wb_range == (void *) cpufunc_nullop
7165 #if ARM_MMU_V7 > 0
7166 || CPU_ID_CORTEX_P(curcpu()->ci_arm_cpuid)
7167 #endif
7168 #if ARM_MMU_V6 > 0
7169 || CPU_ID_ARM11_P(curcpu()->ci_arm_cpuid) /* arm116 errata 399234 */
7170 #endif
7171 || false) {
7172 pte_l1_s_cache_mode_pt = L1_S_B|L1_S_C;
7173 pte_l2_l_cache_mode_pt = L2_B|L2_C;
7174 pte_l2_s_cache_mode_pt = L2_B|L2_C;
7175 } else {
7176 pte_l1_s_cache_mode_pt = L1_S_C; /* write through */
7177 pte_l2_l_cache_mode_pt = L2_C; /* write through */
7178 pte_l2_s_cache_mode_pt = L2_C; /* write through */
7179 }
7180
7181 pte_l1_s_prot_u = L1_S_PROT_U_generic;
7182 pte_l1_s_prot_w = L1_S_PROT_W_generic;
7183 pte_l1_s_prot_ro = L1_S_PROT_RO_generic;
7184 pte_l1_s_prot_mask = L1_S_PROT_MASK_generic;
7185
7186 pte_l2_s_prot_u = L2_S_PROT_U_generic;
7187 pte_l2_s_prot_w = L2_S_PROT_W_generic;
7188 pte_l2_s_prot_ro = L2_S_PROT_RO_generic;
7189 pte_l2_s_prot_mask = L2_S_PROT_MASK_generic;
7190
7191 pte_l2_l_prot_u = L2_L_PROT_U_generic;
7192 pte_l2_l_prot_w = L2_L_PROT_W_generic;
7193 pte_l2_l_prot_ro = L2_L_PROT_RO_generic;
7194 pte_l2_l_prot_mask = L2_L_PROT_MASK_generic;
7195
7196 pte_l1_ss_proto = L1_SS_PROTO_generic;
7197 pte_l1_s_proto = L1_S_PROTO_generic;
7198 pte_l1_c_proto = L1_C_PROTO_generic;
7199 pte_l2_s_proto = L2_S_PROTO_generic;
7200
7201 pmap_copy_page_func = pmap_copy_page_generic;
7202 pmap_zero_page_func = pmap_zero_page_generic;
7203 }
7204
7205 #if defined(CPU_ARM8)
7206 void
7207 pmap_pte_init_arm8(void)
7208 {
7209
7210 /*
7211 * ARM8 is compatible with generic, but we need to use
7212 * the page tables uncached.
7213 */
7214 pmap_pte_init_generic();
7215
7216 pte_l1_s_cache_mode_pt = 0;
7217 pte_l2_l_cache_mode_pt = 0;
7218 pte_l2_s_cache_mode_pt = 0;
7219 }
7220 #endif /* CPU_ARM8 */
7221
7222 #if defined(CPU_ARM9) && defined(ARM9_CACHE_WRITE_THROUGH)
7223 void
7224 pmap_pte_init_arm9(void)
7225 {
7226
7227 /*
7228 * ARM9 is compatible with generic, but we want to use
7229 * write-through caching for now.
7230 */
7231 pmap_pte_init_generic();
7232
7233 pte_l1_s_cache_mode = L1_S_C;
7234 pte_l2_l_cache_mode = L2_C;
7235 pte_l2_s_cache_mode = L2_C;
7236
7237 pte_l1_s_wc_mode = L1_S_B;
7238 pte_l2_l_wc_mode = L2_B;
7239 pte_l2_s_wc_mode = L2_B;
7240
7241 pte_l1_s_cache_mode_pt = L1_S_C;
7242 pte_l2_l_cache_mode_pt = L2_C;
7243 pte_l2_s_cache_mode_pt = L2_C;
7244 }
7245 #endif /* CPU_ARM9 && ARM9_CACHE_WRITE_THROUGH */
7246 #endif /* (ARM_MMU_GENERIC + ARM_MMU_SA1 + ARM_MMU_V6) != 0 */
7247
7248 #if defined(CPU_ARM10)
7249 void
7250 pmap_pte_init_arm10(void)
7251 {
7252
7253 /*
7254 * ARM10 is compatible with generic, but we want to use
7255 * write-through caching for now.
7256 */
7257 pmap_pte_init_generic();
7258
7259 pte_l1_s_cache_mode = L1_S_B | L1_S_C;
7260 pte_l2_l_cache_mode = L2_B | L2_C;
7261 pte_l2_s_cache_mode = L2_B | L2_C;
7262
7263 pte_l1_s_cache_mode = L1_S_B;
7264 pte_l2_l_cache_mode = L2_B;
7265 pte_l2_s_cache_mode = L2_B;
7266
7267 pte_l1_s_cache_mode_pt = L1_S_C;
7268 pte_l2_l_cache_mode_pt = L2_C;
7269 pte_l2_s_cache_mode_pt = L2_C;
7270
7271 }
7272 #endif /* CPU_ARM10 */
7273
7274 #if defined(CPU_ARM11) && defined(ARM11_CACHE_WRITE_THROUGH)
7275 void
7276 pmap_pte_init_arm11(void)
7277 {
7278
7279 /*
7280 * ARM11 is compatible with generic, but we want to use
7281 * write-through caching for now.
7282 */
7283 pmap_pte_init_generic();
7284
7285 pte_l1_s_cache_mode = L1_S_C;
7286 pte_l2_l_cache_mode = L2_C;
7287 pte_l2_s_cache_mode = L2_C;
7288
7289 pte_l1_s_wc_mode = L1_S_B;
7290 pte_l2_l_wc_mode = L2_B;
7291 pte_l2_s_wc_mode = L2_B;
7292
7293 pte_l1_s_cache_mode_pt = L1_S_C;
7294 pte_l2_l_cache_mode_pt = L2_C;
7295 pte_l2_s_cache_mode_pt = L2_C;
7296 }
7297 #endif /* CPU_ARM11 && ARM11_CACHE_WRITE_THROUGH */
7298
7299 #if ARM_MMU_SA1 == 1
7300 void
7301 pmap_pte_init_sa1(void)
7302 {
7303
7304 /*
7305 * The StrongARM SA-1 cache does not have a write-through
7306 * mode. So, do the generic initialization, then reset
7307 * the page table cache mode to B=1,C=1, and note that
7308 * the PTEs need to be sync'd.
7309 */
7310 pmap_pte_init_generic();
7311
7312 pte_l1_s_cache_mode_pt = L1_S_B|L1_S_C;
7313 pte_l2_l_cache_mode_pt = L2_B|L2_C;
7314 pte_l2_s_cache_mode_pt = L2_B|L2_C;
7315
7316 pmap_needs_pte_sync = 1;
7317 }
7318 #endif /* ARM_MMU_SA1 == 1*/
7319
7320 #if ARM_MMU_XSCALE == 1
7321 #if (ARM_NMMUS > 1)
7322 static u_int xscale_use_minidata;
7323 #endif
7324
7325 void
7326 pmap_pte_init_xscale(void)
7327 {
7328 uint32_t auxctl;
7329 int write_through = 0;
7330
7331 pte_l1_s_cache_mode = L1_S_B|L1_S_C;
7332 pte_l1_s_wc_mode = L1_S_B;
7333 pte_l1_s_cache_mask = L1_S_CACHE_MASK_xscale;
7334
7335 pte_l2_l_cache_mode = L2_B|L2_C;
7336 pte_l2_l_wc_mode = L2_B;
7337 pte_l2_l_cache_mask = L2_L_CACHE_MASK_xscale;
7338
7339 pte_l2_s_cache_mode = L2_B|L2_C;
7340 pte_l2_s_wc_mode = L2_B;
7341 pte_l2_s_cache_mask = L2_S_CACHE_MASK_xscale;
7342
7343 pte_l1_s_cache_mode_pt = L1_S_C;
7344 pte_l2_l_cache_mode_pt = L2_C;
7345 pte_l2_s_cache_mode_pt = L2_C;
7346
7347 #ifdef XSCALE_CACHE_READ_WRITE_ALLOCATE
7348 /*
7349 * The XScale core has an enhanced mode where writes that
7350 * miss the cache cause a cache line to be allocated. This
7351 * is significantly faster than the traditional, write-through
7352 * behavior of this case.
7353 */
7354 pte_l1_s_cache_mode |= L1_S_XS_TEX(TEX_XSCALE_X);
7355 pte_l2_l_cache_mode |= L2_XS_L_TEX(TEX_XSCALE_X);
7356 pte_l2_s_cache_mode |= L2_XS_T_TEX(TEX_XSCALE_X);
7357 #endif /* XSCALE_CACHE_READ_WRITE_ALLOCATE */
7358
7359 #ifdef XSCALE_CACHE_WRITE_THROUGH
7360 /*
7361 * Some versions of the XScale core have various bugs in
7362 * their cache units, the work-around for which is to run
7363 * the cache in write-through mode. Unfortunately, this
7364 * has a major (negative) impact on performance. So, we
7365 * go ahead and run fast-and-loose, in the hopes that we
7366 * don't line up the planets in a way that will trip the
7367 * bugs.
7368 *
7369 * However, we give you the option to be slow-but-correct.
7370 */
7371 write_through = 1;
7372 #elif defined(XSCALE_CACHE_WRITE_BACK)
7373 /* force write back cache mode */
7374 write_through = 0;
7375 #elif defined(CPU_XSCALE_PXA250) || defined(CPU_XSCALE_PXA270)
7376 /*
7377 * Intel PXA2[15]0 processors are known to have a bug in
7378 * write-back cache on revision 4 and earlier (stepping
7379 * A[01] and B[012]). Fixed for C0 and later.
7380 */
7381 {
7382 uint32_t id, type;
7383
7384 id = cpufunc_id();
7385 type = id & ~(CPU_ID_XSCALE_COREREV_MASK|CPU_ID_REVISION_MASK);
7386
7387 if (type == CPU_ID_PXA250 || type == CPU_ID_PXA210) {
7388 if ((id & CPU_ID_REVISION_MASK) < 5) {
7389 /* write through for stepping A0-1 and B0-2 */
7390 write_through = 1;
7391 }
7392 }
7393 }
7394 #endif /* XSCALE_CACHE_WRITE_THROUGH */
7395
7396 if (write_through) {
7397 pte_l1_s_cache_mode = L1_S_C;
7398 pte_l2_l_cache_mode = L2_C;
7399 pte_l2_s_cache_mode = L2_C;
7400 }
7401
7402 #if (ARM_NMMUS > 1)
7403 xscale_use_minidata = 1;
7404 #endif
7405
7406 pte_l1_s_prot_u = L1_S_PROT_U_xscale;
7407 pte_l1_s_prot_w = L1_S_PROT_W_xscale;
7408 pte_l1_s_prot_ro = L1_S_PROT_RO_xscale;
7409 pte_l1_s_prot_mask = L1_S_PROT_MASK_xscale;
7410
7411 pte_l2_s_prot_u = L2_S_PROT_U_xscale;
7412 pte_l2_s_prot_w = L2_S_PROT_W_xscale;
7413 pte_l2_s_prot_ro = L2_S_PROT_RO_xscale;
7414 pte_l2_s_prot_mask = L2_S_PROT_MASK_xscale;
7415
7416 pte_l2_l_prot_u = L2_L_PROT_U_xscale;
7417 pte_l2_l_prot_w = L2_L_PROT_W_xscale;
7418 pte_l2_l_prot_ro = L2_L_PROT_RO_xscale;
7419 pte_l2_l_prot_mask = L2_L_PROT_MASK_xscale;
7420
7421 pte_l1_ss_proto = L1_SS_PROTO_xscale;
7422 pte_l1_s_proto = L1_S_PROTO_xscale;
7423 pte_l1_c_proto = L1_C_PROTO_xscale;
7424 pte_l2_s_proto = L2_S_PROTO_xscale;
7425
7426 pmap_copy_page_func = pmap_copy_page_xscale;
7427 pmap_zero_page_func = pmap_zero_page_xscale;
7428
7429 /*
7430 * Disable ECC protection of page table access, for now.
7431 */
7432 auxctl = armreg_auxctl_read();
7433 auxctl &= ~XSCALE_AUXCTL_P;
7434 armreg_auxctl_write(auxctl);
7435 }
7436
7437 /*
7438 * xscale_setup_minidata:
7439 *
7440 * Set up the mini-data cache clean area. We require the
7441 * caller to allocate the right amount of physically and
7442 * virtually contiguous space.
7443 */
7444 void
7445 xscale_setup_minidata(vaddr_t l1pt, vaddr_t va, paddr_t pa)
7446 {
7447 extern vaddr_t xscale_minidata_clean_addr;
7448 extern vsize_t xscale_minidata_clean_size; /* already initialized */
7449 pd_entry_t *pde = (pd_entry_t *) l1pt;
7450 vsize_t size;
7451 uint32_t auxctl;
7452
7453 xscale_minidata_clean_addr = va;
7454
7455 /* Round it to page size. */
7456 size = (xscale_minidata_clean_size + L2_S_OFFSET) & L2_S_FRAME;
7457
7458 for (; size != 0;
7459 va += L2_S_SIZE, pa += L2_S_SIZE, size -= L2_S_SIZE) {
7460 const size_t l1slot = l1pte_index(va);
7461 pt_entry_t *ptep = (pt_entry_t *) kernel_pt_lookup(l1pte_pa(pde[l1slot]));
7462 if (ptep == NULL)
7463 panic("xscale_setup_minidata: can't find L2 table for "
7464 "VA 0x%08lx", va);
7465
7466 ptep += l2pte_index(va);
7467 pt_entry_t opte = *ptep;
7468 l2pte_set(ptep,
7469 L2_S_PROTO | pa | L2_S_PROT(PTE_KERNEL, VM_PROT_READ)
7470 | L2_C | L2_XS_T_TEX(TEX_XSCALE_X), opte);
7471 }
7472
7473 /*
7474 * Configure the mini-data cache for write-back with
7475 * read/write-allocate.
7476 *
7477 * NOTE: In order to reconfigure the mini-data cache, we must
7478 * make sure it contains no valid data! In order to do that,
7479 * we must issue a global data cache invalidate command!
7480 *
7481 * WE ASSUME WE ARE RUNNING UN-CACHED WHEN THIS ROUTINE IS CALLED!
7482 * THIS IS VERY IMPORTANT!
7483 */
7484
7485 /* Invalidate data and mini-data. */
7486 __asm volatile("mcr p15, 0, %0, c7, c6, 0" : : "r" (0));
7487 auxctl = armreg_auxctl_read();
7488 auxctl = (auxctl & ~XSCALE_AUXCTL_MD_MASK) | XSCALE_AUXCTL_MD_WB_RWA;
7489 armreg_auxctl_write(auxctl);
7490 }
7491
7492 /*
7493 * Change the PTEs for the specified kernel mappings such that they
7494 * will use the mini data cache instead of the main data cache.
7495 */
7496 void
7497 pmap_uarea(vaddr_t va)
7498 {
7499 vaddr_t next_bucket, eva;
7500
7501 #if (ARM_NMMUS > 1)
7502 if (xscale_use_minidata == 0)
7503 return;
7504 #endif
7505
7506 eva = va + USPACE;
7507
7508 while (va < eva) {
7509 next_bucket = L2_NEXT_BUCKET_VA(va);
7510 if (next_bucket > eva)
7511 next_bucket = eva;
7512
7513 struct l2_bucket *l2b = pmap_get_l2_bucket(pmap_kernel(), va);
7514 KDASSERT(l2b != NULL);
7515
7516 pt_entry_t * const sptep = &l2b->l2b_kva[l2pte_index(va)];
7517 pt_entry_t *ptep = sptep;
7518
7519 while (va < next_bucket) {
7520 const pt_entry_t opte = *ptep;
7521 if (!l2pte_minidata_p(opte)) {
7522 cpu_dcache_wbinv_range(va, PAGE_SIZE);
7523 cpu_tlb_flushD_SE(va);
7524 l2pte_set(ptep, opte & ~L2_B, opte);
7525 }
7526 ptep += PAGE_SIZE / L2_S_SIZE;
7527 va += PAGE_SIZE;
7528 }
7529 PTE_SYNC_RANGE(sptep, (u_int)(ptep - sptep));
7530 }
7531 cpu_cpwait();
7532 }
7533 #endif /* ARM_MMU_XSCALE == 1 */
7534
7535
7536 #if defined(CPU_ARM11MPCORE)
7537
7538 void
7539 pmap_pte_init_arm11mpcore(void)
7540 {
7541
7542 /* cache mode is controlled by 5 bits (B, C, TEX) */
7543 pte_l1_s_cache_mask = L1_S_CACHE_MASK_armv6;
7544 pte_l2_l_cache_mask = L2_L_CACHE_MASK_armv6;
7545 #if defined(ARM11MPCORE_COMPAT_MMU) || defined(ARMV6_EXTENDED_SMALL_PAGE)
7546 /* use extended small page (without APn, with TEX) */
7547 pte_l2_s_cache_mask = L2_XS_CACHE_MASK_armv6;
7548 #else
7549 pte_l2_s_cache_mask = L2_S_CACHE_MASK_armv6c;
7550 #endif
7551
7552 /* write-back, write-allocate */
7553 pte_l1_s_cache_mode = L1_S_C | L1_S_B | L1_S_V6_TEX(0x01);
7554 pte_l2_l_cache_mode = L2_C | L2_B | L2_V6_L_TEX(0x01);
7555 #if defined(ARM11MPCORE_COMPAT_MMU) || defined(ARMV6_EXTENDED_SMALL_PAGE)
7556 pte_l2_s_cache_mode = L2_C | L2_B | L2_V6_XS_TEX(0x01);
7557 #else
7558 /* no TEX. read-allocate */
7559 pte_l2_s_cache_mode = L2_C | L2_B;
7560 #endif
7561 /*
7562 * write-back, write-allocate for page tables.
7563 */
7564 pte_l1_s_cache_mode_pt = L1_S_C | L1_S_B | L1_S_V6_TEX(0x01);
7565 pte_l2_l_cache_mode_pt = L2_C | L2_B | L2_V6_L_TEX(0x01);
7566 #if defined(ARM11MPCORE_COMPAT_MMU) || defined(ARMV6_EXTENDED_SMALL_PAGE)
7567 pte_l2_s_cache_mode_pt = L2_C | L2_B | L2_V6_XS_TEX(0x01);
7568 #else
7569 pte_l2_s_cache_mode_pt = L2_C | L2_B;
7570 #endif
7571
7572 pte_l1_s_prot_u = L1_S_PROT_U_armv6;
7573 pte_l1_s_prot_w = L1_S_PROT_W_armv6;
7574 pte_l1_s_prot_ro = L1_S_PROT_RO_armv6;
7575 pte_l1_s_prot_mask = L1_S_PROT_MASK_armv6;
7576
7577 #if defined(ARM11MPCORE_COMPAT_MMU) || defined(ARMV6_EXTENDED_SMALL_PAGE)
7578 pte_l2_s_prot_u = L2_S_PROT_U_armv6n;
7579 pte_l2_s_prot_w = L2_S_PROT_W_armv6n;
7580 pte_l2_s_prot_ro = L2_S_PROT_RO_armv6n;
7581 pte_l2_s_prot_mask = L2_S_PROT_MASK_armv6n;
7582
7583 #else
7584 /* with AP[0..3] */
7585 pte_l2_s_prot_u = L2_S_PROT_U_generic;
7586 pte_l2_s_prot_w = L2_S_PROT_W_generic;
7587 pte_l2_s_prot_ro = L2_S_PROT_RO_generic;
7588 pte_l2_s_prot_mask = L2_S_PROT_MASK_generic;
7589 #endif
7590
7591 #ifdef ARM11MPCORE_COMPAT_MMU
7592 /* with AP[0..3] */
7593 pte_l2_l_prot_u = L2_L_PROT_U_generic;
7594 pte_l2_l_prot_w = L2_L_PROT_W_generic;
7595 pte_l2_l_prot_ro = L2_L_PROT_RO_generic;
7596 pte_l2_l_prot_mask = L2_L_PROT_MASK_generic;
7597
7598 pte_l1_ss_proto = L1_SS_PROTO_armv6;
7599 pte_l1_s_proto = L1_S_PROTO_armv6;
7600 pte_l1_c_proto = L1_C_PROTO_armv6;
7601 pte_l2_s_proto = L2_S_PROTO_armv6c;
7602 #else
7603 pte_l2_l_prot_u = L2_L_PROT_U_armv6n;
7604 pte_l2_l_prot_w = L2_L_PROT_W_armv6n;
7605 pte_l2_l_prot_ro = L2_L_PROT_RO_armv6n;
7606 pte_l2_l_prot_mask = L2_L_PROT_MASK_armv6n;
7607
7608 pte_l1_ss_proto = L1_SS_PROTO_armv6;
7609 pte_l1_s_proto = L1_S_PROTO_armv6;
7610 pte_l1_c_proto = L1_C_PROTO_armv6;
7611 pte_l2_s_proto = L2_S_PROTO_armv6n;
7612 #endif
7613
7614 pmap_copy_page_func = pmap_copy_page_generic;
7615 pmap_zero_page_func = pmap_zero_page_generic;
7616 pmap_needs_pte_sync = 1;
7617 }
7618 #endif /* CPU_ARM11MPCORE */
7619
7620
7621 #if ARM_MMU_V7 == 1
7622 void
7623 pmap_pte_init_armv7(void)
7624 {
7625 /*
7626 * The ARMv7-A MMU is mostly compatible with generic. If the
7627 * AP field is zero, that now means "no access" rather than
7628 * read-only. The prototypes are a little different because of
7629 * the XN bit.
7630 */
7631 pmap_pte_init_generic();
7632
7633 pmap_needs_pte_sync = 1;
7634
7635 pte_l1_s_cache_mask = L1_S_CACHE_MASK_armv7;
7636 pte_l2_l_cache_mask = L2_L_CACHE_MASK_armv7;
7637 pte_l2_s_cache_mask = L2_S_CACHE_MASK_armv7;
7638
7639 /*
7640 * If the core support coherent walk then updates to translation tables
7641 * do not require a clean to the point of unification to ensure
7642 * visibility by subsequent translation table walks. That means we can
7643 * map everything shareable and cached and the right thing will happen.
7644 */
7645 if (__SHIFTOUT(armreg_mmfr3_read(), __BITS(23,20))) {
7646 pmap_needs_pte_sync = 0;
7647
7648 /*
7649 * write-back, no write-allocate, shareable for normal pages.
7650 */
7651 pte_l1_s_cache_mode |= L1_S_V6_S;
7652 pte_l2_l_cache_mode |= L2_XS_S;
7653 pte_l2_s_cache_mode |= L2_XS_S;
7654 }
7655
7656 /*
7657 * Page tables are just all other memory. We can use write-back since
7658 * pmap_needs_pte_sync is 1 (or the MMU can read out of cache).
7659 */
7660 pte_l1_s_cache_mode_pt = pte_l1_s_cache_mode;
7661 pte_l2_l_cache_mode_pt = pte_l2_l_cache_mode;
7662 pte_l2_s_cache_mode_pt = pte_l2_s_cache_mode;
7663
7664 /*
7665 * Check the Memory Model Features to see if this CPU supports
7666 * the TLBIASID coproc op.
7667 */
7668 if (__SHIFTOUT(armreg_mmfr2_read(), __BITS(16,19)) >= 2) {
7669 arm_has_tlbiasid_p = true;
7670 } else if (__SHIFTOUT(armreg_mmfr2_read(), __BITS(12,15)) >= 2) {
7671 arm_has_tlbiasid_p = true;
7672 }
7673
7674 /*
7675 * Check the MPIDR to see if this CPU supports MP extensions.
7676 */
7677 #ifdef MULTIPROCESSOR
7678 arm_has_mpext_p = (armreg_mpidr_read() & (MPIDR_MP|MPIDR_U)) == MPIDR_MP;
7679 #else
7680 arm_has_mpext_p = false;
7681 #endif
7682
7683 pte_l1_s_prot_u = L1_S_PROT_U_armv7;
7684 pte_l1_s_prot_w = L1_S_PROT_W_armv7;
7685 pte_l1_s_prot_ro = L1_S_PROT_RO_armv7;
7686 pte_l1_s_prot_mask = L1_S_PROT_MASK_armv7;
7687
7688 pte_l2_s_prot_u = L2_S_PROT_U_armv7;
7689 pte_l2_s_prot_w = L2_S_PROT_W_armv7;
7690 pte_l2_s_prot_ro = L2_S_PROT_RO_armv7;
7691 pte_l2_s_prot_mask = L2_S_PROT_MASK_armv7;
7692
7693 pte_l2_l_prot_u = L2_L_PROT_U_armv7;
7694 pte_l2_l_prot_w = L2_L_PROT_W_armv7;
7695 pte_l2_l_prot_ro = L2_L_PROT_RO_armv7;
7696 pte_l2_l_prot_mask = L2_L_PROT_MASK_armv7;
7697
7698 pte_l1_ss_proto = L1_SS_PROTO_armv7;
7699 pte_l1_s_proto = L1_S_PROTO_armv7;
7700 pte_l1_c_proto = L1_C_PROTO_armv7;
7701 pte_l2_s_proto = L2_S_PROTO_armv7;
7702
7703 }
7704 #endif /* ARM_MMU_V7 */
7705
7706 /*
7707 * return the PA of the current L1 table, for use when handling a crash dump
7708 */
7709 uint32_t
7710 pmap_kernel_L1_addr(void)
7711 {
7712 #ifdef ARM_MMU_EXTENDED
7713 return pmap_kernel()->pm_l1_pa;
7714 #else
7715 return pmap_kernel()->pm_l1->l1_physaddr;
7716 #endif
7717 }
7718
7719 #if defined(DDB)
7720 /*
7721 * A couple of ddb-callable functions for dumping pmaps
7722 */
7723 void pmap_dump(pmap_t);
7724
7725 static pt_entry_t ncptes[64];
7726 static void pmap_dump_ncpg(pmap_t);
7727
7728 void
7729 pmap_dump(pmap_t pm)
7730 {
7731 struct l2_dtable *l2;
7732 struct l2_bucket *l2b;
7733 pt_entry_t *ptep, pte;
7734 vaddr_t l2_va, l2b_va, va;
7735 int i, j, k, occ, rows = 0;
7736
7737 if (pm == pmap_kernel())
7738 printf("pmap_kernel (%p): ", pm);
7739 else
7740 printf("user pmap (%p): ", pm);
7741
7742 #ifdef ARM_MMU_EXTENDED
7743 printf("l1 at %p\n", pmap_l1_kva(pm));
7744 #else
7745 printf("domain %d, l1 at %p\n", pmap_domain(pm), pmap_l1_kva(pm));
7746 #endif
7747
7748 l2_va = 0;
7749 for (i = 0; i < L2_SIZE; i++, l2_va += 0x01000000) {
7750 l2 = pm->pm_l2[i];
7751
7752 if (l2 == NULL || l2->l2_occupancy == 0)
7753 continue;
7754
7755 l2b_va = l2_va;
7756 for (j = 0; j < L2_BUCKET_SIZE; j++, l2b_va += 0x00100000) {
7757 l2b = &l2->l2_bucket[j];
7758
7759 if (l2b->l2b_occupancy == 0 || l2b->l2b_kva == NULL)
7760 continue;
7761
7762 ptep = l2b->l2b_kva;
7763
7764 for (k = 0; k < 256 && ptep[k] == 0; k++)
7765 ;
7766
7767 k &= ~63;
7768 occ = l2b->l2b_occupancy;
7769 va = l2b_va + (k * 4096);
7770 for (; k < 256; k++, va += 0x1000) {
7771 char ch = ' ';
7772 if ((k % 64) == 0) {
7773 if ((rows % 8) == 0) {
7774 printf(
7775 " |0000 |8000 |10000 |18000 |20000 |28000 |30000 |38000\n");
7776 }
7777 printf("%08lx: ", va);
7778 }
7779
7780 ncptes[k & 63] = 0;
7781 pte = ptep[k];
7782 if (pte == 0) {
7783 ch = '.';
7784 } else {
7785 occ--;
7786 switch (pte & 0x0c) {
7787 case 0x00:
7788 ch = 'D'; /* No cache No buff */
7789 break;
7790 case 0x04:
7791 ch = 'B'; /* No cache buff */
7792 break;
7793 case 0x08:
7794 if (pte & 0x40)
7795 ch = 'm';
7796 else
7797 ch = 'C'; /* Cache No buff */
7798 break;
7799 case 0x0c:
7800 ch = 'F'; /* Cache Buff */
7801 break;
7802 }
7803
7804 if ((pte & L2_S_PROT_U) == L2_S_PROT_U)
7805 ch += 0x20;
7806
7807 if ((pte & 0xc) == 0)
7808 ncptes[k & 63] = pte;
7809 }
7810
7811 if ((k % 64) == 63) {
7812 rows++;
7813 printf("%c\n", ch);
7814 pmap_dump_ncpg(pm);
7815 if (occ == 0)
7816 break;
7817 } else
7818 printf("%c", ch);
7819 }
7820 }
7821 }
7822 }
7823
7824 static void
7825 pmap_dump_ncpg(pmap_t pm)
7826 {
7827 struct vm_page *pg;
7828 struct vm_page_md *md;
7829 struct pv_entry *pv;
7830 int i;
7831
7832 for (i = 0; i < 63; i++) {
7833 if (ncptes[i] == 0)
7834 continue;
7835
7836 pg = PHYS_TO_VM_PAGE(l2pte_pa(ncptes[i]));
7837 if (pg == NULL)
7838 continue;
7839 md = VM_PAGE_TO_MD(pg);
7840
7841 printf(" pa 0x%08lx: krw %d kro %d urw %d uro %d\n",
7842 VM_PAGE_TO_PHYS(pg),
7843 md->krw_mappings, md->kro_mappings,
7844 md->urw_mappings, md->uro_mappings);
7845
7846 SLIST_FOREACH(pv, &md->pvh_list, pv_link) {
7847 printf(" %c va 0x%08lx, flags 0x%x\n",
7848 (pm == pv->pv_pmap) ? '*' : ' ',
7849 pv->pv_va, pv->pv_flags);
7850 }
7851 }
7852 }
7853 #endif
7854
7855 #ifdef PMAP_STEAL_MEMORY
7856 void
7857 pmap_boot_pageadd(pv_addr_t *newpv)
7858 {
7859 pv_addr_t *pv, *npv;
7860
7861 if ((pv = SLIST_FIRST(&pmap_boot_freeq)) != NULL) {
7862 if (newpv->pv_pa < pv->pv_va) {
7863 KASSERT(newpv->pv_pa + newpv->pv_size <= pv->pv_pa);
7864 if (newpv->pv_pa + newpv->pv_size == pv->pv_pa) {
7865 newpv->pv_size += pv->pv_size;
7866 SLIST_REMOVE_HEAD(&pmap_boot_freeq, pv_list);
7867 }
7868 pv = NULL;
7869 } else {
7870 for (; (npv = SLIST_NEXT(pv, pv_list)) != NULL;
7871 pv = npv) {
7872 KASSERT(pv->pv_pa + pv->pv_size < npv->pv_pa);
7873 KASSERT(pv->pv_pa < newpv->pv_pa);
7874 if (newpv->pv_pa > npv->pv_pa)
7875 continue;
7876 if (pv->pv_pa + pv->pv_size == newpv->pv_pa) {
7877 pv->pv_size += newpv->pv_size;
7878 return;
7879 }
7880 if (newpv->pv_pa + newpv->pv_size < npv->pv_pa)
7881 break;
7882 newpv->pv_size += npv->pv_size;
7883 SLIST_INSERT_AFTER(pv, newpv, pv_list);
7884 SLIST_REMOVE_AFTER(newpv, pv_list);
7885 return;
7886 }
7887 }
7888 }
7889
7890 if (pv) {
7891 SLIST_INSERT_AFTER(pv, newpv, pv_list);
7892 } else {
7893 SLIST_INSERT_HEAD(&pmap_boot_freeq, newpv, pv_list);
7894 }
7895 }
7896
7897 void
7898 pmap_boot_pagealloc(psize_t amount, psize_t mask, psize_t match,
7899 pv_addr_t *rpv)
7900 {
7901 pv_addr_t *pv, **pvp;
7902
7903 KASSERT(amount & PGOFSET);
7904 KASSERT((mask & PGOFSET) == 0);
7905 KASSERT((match & PGOFSET) == 0);
7906 KASSERT(amount != 0);
7907
7908 for (pvp = &SLIST_FIRST(&pmap_boot_freeq);
7909 (pv = *pvp) != NULL;
7910 pvp = &SLIST_NEXT(pv, pv_list)) {
7911 pv_addr_t *newpv;
7912 psize_t off;
7913 /*
7914 * If this entry is too small to satisfy the request...
7915 */
7916 KASSERT(pv->pv_size > 0);
7917 if (pv->pv_size < amount)
7918 continue;
7919
7920 for (off = 0; off <= mask; off += PAGE_SIZE) {
7921 if (((pv->pv_pa + off) & mask) == match
7922 && off + amount <= pv->pv_size)
7923 break;
7924 }
7925 if (off > mask)
7926 continue;
7927
7928 rpv->pv_va = pv->pv_va + off;
7929 rpv->pv_pa = pv->pv_pa + off;
7930 rpv->pv_size = amount;
7931 pv->pv_size -= amount;
7932 if (pv->pv_size == 0) {
7933 KASSERT(off == 0);
7934 KASSERT((vaddr_t) pv == rpv->pv_va);
7935 *pvp = SLIST_NEXT(pv, pv_list);
7936 } else if (off == 0) {
7937 KASSERT((vaddr_t) pv == rpv->pv_va);
7938 newpv = (pv_addr_t *) (rpv->pv_va + amount);
7939 *newpv = *pv;
7940 newpv->pv_pa += amount;
7941 newpv->pv_va += amount;
7942 *pvp = newpv;
7943 } else if (off < pv->pv_size) {
7944 newpv = (pv_addr_t *) (rpv->pv_va + amount);
7945 *newpv = *pv;
7946 newpv->pv_size -= off;
7947 newpv->pv_pa += off + amount;
7948 newpv->pv_va += off + amount;
7949
7950 SLIST_NEXT(pv, pv_list) = newpv;
7951 pv->pv_size = off;
7952 } else {
7953 KASSERT((vaddr_t) pv != rpv->pv_va);
7954 }
7955 memset((void *)rpv->pv_va, 0, amount);
7956 return;
7957 }
7958
7959 if (!uvm_physseg_valid_p(uvm_physseg_get_first()))
7960 panic("pmap_boot_pagealloc: couldn't allocate memory");
7961
7962 for (pvp = &SLIST_FIRST(&pmap_boot_freeq);
7963 (pv = *pvp) != NULL;
7964 pvp = &SLIST_NEXT(pv, pv_list)) {
7965 if (SLIST_NEXT(pv, pv_list) == NULL)
7966 break;
7967 }
7968 KASSERT(mask == 0);
7969
7970 for (uvm_physseg_t ups = uvm_physseg_get_first();
7971 uvm_physseg_valid_p(ups);
7972 ups = uvm_physseg_get_next(ups)) {
7973
7974 paddr_t spn = uvm_physseg_get_start(ups);
7975 paddr_t epn = uvm_physseg_get_end(ups);
7976 if (spn == atop(pv->pv_pa + pv->pv_size)
7977 && pv->pv_va + pv->pv_size <= ptoa(epn)) {
7978 rpv->pv_va = pv->pv_va;
7979 rpv->pv_pa = pv->pv_pa;
7980 rpv->pv_size = amount;
7981 *pvp = NULL;
7982 pmap_map_chunk(kernel_l1pt.pv_va,
7983 ptoa(spn) + (pv->pv_va - pv->pv_pa),
7984 ptoa(spn),
7985 amount - pv->pv_size,
7986 VM_PROT_READ|VM_PROT_WRITE,
7987 PTE_CACHE);
7988
7989 uvm_physseg_unplug(spn, atop(amount - pv->pv_size));
7990 memset((void *)rpv->pv_va, 0, rpv->pv_size);
7991 return;
7992 }
7993 }
7994
7995 panic("pmap_boot_pagealloc: couldn't allocate memory");
7996 }
7997
7998 vaddr_t
7999 pmap_steal_memory(vsize_t size, vaddr_t *vstartp, vaddr_t *vendp)
8000 {
8001 pv_addr_t pv;
8002
8003 pmap_boot_pagealloc(size, 0, 0, &pv);
8004
8005 return pv.pv_va;
8006 }
8007 #endif /* PMAP_STEAL_MEMORY */
8008
8009 SYSCTL_SETUP(sysctl_machdep_pmap_setup, "sysctl machdep.kmpages setup")
8010 {
8011 sysctl_createv(clog, 0, NULL, NULL,
8012 CTLFLAG_PERMANENT,
8013 CTLTYPE_NODE, "machdep", NULL,
8014 NULL, 0, NULL, 0,
8015 CTL_MACHDEP, CTL_EOL);
8016
8017 sysctl_createv(clog, 0, NULL, NULL,
8018 CTLFLAG_PERMANENT,
8019 CTLTYPE_INT, "kmpages",
8020 SYSCTL_DESCR("count of pages allocated to kernel memory allocators"),
8021 NULL, 0, &pmap_kmpages, 0,
8022 CTL_MACHDEP, CTL_CREATE, CTL_EOL);
8023 }
8024
8025 #ifdef PMAP_NEED_ALLOC_POOLPAGE
8026 struct vm_page *
8027 arm_pmap_alloc_poolpage(int flags)
8028 {
8029 /*
8030 * On some systems, only some pages may be "coherent" for dma and we
8031 * want to prefer those for pool pages (think mbufs) but fallback to
8032 * any page if none is available.
8033 */
8034 if (arm_poolpage_vmfreelist != VM_FREELIST_DEFAULT) {
8035 return uvm_pagealloc_strat(NULL, 0, NULL, flags,
8036 UVM_PGA_STRAT_FALLBACK, arm_poolpage_vmfreelist);
8037 }
8038
8039 return uvm_pagealloc(NULL, 0, NULL, flags);
8040 }
8041 #endif
8042
8043 #if defined(ARM_MMU_EXTENDED) && defined(MULTIPROCESSOR)
8044 void
8045 pmap_md_tlb_info_attach(struct pmap_tlb_info *ti, struct cpu_info *ci)
8046 {
8047 /* nothing */
8048 }
8049
8050 int
8051 pic_ipi_shootdown(void *arg)
8052 {
8053 #if PMAP_TLB_NEED_SHOOTDOWN
8054 pmap_tlb_shootdown_process();
8055 #endif
8056 return 1;
8057 }
8058 #endif /* ARM_MMU_EXTENDED && MULTIPROCESSOR */
8059
8060
8061 #ifdef __HAVE_MM_MD_DIRECT_MAPPED_PHYS
8062 vaddr_t
8063 pmap_direct_mapped_phys(paddr_t pa, bool *ok_p, vaddr_t va)
8064 {
8065 bool ok = false;
8066 if (physical_start <= pa && pa < physical_end) {
8067 #ifdef KERNEL_BASE_VOFFSET
8068 const vaddr_t newva = pa + KERNEL_BASE_VOFFSET;
8069 #else
8070 const vaddr_t newva = KERNEL_BASE + pa - physical_start;
8071 #endif
8072 #ifdef ARM_MMU_EXTENDED
8073 if (newva >= KERNEL_BASE && newva < pmap_directlimit) {
8074 #endif
8075 va = newva;
8076 ok = true;
8077 #ifdef ARM_MMU_EXTENDED
8078 }
8079 #endif
8080 }
8081 KASSERT(ok_p);
8082 *ok_p = ok;
8083 return va;
8084 }
8085
8086 vaddr_t
8087 pmap_map_poolpage(paddr_t pa)
8088 {
8089 bool ok __diagused;
8090 vaddr_t va = pmap_direct_mapped_phys(pa, &ok, 0);
8091 KASSERTMSG(ok, "pa %#lx not direct mappable", pa);
8092 #if defined(PMAP_CACHE_VIPT) && !defined(ARM_MMU_EXTENDED)
8093 if (arm_cache_prefer_mask != 0) {
8094 struct vm_page * const pg = PHYS_TO_VM_PAGE(pa);
8095 struct vm_page_md * const md = VM_PAGE_TO_MD(pg);
8096 pmap_acquire_page_lock(md);
8097 pmap_vac_me_harder(md, pa, pmap_kernel(), va);
8098 pmap_release_page_lock(md);
8099 }
8100 #endif
8101 return va;
8102 }
8103
8104 paddr_t
8105 pmap_unmap_poolpage(vaddr_t va)
8106 {
8107 KASSERT(va >= KERNEL_BASE);
8108 #ifdef PMAP_CACHE_VIVT
8109 cpu_idcache_wbinv_range(va, PAGE_SIZE);
8110 #endif
8111 #if defined(KERNEL_BASE_VOFFSET)
8112 return va - KERNEL_BASE_VOFFSET;
8113 #else
8114 return va - KERNEL_BASE + physical_start;
8115 #endif
8116 }
8117 #endif /* __HAVE_MM_MD_DIRECT_MAPPED_PHYS */
8118