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