Wed Nov 3 16:59:19 2010 UTC ()
Fix build of IXM1200 too.  Pointed out by cegger, thanks.


(uebayasi)
diff -r1.216 -r1.217 src/sys/arch/arm/arm32/pmap.c

cvs diff -r1.216 -r1.217 src/sys/arch/arm/arm32/pmap.c (switch to unified diff)

--- src/sys/arch/arm/arm32/pmap.c 2010/11/02 06:33:22 1.216
+++ src/sys/arch/arm/arm32/pmap.c 2010/11/03 16:59:19 1.217
@@ -1,1213 +1,1213 @@ @@ -1,1213 +1,1213 @@
1/* $NetBSD: pmap.c,v 1.216 2010/11/02 06:33:22 uebayasi Exp $ */ 1/* $NetBSD: pmap.c,v 1.217 2010/11/03 16:59:19 uebayasi Exp $ */
2 2
3/* 3/*
4 * Copyright 2003 Wasabi Systems, Inc. 4 * Copyright 2003 Wasabi Systems, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Written by Steve C. Woodford for Wasabi Systems, Inc. 7 * Written by Steve C. Woodford for Wasabi Systems, Inc.
8 * 8 *
9 * Redistribution and use in source and binary forms, with or without 9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions 10 * modification, are permitted provided that the following conditions
11 * are met: 11 * are met:
12 * 1. Redistributions of source code must retain the above copyright 12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer. 13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright 14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the 15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution. 16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software 17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement: 18 * must display the following acknowledgement:
19 * This product includes software developed for the NetBSD Project by 19 * This product includes software developed for the NetBSD Project by
20 * Wasabi Systems, Inc. 20 * Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse 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 22 * or promote products derived from this software without specific prior
23 * written permission. 23 * written permission.
24 * 24 *
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 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 32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 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 34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE. 35 * POSSIBILITY OF SUCH DAMAGE.
36 */ 36 */
37 37
38/* 38/*
39 * Copyright (c) 2002-2003 Wasabi Systems, Inc. 39 * Copyright (c) 2002-2003 Wasabi Systems, Inc.
40 * Copyright (c) 2001 Richard Earnshaw 40 * Copyright (c) 2001 Richard Earnshaw
41 * Copyright (c) 2001-2002 Christopher Gilbert 41 * Copyright (c) 2001-2002 Christopher Gilbert
42 * All rights reserved. 42 * All rights reserved.
43 * 43 *
44 * 1. Redistributions of source code must retain the above copyright 44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer. 45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright 46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the 47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution. 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 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 50 * endorse or promote products derived from this software without specific
51 * prior written permission. 51 * prior written permission.
52 * 52 *
53 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 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 54 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
55 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 55 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
56 * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 56 * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
57 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 57 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
58 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 58 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
59 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 59 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 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 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 62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * SUCH DAMAGE. 63 * SUCH DAMAGE.
64 */ 64 */
65 65
66/*- 66/*-
67 * Copyright (c) 1999 The NetBSD Foundation, Inc. 67 * Copyright (c) 1999 The NetBSD Foundation, Inc.
68 * All rights reserved. 68 * All rights reserved.
69 * 69 *
70 * This code is derived from software contributed to The NetBSD Foundation 70 * This code is derived from software contributed to The NetBSD Foundation
71 * by Charles M. Hannum. 71 * by Charles M. Hannum.
72 * 72 *
73 * Redistribution and use in source and binary forms, with or without 73 * Redistribution and use in source and binary forms, with or without
74 * modification, are permitted provided that the following conditions 74 * modification, are permitted provided that the following conditions
75 * are met: 75 * are met:
76 * 1. Redistributions of source code must retain the above copyright 76 * 1. Redistributions of source code must retain the above copyright
77 * notice, this list of conditions and the following disclaimer. 77 * notice, this list of conditions and the following disclaimer.
78 * 2. Redistributions in binary form must reproduce the above copyright 78 * 2. Redistributions in binary form must reproduce the above copyright
79 * notice, this list of conditions and the following disclaimer in the 79 * notice, this list of conditions and the following disclaimer in the
80 * documentation and/or other materials provided with the distribution. 80 * documentation and/or other materials provided with the distribution.
81 * 81 *
82 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 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 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 84 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
85 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 85 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
86 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 86 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
87 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 87 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
88 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 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 89 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
90 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 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 91 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
92 * POSSIBILITY OF SUCH DAMAGE. 92 * POSSIBILITY OF SUCH DAMAGE.
93 */ 93 */
94 94
95/* 95/*
96 * Copyright (c) 1994-1998 Mark Brinicombe. 96 * Copyright (c) 1994-1998 Mark Brinicombe.
97 * Copyright (c) 1994 Brini. 97 * Copyright (c) 1994 Brini.
98 * All rights reserved. 98 * All rights reserved.
99 * 99 *
100 * This code is derived from software written for Brini by Mark Brinicombe 100 * This code is derived from software written for Brini by Mark Brinicombe
101 * 101 *
102 * Redistribution and use in source and binary forms, with or without 102 * Redistribution and use in source and binary forms, with or without
103 * modification, are permitted provided that the following conditions 103 * modification, are permitted provided that the following conditions
104 * are met: 104 * are met:
105 * 1. Redistributions of source code must retain the above copyright 105 * 1. Redistributions of source code must retain the above copyright
106 * notice, this list of conditions and the following disclaimer. 106 * notice, this list of conditions and the following disclaimer.
107 * 2. Redistributions in binary form must reproduce the above copyright 107 * 2. Redistributions in binary form must reproduce the above copyright
108 * notice, this list of conditions and the following disclaimer in the 108 * notice, this list of conditions and the following disclaimer in the
109 * documentation and/or other materials provided with the distribution. 109 * documentation and/or other materials provided with the distribution.
110 * 3. All advertising materials mentioning features or use of this software 110 * 3. All advertising materials mentioning features or use of this software
111 * must display the following acknowledgement: 111 * must display the following acknowledgement:
112 * This product includes software developed by Mark Brinicombe. 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 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. 114 * derived from this software without specific prior written permission.
115 * 115 *
116 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 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 117 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
118 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 118 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
119 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 119 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
120 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 120 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
121 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 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 122 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
123 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 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 124 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
125 * 125 *
126 * RiscBSD kernel project 126 * RiscBSD kernel project
127 * 127 *
128 * pmap.c 128 * pmap.c
129 * 129 *
130 * Machine dependant vm stuff 130 * Machine dependant vm stuff
131 * 131 *
132 * Created : 20/09/94 132 * Created : 20/09/94
133 */ 133 */
134 134
135/* 135/*
136 * armv6 and VIPT cache support by 3am Software Foundry, 136 * armv6 and VIPT cache support by 3am Software Foundry,
137 * Copyright (c) 2007 Microsoft 137 * Copyright (c) 2007 Microsoft
138 */ 138 */
139 139
140/* 140/*
141 * Performance improvements, UVM changes, overhauls and part-rewrites 141 * Performance improvements, UVM changes, overhauls and part-rewrites
142 * were contributed by Neil A. Carson <neil@causality.com>. 142 * were contributed by Neil A. Carson <neil@causality.com>.
143 */ 143 */
144 144
145/* 145/*
146 * Overhauled again to speedup the pmap, use MMU Domains so that L1 tables 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 147 * can be shared, and re-work the KVM layout, by Steve Woodford of Wasabi
148 * Systems, Inc. 148 * Systems, Inc.
149 * 149 *
150 * There are still a few things outstanding at this time: 150 * There are still a few things outstanding at this time:
151 * 151 *
152 * - There are some unresolved issues for MP systems: 152 * - There are some unresolved issues for MP systems:
153 * 153 *
154 * o The L1 metadata needs a lock, or more specifically, some places 154 * o The L1 metadata needs a lock, or more specifically, some places
155 * need to acquire an exclusive lock when modifying L1 translation 155 * need to acquire an exclusive lock when modifying L1 translation
156 * table entries. 156 * table entries.
157 * 157 *
158 * o When one cpu modifies an L1 entry, and that L1 table is also 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 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 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 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 162 * the active domain on that cpu). I guess there are lots more tlb
163 * shootdown issues too... 163 * shootdown issues too...
164 * 164 *
165 * o If the vector_page is at 0x00000000 instead of 0xffff0000, then 165 * o If the vector_page is at 0x00000000 instead of 0xffff0000, then
166 * MP systems will lose big-time because of the MMU domain hack. 166 * 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 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 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 169 * space for kernel use only. This would require re-linking all
170 * applications so that the text section starts above this 1MB 170 * applications so that the text section starts above this 1MB
171 * boundary. 171 * boundary.
172 * 172 *
173 * o Tracking which VM space is resident in the cache/tlb has not yet 173 * o Tracking which VM space is resident in the cache/tlb has not yet
174 * been implemented for MP systems. 174 * been implemented for MP systems.
175 * 175 *
176 * o Finally, there is a pathological condition where two cpus running 176 * o Finally, there is a pathological condition where two cpus running
177 * two separate processes (not lwps) which happen to share an L1 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 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. 179 * in a significant slow-down if both processes are in tight loops.
180 */ 180 */
181 181
182/* 182/*
183 * Special compilation symbols 183 * Special compilation symbols
184 * PMAP_DEBUG - Build in pmap_debug_level code 184 * PMAP_DEBUG - Build in pmap_debug_level code
185 */ 185 */
186 186
187/* Include header files */ 187/* Include header files */
188 188
189#include "opt_cpuoptions.h" 189#include "opt_cpuoptions.h"
190#include "opt_pmap_debug.h" 190#include "opt_pmap_debug.h"
191#include "opt_ddb.h" 191#include "opt_ddb.h"
192#include "opt_lockdebug.h" 192#include "opt_lockdebug.h"
193#include "opt_multiprocessor.h" 193#include "opt_multiprocessor.h"
194 194
195#include <sys/param.h> 195#include <sys/param.h>
196#include <sys/types.h> 196#include <sys/types.h>
197#include <sys/kernel.h> 197#include <sys/kernel.h>
198#include <sys/systm.h> 198#include <sys/systm.h>
199#include <sys/proc.h> 199#include <sys/proc.h>
200#include <sys/malloc.h> 200#include <sys/malloc.h>
201#include <sys/pool.h> 201#include <sys/pool.h>
202#include <sys/cdefs.h> 202#include <sys/cdefs.h>
203#include <sys/cpu.h> 203#include <sys/cpu.h>
204#include <sys/sysctl.h> 204#include <sys/sysctl.h>
205  205
206#include <uvm/uvm.h> 206#include <uvm/uvm.h>
207 207
208#include <machine/bus.h> 208#include <machine/bus.h>
209#include <machine/pmap.h> 209#include <machine/pmap.h>
210#include <machine/pcb.h> 210#include <machine/pcb.h>
211#include <machine/param.h> 211#include <machine/param.h>
212#include <arm/arm32/katelib.h> 212#include <arm/arm32/katelib.h>
213 213
214__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.216 2010/11/02 06:33:22 uebayasi Exp $"); 214__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.217 2010/11/03 16:59:19 uebayasi Exp $");
215 215
216#define VM_PAGE_TO_MD(pg) (&(pg)->mdpage) 216#define VM_PAGE_TO_MD(pg) (&(pg)->mdpage)
217 217
218#ifdef PMAP_DEBUG 218#ifdef PMAP_DEBUG
219 219
220/* XXX need to get rid of all refs to this */ 220/* XXX need to get rid of all refs to this */
221int pmap_debug_level = 0; 221int pmap_debug_level = 0;
222 222
223/* 223/*
224 * for switching to potentially finer grained debugging 224 * for switching to potentially finer grained debugging
225 */ 225 */
226#define PDB_FOLLOW 0x0001 226#define PDB_FOLLOW 0x0001
227#define PDB_INIT 0x0002 227#define PDB_INIT 0x0002
228#define PDB_ENTER 0x0004 228#define PDB_ENTER 0x0004
229#define PDB_REMOVE 0x0008 229#define PDB_REMOVE 0x0008
230#define PDB_CREATE 0x0010 230#define PDB_CREATE 0x0010
231#define PDB_PTPAGE 0x0020 231#define PDB_PTPAGE 0x0020
232#define PDB_GROWKERN 0x0040 232#define PDB_GROWKERN 0x0040
233#define PDB_BITS 0x0080 233#define PDB_BITS 0x0080
234#define PDB_COLLECT 0x0100 234#define PDB_COLLECT 0x0100
235#define PDB_PROTECT 0x0200 235#define PDB_PROTECT 0x0200
236#define PDB_MAP_L1 0x0400 236#define PDB_MAP_L1 0x0400
237#define PDB_BOOTSTRAP 0x1000 237#define PDB_BOOTSTRAP 0x1000
238#define PDB_PARANOIA 0x2000 238#define PDB_PARANOIA 0x2000
239#define PDB_WIRING 0x4000 239#define PDB_WIRING 0x4000
240#define PDB_PVDUMP 0x8000 240#define PDB_PVDUMP 0x8000
241#define PDB_VAC 0x10000 241#define PDB_VAC 0x10000
242#define PDB_KENTER 0x20000 242#define PDB_KENTER 0x20000
243#define PDB_KREMOVE 0x40000 243#define PDB_KREMOVE 0x40000
244#define PDB_EXEC 0x80000 244#define PDB_EXEC 0x80000
245 245
246int debugmap = 1; 246int debugmap = 1;
247int pmapdebug = 0;  247int pmapdebug = 0;
248#define NPDEBUG(_lev_,_stat_) \ 248#define NPDEBUG(_lev_,_stat_) \
249 if (pmapdebug & (_lev_)) \ 249 if (pmapdebug & (_lev_)) \
250 ((_stat_)) 250 ((_stat_))
251  251
252#else /* PMAP_DEBUG */ 252#else /* PMAP_DEBUG */
253#define NPDEBUG(_lev_,_stat_) /* Nothing */ 253#define NPDEBUG(_lev_,_stat_) /* Nothing */
254#endif /* PMAP_DEBUG */ 254#endif /* PMAP_DEBUG */
255 255
256/* 256/*
257 * pmap_kernel() points here 257 * pmap_kernel() points here
258 */ 258 */
259static struct pmap kernel_pmap_store; 259static struct pmap kernel_pmap_store;
260struct pmap *const kernel_pmap_ptr = &kernel_pmap_store; 260struct pmap *const kernel_pmap_ptr = &kernel_pmap_store;
261 261
262/* 262/*
263 * Which pmap is currently 'live' in the cache 263 * Which pmap is currently 'live' in the cache
264 * 264 *
265 * XXXSCW: Fix for SMP ... 265 * XXXSCW: Fix for SMP ...
266 */ 266 */
267static pmap_t pmap_recent_user; 267static pmap_t pmap_recent_user;
268 268
269/* 269/*
270 * Pointer to last active lwp, or NULL if it exited. 270 * Pointer to last active lwp, or NULL if it exited.
271 */ 271 */
272struct lwp *pmap_previous_active_lwp; 272struct lwp *pmap_previous_active_lwp;
273 273
274/* 274/*
275 * Pool and cache that pmap structures are allocated from. 275 * Pool and cache that pmap structures are allocated from.
276 * We use a cache to avoid clearing the pm_l2[] array (1KB) 276 * We use a cache to avoid clearing the pm_l2[] array (1KB)
277 * in pmap_create(). 277 * in pmap_create().
278 */ 278 */
279static struct pool_cache pmap_cache; 279static struct pool_cache pmap_cache;
280static LIST_HEAD(, pmap) pmap_pmaps; 280static LIST_HEAD(, pmap) pmap_pmaps;
281 281
282/* 282/*
283 * Pool of PV structures 283 * Pool of PV structures
284 */ 284 */
285static struct pool pmap_pv_pool; 285static struct pool pmap_pv_pool;
286static void *pmap_bootstrap_pv_page_alloc(struct pool *, int); 286static void *pmap_bootstrap_pv_page_alloc(struct pool *, int);
287static void pmap_bootstrap_pv_page_free(struct pool *, void *); 287static void pmap_bootstrap_pv_page_free(struct pool *, void *);
288static struct pool_allocator pmap_bootstrap_pv_allocator = { 288static struct pool_allocator pmap_bootstrap_pv_allocator = {
289 pmap_bootstrap_pv_page_alloc, pmap_bootstrap_pv_page_free 289 pmap_bootstrap_pv_page_alloc, pmap_bootstrap_pv_page_free
290}; 290};
291 291
292/* 292/*
293 * Pool and cache of l2_dtable structures. 293 * Pool and cache of l2_dtable structures.
294 * We use a cache to avoid clearing the structures when they're 294 * We use a cache to avoid clearing the structures when they're
295 * allocated. (196 bytes) 295 * allocated. (196 bytes)
296 */ 296 */
297static struct pool_cache pmap_l2dtable_cache; 297static struct pool_cache pmap_l2dtable_cache;
298static vaddr_t pmap_kernel_l2dtable_kva; 298static vaddr_t pmap_kernel_l2dtable_kva;
299 299
300/* 300/*
301 * Pool and cache of L2 page descriptors. 301 * Pool and cache of L2 page descriptors.
302 * We use a cache to avoid clearing the descriptor table 302 * We use a cache to avoid clearing the descriptor table
303 * when they're allocated. (1KB) 303 * when they're allocated. (1KB)
304 */ 304 */
305static struct pool_cache pmap_l2ptp_cache; 305static struct pool_cache pmap_l2ptp_cache;
306static vaddr_t pmap_kernel_l2ptp_kva; 306static vaddr_t pmap_kernel_l2ptp_kva;
307static paddr_t pmap_kernel_l2ptp_phys; 307static paddr_t pmap_kernel_l2ptp_phys;
308 308
309#ifdef PMAPCOUNTERS 309#ifdef PMAPCOUNTERS
310#define PMAP_EVCNT_INITIALIZER(name) \ 310#define PMAP_EVCNT_INITIALIZER(name) \
311 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap", name) 311 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap", name)
312 312
313#ifdef PMAP_CACHE_VIPT 313#ifdef PMAP_CACHE_VIPT
314static struct evcnt pmap_ev_vac_clean_one = 314static struct evcnt pmap_ev_vac_clean_one =
315 PMAP_EVCNT_INITIALIZER("clean page (1 color)"); 315 PMAP_EVCNT_INITIALIZER("clean page (1 color)");
316static struct evcnt pmap_ev_vac_flush_one = 316static struct evcnt pmap_ev_vac_flush_one =
317 PMAP_EVCNT_INITIALIZER("flush page (1 color)"); 317 PMAP_EVCNT_INITIALIZER("flush page (1 color)");
318static struct evcnt pmap_ev_vac_flush_lots = 318static struct evcnt pmap_ev_vac_flush_lots =
319 PMAP_EVCNT_INITIALIZER("flush page (2+ colors)"); 319 PMAP_EVCNT_INITIALIZER("flush page (2+ colors)");
320static struct evcnt pmap_ev_vac_flush_lots2 = 320static struct evcnt pmap_ev_vac_flush_lots2 =
321 PMAP_EVCNT_INITIALIZER("flush page (2+ colors, kmpage)"); 321 PMAP_EVCNT_INITIALIZER("flush page (2+ colors, kmpage)");
322EVCNT_ATTACH_STATIC(pmap_ev_vac_clean_one); 322EVCNT_ATTACH_STATIC(pmap_ev_vac_clean_one);
323EVCNT_ATTACH_STATIC(pmap_ev_vac_flush_one); 323EVCNT_ATTACH_STATIC(pmap_ev_vac_flush_one);
324EVCNT_ATTACH_STATIC(pmap_ev_vac_flush_lots); 324EVCNT_ATTACH_STATIC(pmap_ev_vac_flush_lots);
325EVCNT_ATTACH_STATIC(pmap_ev_vac_flush_lots2); 325EVCNT_ATTACH_STATIC(pmap_ev_vac_flush_lots2);
326 326
327static struct evcnt pmap_ev_vac_color_new = 327static struct evcnt pmap_ev_vac_color_new =
328 PMAP_EVCNT_INITIALIZER("new page color"); 328 PMAP_EVCNT_INITIALIZER("new page color");
329static struct evcnt pmap_ev_vac_color_reuse = 329static struct evcnt pmap_ev_vac_color_reuse =
330 PMAP_EVCNT_INITIALIZER("ok first page color"); 330 PMAP_EVCNT_INITIALIZER("ok first page color");
331static struct evcnt pmap_ev_vac_color_ok = 331static struct evcnt pmap_ev_vac_color_ok =
332 PMAP_EVCNT_INITIALIZER("ok page color"); 332 PMAP_EVCNT_INITIALIZER("ok page color");
333static struct evcnt pmap_ev_vac_color_blind = 333static struct evcnt pmap_ev_vac_color_blind =
334 PMAP_EVCNT_INITIALIZER("blind page color"); 334 PMAP_EVCNT_INITIALIZER("blind page color");
335static struct evcnt pmap_ev_vac_color_change = 335static struct evcnt pmap_ev_vac_color_change =
336 PMAP_EVCNT_INITIALIZER("change page color"); 336 PMAP_EVCNT_INITIALIZER("change page color");
337static struct evcnt pmap_ev_vac_color_erase = 337static struct evcnt pmap_ev_vac_color_erase =
338 PMAP_EVCNT_INITIALIZER("erase page color"); 338 PMAP_EVCNT_INITIALIZER("erase page color");
339static struct evcnt pmap_ev_vac_color_none = 339static struct evcnt pmap_ev_vac_color_none =
340 PMAP_EVCNT_INITIALIZER("no page color"); 340 PMAP_EVCNT_INITIALIZER("no page color");
341static struct evcnt pmap_ev_vac_color_restore = 341static struct evcnt pmap_ev_vac_color_restore =
342 PMAP_EVCNT_INITIALIZER("restore page color"); 342 PMAP_EVCNT_INITIALIZER("restore page color");
343 343
344EVCNT_ATTACH_STATIC(pmap_ev_vac_color_new); 344EVCNT_ATTACH_STATIC(pmap_ev_vac_color_new);
345EVCNT_ATTACH_STATIC(pmap_ev_vac_color_reuse); 345EVCNT_ATTACH_STATIC(pmap_ev_vac_color_reuse);
346EVCNT_ATTACH_STATIC(pmap_ev_vac_color_ok); 346EVCNT_ATTACH_STATIC(pmap_ev_vac_color_ok);
347EVCNT_ATTACH_STATIC(pmap_ev_vac_color_blind); 347EVCNT_ATTACH_STATIC(pmap_ev_vac_color_blind);
348EVCNT_ATTACH_STATIC(pmap_ev_vac_color_change); 348EVCNT_ATTACH_STATIC(pmap_ev_vac_color_change);
349EVCNT_ATTACH_STATIC(pmap_ev_vac_color_erase); 349EVCNT_ATTACH_STATIC(pmap_ev_vac_color_erase);
350EVCNT_ATTACH_STATIC(pmap_ev_vac_color_none); 350EVCNT_ATTACH_STATIC(pmap_ev_vac_color_none);
351EVCNT_ATTACH_STATIC(pmap_ev_vac_color_restore); 351EVCNT_ATTACH_STATIC(pmap_ev_vac_color_restore);
352#endif 352#endif
353 353
354static struct evcnt pmap_ev_mappings = 354static struct evcnt pmap_ev_mappings =
355 PMAP_EVCNT_INITIALIZER("pages mapped"); 355 PMAP_EVCNT_INITIALIZER("pages mapped");
356static struct evcnt pmap_ev_unmappings = 356static struct evcnt pmap_ev_unmappings =
357 PMAP_EVCNT_INITIALIZER("pages unmapped"); 357 PMAP_EVCNT_INITIALIZER("pages unmapped");
358static struct evcnt pmap_ev_remappings = 358static struct evcnt pmap_ev_remappings =
359 PMAP_EVCNT_INITIALIZER("pages remapped"); 359 PMAP_EVCNT_INITIALIZER("pages remapped");
360 360
361EVCNT_ATTACH_STATIC(pmap_ev_mappings); 361EVCNT_ATTACH_STATIC(pmap_ev_mappings);
362EVCNT_ATTACH_STATIC(pmap_ev_unmappings); 362EVCNT_ATTACH_STATIC(pmap_ev_unmappings);
363EVCNT_ATTACH_STATIC(pmap_ev_remappings); 363EVCNT_ATTACH_STATIC(pmap_ev_remappings);
364 364
365static struct evcnt pmap_ev_kernel_mappings = 365static struct evcnt pmap_ev_kernel_mappings =
366 PMAP_EVCNT_INITIALIZER("kernel pages mapped"); 366 PMAP_EVCNT_INITIALIZER("kernel pages mapped");
367static struct evcnt pmap_ev_kernel_unmappings = 367static struct evcnt pmap_ev_kernel_unmappings =
368 PMAP_EVCNT_INITIALIZER("kernel pages unmapped"); 368 PMAP_EVCNT_INITIALIZER("kernel pages unmapped");
369static struct evcnt pmap_ev_kernel_remappings = 369static struct evcnt pmap_ev_kernel_remappings =
370 PMAP_EVCNT_INITIALIZER("kernel pages remapped"); 370 PMAP_EVCNT_INITIALIZER("kernel pages remapped");
371 371
372EVCNT_ATTACH_STATIC(pmap_ev_kernel_mappings); 372EVCNT_ATTACH_STATIC(pmap_ev_kernel_mappings);
373EVCNT_ATTACH_STATIC(pmap_ev_kernel_unmappings); 373EVCNT_ATTACH_STATIC(pmap_ev_kernel_unmappings);
374EVCNT_ATTACH_STATIC(pmap_ev_kernel_remappings); 374EVCNT_ATTACH_STATIC(pmap_ev_kernel_remappings);
375 375
376static struct evcnt pmap_ev_kenter_mappings = 376static struct evcnt pmap_ev_kenter_mappings =
377 PMAP_EVCNT_INITIALIZER("kenter pages mapped"); 377 PMAP_EVCNT_INITIALIZER("kenter pages mapped");
378static struct evcnt pmap_ev_kenter_unmappings = 378static struct evcnt pmap_ev_kenter_unmappings =
379 PMAP_EVCNT_INITIALIZER("kenter pages unmapped"); 379 PMAP_EVCNT_INITIALIZER("kenter pages unmapped");
380static struct evcnt pmap_ev_kenter_remappings = 380static struct evcnt pmap_ev_kenter_remappings =
381 PMAP_EVCNT_INITIALIZER("kenter pages remapped"); 381 PMAP_EVCNT_INITIALIZER("kenter pages remapped");
382static struct evcnt pmap_ev_pt_mappings = 382static struct evcnt pmap_ev_pt_mappings =
383 PMAP_EVCNT_INITIALIZER("page table pages mapped"); 383 PMAP_EVCNT_INITIALIZER("page table pages mapped");
384 384
385EVCNT_ATTACH_STATIC(pmap_ev_kenter_mappings); 385EVCNT_ATTACH_STATIC(pmap_ev_kenter_mappings);
386EVCNT_ATTACH_STATIC(pmap_ev_kenter_unmappings); 386EVCNT_ATTACH_STATIC(pmap_ev_kenter_unmappings);
387EVCNT_ATTACH_STATIC(pmap_ev_kenter_remappings); 387EVCNT_ATTACH_STATIC(pmap_ev_kenter_remappings);
388EVCNT_ATTACH_STATIC(pmap_ev_pt_mappings); 388EVCNT_ATTACH_STATIC(pmap_ev_pt_mappings);
389 389
390#ifdef PMAP_CACHE_VIPT 390#ifdef PMAP_CACHE_VIPT
391static struct evcnt pmap_ev_exec_mappings = 391static struct evcnt pmap_ev_exec_mappings =
392 PMAP_EVCNT_INITIALIZER("exec pages mapped"); 392 PMAP_EVCNT_INITIALIZER("exec pages mapped");
393static struct evcnt pmap_ev_exec_cached = 393static struct evcnt pmap_ev_exec_cached =
394 PMAP_EVCNT_INITIALIZER("exec pages cached"); 394 PMAP_EVCNT_INITIALIZER("exec pages cached");
395 395
396EVCNT_ATTACH_STATIC(pmap_ev_exec_mappings); 396EVCNT_ATTACH_STATIC(pmap_ev_exec_mappings);
397EVCNT_ATTACH_STATIC(pmap_ev_exec_cached); 397EVCNT_ATTACH_STATIC(pmap_ev_exec_cached);
398 398
399static struct evcnt pmap_ev_exec_synced = 399static struct evcnt pmap_ev_exec_synced =
400 PMAP_EVCNT_INITIALIZER("exec pages synced"); 400 PMAP_EVCNT_INITIALIZER("exec pages synced");
401static struct evcnt pmap_ev_exec_synced_map = 401static struct evcnt pmap_ev_exec_synced_map =
402 PMAP_EVCNT_INITIALIZER("exec pages synced (MP)"); 402 PMAP_EVCNT_INITIALIZER("exec pages synced (MP)");
403static struct evcnt pmap_ev_exec_synced_unmap = 403static struct evcnt pmap_ev_exec_synced_unmap =
404 PMAP_EVCNT_INITIALIZER("exec pages synced (UM)"); 404 PMAP_EVCNT_INITIALIZER("exec pages synced (UM)");
405static struct evcnt pmap_ev_exec_synced_remap = 405static struct evcnt pmap_ev_exec_synced_remap =
406 PMAP_EVCNT_INITIALIZER("exec pages synced (RM)"); 406 PMAP_EVCNT_INITIALIZER("exec pages synced (RM)");
407static struct evcnt pmap_ev_exec_synced_clearbit = 407static struct evcnt pmap_ev_exec_synced_clearbit =
408 PMAP_EVCNT_INITIALIZER("exec pages synced (DG)"); 408 PMAP_EVCNT_INITIALIZER("exec pages synced (DG)");
409static struct evcnt pmap_ev_exec_synced_kremove = 409static struct evcnt pmap_ev_exec_synced_kremove =
410 PMAP_EVCNT_INITIALIZER("exec pages synced (KU)"); 410 PMAP_EVCNT_INITIALIZER("exec pages synced (KU)");
411 411
412EVCNT_ATTACH_STATIC(pmap_ev_exec_synced); 412EVCNT_ATTACH_STATIC(pmap_ev_exec_synced);
413EVCNT_ATTACH_STATIC(pmap_ev_exec_synced_map); 413EVCNT_ATTACH_STATIC(pmap_ev_exec_synced_map);
414EVCNT_ATTACH_STATIC(pmap_ev_exec_synced_unmap); 414EVCNT_ATTACH_STATIC(pmap_ev_exec_synced_unmap);
415EVCNT_ATTACH_STATIC(pmap_ev_exec_synced_remap); 415EVCNT_ATTACH_STATIC(pmap_ev_exec_synced_remap);
416EVCNT_ATTACH_STATIC(pmap_ev_exec_synced_clearbit); 416EVCNT_ATTACH_STATIC(pmap_ev_exec_synced_clearbit);
417EVCNT_ATTACH_STATIC(pmap_ev_exec_synced_kremove); 417EVCNT_ATTACH_STATIC(pmap_ev_exec_synced_kremove);
418 418
419static struct evcnt pmap_ev_exec_discarded_unmap = 419static struct evcnt pmap_ev_exec_discarded_unmap =
420 PMAP_EVCNT_INITIALIZER("exec pages discarded (UM)"); 420 PMAP_EVCNT_INITIALIZER("exec pages discarded (UM)");
421static struct evcnt pmap_ev_exec_discarded_zero = 421static struct evcnt pmap_ev_exec_discarded_zero =
422 PMAP_EVCNT_INITIALIZER("exec pages discarded (ZP)"); 422 PMAP_EVCNT_INITIALIZER("exec pages discarded (ZP)");
423static struct evcnt pmap_ev_exec_discarded_copy = 423static struct evcnt pmap_ev_exec_discarded_copy =
424 PMAP_EVCNT_INITIALIZER("exec pages discarded (CP)"); 424 PMAP_EVCNT_INITIALIZER("exec pages discarded (CP)");
425static struct evcnt pmap_ev_exec_discarded_page_protect = 425static struct evcnt pmap_ev_exec_discarded_page_protect =
426 PMAP_EVCNT_INITIALIZER("exec pages discarded (PP)"); 426 PMAP_EVCNT_INITIALIZER("exec pages discarded (PP)");
427static struct evcnt pmap_ev_exec_discarded_clearbit = 427static struct evcnt pmap_ev_exec_discarded_clearbit =
428 PMAP_EVCNT_INITIALIZER("exec pages discarded (DG)"); 428 PMAP_EVCNT_INITIALIZER("exec pages discarded (DG)");
429static struct evcnt pmap_ev_exec_discarded_kremove = 429static struct evcnt pmap_ev_exec_discarded_kremove =
430 PMAP_EVCNT_INITIALIZER("exec pages discarded (KU)"); 430 PMAP_EVCNT_INITIALIZER("exec pages discarded (KU)");
431 431
432EVCNT_ATTACH_STATIC(pmap_ev_exec_discarded_unmap); 432EVCNT_ATTACH_STATIC(pmap_ev_exec_discarded_unmap);
433EVCNT_ATTACH_STATIC(pmap_ev_exec_discarded_zero); 433EVCNT_ATTACH_STATIC(pmap_ev_exec_discarded_zero);
434EVCNT_ATTACH_STATIC(pmap_ev_exec_discarded_copy); 434EVCNT_ATTACH_STATIC(pmap_ev_exec_discarded_copy);
435EVCNT_ATTACH_STATIC(pmap_ev_exec_discarded_page_protect); 435EVCNT_ATTACH_STATIC(pmap_ev_exec_discarded_page_protect);
436EVCNT_ATTACH_STATIC(pmap_ev_exec_discarded_clearbit); 436EVCNT_ATTACH_STATIC(pmap_ev_exec_discarded_clearbit);
437EVCNT_ATTACH_STATIC(pmap_ev_exec_discarded_kremove); 437EVCNT_ATTACH_STATIC(pmap_ev_exec_discarded_kremove);
438#endif /* PMAP_CACHE_VIPT */ 438#endif /* PMAP_CACHE_VIPT */
439 439
440static struct evcnt pmap_ev_updates = PMAP_EVCNT_INITIALIZER("updates"); 440static struct evcnt pmap_ev_updates = PMAP_EVCNT_INITIALIZER("updates");
441static struct evcnt pmap_ev_collects = PMAP_EVCNT_INITIALIZER("collects"); 441static struct evcnt pmap_ev_collects = PMAP_EVCNT_INITIALIZER("collects");
442static struct evcnt pmap_ev_activations = PMAP_EVCNT_INITIALIZER("activations"); 442static struct evcnt pmap_ev_activations = PMAP_EVCNT_INITIALIZER("activations");
443 443
444EVCNT_ATTACH_STATIC(pmap_ev_updates); 444EVCNT_ATTACH_STATIC(pmap_ev_updates);
445EVCNT_ATTACH_STATIC(pmap_ev_collects); 445EVCNT_ATTACH_STATIC(pmap_ev_collects);
446EVCNT_ATTACH_STATIC(pmap_ev_activations); 446EVCNT_ATTACH_STATIC(pmap_ev_activations);
447 447
448#define PMAPCOUNT(x) ((void)(pmap_ev_##x.ev_count++)) 448#define PMAPCOUNT(x) ((void)(pmap_ev_##x.ev_count++))
449#else 449#else
450#define PMAPCOUNT(x) ((void)0) 450#define PMAPCOUNT(x) ((void)0)
451#endif 451#endif
452 452
453/* 453/*
454 * pmap copy/zero page, and mem(5) hook point 454 * pmap copy/zero page, and mem(5) hook point
455 */ 455 */
456static pt_entry_t *csrc_pte, *cdst_pte; 456static pt_entry_t *csrc_pte, *cdst_pte;
457static vaddr_t csrcp, cdstp; 457static vaddr_t csrcp, cdstp;
458vaddr_t memhook; /* used by mem.c */ 458vaddr_t memhook; /* used by mem.c */
459kmutex_t memlock; /* used by mem.c */ 459kmutex_t memlock; /* used by mem.c */
460void *zeropage; /* used by mem.c */ 460void *zeropage; /* used by mem.c */
461extern void *msgbufaddr; 461extern void *msgbufaddr;
462int pmap_kmpages; 462int pmap_kmpages;
463/* 463/*
464 * Flag to indicate if pmap_init() has done its thing 464 * Flag to indicate if pmap_init() has done its thing
465 */ 465 */
466bool pmap_initialized; 466bool pmap_initialized;
467 467
468/* 468/*
469 * Misc. locking data structures 469 * Misc. locking data structures
470 */ 470 */
471 471
472#if 0 /* defined(MULTIPROCESSOR) || defined(LOCKDEBUG) */ 472#if 0 /* defined(MULTIPROCESSOR) || defined(LOCKDEBUG) */
473static struct lock pmap_main_lock; 473static struct lock pmap_main_lock;
474 474
475#define PMAP_MAP_TO_HEAD_LOCK() \ 475#define PMAP_MAP_TO_HEAD_LOCK() \
476 (void) spinlockmgr(&pmap_main_lock, LK_SHARED, NULL) 476 (void) spinlockmgr(&pmap_main_lock, LK_SHARED, NULL)
477#define PMAP_MAP_TO_HEAD_UNLOCK() \ 477#define PMAP_MAP_TO_HEAD_UNLOCK() \
478 (void) spinlockmgr(&pmap_main_lock, LK_RELEASE, NULL) 478 (void) spinlockmgr(&pmap_main_lock, LK_RELEASE, NULL)
479#define PMAP_HEAD_TO_MAP_LOCK() \ 479#define PMAP_HEAD_TO_MAP_LOCK() \
480 (void) spinlockmgr(&pmap_main_lock, LK_EXCLUSIVE, NULL) 480 (void) spinlockmgr(&pmap_main_lock, LK_EXCLUSIVE, NULL)
481#define PMAP_HEAD_TO_MAP_UNLOCK() \ 481#define PMAP_HEAD_TO_MAP_UNLOCK() \
482 spinlockmgr(&pmap_main_lock, LK_RELEASE, (void *) 0) 482 spinlockmgr(&pmap_main_lock, LK_RELEASE, (void *) 0)
483#else 483#else
484#define PMAP_MAP_TO_HEAD_LOCK() /* null */ 484#define PMAP_MAP_TO_HEAD_LOCK() /* null */
485#define PMAP_MAP_TO_HEAD_UNLOCK() /* null */ 485#define PMAP_MAP_TO_HEAD_UNLOCK() /* null */
486#define PMAP_HEAD_TO_MAP_LOCK() /* null */ 486#define PMAP_HEAD_TO_MAP_LOCK() /* null */
487#define PMAP_HEAD_TO_MAP_UNLOCK() /* null */ 487#define PMAP_HEAD_TO_MAP_UNLOCK() /* null */
488#endif 488#endif
489 489
490#define pmap_acquire_pmap_lock(pm) \ 490#define pmap_acquire_pmap_lock(pm) \
491 do { \ 491 do { \
492 if ((pm) != pmap_kernel()) \ 492 if ((pm) != pmap_kernel()) \
493 mutex_enter(&(pm)->pm_lock); \ 493 mutex_enter(&(pm)->pm_lock); \
494 } while (/*CONSTCOND*/0) 494 } while (/*CONSTCOND*/0)
495 495
496#define pmap_release_pmap_lock(pm) \ 496#define pmap_release_pmap_lock(pm) \
497 do { \ 497 do { \
498 if ((pm) != pmap_kernel()) \ 498 if ((pm) != pmap_kernel()) \
499 mutex_exit(&(pm)->pm_lock); \ 499 mutex_exit(&(pm)->pm_lock); \
500 } while (/*CONSTCOND*/0) 500 } while (/*CONSTCOND*/0)
501 501
502 502
503/* 503/*
504 * Metadata for L1 translation tables. 504 * Metadata for L1 translation tables.
505 */ 505 */
506struct l1_ttable { 506struct l1_ttable {
507 /* Entry on the L1 Table list */ 507 /* Entry on the L1 Table list */
508 SLIST_ENTRY(l1_ttable) l1_link; 508 SLIST_ENTRY(l1_ttable) l1_link;
509 509
510 /* Entry on the L1 Least Recently Used list */ 510 /* Entry on the L1 Least Recently Used list */
511 TAILQ_ENTRY(l1_ttable) l1_lru; 511 TAILQ_ENTRY(l1_ttable) l1_lru;
512 512
513 /* Track how many domains are allocated from this L1 */ 513 /* Track how many domains are allocated from this L1 */
514 volatile u_int l1_domain_use_count; 514 volatile u_int l1_domain_use_count;
515 515
516 /* 516 /*
517 * A free-list of domain numbers for this L1. 517 * A free-list of domain numbers for this L1.
518 * We avoid using ffs() and a bitmap to track domains since ffs() 518 * We avoid using ffs() and a bitmap to track domains since ffs()
519 * is slow on ARM. 519 * is slow on ARM.
520 */ 520 */
521 u_int8_t l1_domain_first; 521 u_int8_t l1_domain_first;
522 u_int8_t l1_domain_free[PMAP_DOMAINS]; 522 u_int8_t l1_domain_free[PMAP_DOMAINS];
523 523
524 /* Physical address of this L1 page table */ 524 /* Physical address of this L1 page table */
525 paddr_t l1_physaddr; 525 paddr_t l1_physaddr;
526 526
527 /* KVA of this L1 page table */ 527 /* KVA of this L1 page table */
528 pd_entry_t *l1_kva; 528 pd_entry_t *l1_kva;
529}; 529};
530 530
531/* 531/*
532 * Convert a virtual address into its L1 table index. That is, the 532 * Convert a virtual address into its L1 table index. That is, the
533 * index used to locate the L2 descriptor table pointer in an L1 table. 533 * index used to locate the L2 descriptor table pointer in an L1 table.
534 * This is basically used to index l1->l1_kva[]. 534 * This is basically used to index l1->l1_kva[].
535 * 535 *
536 * Each L2 descriptor table represents 1MB of VA space. 536 * Each L2 descriptor table represents 1MB of VA space.
537 */ 537 */
538#define L1_IDX(va) (((vaddr_t)(va)) >> L1_S_SHIFT) 538#define L1_IDX(va) (((vaddr_t)(va)) >> L1_S_SHIFT)
539 539
540/* 540/*
541 * L1 Page Tables are tracked using a Least Recently Used list. 541 * L1 Page Tables are tracked using a Least Recently Used list.
542 * - New L1s are allocated from the HEAD. 542 * - New L1s are allocated from the HEAD.
543 * - Freed L1s are added to the TAIl. 543 * - Freed L1s are added to the TAIl.
544 * - Recently accessed L1s (where an 'access' is some change to one of 544 * - Recently accessed L1s (where an 'access' is some change to one of
545 * the userland pmaps which owns this L1) are moved to the TAIL. 545 * the userland pmaps which owns this L1) are moved to the TAIL.
546 */ 546 */
547static TAILQ_HEAD(, l1_ttable) l1_lru_list; 547static TAILQ_HEAD(, l1_ttable) l1_lru_list;
548static struct simplelock l1_lru_lock; 548static struct simplelock l1_lru_lock;
549 549
550/* 550/*
551 * A list of all L1 tables 551 * A list of all L1 tables
552 */ 552 */
553static SLIST_HEAD(, l1_ttable) l1_list; 553static SLIST_HEAD(, l1_ttable) l1_list;
554 554
555/* 555/*
556 * The l2_dtable tracks L2_BUCKET_SIZE worth of L1 slots. 556 * The l2_dtable tracks L2_BUCKET_SIZE worth of L1 slots.
557 * 557 *
558 * This is normally 16MB worth L2 page descriptors for any given pmap. 558 * This is normally 16MB worth L2 page descriptors for any given pmap.
559 * Reference counts are maintained for L2 descriptors so they can be 559 * Reference counts are maintained for L2 descriptors so they can be
560 * freed when empty. 560 * freed when empty.
561 */ 561 */
562struct l2_dtable { 562struct l2_dtable {
563 /* The number of L2 page descriptors allocated to this l2_dtable */ 563 /* The number of L2 page descriptors allocated to this l2_dtable */
564 u_int l2_occupancy; 564 u_int l2_occupancy;
565 565
566 /* List of L2 page descriptors */ 566 /* List of L2 page descriptors */
567 struct l2_bucket { 567 struct l2_bucket {
568 pt_entry_t *l2b_kva; /* KVA of L2 Descriptor Table */ 568 pt_entry_t *l2b_kva; /* KVA of L2 Descriptor Table */
569 paddr_t l2b_phys; /* Physical address of same */ 569 paddr_t l2b_phys; /* Physical address of same */
570 u_short l2b_l1idx; /* This L2 table's L1 index */ 570 u_short l2b_l1idx; /* This L2 table's L1 index */
571 u_short l2b_occupancy; /* How many active descriptors */ 571 u_short l2b_occupancy; /* How many active descriptors */
572 } l2_bucket[L2_BUCKET_SIZE]; 572 } l2_bucket[L2_BUCKET_SIZE];
573}; 573};
574 574
575/* 575/*
576 * Given an L1 table index, calculate the corresponding l2_dtable index 576 * Given an L1 table index, calculate the corresponding l2_dtable index
577 * and bucket index within the l2_dtable. 577 * and bucket index within the l2_dtable.
578 */ 578 */
579#define L2_IDX(l1idx) (((l1idx) >> L2_BUCKET_LOG2) & \ 579#define L2_IDX(l1idx) (((l1idx) >> L2_BUCKET_LOG2) & \
580 (L2_SIZE - 1)) 580 (L2_SIZE - 1))
581#define L2_BUCKET(l1idx) ((l1idx) & (L2_BUCKET_SIZE - 1)) 581#define L2_BUCKET(l1idx) ((l1idx) & (L2_BUCKET_SIZE - 1))
582 582
583/* 583/*
584 * Given a virtual address, this macro returns the 584 * Given a virtual address, this macro returns the
585 * virtual address required to drop into the next L2 bucket. 585 * virtual address required to drop into the next L2 bucket.
586 */ 586 */
587#define L2_NEXT_BUCKET(va) (((va) & L1_S_FRAME) + L1_S_SIZE) 587#define L2_NEXT_BUCKET(va) (((va) & L1_S_FRAME) + L1_S_SIZE)
588 588
589/* 589/*
590 * L2 allocation. 590 * L2 allocation.
591 */ 591 */
592#define pmap_alloc_l2_dtable() \ 592#define pmap_alloc_l2_dtable() \
593 pool_cache_get(&pmap_l2dtable_cache, PR_NOWAIT) 593 pool_cache_get(&pmap_l2dtable_cache, PR_NOWAIT)
594#define pmap_free_l2_dtable(l2) \ 594#define pmap_free_l2_dtable(l2) \
595 pool_cache_put(&pmap_l2dtable_cache, (l2)) 595 pool_cache_put(&pmap_l2dtable_cache, (l2))
596#define pmap_alloc_l2_ptp(pap) \ 596#define pmap_alloc_l2_ptp(pap) \
597 ((pt_entry_t *)pool_cache_get_paddr(&pmap_l2ptp_cache,\ 597 ((pt_entry_t *)pool_cache_get_paddr(&pmap_l2ptp_cache,\
598 PR_NOWAIT, (pap))) 598 PR_NOWAIT, (pap)))
599 599
600/* 600/*
601 * We try to map the page tables write-through, if possible. However, not 601 * We try to map the page tables write-through, if possible. However, not
602 * all CPUs have a write-through cache mode, so on those we have to sync 602 * all CPUs have a write-through cache mode, so on those we have to sync
603 * the cache when we frob page tables. 603 * the cache when we frob page tables.
604 * 604 *
605 * We try to evaluate this at compile time, if possible. However, it's 605 * We try to evaluate this at compile time, if possible. However, it's
606 * not always possible to do that, hence this run-time var. 606 * not always possible to do that, hence this run-time var.
607 */ 607 */
608int pmap_needs_pte_sync; 608int pmap_needs_pte_sync;
609 609
610/* 610/*
611 * Real definition of pv_entry. 611 * Real definition of pv_entry.
612 */ 612 */
613struct pv_entry { 613struct pv_entry {
614 SLIST_ENTRY(pv_entry) pv_link; /* next pv_entry */ 614 SLIST_ENTRY(pv_entry) pv_link; /* next pv_entry */
615 pmap_t pv_pmap; /* pmap where mapping lies */ 615 pmap_t pv_pmap; /* pmap where mapping lies */
616 vaddr_t pv_va; /* virtual address for mapping */ 616 vaddr_t pv_va; /* virtual address for mapping */
617 u_int pv_flags; /* flags */ 617 u_int pv_flags; /* flags */
618}; 618};
619 619
620/* 620/*
621 * Macro to determine if a mapping might be resident in the 621 * Macro to determine if a mapping might be resident in the
622 * instruction cache and/or TLB 622 * instruction cache and/or TLB
623 */ 623 */
624#define PV_BEEN_EXECD(f) (((f) & (PVF_REF | PVF_EXEC)) == (PVF_REF | PVF_EXEC)) 624#define PV_BEEN_EXECD(f) (((f) & (PVF_REF | PVF_EXEC)) == (PVF_REF | PVF_EXEC))
625#define PV_IS_EXEC_P(f) (((f) & PVF_EXEC) != 0) 625#define PV_IS_EXEC_P(f) (((f) & PVF_EXEC) != 0)
626 626
627/* 627/*
628 * Macro to determine if a mapping might be resident in the 628 * Macro to determine if a mapping might be resident in the
629 * data cache and/or TLB 629 * data cache and/or TLB
630 */ 630 */
631#define PV_BEEN_REFD(f) (((f) & PVF_REF) != 0) 631#define PV_BEEN_REFD(f) (((f) & PVF_REF) != 0)
632 632
633/* 633/*
634 * Local prototypes 634 * Local prototypes
635 */ 635 */
636static int pmap_set_pt_cache_mode(pd_entry_t *, vaddr_t); 636static int pmap_set_pt_cache_mode(pd_entry_t *, vaddr_t);
637static void pmap_alloc_specials(vaddr_t *, int, vaddr_t *, 637static void pmap_alloc_specials(vaddr_t *, int, vaddr_t *,
638 pt_entry_t **); 638 pt_entry_t **);
639static bool pmap_is_current(pmap_t); 639static bool pmap_is_current(pmap_t);
640static bool pmap_is_cached(pmap_t); 640static bool pmap_is_cached(pmap_t);
641static void pmap_enter_pv(struct vm_page_md *, paddr_t, struct pv_entry *, 641static void pmap_enter_pv(struct vm_page_md *, paddr_t, struct pv_entry *,
642 pmap_t, vaddr_t, u_int); 642 pmap_t, vaddr_t, u_int);
643static struct pv_entry *pmap_find_pv(struct vm_page_md *, pmap_t, vaddr_t); 643static struct pv_entry *pmap_find_pv(struct vm_page_md *, pmap_t, vaddr_t);
644static struct pv_entry *pmap_remove_pv(struct vm_page_md *, paddr_t, pmap_t, vaddr_t); 644static struct pv_entry *pmap_remove_pv(struct vm_page_md *, paddr_t, pmap_t, vaddr_t);
645static u_int pmap_modify_pv(struct vm_page_md *, paddr_t, pmap_t, vaddr_t, 645static u_int pmap_modify_pv(struct vm_page_md *, paddr_t, pmap_t, vaddr_t,
646 u_int, u_int); 646 u_int, u_int);
647 647
648static void pmap_pinit(pmap_t); 648static void pmap_pinit(pmap_t);
649static int pmap_pmap_ctor(void *, void *, int); 649static int pmap_pmap_ctor(void *, void *, int);
650 650
651static void pmap_alloc_l1(pmap_t); 651static void pmap_alloc_l1(pmap_t);
652static void pmap_free_l1(pmap_t); 652static void pmap_free_l1(pmap_t);
653static void pmap_use_l1(pmap_t); 653static void pmap_use_l1(pmap_t);
654 654
655static struct l2_bucket *pmap_get_l2_bucket(pmap_t, vaddr_t); 655static struct l2_bucket *pmap_get_l2_bucket(pmap_t, vaddr_t);
656static struct l2_bucket *pmap_alloc_l2_bucket(pmap_t, vaddr_t); 656static struct l2_bucket *pmap_alloc_l2_bucket(pmap_t, vaddr_t);
657static void pmap_free_l2_bucket(pmap_t, struct l2_bucket *, u_int); 657static void pmap_free_l2_bucket(pmap_t, struct l2_bucket *, u_int);
658static int pmap_l2ptp_ctor(void *, void *, int); 658static int pmap_l2ptp_ctor(void *, void *, int);
659static int pmap_l2dtable_ctor(void *, void *, int); 659static int pmap_l2dtable_ctor(void *, void *, int);
660 660
661static void pmap_vac_me_harder(struct vm_page_md *, paddr_t, pmap_t, vaddr_t); 661static void pmap_vac_me_harder(struct vm_page_md *, paddr_t, pmap_t, vaddr_t);
662#ifdef PMAP_CACHE_VIVT 662#ifdef PMAP_CACHE_VIVT
663static void pmap_vac_me_kpmap(struct vm_page_md *, paddr_t, pmap_t, vaddr_t); 663static void pmap_vac_me_kpmap(struct vm_page_md *, paddr_t, pmap_t, vaddr_t);
664static void pmap_vac_me_user(struct vm_page_md *, paddr_t, pmap_t, vaddr_t); 664static void pmap_vac_me_user(struct vm_page_md *, paddr_t, pmap_t, vaddr_t);
665#endif 665#endif
666 666
667static void pmap_clearbit(struct vm_page_md *, paddr_t, u_int); 667static void pmap_clearbit(struct vm_page_md *, paddr_t, u_int);
668#ifdef PMAP_CACHE_VIVT 668#ifdef PMAP_CACHE_VIVT
669static int pmap_clean_page(struct pv_entry *, bool); 669static int pmap_clean_page(struct pv_entry *, bool);
670#endif 670#endif
671#ifdef PMAP_CACHE_VIPT 671#ifdef PMAP_CACHE_VIPT
672static void pmap_syncicache_page(struct vm_page_md *, paddr_t); 672static void pmap_syncicache_page(struct vm_page_md *, paddr_t);
673enum pmap_flush_op { 673enum pmap_flush_op {
674 PMAP_FLUSH_PRIMARY, 674 PMAP_FLUSH_PRIMARY,
675 PMAP_FLUSH_SECONDARY, 675 PMAP_FLUSH_SECONDARY,
676 PMAP_CLEAN_PRIMARY 676 PMAP_CLEAN_PRIMARY
677}; 677};
678static void pmap_flush_page(struct vm_page_md *, paddr_t, enum pmap_flush_op); 678static void pmap_flush_page(struct vm_page_md *, paddr_t, enum pmap_flush_op);
679#endif 679#endif
680static void pmap_page_remove(struct vm_page_md *, paddr_t); 680static void pmap_page_remove(struct vm_page_md *, paddr_t);
681 681
682static void pmap_init_l1(struct l1_ttable *, pd_entry_t *); 682static void pmap_init_l1(struct l1_ttable *, pd_entry_t *);
683static vaddr_t kernel_pt_lookup(paddr_t); 683static vaddr_t kernel_pt_lookup(paddr_t);
684 684
685 685
686/* 686/*
687 * External function prototypes 687 * External function prototypes
688 */ 688 */
689extern void bzero_page(vaddr_t); 689extern void bzero_page(vaddr_t);
690extern void bcopy_page(vaddr_t, vaddr_t); 690extern void bcopy_page(vaddr_t, vaddr_t);
691 691
692/* 692/*
693 * Misc variables 693 * Misc variables
694 */ 694 */
695vaddr_t virtual_avail; 695vaddr_t virtual_avail;
696vaddr_t virtual_end; 696vaddr_t virtual_end;
697vaddr_t pmap_curmaxkvaddr; 697vaddr_t pmap_curmaxkvaddr;
698 698
699paddr_t avail_start; 699paddr_t avail_start;
700paddr_t avail_end; 700paddr_t avail_end;
701 701
702pv_addrqh_t pmap_boot_freeq = SLIST_HEAD_INITIALIZER(&pmap_boot_freeq); 702pv_addrqh_t pmap_boot_freeq = SLIST_HEAD_INITIALIZER(&pmap_boot_freeq);
703pv_addr_t kernelpages; 703pv_addr_t kernelpages;
704pv_addr_t kernel_l1pt; 704pv_addr_t kernel_l1pt;
705pv_addr_t systempage; 705pv_addr_t systempage;
706 706
707/* Function to set the debug level of the pmap code */ 707/* Function to set the debug level of the pmap code */
708 708
709#ifdef PMAP_DEBUG 709#ifdef PMAP_DEBUG
710void 710void
711pmap_debug(int level) 711pmap_debug(int level)
712{ 712{
713 pmap_debug_level = level; 713 pmap_debug_level = level;
714 printf("pmap_debug: level=%d\n", pmap_debug_level); 714 printf("pmap_debug: level=%d\n", pmap_debug_level);
715} 715}
716#endif /* PMAP_DEBUG */ 716#endif /* PMAP_DEBUG */
717 717
718/* 718/*
719 * A bunch of routines to conditionally flush the caches/TLB depending 719 * A bunch of routines to conditionally flush the caches/TLB depending
720 * on whether the specified pmap actually needs to be flushed at any 720 * on whether the specified pmap actually needs to be flushed at any
721 * given time. 721 * given time.
722 */ 722 */
723static inline void 723static inline void
724pmap_tlb_flushID_SE(pmap_t pm, vaddr_t va) 724pmap_tlb_flushID_SE(pmap_t pm, vaddr_t va)
725{ 725{
726 726
727 if (pm->pm_cstate.cs_tlb_id) 727 if (pm->pm_cstate.cs_tlb_id)
728 cpu_tlb_flushID_SE(va); 728 cpu_tlb_flushID_SE(va);
729} 729}
730 730
731static inline void 731static inline void
732pmap_tlb_flushD_SE(pmap_t pm, vaddr_t va) 732pmap_tlb_flushD_SE(pmap_t pm, vaddr_t va)
733{ 733{
734 734
735 if (pm->pm_cstate.cs_tlb_d) 735 if (pm->pm_cstate.cs_tlb_d)
736 cpu_tlb_flushD_SE(va); 736 cpu_tlb_flushD_SE(va);
737} 737}
738 738
739static inline void 739static inline void
740pmap_tlb_flushID(pmap_t pm) 740pmap_tlb_flushID(pmap_t pm)
741{ 741{
742 742
743 if (pm->pm_cstate.cs_tlb_id) { 743 if (pm->pm_cstate.cs_tlb_id) {
744 cpu_tlb_flushID(); 744 cpu_tlb_flushID();
745 pm->pm_cstate.cs_tlb = 0; 745 pm->pm_cstate.cs_tlb = 0;
746 } 746 }
747} 747}
748 748
749static inline void 749static inline void
750pmap_tlb_flushD(pmap_t pm) 750pmap_tlb_flushD(pmap_t pm)
751{ 751{
752 752
753 if (pm->pm_cstate.cs_tlb_d) { 753 if (pm->pm_cstate.cs_tlb_d) {
754 cpu_tlb_flushD(); 754 cpu_tlb_flushD();
755 pm->pm_cstate.cs_tlb_d = 0; 755 pm->pm_cstate.cs_tlb_d = 0;
756 } 756 }
757} 757}
758 758
759#ifdef PMAP_CACHE_VIVT 759#ifdef PMAP_CACHE_VIVT
760static inline void 760static inline void
761pmap_idcache_wbinv_range(pmap_t pm, vaddr_t va, vsize_t len) 761pmap_idcache_wbinv_range(pmap_t pm, vaddr_t va, vsize_t len)
762{ 762{
763 if (pm->pm_cstate.cs_cache_id) { 763 if (pm->pm_cstate.cs_cache_id) {
764 cpu_idcache_wbinv_range(va, len); 764 cpu_idcache_wbinv_range(va, len);
765 } 765 }
766} 766}
767 767
768static inline void 768static inline void
769pmap_dcache_wb_range(pmap_t pm, vaddr_t va, vsize_t len, 769pmap_dcache_wb_range(pmap_t pm, vaddr_t va, vsize_t len,
770 bool do_inv, bool rd_only) 770 bool do_inv, bool rd_only)
771{ 771{
772 772
773 if (pm->pm_cstate.cs_cache_d) { 773 if (pm->pm_cstate.cs_cache_d) {
774 if (do_inv) { 774 if (do_inv) {
775 if (rd_only) 775 if (rd_only)
776 cpu_dcache_inv_range(va, len); 776 cpu_dcache_inv_range(va, len);
777 else 777 else
778 cpu_dcache_wbinv_range(va, len); 778 cpu_dcache_wbinv_range(va, len);
779 } else 779 } else
780 if (!rd_only) 780 if (!rd_only)
781 cpu_dcache_wb_range(va, len); 781 cpu_dcache_wb_range(va, len);
782 } 782 }
783} 783}
784 784
785static inline void 785static inline void
786pmap_idcache_wbinv_all(pmap_t pm) 786pmap_idcache_wbinv_all(pmap_t pm)
787{ 787{
788 if (pm->pm_cstate.cs_cache_id) { 788 if (pm->pm_cstate.cs_cache_id) {
789 cpu_idcache_wbinv_all(); 789 cpu_idcache_wbinv_all();
790 pm->pm_cstate.cs_cache = 0; 790 pm->pm_cstate.cs_cache = 0;
791 } 791 }
792} 792}
793 793
794static inline void 794static inline void
795pmap_dcache_wbinv_all(pmap_t pm) 795pmap_dcache_wbinv_all(pmap_t pm)
796{ 796{
797 if (pm->pm_cstate.cs_cache_d) { 797 if (pm->pm_cstate.cs_cache_d) {
798 cpu_dcache_wbinv_all(); 798 cpu_dcache_wbinv_all();
799 pm->pm_cstate.cs_cache_d = 0; 799 pm->pm_cstate.cs_cache_d = 0;
800 } 800 }
801} 801}
802#endif /* PMAP_CACHE_VIVT */ 802#endif /* PMAP_CACHE_VIVT */
803 803
804static inline bool 804static inline bool
805pmap_is_current(pmap_t pm) 805pmap_is_current(pmap_t pm)
806{ 806{
807 807
808 if (pm == pmap_kernel() || curproc->p_vmspace->vm_map.pmap == pm) 808 if (pm == pmap_kernel() || curproc->p_vmspace->vm_map.pmap == pm)
809 return true; 809 return true;
810 810
811 return false; 811 return false;
812} 812}
813 813
814static inline bool 814static inline bool
815pmap_is_cached(pmap_t pm) 815pmap_is_cached(pmap_t pm)
816{ 816{
817 817
818 if (pm == pmap_kernel() || pmap_recent_user == NULL || 818 if (pm == pmap_kernel() || pmap_recent_user == NULL ||
819 pmap_recent_user == pm) 819 pmap_recent_user == pm)
820 return (true); 820 return (true);
821 821
822 return false; 822 return false;
823} 823}
824 824
825/* 825/*
826 * PTE_SYNC_CURRENT: 826 * PTE_SYNC_CURRENT:
827 * 827 *
828 * Make sure the pte is written out to RAM. 828 * Make sure the pte is written out to RAM.
829 * We need to do this for one of two cases: 829 * We need to do this for one of two cases:
830 * - We're dealing with the kernel pmap 830 * - We're dealing with the kernel pmap
831 * - There is no pmap active in the cache/tlb. 831 * - There is no pmap active in the cache/tlb.
832 * - The specified pmap is 'active' in the cache/tlb. 832 * - The specified pmap is 'active' in the cache/tlb.
833 */ 833 */
834#ifdef PMAP_INCLUDE_PTE_SYNC 834#ifdef PMAP_INCLUDE_PTE_SYNC
835#define PTE_SYNC_CURRENT(pm, ptep) \ 835#define PTE_SYNC_CURRENT(pm, ptep) \
836do { \ 836do { \
837 if (PMAP_NEEDS_PTE_SYNC && \ 837 if (PMAP_NEEDS_PTE_SYNC && \
838 pmap_is_cached(pm)) \ 838 pmap_is_cached(pm)) \
839 PTE_SYNC(ptep); \ 839 PTE_SYNC(ptep); \
840} while (/*CONSTCOND*/0) 840} while (/*CONSTCOND*/0)
841#else 841#else
842#define PTE_SYNC_CURRENT(pm, ptep) /* nothing */ 842#define PTE_SYNC_CURRENT(pm, ptep) /* nothing */
843#endif 843#endif
844 844
845/* 845/*
846 * main pv_entry manipulation functions: 846 * main pv_entry manipulation functions:
847 * pmap_enter_pv: enter a mapping onto a vm_page list 847 * pmap_enter_pv: enter a mapping onto a vm_page list
848 * pmap_remove_pv: remove a mappiing from a vm_page list 848 * pmap_remove_pv: remove a mappiing from a vm_page list
849 * 849 *
850 * NOTE: pmap_enter_pv expects to lock the pvh itself 850 * NOTE: pmap_enter_pv expects to lock the pvh itself
851 * pmap_remove_pv expects te caller to lock the pvh before calling 851 * pmap_remove_pv expects te caller to lock the pvh before calling
852 */ 852 */
853 853
854/* 854/*
855 * pmap_enter_pv: enter a mapping onto a vm_page lst 855 * pmap_enter_pv: enter a mapping onto a vm_page lst
856 * 856 *
857 * => caller should hold the proper lock on pmap_main_lock 857 * => caller should hold the proper lock on pmap_main_lock
858 * => caller should have pmap locked 858 * => caller should have pmap locked
859 * => we will gain the lock on the vm_page and allocate the new pv_entry 859 * => we will gain the lock on the vm_page and allocate the new pv_entry
860 * => caller should adjust ptp's wire_count before calling 860 * => caller should adjust ptp's wire_count before calling
861 * => caller should not adjust pmap's wire_count 861 * => caller should not adjust pmap's wire_count
862 */ 862 */
863static void 863static void
864pmap_enter_pv(struct vm_page_md *md, paddr_t pa, struct pv_entry *pv, pmap_t pm, 864pmap_enter_pv(struct vm_page_md *md, paddr_t pa, struct pv_entry *pv, pmap_t pm,
865 vaddr_t va, u_int flags) 865 vaddr_t va, u_int flags)
866{ 866{
867 struct pv_entry **pvp; 867 struct pv_entry **pvp;
868 868
869 NPDEBUG(PDB_PVDUMP, 869 NPDEBUG(PDB_PVDUMP,
870 printf("pmap_enter_pv: pm %p, md %p, flags 0x%x\n", pm, md, flags)); 870 printf("pmap_enter_pv: pm %p, md %p, flags 0x%x\n", pm, md, flags));
871 871
872 pv->pv_pmap = pm; 872 pv->pv_pmap = pm;
873 pv->pv_va = va; 873 pv->pv_va = va;
874 pv->pv_flags = flags; 874 pv->pv_flags = flags;
875 875
876 simple_lock(&md->pvh_slock); /* lock vm_page */ 876 simple_lock(&md->pvh_slock); /* lock vm_page */
877 pvp = &SLIST_FIRST(&md->pvh_list); 877 pvp = &SLIST_FIRST(&md->pvh_list);
878#ifdef PMAP_CACHE_VIPT 878#ifdef PMAP_CACHE_VIPT
879 /* 879 /*
880 * Insert unmanaged entries, writeable first, at the head of 880 * Insert unmanaged entries, writeable first, at the head of
881 * the pv list. 881 * the pv list.
882 */ 882 */
883 if (__predict_true((flags & PVF_KENTRY) == 0)) { 883 if (__predict_true((flags & PVF_KENTRY) == 0)) {
884 while (*pvp != NULL && (*pvp)->pv_flags & PVF_KENTRY) 884 while (*pvp != NULL && (*pvp)->pv_flags & PVF_KENTRY)
885 pvp = &SLIST_NEXT(*pvp, pv_link); 885 pvp = &SLIST_NEXT(*pvp, pv_link);
886 } else if ((flags & PVF_WRITE) == 0) { 886 } else if ((flags & PVF_WRITE) == 0) {
887 while (*pvp != NULL && (*pvp)->pv_flags & PVF_WRITE) 887 while (*pvp != NULL && (*pvp)->pv_flags & PVF_WRITE)
888 pvp = &SLIST_NEXT(*pvp, pv_link); 888 pvp = &SLIST_NEXT(*pvp, pv_link);
889 } 889 }
890#endif 890#endif
891 SLIST_NEXT(pv, pv_link) = *pvp; /* add to ... */ 891 SLIST_NEXT(pv, pv_link) = *pvp; /* add to ... */
892 *pvp = pv; /* ... locked list */ 892 *pvp = pv; /* ... locked list */
893 md->pvh_attrs |= flags & (PVF_REF | PVF_MOD); 893 md->pvh_attrs |= flags & (PVF_REF | PVF_MOD);
894#ifdef PMAP_CACHE_VIPT 894#ifdef PMAP_CACHE_VIPT
895 if ((pv->pv_flags & PVF_KWRITE) == PVF_KWRITE) 895 if ((pv->pv_flags & PVF_KWRITE) == PVF_KWRITE)
896 md->pvh_attrs |= PVF_KMOD; 896 md->pvh_attrs |= PVF_KMOD;
897 if ((md->pvh_attrs & (PVF_DMOD|PVF_NC)) != PVF_NC) 897 if ((md->pvh_attrs & (PVF_DMOD|PVF_NC)) != PVF_NC)
898 md->pvh_attrs |= PVF_DIRTY; 898 md->pvh_attrs |= PVF_DIRTY;
899 KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC))); 899 KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC)));
900#endif 900#endif
901 if (pm == pmap_kernel()) { 901 if (pm == pmap_kernel()) {
902 PMAPCOUNT(kernel_mappings); 902 PMAPCOUNT(kernel_mappings);
903 if (flags & PVF_WRITE) 903 if (flags & PVF_WRITE)
904 md->krw_mappings++; 904 md->krw_mappings++;
905 else 905 else
906 md->kro_mappings++; 906 md->kro_mappings++;
907 } else { 907 } else {
908 if (flags & PVF_WRITE) 908 if (flags & PVF_WRITE)
909 md->urw_mappings++; 909 md->urw_mappings++;
910 else 910 else
911 md->uro_mappings++; 911 md->uro_mappings++;
912 } 912 }
913 913
914#ifdef PMAP_CACHE_VIPT 914#ifdef PMAP_CACHE_VIPT
915 /* 915 /*
916 * If this is an exec mapping and its the first exec mapping 916 * If this is an exec mapping and its the first exec mapping
917 * for this page, make sure to sync the I-cache. 917 * for this page, make sure to sync the I-cache.
918 */ 918 */
919 if (PV_IS_EXEC_P(flags)) { 919 if (PV_IS_EXEC_P(flags)) {
920 if (!PV_IS_EXEC_P(md->pvh_attrs)) { 920 if (!PV_IS_EXEC_P(md->pvh_attrs)) {
921 pmap_syncicache_page(md, pa); 921 pmap_syncicache_page(md, pa);
922 PMAPCOUNT(exec_synced_map); 922 PMAPCOUNT(exec_synced_map);
923 } 923 }
924 PMAPCOUNT(exec_mappings); 924 PMAPCOUNT(exec_mappings);
925 } 925 }
926#endif 926#endif
927 927
928 PMAPCOUNT(mappings); 928 PMAPCOUNT(mappings);
929 simple_unlock(&md->pvh_slock); /* unlock, done! */ 929 simple_unlock(&md->pvh_slock); /* unlock, done! */
930 930
931 if (pv->pv_flags & PVF_WIRED) 931 if (pv->pv_flags & PVF_WIRED)
932 ++pm->pm_stats.wired_count; 932 ++pm->pm_stats.wired_count;
933} 933}
934 934
935/* 935/*
936 * 936 *
937 * pmap_find_pv: Find a pv entry 937 * pmap_find_pv: Find a pv entry
938 * 938 *
939 * => caller should hold lock on vm_page 939 * => caller should hold lock on vm_page
940 */ 940 */
941static inline struct pv_entry * 941static inline struct pv_entry *
942pmap_find_pv(struct vm_page_md *md, pmap_t pm, vaddr_t va) 942pmap_find_pv(struct vm_page_md *md, pmap_t pm, vaddr_t va)
943{ 943{
944 struct pv_entry *pv; 944 struct pv_entry *pv;
945 945
946 SLIST_FOREACH(pv, &md->pvh_list, pv_link) { 946 SLIST_FOREACH(pv, &md->pvh_list, pv_link) {
947 if (pm == pv->pv_pmap && va == pv->pv_va) 947 if (pm == pv->pv_pmap && va == pv->pv_va)
948 break; 948 break;
949 } 949 }
950 950
951 return (pv); 951 return (pv);
952} 952}
953 953
954/* 954/*
955 * pmap_remove_pv: try to remove a mapping from a pv_list 955 * pmap_remove_pv: try to remove a mapping from a pv_list
956 * 956 *
957 * => caller should hold proper lock on pmap_main_lock 957 * => caller should hold proper lock on pmap_main_lock
958 * => pmap should be locked 958 * => pmap should be locked
959 * => caller should hold lock on vm_page [so that attrs can be adjusted] 959 * => caller should hold lock on vm_page [so that attrs can be adjusted]
960 * => caller should adjust ptp's wire_count and free PTP if needed 960 * => caller should adjust ptp's wire_count and free PTP if needed
961 * => caller should NOT adjust pmap's wire_count 961 * => caller should NOT adjust pmap's wire_count
962 * => we return the removed pv 962 * => we return the removed pv
963 */ 963 */
964static struct pv_entry * 964static struct pv_entry *
965pmap_remove_pv(struct vm_page_md *md, paddr_t pa, pmap_t pm, vaddr_t va) 965pmap_remove_pv(struct vm_page_md *md, paddr_t pa, pmap_t pm, vaddr_t va)
966{ 966{
967 struct pv_entry *pv, **prevptr; 967 struct pv_entry *pv, **prevptr;
968 968
969 NPDEBUG(PDB_PVDUMP, 969 NPDEBUG(PDB_PVDUMP,
970 printf("pmap_remove_pv: pm %p, md %p, va 0x%08lx\n", pm, md, va)); 970 printf("pmap_remove_pv: pm %p, md %p, va 0x%08lx\n", pm, md, va));
971 971
972 prevptr = &SLIST_FIRST(&md->pvh_list); /* prev pv_entry ptr */ 972 prevptr = &SLIST_FIRST(&md->pvh_list); /* prev pv_entry ptr */
973 pv = *prevptr; 973 pv = *prevptr;
974 974
975 while (pv) { 975 while (pv) {
976 if (pv->pv_pmap == pm && pv->pv_va == va) { /* match? */ 976 if (pv->pv_pmap == pm && pv->pv_va == va) { /* match? */
977 NPDEBUG(PDB_PVDUMP, printf("pmap_remove_pv: pm %p, md " 977 NPDEBUG(PDB_PVDUMP, printf("pmap_remove_pv: pm %p, md "
978 "%p, flags 0x%x\n", pm, md, pv->pv_flags)); 978 "%p, flags 0x%x\n", pm, md, pv->pv_flags));
979 if (pv->pv_flags & PVF_WIRED) { 979 if (pv->pv_flags & PVF_WIRED) {
980 --pm->pm_stats.wired_count; 980 --pm->pm_stats.wired_count;
981 } 981 }
982 *prevptr = SLIST_NEXT(pv, pv_link); /* remove it! */ 982 *prevptr = SLIST_NEXT(pv, pv_link); /* remove it! */
983 if (pm == pmap_kernel()) { 983 if (pm == pmap_kernel()) {
984 PMAPCOUNT(kernel_unmappings); 984 PMAPCOUNT(kernel_unmappings);
985 if (pv->pv_flags & PVF_WRITE) 985 if (pv->pv_flags & PVF_WRITE)
986 md->krw_mappings--; 986 md->krw_mappings--;
987 else 987 else
988 md->kro_mappings--; 988 md->kro_mappings--;
989 } else { 989 } else {
990 if (pv->pv_flags & PVF_WRITE) 990 if (pv->pv_flags & PVF_WRITE)
991 md->urw_mappings--; 991 md->urw_mappings--;
992 else 992 else
993 md->uro_mappings--; 993 md->uro_mappings--;
994 } 994 }
995 995
996 PMAPCOUNT(unmappings); 996 PMAPCOUNT(unmappings);
997#ifdef PMAP_CACHE_VIPT 997#ifdef PMAP_CACHE_VIPT
998 if (!(pv->pv_flags & PVF_WRITE)) 998 if (!(pv->pv_flags & PVF_WRITE))
999 break; 999 break;
1000 /* 1000 /*
1001 * If this page has had an exec mapping, then if 1001 * If this page has had an exec mapping, then if
1002 * this was the last mapping, discard the contents, 1002 * this was the last mapping, discard the contents,
1003 * otherwise sync the i-cache for this page. 1003 * otherwise sync the i-cache for this page.
1004 */ 1004 */
1005 if (PV_IS_EXEC_P(md->pvh_attrs)) { 1005 if (PV_IS_EXEC_P(md->pvh_attrs)) {
1006 if (SLIST_EMPTY(&md->pvh_list)) { 1006 if (SLIST_EMPTY(&md->pvh_list)) {
1007 md->pvh_attrs &= ~PVF_EXEC; 1007 md->pvh_attrs &= ~PVF_EXEC;
1008 PMAPCOUNT(exec_discarded_unmap); 1008 PMAPCOUNT(exec_discarded_unmap);
1009 } else { 1009 } else {
1010 pmap_syncicache_page(md, pa); 1010 pmap_syncicache_page(md, pa);
1011 PMAPCOUNT(exec_synced_unmap); 1011 PMAPCOUNT(exec_synced_unmap);
1012 } 1012 }
1013 } 1013 }
1014#endif /* PMAP_CACHE_VIPT */ 1014#endif /* PMAP_CACHE_VIPT */
1015 break; 1015 break;
1016 } 1016 }
1017 prevptr = &SLIST_NEXT(pv, pv_link); /* previous pointer */ 1017 prevptr = &SLIST_NEXT(pv, pv_link); /* previous pointer */
1018 pv = *prevptr; /* advance */ 1018 pv = *prevptr; /* advance */
1019 } 1019 }
1020 1020
1021#ifdef PMAP_CACHE_VIPT 1021#ifdef PMAP_CACHE_VIPT
1022 /* 1022 /*
1023 * If we no longer have a WRITEABLE KENTRY at the head of list, 1023 * If we no longer have a WRITEABLE KENTRY at the head of list,
1024 * clear the KMOD attribute from the page. 1024 * clear the KMOD attribute from the page.
1025 */ 1025 */
1026 if (SLIST_FIRST(&md->pvh_list) == NULL 1026 if (SLIST_FIRST(&md->pvh_list) == NULL
1027 || (SLIST_FIRST(&md->pvh_list)->pv_flags & PVF_KWRITE) != PVF_KWRITE) 1027 || (SLIST_FIRST(&md->pvh_list)->pv_flags & PVF_KWRITE) != PVF_KWRITE)
1028 md->pvh_attrs &= ~PVF_KMOD; 1028 md->pvh_attrs &= ~PVF_KMOD;
1029 1029
1030 /* 1030 /*
1031 * If this was a writeable page and there are no more writeable 1031 * If this was a writeable page and there are no more writeable
1032 * mappings (ignoring KMPAGE), clear the WRITE flag and writeback 1032 * mappings (ignoring KMPAGE), clear the WRITE flag and writeback
1033 * the contents to memory. 1033 * the contents to memory.
1034 */ 1034 */
1035 if (md->krw_mappings + md->urw_mappings == 0) 1035 if (md->krw_mappings + md->urw_mappings == 0)
1036 md->pvh_attrs &= ~PVF_WRITE; 1036 md->pvh_attrs &= ~PVF_WRITE;
1037 KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC))); 1037 KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC)));
1038#endif /* PMAP_CACHE_VIPT */ 1038#endif /* PMAP_CACHE_VIPT */
1039 1039
1040 return(pv); /* return removed pv */ 1040 return(pv); /* return removed pv */
1041} 1041}
1042 1042
1043/* 1043/*
1044 * 1044 *
1045 * pmap_modify_pv: Update pv flags 1045 * pmap_modify_pv: Update pv flags
1046 * 1046 *
1047 * => caller should hold lock on vm_page [so that attrs can be adjusted] 1047 * => caller should hold lock on vm_page [so that attrs can be adjusted]
1048 * => caller should NOT adjust pmap's wire_count 1048 * => caller should NOT adjust pmap's wire_count
1049 * => caller must call pmap_vac_me_harder() if writable status of a page 1049 * => caller must call pmap_vac_me_harder() if writable status of a page
1050 * may have changed. 1050 * may have changed.
1051 * => we return the old flags 1051 * => we return the old flags
1052 *  1052 *
1053 * Modify a physical-virtual mapping in the pv table 1053 * Modify a physical-virtual mapping in the pv table
1054 */ 1054 */
1055static u_int 1055static u_int
1056pmap_modify_pv(struct vm_page_md *md, paddr_t pa, pmap_t pm, vaddr_t va, 1056pmap_modify_pv(struct vm_page_md *md, paddr_t pa, pmap_t pm, vaddr_t va,
1057 u_int clr_mask, u_int set_mask) 1057 u_int clr_mask, u_int set_mask)
1058{ 1058{
1059 struct pv_entry *npv; 1059 struct pv_entry *npv;
1060 u_int flags, oflags; 1060 u_int flags, oflags;
1061 1061
1062 KASSERT((clr_mask & PVF_KENTRY) == 0); 1062 KASSERT((clr_mask & PVF_KENTRY) == 0);
1063 KASSERT((set_mask & PVF_KENTRY) == 0); 1063 KASSERT((set_mask & PVF_KENTRY) == 0);
1064 1064
1065 if ((npv = pmap_find_pv(md, pm, va)) == NULL) 1065 if ((npv = pmap_find_pv(md, pm, va)) == NULL)
1066 return (0); 1066 return (0);
1067 1067
1068 NPDEBUG(PDB_PVDUMP, 1068 NPDEBUG(PDB_PVDUMP,
1069 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)); 1069 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));
1070 1070
1071 /* 1071 /*
1072 * There is at least one VA mapping this page. 1072 * There is at least one VA mapping this page.
1073 */ 1073 */
1074 1074
1075 if (clr_mask & (PVF_REF | PVF_MOD)) { 1075 if (clr_mask & (PVF_REF | PVF_MOD)) {
1076 md->pvh_attrs |= set_mask & (PVF_REF | PVF_MOD); 1076 md->pvh_attrs |= set_mask & (PVF_REF | PVF_MOD);
1077#ifdef PMAP_CACHE_VIPT 1077#ifdef PMAP_CACHE_VIPT
1078 if ((md->pvh_attrs & (PVF_DMOD|PVF_NC)) != PVF_NC) 1078 if ((md->pvh_attrs & (PVF_DMOD|PVF_NC)) != PVF_NC)
1079 md->pvh_attrs |= PVF_DIRTY; 1079 md->pvh_attrs |= PVF_DIRTY;
1080 KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC))); 1080 KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC)));
1081#endif 1081#endif
1082 } 1082 }
1083 1083
1084 oflags = npv->pv_flags; 1084 oflags = npv->pv_flags;
1085 npv->pv_flags = flags = (oflags & ~clr_mask) | set_mask; 1085 npv->pv_flags = flags = (oflags & ~clr_mask) | set_mask;
1086 1086
1087 if ((flags ^ oflags) & PVF_WIRED) { 1087 if ((flags ^ oflags) & PVF_WIRED) {
1088 if (flags & PVF_WIRED) 1088 if (flags & PVF_WIRED)
1089 ++pm->pm_stats.wired_count; 1089 ++pm->pm_stats.wired_count;
1090 else 1090 else
1091 --pm->pm_stats.wired_count; 1091 --pm->pm_stats.wired_count;
1092 } 1092 }
1093 1093
1094 if ((flags ^ oflags) & PVF_WRITE) { 1094 if ((flags ^ oflags) & PVF_WRITE) {
1095 if (pm == pmap_kernel()) { 1095 if (pm == pmap_kernel()) {
1096 if (flags & PVF_WRITE) { 1096 if (flags & PVF_WRITE) {
1097 md->krw_mappings++; 1097 md->krw_mappings++;
1098 md->kro_mappings--; 1098 md->kro_mappings--;
1099 } else { 1099 } else {
1100 md->kro_mappings++; 1100 md->kro_mappings++;
1101 md->krw_mappings--; 1101 md->krw_mappings--;
1102 } 1102 }
1103 } else { 1103 } else {
1104 if (flags & PVF_WRITE) { 1104 if (flags & PVF_WRITE) {
1105 md->urw_mappings++; 1105 md->urw_mappings++;
1106 md->uro_mappings--; 1106 md->uro_mappings--;
1107 } else { 1107 } else {
1108 md->uro_mappings++; 1108 md->uro_mappings++;
1109 md->urw_mappings--; 1109 md->urw_mappings--;
1110 } 1110 }
1111 } 1111 }
1112 } 1112 }
1113#ifdef PMAP_CACHE_VIPT 1113#ifdef PMAP_CACHE_VIPT
1114 if (md->urw_mappings + md->krw_mappings == 0) 1114 if (md->urw_mappings + md->krw_mappings == 0)
1115 md->pvh_attrs &= ~PVF_WRITE; 1115 md->pvh_attrs &= ~PVF_WRITE;
1116 /* 1116 /*
1117 * We have two cases here: the first is from enter_pv (new exec 1117 * We have two cases here: the first is from enter_pv (new exec
1118 * page), the second is a combined pmap_remove_pv/pmap_enter_pv. 1118 * page), the second is a combined pmap_remove_pv/pmap_enter_pv.
1119 * Since in latter, pmap_enter_pv won't do anything, we just have 1119 * Since in latter, pmap_enter_pv won't do anything, we just have
1120 * to do what pmap_remove_pv would do. 1120 * to do what pmap_remove_pv would do.
1121 */ 1121 */
1122 if ((PV_IS_EXEC_P(flags) && !PV_IS_EXEC_P(md->pvh_attrs)) 1122 if ((PV_IS_EXEC_P(flags) && !PV_IS_EXEC_P(md->pvh_attrs))
1123 || (PV_IS_EXEC_P(md->pvh_attrs) 1123 || (PV_IS_EXEC_P(md->pvh_attrs)
1124 || (!(flags & PVF_WRITE) && (oflags & PVF_WRITE)))) { 1124 || (!(flags & PVF_WRITE) && (oflags & PVF_WRITE)))) {
1125 pmap_syncicache_page(md, pa); 1125 pmap_syncicache_page(md, pa);
1126 PMAPCOUNT(exec_synced_remap); 1126 PMAPCOUNT(exec_synced_remap);
1127 } 1127 }
1128 KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC))); 1128 KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC)));
1129#endif 1129#endif
1130 1130
1131 PMAPCOUNT(remappings); 1131 PMAPCOUNT(remappings);
1132 1132
1133 return (oflags); 1133 return (oflags);
1134} 1134}
1135 1135
1136/* 1136/*
1137 * Allocate an L1 translation table for the specified pmap. 1137 * Allocate an L1 translation table for the specified pmap.
1138 * This is called at pmap creation time. 1138 * This is called at pmap creation time.
1139 */ 1139 */
1140static void 1140static void
1141pmap_alloc_l1(pmap_t pm) 1141pmap_alloc_l1(pmap_t pm)
1142{ 1142{
1143 struct l1_ttable *l1; 1143 struct l1_ttable *l1;
1144 u_int8_t domain; 1144 u_int8_t domain;
1145 1145
1146 /* 1146 /*
1147 * Remove the L1 at the head of the LRU list 1147 * Remove the L1 at the head of the LRU list
1148 */ 1148 */
1149 simple_lock(&l1_lru_lock); 1149 simple_lock(&l1_lru_lock);
1150 l1 = TAILQ_FIRST(&l1_lru_list); 1150 l1 = TAILQ_FIRST(&l1_lru_list);
1151 KDASSERT(l1 != NULL); 1151 KDASSERT(l1 != NULL);
1152 TAILQ_REMOVE(&l1_lru_list, l1, l1_lru); 1152 TAILQ_REMOVE(&l1_lru_list, l1, l1_lru);
1153 1153
1154 /* 1154 /*
1155 * Pick the first available domain number, and update 1155 * Pick the first available domain number, and update
1156 * the link to the next number. 1156 * the link to the next number.
1157 */ 1157 */
1158 domain = l1->l1_domain_first; 1158 domain = l1->l1_domain_first;
1159 l1->l1_domain_first = l1->l1_domain_free[domain]; 1159 l1->l1_domain_first = l1->l1_domain_free[domain];
1160 1160
1161 /* 1161 /*
1162 * If there are still free domain numbers in this L1, 1162 * If there are still free domain numbers in this L1,
1163 * put it back on the TAIL of the LRU list. 1163 * put it back on the TAIL of the LRU list.
1164 */ 1164 */
1165 if (++l1->l1_domain_use_count < PMAP_DOMAINS) 1165 if (++l1->l1_domain_use_count < PMAP_DOMAINS)
1166 TAILQ_INSERT_TAIL(&l1_lru_list, l1, l1_lru); 1166 TAILQ_INSERT_TAIL(&l1_lru_list, l1, l1_lru);
1167 1167
1168 simple_unlock(&l1_lru_lock); 1168 simple_unlock(&l1_lru_lock);
1169 1169
1170 /* 1170 /*
1171 * Fix up the relevant bits in the pmap structure 1171 * Fix up the relevant bits in the pmap structure
1172 */ 1172 */
1173 pm->pm_l1 = l1; 1173 pm->pm_l1 = l1;
1174 pm->pm_domain = domain; 1174 pm->pm_domain = domain;
1175} 1175}
1176 1176
1177/* 1177/*
1178 * Free an L1 translation table. 1178 * Free an L1 translation table.
1179 * This is called at pmap destruction time. 1179 * This is called at pmap destruction time.
1180 */ 1180 */
1181static void 1181static void
1182pmap_free_l1(pmap_t pm) 1182pmap_free_l1(pmap_t pm)
1183{ 1183{
1184 struct l1_ttable *l1 = pm->pm_l1; 1184 struct l1_ttable *l1 = pm->pm_l1;
1185 1185
1186 simple_lock(&l1_lru_lock); 1186 simple_lock(&l1_lru_lock);
1187 1187
1188 /* 1188 /*
1189 * If this L1 is currently on the LRU list, remove it. 1189 * If this L1 is currently on the LRU list, remove it.
1190 */ 1190 */
1191 if (l1->l1_domain_use_count < PMAP_DOMAINS) 1191 if (l1->l1_domain_use_count < PMAP_DOMAINS)
1192 TAILQ_REMOVE(&l1_lru_list, l1, l1_lru); 1192 TAILQ_REMOVE(&l1_lru_list, l1, l1_lru);
1193 1193
1194 /* 1194 /*
1195 * Free up the domain number which was allocated to the pmap 1195 * Free up the domain number which was allocated to the pmap
1196 */ 1196 */
1197 l1->l1_domain_free[pm->pm_domain] = l1->l1_domain_first; 1197 l1->l1_domain_free[pm->pm_domain] = l1->l1_domain_first;
1198 l1->l1_domain_first = pm->pm_domain; 1198 l1->l1_domain_first = pm->pm_domain;
1199 l1->l1_domain_use_count--; 1199 l1->l1_domain_use_count--;
1200 1200
1201 /* 1201 /*
1202 * The L1 now must have at least 1 free domain, so add 1202 * The L1 now must have at least 1 free domain, so add
1203 * it back to the LRU list. If the use count is zero, 1203 * it back to the LRU list. If the use count is zero,
1204 * put it at the head of the list, otherwise it goes 1204 * put it at the head of the list, otherwise it goes
1205 * to the tail. 1205 * to the tail.
1206 */ 1206 */
1207 if (l1->l1_domain_use_count == 0) 1207 if (l1->l1_domain_use_count == 0)
1208 TAILQ_INSERT_HEAD(&l1_lru_list, l1, l1_lru); 1208 TAILQ_INSERT_HEAD(&l1_lru_list, l1, l1_lru);
1209 else 1209 else
1210 TAILQ_INSERT_TAIL(&l1_lru_list, l1, l1_lru); 1210 TAILQ_INSERT_TAIL(&l1_lru_list, l1, l1_lru);
1211 1211
1212 simple_unlock(&l1_lru_lock); 1212 simple_unlock(&l1_lru_lock);
1213} 1213}
@@ -1581,1999 +1581,1999 @@ pmap_get_vac_flags(const struct vm_page_ @@ -1581,1999 +1581,1999 @@ pmap_get_vac_flags(const struct vm_page_
1581 1581
1582 kidx = 0; 1582 kidx = 0;
1583 if (md->kro_mappings || md->krw_mappings > 1) 1583 if (md->kro_mappings || md->krw_mappings > 1)
1584 kidx |= 1; 1584 kidx |= 1;
1585 if (md->krw_mappings) 1585 if (md->krw_mappings)
1586 kidx |= 2; 1586 kidx |= 2;
1587 1587
1588 uidx = 0; 1588 uidx = 0;
1589 if (md->uro_mappings || md->urw_mappings > 1) 1589 if (md->uro_mappings || md->urw_mappings > 1)
1590 uidx |= 1; 1590 uidx |= 1;
1591 if (md->urw_mappings) 1591 if (md->urw_mappings)
1592 uidx |= 2; 1592 uidx |= 2;
1593 1593
1594 return (pmap_vac_flags[uidx][kidx]); 1594 return (pmap_vac_flags[uidx][kidx]);
1595} 1595}
1596 1596
1597static inline void 1597static inline void
1598pmap_vac_me_harder(struct vm_page_md *md, paddr_t pa, pmap_t pm, vaddr_t va) 1598pmap_vac_me_harder(struct vm_page_md *md, paddr_t pa, pmap_t pm, vaddr_t va)
1599{ 1599{
1600 int nattr; 1600 int nattr;
1601 1601
1602 nattr = pmap_get_vac_flags(md); 1602 nattr = pmap_get_vac_flags(md);
1603 1603
1604 if (nattr < 0) { 1604 if (nattr < 0) {
1605 md->pvh_attrs &= ~PVF_NC; 1605 md->pvh_attrs &= ~PVF_NC;
1606 return; 1606 return;
1607 } 1607 }
1608 1608
1609 if (nattr == 0 && (md->pvh_attrs & PVF_NC) == 0) 1609 if (nattr == 0 && (md->pvh_attrs & PVF_NC) == 0)
1610 return; 1610 return;
1611 1611
1612 if (pm == pmap_kernel()) 1612 if (pm == pmap_kernel())
1613 pmap_vac_me_kpmap(md, pa, pm, va); 1613 pmap_vac_me_kpmap(md, pa, pm, va);
1614 else 1614 else
1615 pmap_vac_me_user(md, pa, pm, va); 1615 pmap_vac_me_user(md, pa, pm, va);
1616 1616
1617 md->pvh_attrs = (md->pvh_attrs & ~PVF_NC) | nattr; 1617 md->pvh_attrs = (md->pvh_attrs & ~PVF_NC) | nattr;
1618} 1618}
1619 1619
1620static void 1620static void
1621pmap_vac_me_kpmap(struct vm_page_md *md, paddr_t pa, pmap_t pm, vaddr_t va) 1621pmap_vac_me_kpmap(struct vm_page_md *md, paddr_t pa, pmap_t pm, vaddr_t va)
1622{ 1622{
1623 u_int u_cacheable, u_entries; 1623 u_int u_cacheable, u_entries;
1624 struct pv_entry *pv; 1624 struct pv_entry *pv;
1625 pmap_t last_pmap = pm; 1625 pmap_t last_pmap = pm;
1626 1626
1627 /*  1627 /*
1628 * Pass one, see if there are both kernel and user pmaps for 1628 * Pass one, see if there are both kernel and user pmaps for
1629 * this page. Calculate whether there are user-writable or 1629 * this page. Calculate whether there are user-writable or
1630 * kernel-writable pages. 1630 * kernel-writable pages.
1631 */ 1631 */
1632 u_cacheable = 0; 1632 u_cacheable = 0;
1633 SLIST_FOREACH(pv, &md->pvh_list, pv_link) { 1633 SLIST_FOREACH(pv, &md->pvh_list, pv_link) {
1634 if (pv->pv_pmap != pm && (pv->pv_flags & PVF_NC) == 0) 1634 if (pv->pv_pmap != pm && (pv->pv_flags & PVF_NC) == 0)
1635 u_cacheable++; 1635 u_cacheable++;
1636 } 1636 }
1637 1637
1638 u_entries = md->urw_mappings + md->uro_mappings; 1638 u_entries = md->urw_mappings + md->uro_mappings;
1639 1639
1640 /*  1640 /*
1641 * We know we have just been updating a kernel entry, so if 1641 * We know we have just been updating a kernel entry, so if
1642 * all user pages are already cacheable, then there is nothing 1642 * all user pages are already cacheable, then there is nothing
1643 * further to do. 1643 * further to do.
1644 */ 1644 */
1645 if (md->k_mappings == 0 && u_cacheable == u_entries) 1645 if (md->k_mappings == 0 && u_cacheable == u_entries)
1646 return; 1646 return;
1647 1647
1648 if (u_entries) { 1648 if (u_entries) {
1649 /*  1649 /*
1650 * Scan over the list again, for each entry, if it 1650 * Scan over the list again, for each entry, if it
1651 * might not be set correctly, call pmap_vac_me_user 1651 * might not be set correctly, call pmap_vac_me_user
1652 * to recalculate the settings. 1652 * to recalculate the settings.
1653 */ 1653 */
1654 SLIST_FOREACH(pv, &md->pvh_list, pv_link) { 1654 SLIST_FOREACH(pv, &md->pvh_list, pv_link) {
1655 /*  1655 /*
1656 * We know kernel mappings will get set 1656 * We know kernel mappings will get set
1657 * correctly in other calls. We also know 1657 * correctly in other calls. We also know
1658 * that if the pmap is the same as last_pmap 1658 * that if the pmap is the same as last_pmap
1659 * then we've just handled this entry. 1659 * then we've just handled this entry.
1660 */ 1660 */
1661 if (pv->pv_pmap == pm || pv->pv_pmap == last_pmap) 1661 if (pv->pv_pmap == pm || pv->pv_pmap == last_pmap)
1662 continue; 1662 continue;
1663 1663
1664 /*  1664 /*
1665 * If there are kernel entries and this page 1665 * If there are kernel entries and this page
1666 * is writable but non-cacheable, then we can 1666 * is writable but non-cacheable, then we can
1667 * skip this entry also.  1667 * skip this entry also.
1668 */ 1668 */
1669 if (md->k_mappings && 1669 if (md->k_mappings &&
1670 (pv->pv_flags & (PVF_NC | PVF_WRITE)) == 1670 (pv->pv_flags & (PVF_NC | PVF_WRITE)) ==
1671 (PVF_NC | PVF_WRITE)) 1671 (PVF_NC | PVF_WRITE))
1672 continue; 1672 continue;
1673 1673
1674 /*  1674 /*
1675 * Similarly if there are no kernel-writable  1675 * Similarly if there are no kernel-writable
1676 * entries and the page is already  1676 * entries and the page is already
1677 * read-only/cacheable. 1677 * read-only/cacheable.
1678 */ 1678 */
1679 if (md->krw_mappings == 0 && 1679 if (md->krw_mappings == 0 &&
1680 (pv->pv_flags & (PVF_NC | PVF_WRITE)) == 0) 1680 (pv->pv_flags & (PVF_NC | PVF_WRITE)) == 0)
1681 continue; 1681 continue;
1682 1682
1683 /*  1683 /*
1684 * For some of the remaining cases, we know 1684 * For some of the remaining cases, we know
1685 * that we must recalculate, but for others we 1685 * that we must recalculate, but for others we
1686 * can't tell if they are correct or not, so 1686 * can't tell if they are correct or not, so
1687 * we recalculate anyway. 1687 * we recalculate anyway.
1688 */ 1688 */
1689 pmap_vac_me_user(md, pa, (last_pmap = pv->pv_pmap), 0); 1689 pmap_vac_me_user(md, pa, (last_pmap = pv->pv_pmap), 0);
1690 } 1690 }
1691 1691
1692 if (md->k_mappings == 0) 1692 if (md->k_mappings == 0)
1693 return; 1693 return;
1694 } 1694 }
1695 1695
1696 pmap_vac_me_user(md, pa, pm, va); 1696 pmap_vac_me_user(md, pa, pm, va);
1697} 1697}
1698 1698
1699static void 1699static void
1700pmap_vac_me_user(struct vm_page_md *md, paddr_t pa, pmap_t pm, vaddr_t va) 1700pmap_vac_me_user(struct vm_page_md *md, paddr_t pa, pmap_t pm, vaddr_t va)
1701{ 1701{
1702 pmap_t kpmap = pmap_kernel(); 1702 pmap_t kpmap = pmap_kernel();
1703 struct pv_entry *pv, *npv = NULL; 1703 struct pv_entry *pv, *npv = NULL;
1704 struct l2_bucket *l2b; 1704 struct l2_bucket *l2b;
1705 pt_entry_t *ptep, pte; 1705 pt_entry_t *ptep, pte;
1706 u_int entries = 0; 1706 u_int entries = 0;
1707 u_int writable = 0; 1707 u_int writable = 0;
1708 u_int cacheable_entries = 0; 1708 u_int cacheable_entries = 0;
1709 u_int kern_cacheable = 0; 1709 u_int kern_cacheable = 0;
1710 u_int other_writable = 0; 1710 u_int other_writable = 0;
1711 1711
1712 /* 1712 /*
1713 * Count mappings and writable mappings in this pmap. 1713 * Count mappings and writable mappings in this pmap.
1714 * Include kernel mappings as part of our own. 1714 * Include kernel mappings as part of our own.
1715 * Keep a pointer to the first one. 1715 * Keep a pointer to the first one.
1716 */ 1716 */
1717 npv = NULL; 1717 npv = NULL;
1718 SLIST_FOREACH(pv, &md->pvh_list, pv_link) { 1718 SLIST_FOREACH(pv, &md->pvh_list, pv_link) {
1719 /* Count mappings in the same pmap */ 1719 /* Count mappings in the same pmap */
1720 if (pm == pv->pv_pmap || kpmap == pv->pv_pmap) { 1720 if (pm == pv->pv_pmap || kpmap == pv->pv_pmap) {
1721 if (entries++ == 0) 1721 if (entries++ == 0)
1722 npv = pv; 1722 npv = pv;
1723 1723
1724 /* Cacheable mappings */ 1724 /* Cacheable mappings */
1725 if ((pv->pv_flags & PVF_NC) == 0) { 1725 if ((pv->pv_flags & PVF_NC) == 0) {
1726 cacheable_entries++; 1726 cacheable_entries++;
1727 if (kpmap == pv->pv_pmap) 1727 if (kpmap == pv->pv_pmap)
1728 kern_cacheable++; 1728 kern_cacheable++;
1729 } 1729 }
1730 1730
1731 /* Writable mappings */ 1731 /* Writable mappings */
1732 if (pv->pv_flags & PVF_WRITE) 1732 if (pv->pv_flags & PVF_WRITE)
1733 ++writable; 1733 ++writable;
1734 } else 1734 } else
1735 if (pv->pv_flags & PVF_WRITE) 1735 if (pv->pv_flags & PVF_WRITE)
1736 other_writable = 1; 1736 other_writable = 1;
1737 } 1737 }
1738 1738
1739 /* 1739 /*
1740 * Enable or disable caching as necessary. 1740 * Enable or disable caching as necessary.
1741 * Note: the first entry might be part of the kernel pmap, 1741 * Note: the first entry might be part of the kernel pmap,
1742 * so we can't assume this is indicative of the state of the 1742 * so we can't assume this is indicative of the state of the
1743 * other (maybe non-kpmap) entries. 1743 * other (maybe non-kpmap) entries.
1744 */ 1744 */
1745 if ((entries > 1 && writable) || 1745 if ((entries > 1 && writable) ||
1746 (entries > 0 && pm == kpmap && other_writable)) { 1746 (entries > 0 && pm == kpmap && other_writable)) {
1747 if (cacheable_entries == 0) 1747 if (cacheable_entries == 0)
1748 return; 1748 return;
1749 1749
1750 for (pv = npv; pv; pv = SLIST_NEXT(pv, pv_link)) { 1750 for (pv = npv; pv; pv = SLIST_NEXT(pv, pv_link)) {
1751 if ((pm != pv->pv_pmap && kpmap != pv->pv_pmap) || 1751 if ((pm != pv->pv_pmap && kpmap != pv->pv_pmap) ||
1752 (pv->pv_flags & PVF_NC)) 1752 (pv->pv_flags & PVF_NC))
1753 continue; 1753 continue;
1754 1754
1755 pv->pv_flags |= PVF_NC; 1755 pv->pv_flags |= PVF_NC;
1756 1756
1757 l2b = pmap_get_l2_bucket(pv->pv_pmap, pv->pv_va); 1757 l2b = pmap_get_l2_bucket(pv->pv_pmap, pv->pv_va);
1758 KDASSERT(l2b != NULL); 1758 KDASSERT(l2b != NULL);
1759 ptep = &l2b->l2b_kva[l2pte_index(pv->pv_va)]; 1759 ptep = &l2b->l2b_kva[l2pte_index(pv->pv_va)];
1760 pte = *ptep & ~L2_S_CACHE_MASK; 1760 pte = *ptep & ~L2_S_CACHE_MASK;
1761 1761
1762 if ((va != pv->pv_va || pm != pv->pv_pmap) && 1762 if ((va != pv->pv_va || pm != pv->pv_pmap) &&
1763 l2pte_valid(pte)) { 1763 l2pte_valid(pte)) {
1764 if (PV_BEEN_EXECD(pv->pv_flags)) { 1764 if (PV_BEEN_EXECD(pv->pv_flags)) {
1765#ifdef PMAP_CACHE_VIVT 1765#ifdef PMAP_CACHE_VIVT
1766 pmap_idcache_wbinv_range(pv->pv_pmap, 1766 pmap_idcache_wbinv_range(pv->pv_pmap,
1767 pv->pv_va, PAGE_SIZE); 1767 pv->pv_va, PAGE_SIZE);
1768#endif 1768#endif
1769 pmap_tlb_flushID_SE(pv->pv_pmap, 1769 pmap_tlb_flushID_SE(pv->pv_pmap,
1770 pv->pv_va); 1770 pv->pv_va);
1771 } else 1771 } else
1772 if (PV_BEEN_REFD(pv->pv_flags)) { 1772 if (PV_BEEN_REFD(pv->pv_flags)) {
1773#ifdef PMAP_CACHE_VIVT 1773#ifdef PMAP_CACHE_VIVT
1774 pmap_dcache_wb_range(pv->pv_pmap, 1774 pmap_dcache_wb_range(pv->pv_pmap,
1775 pv->pv_va, PAGE_SIZE, true, 1775 pv->pv_va, PAGE_SIZE, true,
1776 (pv->pv_flags & PVF_WRITE) == 0); 1776 (pv->pv_flags & PVF_WRITE) == 0);
1777#endif 1777#endif
1778 pmap_tlb_flushD_SE(pv->pv_pmap, 1778 pmap_tlb_flushD_SE(pv->pv_pmap,
1779 pv->pv_va); 1779 pv->pv_va);
1780 } 1780 }
1781 } 1781 }
1782 1782
1783 *ptep = pte; 1783 *ptep = pte;
1784 PTE_SYNC_CURRENT(pv->pv_pmap, ptep); 1784 PTE_SYNC_CURRENT(pv->pv_pmap, ptep);
1785 } 1785 }
1786 cpu_cpwait(); 1786 cpu_cpwait();
1787 } else 1787 } else
1788 if (entries > cacheable_entries) { 1788 if (entries > cacheable_entries) {
1789 /* 1789 /*
1790 * Turn cacheing back on for some pages. If it is a kernel 1790 * Turn cacheing back on for some pages. If it is a kernel
1791 * page, only do so if there are no other writable pages. 1791 * page, only do so if there are no other writable pages.
1792 */ 1792 */
1793 for (pv = npv; pv; pv = SLIST_NEXT(pv, pv_link)) { 1793 for (pv = npv; pv; pv = SLIST_NEXT(pv, pv_link)) {
1794 if (!(pv->pv_flags & PVF_NC) || (pm != pv->pv_pmap && 1794 if (!(pv->pv_flags & PVF_NC) || (pm != pv->pv_pmap &&
1795 (kpmap != pv->pv_pmap || other_writable))) 1795 (kpmap != pv->pv_pmap || other_writable)))
1796 continue; 1796 continue;
1797 1797
1798 pv->pv_flags &= ~PVF_NC; 1798 pv->pv_flags &= ~PVF_NC;
1799 1799
1800 l2b = pmap_get_l2_bucket(pv->pv_pmap, pv->pv_va); 1800 l2b = pmap_get_l2_bucket(pv->pv_pmap, pv->pv_va);
1801 KDASSERT(l2b != NULL); 1801 KDASSERT(l2b != NULL);
1802 ptep = &l2b->l2b_kva[l2pte_index(pv->pv_va)]; 1802 ptep = &l2b->l2b_kva[l2pte_index(pv->pv_va)];
1803 pte = (*ptep & ~L2_S_CACHE_MASK) | pte_l2_s_cache_mode; 1803 pte = (*ptep & ~L2_S_CACHE_MASK) | pte_l2_s_cache_mode;
1804 1804
1805 if (l2pte_valid(pte)) { 1805 if (l2pte_valid(pte)) {
1806 if (PV_BEEN_EXECD(pv->pv_flags)) { 1806 if (PV_BEEN_EXECD(pv->pv_flags)) {
1807 pmap_tlb_flushID_SE(pv->pv_pmap, 1807 pmap_tlb_flushID_SE(pv->pv_pmap,
1808 pv->pv_va); 1808 pv->pv_va);
1809 } else 1809 } else
1810 if (PV_BEEN_REFD(pv->pv_flags)) { 1810 if (PV_BEEN_REFD(pv->pv_flags)) {
1811 pmap_tlb_flushD_SE(pv->pv_pmap, 1811 pmap_tlb_flushD_SE(pv->pv_pmap,
1812 pv->pv_va); 1812 pv->pv_va);
1813 } 1813 }
1814 } 1814 }
1815 1815
1816 *ptep = pte; 1816 *ptep = pte;
1817 PTE_SYNC_CURRENT(pv->pv_pmap, ptep); 1817 PTE_SYNC_CURRENT(pv->pv_pmap, ptep);
1818 } 1818 }
1819 } 1819 }
1820} 1820}
1821#endif 1821#endif
1822 1822
1823#ifdef PMAP_CACHE_VIPT 1823#ifdef PMAP_CACHE_VIPT
1824static void 1824static void
1825pmap_vac_me_harder(struct vm_page_md *md, paddr_t pa, pmap_t pm, vaddr_t va) 1825pmap_vac_me_harder(struct vm_page_md *md, paddr_t pa, pmap_t pm, vaddr_t va)
1826{ 1826{
1827 struct pv_entry *pv; 1827 struct pv_entry *pv;
1828 vaddr_t tst_mask; 1828 vaddr_t tst_mask;
1829 bool bad_alias; 1829 bool bad_alias;
1830 struct l2_bucket *l2b; 1830 struct l2_bucket *l2b;
1831 pt_entry_t *ptep, pte, opte; 1831 pt_entry_t *ptep, pte, opte;
1832 const u_int 1832 const u_int
1833 rw_mappings = md->urw_mappings + md->krw_mappings, 1833 rw_mappings = md->urw_mappings + md->krw_mappings,
1834 ro_mappings = md->uro_mappings + md->kro_mappings; 1834 ro_mappings = md->uro_mappings + md->kro_mappings;
1835 1835
1836 /* do we need to do anything? */ 1836 /* do we need to do anything? */
1837 if (arm_cache_prefer_mask == 0) 1837 if (arm_cache_prefer_mask == 0)
1838 return; 1838 return;
1839 1839
1840 NPDEBUG(PDB_VAC, printf("pmap_vac_me_harder: md=%p, pmap=%p va=%08lx\n", 1840 NPDEBUG(PDB_VAC, printf("pmap_vac_me_harder: md=%p, pmap=%p va=%08lx\n",
1841 md, pm, va)); 1841 md, pm, va));
1842 1842
1843 KASSERT(!va || pm); 1843 KASSERT(!va || pm);
1844 KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC))); 1844 KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC)));
1845 1845
1846 /* Already a conflict? */ 1846 /* Already a conflict? */
1847 if (__predict_false(md->pvh_attrs & PVF_NC)) { 1847 if (__predict_false(md->pvh_attrs & PVF_NC)) {
1848 /* just an add, things are already non-cached */ 1848 /* just an add, things are already non-cached */
1849 KASSERT(!(md->pvh_attrs & PVF_DIRTY)); 1849 KASSERT(!(md->pvh_attrs & PVF_DIRTY));
1850 KASSERT(!(md->pvh_attrs & PVF_MULTCLR)); 1850 KASSERT(!(md->pvh_attrs & PVF_MULTCLR));
1851 bad_alias = false; 1851 bad_alias = false;
1852 if (va) { 1852 if (va) {
1853 PMAPCOUNT(vac_color_none); 1853 PMAPCOUNT(vac_color_none);
1854 bad_alias = true; 1854 bad_alias = true;
1855 KASSERT((rw_mappings == 0) == !(md->pvh_attrs & PVF_WRITE)); 1855 KASSERT((rw_mappings == 0) == !(md->pvh_attrs & PVF_WRITE));
1856 goto fixup; 1856 goto fixup;
1857 } 1857 }
1858 pv = SLIST_FIRST(&md->pvh_list); 1858 pv = SLIST_FIRST(&md->pvh_list);
1859 /* the list can't be empty because it would be cachable */ 1859 /* the list can't be empty because it would be cachable */
1860 if (md->pvh_attrs & PVF_KMPAGE) { 1860 if (md->pvh_attrs & PVF_KMPAGE) {
1861 tst_mask = md->pvh_attrs; 1861 tst_mask = md->pvh_attrs;
1862 } else { 1862 } else {
1863 KASSERT(pv); 1863 KASSERT(pv);
1864 tst_mask = pv->pv_va; 1864 tst_mask = pv->pv_va;
1865 pv = SLIST_NEXT(pv, pv_link); 1865 pv = SLIST_NEXT(pv, pv_link);
1866 } 1866 }
1867 /* 1867 /*
1868 * Only check for a bad alias if we have writable mappings. 1868 * Only check for a bad alias if we have writable mappings.
1869 */ 1869 */
1870 tst_mask &= arm_cache_prefer_mask; 1870 tst_mask &= arm_cache_prefer_mask;
1871 if (rw_mappings > 0 && arm_cache_prefer_mask) { 1871 if (rw_mappings > 0 && arm_cache_prefer_mask) {
1872 for (; pv && !bad_alias; pv = SLIST_NEXT(pv, pv_link)) { 1872 for (; pv && !bad_alias; pv = SLIST_NEXT(pv, pv_link)) {
1873 /* if there's a bad alias, stop checking. */ 1873 /* if there's a bad alias, stop checking. */
1874 if (tst_mask != (pv->pv_va & arm_cache_prefer_mask)) 1874 if (tst_mask != (pv->pv_va & arm_cache_prefer_mask))
1875 bad_alias = true; 1875 bad_alias = true;
1876 } 1876 }
1877 md->pvh_attrs |= PVF_WRITE; 1877 md->pvh_attrs |= PVF_WRITE;
1878 if (!bad_alias) 1878 if (!bad_alias)
1879 md->pvh_attrs |= PVF_DIRTY; 1879 md->pvh_attrs |= PVF_DIRTY;
1880 } else { 1880 } else {
1881 /* 1881 /*
1882 * We have only read-only mappings. Let's see if there 1882 * We have only read-only mappings. Let's see if there
1883 * are multiple colors in use or if we mapped a KMPAGE. 1883 * are multiple colors in use or if we mapped a KMPAGE.
1884 * If the latter, we have a bad alias. If the former, 1884 * If the latter, we have a bad alias. If the former,
1885 * we need to remember that. 1885 * we need to remember that.
1886 */ 1886 */
1887 for (; pv; pv = SLIST_NEXT(pv, pv_link)) { 1887 for (; pv; pv = SLIST_NEXT(pv, pv_link)) {
1888 if (tst_mask != (pv->pv_va & arm_cache_prefer_mask)) { 1888 if (tst_mask != (pv->pv_va & arm_cache_prefer_mask)) {
1889 if (md->pvh_attrs & PVF_KMPAGE) 1889 if (md->pvh_attrs & PVF_KMPAGE)
1890 bad_alias = true; 1890 bad_alias = true;
1891 break; 1891 break;
1892 } 1892 }
1893 } 1893 }
1894 md->pvh_attrs &= ~PVF_WRITE; 1894 md->pvh_attrs &= ~PVF_WRITE;
1895 /* 1895 /*
1896 * No KMPAGE and we exited early, so we must have  1896 * No KMPAGE and we exited early, so we must have
1897 * multiple color mappings. 1897 * multiple color mappings.
1898 */ 1898 */
1899 if (!bad_alias && pv != NULL) 1899 if (!bad_alias && pv != NULL)
1900 md->pvh_attrs |= PVF_MULTCLR; 1900 md->pvh_attrs |= PVF_MULTCLR;
1901 } 1901 }
1902 1902
1903 /* If no conflicting colors, set everything back to cached */ 1903 /* If no conflicting colors, set everything back to cached */
1904 if (!bad_alias) { 1904 if (!bad_alias) {
1905#ifdef DEBUG 1905#ifdef DEBUG
1906 if ((md->pvh_attrs & PVF_WRITE) 1906 if ((md->pvh_attrs & PVF_WRITE)
1907 || ro_mappings < 2) { 1907 || ro_mappings < 2) {
1908 SLIST_FOREACH(pv, &md->pvh_list, pv_link) 1908 SLIST_FOREACH(pv, &md->pvh_list, pv_link)
1909 KDASSERT(((tst_mask ^ pv->pv_va) & arm_cache_prefer_mask) == 0); 1909 KDASSERT(((tst_mask ^ pv->pv_va) & arm_cache_prefer_mask) == 0);
1910 } 1910 }
1911#endif 1911#endif
1912 md->pvh_attrs &= (PAGE_SIZE - 1) & ~PVF_NC; 1912 md->pvh_attrs &= (PAGE_SIZE - 1) & ~PVF_NC;
1913 md->pvh_attrs |= tst_mask | PVF_COLORED; 1913 md->pvh_attrs |= tst_mask | PVF_COLORED;
1914 /* 1914 /*
1915 * Restore DIRTY bit if page is modified 1915 * Restore DIRTY bit if page is modified
1916 */ 1916 */
1917 if (md->pvh_attrs & PVF_DMOD) 1917 if (md->pvh_attrs & PVF_DMOD)
1918 md->pvh_attrs |= PVF_DIRTY; 1918 md->pvh_attrs |= PVF_DIRTY;
1919 PMAPCOUNT(vac_color_restore); 1919 PMAPCOUNT(vac_color_restore);
1920 } else { 1920 } else {
1921 KASSERT(SLIST_FIRST(&md->pvh_list) != NULL); 1921 KASSERT(SLIST_FIRST(&md->pvh_list) != NULL);
1922 KASSERT(SLIST_NEXT(SLIST_FIRST(&md->pvh_list), pv_link) != NULL); 1922 KASSERT(SLIST_NEXT(SLIST_FIRST(&md->pvh_list), pv_link) != NULL);
1923 } 1923 }
1924 KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC))); 1924 KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC)));
1925 KASSERT((rw_mappings == 0) == !(md->pvh_attrs & PVF_WRITE)); 1925 KASSERT((rw_mappings == 0) == !(md->pvh_attrs & PVF_WRITE));
1926 } else if (!va) { 1926 } else if (!va) {
1927 KASSERT(arm_cache_prefer_mask == 0 || pmap_is_page_colored_p(md)); 1927 KASSERT(arm_cache_prefer_mask == 0 || pmap_is_page_colored_p(md));
1928 KASSERT(!(md->pvh_attrs & PVF_WRITE) 1928 KASSERT(!(md->pvh_attrs & PVF_WRITE)
1929 || (md->pvh_attrs & PVF_DIRTY)); 1929 || (md->pvh_attrs & PVF_DIRTY));
1930 if (rw_mappings == 0) { 1930 if (rw_mappings == 0) {
1931 md->pvh_attrs &= ~PVF_WRITE; 1931 md->pvh_attrs &= ~PVF_WRITE;
1932 if (ro_mappings == 1 1932 if (ro_mappings == 1
1933 && (md->pvh_attrs & PVF_MULTCLR)) { 1933 && (md->pvh_attrs & PVF_MULTCLR)) {
1934 /* 1934 /*
1935 * If this is the last readonly mapping 1935 * If this is the last readonly mapping
1936 * but it doesn't match the current color 1936 * but it doesn't match the current color
1937 * for the page, change the current color 1937 * for the page, change the current color
1938 * to match this last readonly mapping. 1938 * to match this last readonly mapping.
1939 */ 1939 */
1940 pv = SLIST_FIRST(&md->pvh_list); 1940 pv = SLIST_FIRST(&md->pvh_list);
1941 tst_mask = (md->pvh_attrs ^ pv->pv_va) 1941 tst_mask = (md->pvh_attrs ^ pv->pv_va)
1942 & arm_cache_prefer_mask; 1942 & arm_cache_prefer_mask;
1943 if (tst_mask) { 1943 if (tst_mask) {
1944 md->pvh_attrs ^= tst_mask; 1944 md->pvh_attrs ^= tst_mask;
1945 PMAPCOUNT(vac_color_change); 1945 PMAPCOUNT(vac_color_change);
1946 } 1946 }
1947 } 1947 }
1948 } 1948 }
1949 KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC))); 1949 KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC)));
1950 KASSERT((rw_mappings == 0) == !(md->pvh_attrs & PVF_WRITE)); 1950 KASSERT((rw_mappings == 0) == !(md->pvh_attrs & PVF_WRITE));
1951 return; 1951 return;
1952 } else if (!pmap_is_page_colored_p(md)) { 1952 } else if (!pmap_is_page_colored_p(md)) {
1953 /* not colored so we just use its color */ 1953 /* not colored so we just use its color */
1954 KASSERT(md->pvh_attrs & (PVF_WRITE|PVF_DIRTY)); 1954 KASSERT(md->pvh_attrs & (PVF_WRITE|PVF_DIRTY));
1955 KASSERT(!(md->pvh_attrs & PVF_MULTCLR)); 1955 KASSERT(!(md->pvh_attrs & PVF_MULTCLR));
1956 PMAPCOUNT(vac_color_new); 1956 PMAPCOUNT(vac_color_new);
1957 md->pvh_attrs &= PAGE_SIZE - 1; 1957 md->pvh_attrs &= PAGE_SIZE - 1;
1958 md->pvh_attrs |= PVF_COLORED 1958 md->pvh_attrs |= PVF_COLORED
1959 | (va & arm_cache_prefer_mask) 1959 | (va & arm_cache_prefer_mask)
1960 | (rw_mappings > 0 ? PVF_WRITE : 0); 1960 | (rw_mappings > 0 ? PVF_WRITE : 0);
1961 KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC))); 1961 KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC)));
1962 KASSERT((rw_mappings == 0) == !(md->pvh_attrs & PVF_WRITE)); 1962 KASSERT((rw_mappings == 0) == !(md->pvh_attrs & PVF_WRITE));
1963 return; 1963 return;
1964 } else if (((md->pvh_attrs ^ va) & arm_cache_prefer_mask) == 0) { 1964 } else if (((md->pvh_attrs ^ va) & arm_cache_prefer_mask) == 0) {
1965 bad_alias = false; 1965 bad_alias = false;
1966 if (rw_mappings > 0) { 1966 if (rw_mappings > 0) {
1967 /* 1967 /*
1968 * We now have writeable mappings and if we have 1968 * We now have writeable mappings and if we have
1969 * readonly mappings in more than once color, we have 1969 * readonly mappings in more than once color, we have
1970 * an aliasing problem. Regardless mark the page as 1970 * an aliasing problem. Regardless mark the page as
1971 * writeable. 1971 * writeable.
1972 */ 1972 */
1973 if (md->pvh_attrs & PVF_MULTCLR) { 1973 if (md->pvh_attrs & PVF_MULTCLR) {
1974 if (ro_mappings < 2) { 1974 if (ro_mappings < 2) {
1975 /* 1975 /*
1976 * If we only have less than two 1976 * If we only have less than two
1977 * read-only mappings, just flush the 1977 * read-only mappings, just flush the
1978 * non-primary colors from the cache. 1978 * non-primary colors from the cache.
1979 */ 1979 */
1980 pmap_flush_page(md, pa, 1980 pmap_flush_page(md, pa,
1981 PMAP_FLUSH_SECONDARY); 1981 PMAP_FLUSH_SECONDARY);
1982 } else { 1982 } else {
1983 bad_alias = true; 1983 bad_alias = true;
1984 } 1984 }
1985 } 1985 }
1986 md->pvh_attrs |= PVF_WRITE; 1986 md->pvh_attrs |= PVF_WRITE;
1987 } 1987 }
1988 /* If no conflicting colors, set everything back to cached */ 1988 /* If no conflicting colors, set everything back to cached */
1989 if (!bad_alias) { 1989 if (!bad_alias) {
1990#ifdef DEBUG 1990#ifdef DEBUG
1991 if (rw_mappings > 0 1991 if (rw_mappings > 0
1992 || (md->pvh_attrs & PMAP_KMPAGE)) { 1992 || (md->pvh_attrs & PMAP_KMPAGE)) {
1993 tst_mask = md->pvh_attrs & arm_cache_prefer_mask; 1993 tst_mask = md->pvh_attrs & arm_cache_prefer_mask;
1994 SLIST_FOREACH(pv, &md->pvh_list, pv_link) 1994 SLIST_FOREACH(pv, &md->pvh_list, pv_link)
1995 KDASSERT(((tst_mask ^ pv->pv_va) & arm_cache_prefer_mask) == 0); 1995 KDASSERT(((tst_mask ^ pv->pv_va) & arm_cache_prefer_mask) == 0);
1996 } 1996 }
1997#endif 1997#endif
1998 if (SLIST_EMPTY(&md->pvh_list)) 1998 if (SLIST_EMPTY(&md->pvh_list))
1999 PMAPCOUNT(vac_color_reuse); 1999 PMAPCOUNT(vac_color_reuse);
2000 else 2000 else
2001 PMAPCOUNT(vac_color_ok); 2001 PMAPCOUNT(vac_color_ok);
2002 2002
2003 /* matching color, just return */ 2003 /* matching color, just return */
2004 KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC))); 2004 KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC)));
2005 KASSERT((rw_mappings == 0) == !(md->pvh_attrs & PVF_WRITE)); 2005 KASSERT((rw_mappings == 0) == !(md->pvh_attrs & PVF_WRITE));
2006 return; 2006 return;
2007 } 2007 }
2008 KASSERT(SLIST_FIRST(&md->pvh_list) != NULL); 2008 KASSERT(SLIST_FIRST(&md->pvh_list) != NULL);
2009 KASSERT(SLIST_NEXT(SLIST_FIRST(&md->pvh_list), pv_link) != NULL); 2009 KASSERT(SLIST_NEXT(SLIST_FIRST(&md->pvh_list), pv_link) != NULL);
2010 2010
2011 /* color conflict. evict from cache. */ 2011 /* color conflict. evict from cache. */
2012 2012
2013 pmap_flush_page(md, pa, PMAP_FLUSH_PRIMARY); 2013 pmap_flush_page(md, pa, PMAP_FLUSH_PRIMARY);
2014 md->pvh_attrs &= ~PVF_COLORED; 2014 md->pvh_attrs &= ~PVF_COLORED;
2015 md->pvh_attrs |= PVF_NC; 2015 md->pvh_attrs |= PVF_NC;
2016 KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC))); 2016 KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC)));
2017 KASSERT(!(md->pvh_attrs & PVF_MULTCLR)); 2017 KASSERT(!(md->pvh_attrs & PVF_MULTCLR));
2018 PMAPCOUNT(vac_color_erase); 2018 PMAPCOUNT(vac_color_erase);
2019 } else if (rw_mappings == 0 2019 } else if (rw_mappings == 0
2020 && (md->pvh_attrs & PVF_KMPAGE) == 0) { 2020 && (md->pvh_attrs & PVF_KMPAGE) == 0) {
2021 KASSERT((md->pvh_attrs & PVF_WRITE) == 0); 2021 KASSERT((md->pvh_attrs & PVF_WRITE) == 0);
2022 2022
2023 /* 2023 /*
2024 * If the page has dirty cache lines, clean it. 2024 * If the page has dirty cache lines, clean it.
2025 */ 2025 */
2026 if (md->pvh_attrs & PVF_DIRTY) 2026 if (md->pvh_attrs & PVF_DIRTY)
2027 pmap_flush_page(md, pa, PMAP_CLEAN_PRIMARY); 2027 pmap_flush_page(md, pa, PMAP_CLEAN_PRIMARY);
2028 2028
2029 /* 2029 /*
2030 * If this is the first remapping (we know that there are no 2030 * If this is the first remapping (we know that there are no
2031 * writeable mappings), then this is a simple color change. 2031 * writeable mappings), then this is a simple color change.
2032 * Otherwise this is a seconary r/o mapping, which means 2032 * Otherwise this is a seconary r/o mapping, which means
2033 * we don't have to do anything. 2033 * we don't have to do anything.
2034 */ 2034 */
2035 if (ro_mappings == 1) { 2035 if (ro_mappings == 1) {
2036 KASSERT(((md->pvh_attrs ^ va) & arm_cache_prefer_mask) != 0); 2036 KASSERT(((md->pvh_attrs ^ va) & arm_cache_prefer_mask) != 0);
2037 md->pvh_attrs &= PAGE_SIZE - 1; 2037 md->pvh_attrs &= PAGE_SIZE - 1;
2038 md->pvh_attrs |= (va & arm_cache_prefer_mask); 2038 md->pvh_attrs |= (va & arm_cache_prefer_mask);
2039 PMAPCOUNT(vac_color_change); 2039 PMAPCOUNT(vac_color_change);
2040 } else { 2040 } else {
2041 PMAPCOUNT(vac_color_blind); 2041 PMAPCOUNT(vac_color_blind);
2042 } 2042 }
2043 md->pvh_attrs |= PVF_MULTCLR; 2043 md->pvh_attrs |= PVF_MULTCLR;
2044 KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC))); 2044 KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC)));
2045 KASSERT((rw_mappings == 0) == !(md->pvh_attrs & PVF_WRITE)); 2045 KASSERT((rw_mappings == 0) == !(md->pvh_attrs & PVF_WRITE));
2046 return; 2046 return;
2047 } else { 2047 } else {
2048 if (rw_mappings > 0) 2048 if (rw_mappings > 0)
2049 md->pvh_attrs |= PVF_WRITE; 2049 md->pvh_attrs |= PVF_WRITE;
2050 2050
2051 /* color conflict. evict from cache. */ 2051 /* color conflict. evict from cache. */
2052 pmap_flush_page(md, pa, PMAP_FLUSH_PRIMARY); 2052 pmap_flush_page(md, pa, PMAP_FLUSH_PRIMARY);
2053 2053
2054 /* the list can't be empty because this was a enter/modify */ 2054 /* the list can't be empty because this was a enter/modify */
2055 pv = SLIST_FIRST(&md->pvh_list); 2055 pv = SLIST_FIRST(&md->pvh_list);
2056 if ((md->pvh_attrs & PVF_KMPAGE) == 0) { 2056 if ((md->pvh_attrs & PVF_KMPAGE) == 0) {
2057 KASSERT(pv); 2057 KASSERT(pv);
2058 /* 2058 /*
2059 * If there's only one mapped page, change color to the 2059 * If there's only one mapped page, change color to the
2060 * page's new color and return. Restore the DIRTY bit 2060 * page's new color and return. Restore the DIRTY bit
2061 * that was erased by pmap_flush_page. 2061 * that was erased by pmap_flush_page.
2062 */ 2062 */
2063 if (SLIST_NEXT(pv, pv_link) == NULL) { 2063 if (SLIST_NEXT(pv, pv_link) == NULL) {
2064 md->pvh_attrs &= PAGE_SIZE - 1; 2064 md->pvh_attrs &= PAGE_SIZE - 1;
2065 md->pvh_attrs |= (va & arm_cache_prefer_mask); 2065 md->pvh_attrs |= (va & arm_cache_prefer_mask);
2066 if (md->pvh_attrs & PVF_DMOD) 2066 if (md->pvh_attrs & PVF_DMOD)
2067 md->pvh_attrs |= PVF_DIRTY; 2067 md->pvh_attrs |= PVF_DIRTY;
2068 PMAPCOUNT(vac_color_change); 2068 PMAPCOUNT(vac_color_change);
2069 KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC))); 2069 KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC)));
2070 KASSERT((rw_mappings == 0) == !(md->pvh_attrs & PVF_WRITE)); 2070 KASSERT((rw_mappings == 0) == !(md->pvh_attrs & PVF_WRITE));
2071 KASSERT(!(md->pvh_attrs & PVF_MULTCLR)); 2071 KASSERT(!(md->pvh_attrs & PVF_MULTCLR));
2072 return; 2072 return;
2073 } 2073 }
2074 } 2074 }
2075 bad_alias = true; 2075 bad_alias = true;
2076 md->pvh_attrs &= ~PVF_COLORED; 2076 md->pvh_attrs &= ~PVF_COLORED;
2077 md->pvh_attrs |= PVF_NC; 2077 md->pvh_attrs |= PVF_NC;
2078 PMAPCOUNT(vac_color_erase); 2078 PMAPCOUNT(vac_color_erase);
2079 KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC))); 2079 KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC)));
2080 } 2080 }
2081 2081
2082 fixup: 2082 fixup:
2083 KASSERT((rw_mappings == 0) == !(md->pvh_attrs & PVF_WRITE)); 2083 KASSERT((rw_mappings == 0) == !(md->pvh_attrs & PVF_WRITE));
2084 2084
2085 /* 2085 /*
2086 * Turn cacheing on/off for all pages. 2086 * Turn cacheing on/off for all pages.
2087 */ 2087 */
2088 SLIST_FOREACH(pv, &md->pvh_list, pv_link) { 2088 SLIST_FOREACH(pv, &md->pvh_list, pv_link) {
2089 l2b = pmap_get_l2_bucket(pv->pv_pmap, pv->pv_va); 2089 l2b = pmap_get_l2_bucket(pv->pv_pmap, pv->pv_va);
2090 KDASSERT(l2b != NULL); 2090 KDASSERT(l2b != NULL);
2091 ptep = &l2b->l2b_kva[l2pte_index(pv->pv_va)]; 2091 ptep = &l2b->l2b_kva[l2pte_index(pv->pv_va)];
2092 opte = *ptep; 2092 opte = *ptep;
2093 pte = opte & ~L2_S_CACHE_MASK; 2093 pte = opte & ~L2_S_CACHE_MASK;
2094 if (bad_alias) { 2094 if (bad_alias) {
2095 pv->pv_flags |= PVF_NC; 2095 pv->pv_flags |= PVF_NC;
2096 } else { 2096 } else {
2097 pv->pv_flags &= ~PVF_NC; 2097 pv->pv_flags &= ~PVF_NC;
2098 pte |= pte_l2_s_cache_mode; 2098 pte |= pte_l2_s_cache_mode;
2099 } 2099 }
2100 2100
2101 if (opte == pte) /* only update is there's a change */ 2101 if (opte == pte) /* only update is there's a change */
2102 continue; 2102 continue;
2103 2103
2104 if (l2pte_valid(pte)) { 2104 if (l2pte_valid(pte)) {
2105 if (PV_BEEN_EXECD(pv->pv_flags)) { 2105 if (PV_BEEN_EXECD(pv->pv_flags)) {
2106 pmap_tlb_flushID_SE(pv->pv_pmap, pv->pv_va); 2106 pmap_tlb_flushID_SE(pv->pv_pmap, pv->pv_va);
2107 } else if (PV_BEEN_REFD(pv->pv_flags)) { 2107 } else if (PV_BEEN_REFD(pv->pv_flags)) {
2108 pmap_tlb_flushD_SE(pv->pv_pmap, pv->pv_va); 2108 pmap_tlb_flushD_SE(pv->pv_pmap, pv->pv_va);
2109 } 2109 }
2110 } 2110 }
2111 2111
2112 *ptep = pte; 2112 *ptep = pte;
2113 PTE_SYNC_CURRENT(pv->pv_pmap, ptep); 2113 PTE_SYNC_CURRENT(pv->pv_pmap, ptep);
2114 } 2114 }
2115} 2115}
2116#endif /* PMAP_CACHE_VIPT */ 2116#endif /* PMAP_CACHE_VIPT */
2117 2117
2118 2118
2119/* 2119/*
2120 * Modify pte bits for all ptes corresponding to the given physical address. 2120 * Modify pte bits for all ptes corresponding to the given physical address.
2121 * We use `maskbits' rather than `clearbits' because we're always passing 2121 * We use `maskbits' rather than `clearbits' because we're always passing
2122 * constants and the latter would require an extra inversion at run-time. 2122 * constants and the latter would require an extra inversion at run-time.
2123 */ 2123 */
2124static void 2124static void
2125pmap_clearbit(struct vm_page_md *md, paddr_t pa, u_int maskbits) 2125pmap_clearbit(struct vm_page_md *md, paddr_t pa, u_int maskbits)
2126{ 2126{
2127 struct l2_bucket *l2b; 2127 struct l2_bucket *l2b;
2128 struct pv_entry *pv; 2128 struct pv_entry *pv;
2129 pt_entry_t *ptep, npte, opte; 2129 pt_entry_t *ptep, npte, opte;
2130 pmap_t pm; 2130 pmap_t pm;
2131 vaddr_t va; 2131 vaddr_t va;
2132 u_int oflags; 2132 u_int oflags;
2133#ifdef PMAP_CACHE_VIPT 2133#ifdef PMAP_CACHE_VIPT
2134 const bool want_syncicache = PV_IS_EXEC_P(md->pvh_attrs); 2134 const bool want_syncicache = PV_IS_EXEC_P(md->pvh_attrs);
2135 bool need_syncicache = false; 2135 bool need_syncicache = false;
2136 bool did_syncicache = false; 2136 bool did_syncicache = false;
2137 bool need_vac_me_harder = false; 2137 bool need_vac_me_harder = false;
2138#endif 2138#endif
2139 2139
2140 NPDEBUG(PDB_BITS, 2140 NPDEBUG(PDB_BITS,
2141 printf("pmap_clearbit: md %p mask 0x%x\n", 2141 printf("pmap_clearbit: md %p mask 0x%x\n",
2142 md, maskbits)); 2142 md, maskbits));
2143 2143
2144 PMAP_HEAD_TO_MAP_LOCK(); 2144 PMAP_HEAD_TO_MAP_LOCK();
2145 simple_lock(&md->pvh_slock); 2145 simple_lock(&md->pvh_slock);
2146 2146
2147#ifdef PMAP_CACHE_VIPT 2147#ifdef PMAP_CACHE_VIPT
2148 /* 2148 /*
2149 * If we might want to sync the I-cache and we've modified it, 2149 * If we might want to sync the I-cache and we've modified it,
2150 * then we know we definitely need to sync or discard it. 2150 * then we know we definitely need to sync or discard it.
2151 */ 2151 */
2152 if (want_syncicache) 2152 if (want_syncicache)
2153 need_syncicache = md->pvh_attrs & PVF_MOD; 2153 need_syncicache = md->pvh_attrs & PVF_MOD;
2154#endif 2154#endif
2155 /* 2155 /*
2156 * Clear saved attributes (modify, reference) 2156 * Clear saved attributes (modify, reference)
2157 */ 2157 */
2158 md->pvh_attrs &= ~(maskbits & (PVF_MOD | PVF_REF)); 2158 md->pvh_attrs &= ~(maskbits & (PVF_MOD | PVF_REF));
2159 2159
2160 if (SLIST_EMPTY(&md->pvh_list)) { 2160 if (SLIST_EMPTY(&md->pvh_list)) {
2161#ifdef PMAP_CACHE_VIPT 2161#ifdef PMAP_CACHE_VIPT
2162 if (need_syncicache) { 2162 if (need_syncicache) {
2163 /* 2163 /*
2164 * No one has it mapped, so just discard it. The next 2164 * No one has it mapped, so just discard it. The next
2165 * exec remapping will cause it to be synced. 2165 * exec remapping will cause it to be synced.
2166 */ 2166 */
2167 md->pvh_attrs &= ~PVF_EXEC; 2167 md->pvh_attrs &= ~PVF_EXEC;
2168 PMAPCOUNT(exec_discarded_clearbit); 2168 PMAPCOUNT(exec_discarded_clearbit);
2169 } 2169 }
2170#endif 2170#endif
2171 simple_unlock(&md->pvh_slock); 2171 simple_unlock(&md->pvh_slock);
2172 PMAP_HEAD_TO_MAP_UNLOCK(); 2172 PMAP_HEAD_TO_MAP_UNLOCK();
2173 return; 2173 return;
2174 } 2174 }
2175 2175
2176 /* 2176 /*
2177 * Loop over all current mappings setting/clearing as appropos 2177 * Loop over all current mappings setting/clearing as appropos
2178 */ 2178 */
2179 SLIST_FOREACH(pv, &md->pvh_list, pv_link) { 2179 SLIST_FOREACH(pv, &md->pvh_list, pv_link) {
2180 va = pv->pv_va; 2180 va = pv->pv_va;
2181 pm = pv->pv_pmap; 2181 pm = pv->pv_pmap;
2182 oflags = pv->pv_flags; 2182 oflags = pv->pv_flags;
2183 /* 2183 /*
2184 * Kernel entries are unmanaged and as such not to be changed. 2184 * Kernel entries are unmanaged and as such not to be changed.
2185 */ 2185 */
2186 if (oflags & PVF_KENTRY) 2186 if (oflags & PVF_KENTRY)
2187 continue; 2187 continue;
2188 pv->pv_flags &= ~maskbits; 2188 pv->pv_flags &= ~maskbits;
2189 2189
2190 pmap_acquire_pmap_lock(pm); 2190 pmap_acquire_pmap_lock(pm);
2191 2191
2192 l2b = pmap_get_l2_bucket(pm, va); 2192 l2b = pmap_get_l2_bucket(pm, va);
2193 KDASSERT(l2b != NULL); 2193 KDASSERT(l2b != NULL);
2194 2194
2195 ptep = &l2b->l2b_kva[l2pte_index(va)]; 2195 ptep = &l2b->l2b_kva[l2pte_index(va)];
2196 npte = opte = *ptep; 2196 npte = opte = *ptep;
2197 2197
2198 NPDEBUG(PDB_BITS, 2198 NPDEBUG(PDB_BITS,
2199 printf( 2199 printf(
2200 "pmap_clearbit: pv %p, pm %p, va 0x%08lx, flag 0x%x\n", 2200 "pmap_clearbit: pv %p, pm %p, va 0x%08lx, flag 0x%x\n",
2201 pv, pv->pv_pmap, pv->pv_va, oflags)); 2201 pv, pv->pv_pmap, pv->pv_va, oflags));
2202 2202
2203 if (maskbits & (PVF_WRITE|PVF_MOD)) { 2203 if (maskbits & (PVF_WRITE|PVF_MOD)) {
2204#ifdef PMAP_CACHE_VIVT 2204#ifdef PMAP_CACHE_VIVT
2205 if ((pv->pv_flags & PVF_NC)) { 2205 if ((pv->pv_flags & PVF_NC)) {
2206 /*  2206 /*
2207 * Entry is not cacheable: 2207 * Entry is not cacheable:
2208 * 2208 *
2209 * Don't turn caching on again if this is a  2209 * Don't turn caching on again if this is a
2210 * modified emulation. This would be 2210 * modified emulation. This would be
2211 * inconsitent with the settings created by 2211 * inconsitent with the settings created by
2212 * pmap_vac_me_harder(). Otherwise, it's safe 2212 * pmap_vac_me_harder(). Otherwise, it's safe
2213 * to re-enable cacheing. 2213 * to re-enable cacheing.
2214 * 2214 *
2215 * There's no need to call pmap_vac_me_harder() 2215 * There's no need to call pmap_vac_me_harder()
2216 * here: all pages are losing their write 2216 * here: all pages are losing their write
2217 * permission. 2217 * permission.
2218 */ 2218 */
2219 if (maskbits & PVF_WRITE) { 2219 if (maskbits & PVF_WRITE) {
2220 npte |= pte_l2_s_cache_mode; 2220 npte |= pte_l2_s_cache_mode;
2221 pv->pv_flags &= ~PVF_NC; 2221 pv->pv_flags &= ~PVF_NC;
2222 } 2222 }
2223 } else 2223 } else
2224 if (l2pte_writable_p(opte)) { 2224 if (l2pte_writable_p(opte)) {
2225 /*  2225 /*
2226 * Entry is writable/cacheable: check if pmap 2226 * Entry is writable/cacheable: check if pmap
2227 * is current if it is flush it, otherwise it 2227 * is current if it is flush it, otherwise it
2228 * won't be in the cache 2228 * won't be in the cache
2229 */ 2229 */
2230 if (PV_BEEN_EXECD(oflags)) 2230 if (PV_BEEN_EXECD(oflags))
2231 pmap_idcache_wbinv_range(pm, pv->pv_va, 2231 pmap_idcache_wbinv_range(pm, pv->pv_va,
2232 PAGE_SIZE); 2232 PAGE_SIZE);
2233 else 2233 else
2234 if (PV_BEEN_REFD(oflags)) 2234 if (PV_BEEN_REFD(oflags))
2235 pmap_dcache_wb_range(pm, pv->pv_va, 2235 pmap_dcache_wb_range(pm, pv->pv_va,
2236 PAGE_SIZE, 2236 PAGE_SIZE,
2237 (maskbits & PVF_REF) != 0, false); 2237 (maskbits & PVF_REF) != 0, false);
2238 } 2238 }
2239#endif 2239#endif
2240 2240
2241 /* make the pte read only */ 2241 /* make the pte read only */
2242 npte = l2pte_set_readonly(npte); 2242 npte = l2pte_set_readonly(npte);
2243 2243
2244 if (maskbits & oflags & PVF_WRITE) { 2244 if (maskbits & oflags & PVF_WRITE) {
2245 /* 2245 /*
2246 * Keep alias accounting up to date 2246 * Keep alias accounting up to date
2247 */ 2247 */
2248 if (pv->pv_pmap == pmap_kernel()) { 2248 if (pv->pv_pmap == pmap_kernel()) {
2249 md->krw_mappings--; 2249 md->krw_mappings--;
2250 md->kro_mappings++; 2250 md->kro_mappings++;
2251 } else { 2251 } else {
2252 md->urw_mappings--; 2252 md->urw_mappings--;
2253 md->uro_mappings++; 2253 md->uro_mappings++;
2254 } 2254 }
2255#ifdef PMAP_CACHE_VIPT 2255#ifdef PMAP_CACHE_VIPT
2256 if (md->urw_mappings + md->krw_mappings == 0) 2256 if (md->urw_mappings + md->krw_mappings == 0)
2257 md->pvh_attrs &= ~PVF_WRITE; 2257 md->pvh_attrs &= ~PVF_WRITE;
2258 if (want_syncicache) 2258 if (want_syncicache)
2259 need_syncicache = true; 2259 need_syncicache = true;
2260 need_vac_me_harder = true; 2260 need_vac_me_harder = true;
2261#endif 2261#endif
2262 } 2262 }
2263 } 2263 }
2264 2264
2265 if (maskbits & PVF_REF) { 2265 if (maskbits & PVF_REF) {
2266 if ((pv->pv_flags & PVF_NC) == 0 && 2266 if ((pv->pv_flags & PVF_NC) == 0 &&
2267 (maskbits & (PVF_WRITE|PVF_MOD)) == 0 && 2267 (maskbits & (PVF_WRITE|PVF_MOD)) == 0 &&
2268 l2pte_valid(npte)) { 2268 l2pte_valid(npte)) {
2269#ifdef PMAP_CACHE_VIVT 2269#ifdef PMAP_CACHE_VIVT
2270 /* 2270 /*
2271 * Check npte here; we may have already 2271 * Check npte here; we may have already
2272 * done the wbinv above, and the validity 2272 * done the wbinv above, and the validity
2273 * of the PTE is the same for opte and 2273 * of the PTE is the same for opte and
2274 * npte. 2274 * npte.
2275 */ 2275 */
2276 /* XXXJRT need idcache_inv_range */ 2276 /* XXXJRT need idcache_inv_range */
2277 if (PV_BEEN_EXECD(oflags)) 2277 if (PV_BEEN_EXECD(oflags))
2278 pmap_idcache_wbinv_range(pm, 2278 pmap_idcache_wbinv_range(pm,
2279 pv->pv_va, PAGE_SIZE); 2279 pv->pv_va, PAGE_SIZE);
2280 else 2280 else
2281 if (PV_BEEN_REFD(oflags)) 2281 if (PV_BEEN_REFD(oflags))
2282 pmap_dcache_wb_range(pm, 2282 pmap_dcache_wb_range(pm,
2283 pv->pv_va, PAGE_SIZE, 2283 pv->pv_va, PAGE_SIZE,
2284 true, true); 2284 true, true);
2285#endif 2285#endif
2286 } 2286 }
2287 2287
2288 /* 2288 /*
2289 * Make the PTE invalid so that we will take a 2289 * Make the PTE invalid so that we will take a
2290 * page fault the next time the mapping is 2290 * page fault the next time the mapping is
2291 * referenced. 2291 * referenced.
2292 */ 2292 */
2293 npte &= ~L2_TYPE_MASK; 2293 npte &= ~L2_TYPE_MASK;
2294 npte |= L2_TYPE_INV; 2294 npte |= L2_TYPE_INV;
2295 } 2295 }
2296 2296
2297 if (npte != opte) { 2297 if (npte != opte) {
2298 *ptep = npte; 2298 *ptep = npte;
2299 PTE_SYNC(ptep); 2299 PTE_SYNC(ptep);
2300 /* Flush the TLB entry if a current pmap. */ 2300 /* Flush the TLB entry if a current pmap. */
2301 if (PV_BEEN_EXECD(oflags)) 2301 if (PV_BEEN_EXECD(oflags))
2302 pmap_tlb_flushID_SE(pm, pv->pv_va); 2302 pmap_tlb_flushID_SE(pm, pv->pv_va);
2303 else 2303 else
2304 if (PV_BEEN_REFD(oflags)) 2304 if (PV_BEEN_REFD(oflags))
2305 pmap_tlb_flushD_SE(pm, pv->pv_va); 2305 pmap_tlb_flushD_SE(pm, pv->pv_va);
2306 } 2306 }
2307 2307
2308 pmap_release_pmap_lock(pm); 2308 pmap_release_pmap_lock(pm);
2309 2309
2310 NPDEBUG(PDB_BITS, 2310 NPDEBUG(PDB_BITS,
2311 printf("pmap_clearbit: pm %p va 0x%lx opte 0x%08x npte 0x%08x\n", 2311 printf("pmap_clearbit: pm %p va 0x%lx opte 0x%08x npte 0x%08x\n",
2312 pm, va, opte, npte)); 2312 pm, va, opte, npte));
2313 } 2313 }
2314 2314
2315#ifdef PMAP_CACHE_VIPT 2315#ifdef PMAP_CACHE_VIPT
2316 /* 2316 /*
2317 * If we need to sync the I-cache and we haven't done it yet, do it. 2317 * If we need to sync the I-cache and we haven't done it yet, do it.
2318 */ 2318 */
2319 if (need_syncicache && !did_syncicache) { 2319 if (need_syncicache && !did_syncicache) {
2320 pmap_syncicache_page(md, pa); 2320 pmap_syncicache_page(md, pa);
2321 PMAPCOUNT(exec_synced_clearbit); 2321 PMAPCOUNT(exec_synced_clearbit);
2322 } 2322 }
2323 /* 2323 /*
2324 * If we are changing this to read-only, we need to call vac_me_harder 2324 * If we are changing this to read-only, we need to call vac_me_harder
2325 * so we can change all the read-only pages to cacheable. We pretend 2325 * so we can change all the read-only pages to cacheable. We pretend
2326 * this as a page deletion. 2326 * this as a page deletion.
2327 */ 2327 */
2328 if (need_vac_me_harder) { 2328 if (need_vac_me_harder) {
2329 if (md->pvh_attrs & PVF_NC) 2329 if (md->pvh_attrs & PVF_NC)
2330 pmap_vac_me_harder(md, pa, NULL, 0); 2330 pmap_vac_me_harder(md, pa, NULL, 0);
2331 } 2331 }
2332#endif 2332#endif
2333 2333
2334 simple_unlock(&md->pvh_slock); 2334 simple_unlock(&md->pvh_slock);
2335 PMAP_HEAD_TO_MAP_UNLOCK(); 2335 PMAP_HEAD_TO_MAP_UNLOCK();
2336} 2336}
2337 2337
2338/* 2338/*
2339 * pmap_clean_page() 2339 * pmap_clean_page()
2340 * 2340 *
2341 * This is a local function used to work out the best strategy to clean 2341 * This is a local function used to work out the best strategy to clean
2342 * a single page referenced by its entry in the PV table. It's used by 2342 * a single page referenced by its entry in the PV table. It's used by
2343 * pmap_copy_page, pmap_zero page and maybe some others later on. 2343 * pmap_copy_page, pmap_zero page and maybe some others later on.
2344 * 2344 *
2345 * Its policy is effectively: 2345 * Its policy is effectively:
2346 * o If there are no mappings, we don't bother doing anything with the cache. 2346 * o If there are no mappings, we don't bother doing anything with the cache.
2347 * o If there is one mapping, we clean just that page. 2347 * o If there is one mapping, we clean just that page.
2348 * o If there are multiple mappings, we clean the entire cache. 2348 * o If there are multiple mappings, we clean the entire cache.
2349 * 2349 *
2350 * So that some functions can be further optimised, it returns 0 if it didn't 2350 * So that some functions can be further optimised, it returns 0 if it didn't
2351 * clean the entire cache, or 1 if it did. 2351 * clean the entire cache, or 1 if it did.
2352 * 2352 *
2353 * XXX One bug in this routine is that if the pv_entry has a single page 2353 * XXX One bug in this routine is that if the pv_entry has a single page
2354 * mapped at 0x00000000 a whole cache clean will be performed rather than 2354 * mapped at 0x00000000 a whole cache clean will be performed rather than
2355 * just the 1 page. Since this should not occur in everyday use and if it does 2355 * just the 1 page. Since this should not occur in everyday use and if it does
2356 * it will just result in not the most efficient clean for the page. 2356 * it will just result in not the most efficient clean for the page.
2357 */ 2357 */
2358#ifdef PMAP_CACHE_VIVT 2358#ifdef PMAP_CACHE_VIVT
2359static int 2359static int
2360pmap_clean_page(struct pv_entry *pv, bool is_src) 2360pmap_clean_page(struct pv_entry *pv, bool is_src)
2361{ 2361{
2362 pmap_t pm_to_clean = NULL; 2362 pmap_t pm_to_clean = NULL;
2363 struct pv_entry *npv; 2363 struct pv_entry *npv;
2364 u_int cache_needs_cleaning = 0; 2364 u_int cache_needs_cleaning = 0;
2365 u_int flags = 0; 2365 u_int flags = 0;
2366 vaddr_t page_to_clean = 0; 2366 vaddr_t page_to_clean = 0;
2367 2367
2368 if (pv == NULL) { 2368 if (pv == NULL) {
2369 /* nothing mapped in so nothing to flush */ 2369 /* nothing mapped in so nothing to flush */
2370 return (0); 2370 return (0);
2371 } 2371 }
2372 2372
2373 /* 2373 /*
2374 * Since we flush the cache each time we change to a different 2374 * Since we flush the cache each time we change to a different
2375 * user vmspace, we only need to flush the page if it is in the 2375 * user vmspace, we only need to flush the page if it is in the
2376 * current pmap. 2376 * current pmap.
2377 */ 2377 */
2378 2378
2379 for (npv = pv; npv; npv = SLIST_NEXT(npv, pv_link)) { 2379 for (npv = pv; npv; npv = SLIST_NEXT(npv, pv_link)) {
2380 if (pmap_is_current(npv->pv_pmap)) { 2380 if (pmap_is_current(npv->pv_pmap)) {
2381 flags |= npv->pv_flags; 2381 flags |= npv->pv_flags;
2382 /* 2382 /*
2383 * The page is mapped non-cacheable in  2383 * The page is mapped non-cacheable in
2384 * this map. No need to flush the cache. 2384 * this map. No need to flush the cache.
2385 */ 2385 */
2386 if (npv->pv_flags & PVF_NC) { 2386 if (npv->pv_flags & PVF_NC) {
2387#ifdef DIAGNOSTIC 2387#ifdef DIAGNOSTIC
2388 if (cache_needs_cleaning) 2388 if (cache_needs_cleaning)
2389 panic("pmap_clean_page: " 2389 panic("pmap_clean_page: "
2390 "cache inconsistency"); 2390 "cache inconsistency");
2391#endif 2391#endif
2392 break; 2392 break;
2393 } else if (is_src && (npv->pv_flags & PVF_WRITE) == 0) 2393 } else if (is_src && (npv->pv_flags & PVF_WRITE) == 0)
2394 continue; 2394 continue;
2395 if (cache_needs_cleaning) { 2395 if (cache_needs_cleaning) {
2396 page_to_clean = 0; 2396 page_to_clean = 0;
2397 break; 2397 break;
2398 } else { 2398 } else {
2399 page_to_clean = npv->pv_va; 2399 page_to_clean = npv->pv_va;
2400 pm_to_clean = npv->pv_pmap; 2400 pm_to_clean = npv->pv_pmap;
2401 } 2401 }
2402 cache_needs_cleaning = 1; 2402 cache_needs_cleaning = 1;
2403 } 2403 }
2404 } 2404 }
2405 2405
2406 if (page_to_clean) { 2406 if (page_to_clean) {
2407 if (PV_BEEN_EXECD(flags)) 2407 if (PV_BEEN_EXECD(flags))
2408 pmap_idcache_wbinv_range(pm_to_clean, page_to_clean, 2408 pmap_idcache_wbinv_range(pm_to_clean, page_to_clean,
2409 PAGE_SIZE); 2409 PAGE_SIZE);
2410 else 2410 else
2411 pmap_dcache_wb_range(pm_to_clean, page_to_clean, 2411 pmap_dcache_wb_range(pm_to_clean, page_to_clean,
2412 PAGE_SIZE, !is_src, (flags & PVF_WRITE) == 0); 2412 PAGE_SIZE, !is_src, (flags & PVF_WRITE) == 0);
2413 } else if (cache_needs_cleaning) { 2413 } else if (cache_needs_cleaning) {
2414 pmap_t const pm = curproc->p_vmspace->vm_map.pmap; 2414 pmap_t const pm = curproc->p_vmspace->vm_map.pmap;
2415 2415
2416 if (PV_BEEN_EXECD(flags)) 2416 if (PV_BEEN_EXECD(flags))
2417 pmap_idcache_wbinv_all(pm); 2417 pmap_idcache_wbinv_all(pm);
2418 else 2418 else
2419 pmap_dcache_wbinv_all(pm); 2419 pmap_dcache_wbinv_all(pm);
2420 return (1); 2420 return (1);
2421 } 2421 }
2422 return (0); 2422 return (0);
2423} 2423}
2424#endif 2424#endif
2425 2425
2426#ifdef PMAP_CACHE_VIPT 2426#ifdef PMAP_CACHE_VIPT
2427/* 2427/*
2428 * Sync a page with the I-cache. Since this is a VIPT, we must pick the 2428 * Sync a page with the I-cache. Since this is a VIPT, we must pick the
2429 * right cache alias to make sure we flush the right stuff. 2429 * right cache alias to make sure we flush the right stuff.
2430 */ 2430 */
2431void 2431void
2432pmap_syncicache_page(struct vm_page_md *md, paddr_t pa) 2432pmap_syncicache_page(struct vm_page_md *md, paddr_t pa)
2433{ 2433{
2434 const vsize_t va_offset = md->pvh_attrs & arm_cache_prefer_mask; 2434 const vsize_t va_offset = md->pvh_attrs & arm_cache_prefer_mask;
2435 pt_entry_t * const ptep = &cdst_pte[va_offset >> PGSHIFT]; 2435 pt_entry_t * const ptep = &cdst_pte[va_offset >> PGSHIFT];
2436 2436
2437 NPDEBUG(PDB_EXEC, printf("pmap_syncicache_page: md=%p (attrs=%#x)\n", 2437 NPDEBUG(PDB_EXEC, printf("pmap_syncicache_page: md=%p (attrs=%#x)\n",
2438 md, md->pvh_attrs)); 2438 md, md->pvh_attrs));
2439 /* 2439 /*
2440 * No need to clean the page if it's non-cached. 2440 * No need to clean the page if it's non-cached.
2441 */ 2441 */
2442 if (md->pvh_attrs & PVF_NC) 2442 if (md->pvh_attrs & PVF_NC)
2443 return; 2443 return;
2444 KASSERT(arm_cache_prefer_mask == 0 || md->pvh_attrs & PVF_COLORED); 2444 KASSERT(arm_cache_prefer_mask == 0 || md->pvh_attrs & PVF_COLORED);
2445 2445
2446 pmap_tlb_flushID_SE(pmap_kernel(), cdstp + va_offset); 2446 pmap_tlb_flushID_SE(pmap_kernel(), cdstp + va_offset);
2447 /* 2447 /*
2448 * Set up a PTE with the right coloring to flush existing cache lines. 2448 * Set up a PTE with the right coloring to flush existing cache lines.
2449 */ 2449 */
2450 *ptep = L2_S_PROTO | 2450 *ptep = L2_S_PROTO |
2451 pa 2451 pa
2452 | L2_S_PROT(PTE_KERNEL, VM_PROT_READ|VM_PROT_WRITE) 2452 | L2_S_PROT(PTE_KERNEL, VM_PROT_READ|VM_PROT_WRITE)
2453 | pte_l2_s_cache_mode; 2453 | pte_l2_s_cache_mode;
2454 PTE_SYNC(ptep); 2454 PTE_SYNC(ptep);
2455 2455
2456 /* 2456 /*
2457 * Flush it. 2457 * Flush it.
2458 */ 2458 */
2459 cpu_icache_sync_range(cdstp + va_offset, PAGE_SIZE); 2459 cpu_icache_sync_range(cdstp + va_offset, PAGE_SIZE);
2460 /* 2460 /*
2461 * Unmap the page. 2461 * Unmap the page.
2462 */ 2462 */
2463 *ptep = 0; 2463 *ptep = 0;
2464 PTE_SYNC(ptep); 2464 PTE_SYNC(ptep);
2465 pmap_tlb_flushID_SE(pmap_kernel(), cdstp + va_offset); 2465 pmap_tlb_flushID_SE(pmap_kernel(), cdstp + va_offset);
2466 2466
2467 md->pvh_attrs |= PVF_EXEC; 2467 md->pvh_attrs |= PVF_EXEC;
2468 PMAPCOUNT(exec_synced); 2468 PMAPCOUNT(exec_synced);
2469} 2469}
2470 2470
2471void 2471void
2472pmap_flush_page(struct vm_page_md *md, paddr_t pa, enum pmap_flush_op flush) 2472pmap_flush_page(struct vm_page_md *md, paddr_t pa, enum pmap_flush_op flush)
2473{ 2473{
2474 vsize_t va_offset, end_va; 2474 vsize_t va_offset, end_va;
2475 void (*cf)(vaddr_t, vsize_t); 2475 void (*cf)(vaddr_t, vsize_t);
2476 2476
2477 if (arm_cache_prefer_mask == 0) 2477 if (arm_cache_prefer_mask == 0)
2478 return; 2478 return;
2479 2479
2480 switch (flush) { 2480 switch (flush) {
2481 case PMAP_FLUSH_PRIMARY: 2481 case PMAP_FLUSH_PRIMARY:
2482 if (md->pvh_attrs & PVF_MULTCLR) { 2482 if (md->pvh_attrs & PVF_MULTCLR) {
2483 va_offset = 0; 2483 va_offset = 0;
2484 end_va = arm_cache_prefer_mask; 2484 end_va = arm_cache_prefer_mask;
2485 md->pvh_attrs &= ~PVF_MULTCLR; 2485 md->pvh_attrs &= ~PVF_MULTCLR;
2486 PMAPCOUNT(vac_flush_lots); 2486 PMAPCOUNT(vac_flush_lots);
2487 } else { 2487 } else {
2488 va_offset = md->pvh_attrs & arm_cache_prefer_mask; 2488 va_offset = md->pvh_attrs & arm_cache_prefer_mask;
2489 end_va = va_offset; 2489 end_va = va_offset;
2490 PMAPCOUNT(vac_flush_one); 2490 PMAPCOUNT(vac_flush_one);
2491 } 2491 }
2492 /* 2492 /*
2493 * Mark that the page is no longer dirty. 2493 * Mark that the page is no longer dirty.
2494 */ 2494 */
2495 md->pvh_attrs &= ~PVF_DIRTY; 2495 md->pvh_attrs &= ~PVF_DIRTY;
2496 cf = cpufuncs.cf_idcache_wbinv_range; 2496 cf = cpufuncs.cf_idcache_wbinv_range;
2497 break; 2497 break;
2498 case PMAP_FLUSH_SECONDARY: 2498 case PMAP_FLUSH_SECONDARY:
2499 va_offset = 0; 2499 va_offset = 0;
2500 end_va = arm_cache_prefer_mask; 2500 end_va = arm_cache_prefer_mask;
2501 cf = cpufuncs.cf_idcache_wbinv_range; 2501 cf = cpufuncs.cf_idcache_wbinv_range;
2502 md->pvh_attrs &= ~PVF_MULTCLR; 2502 md->pvh_attrs &= ~PVF_MULTCLR;
2503 PMAPCOUNT(vac_flush_lots); 2503 PMAPCOUNT(vac_flush_lots);
2504 break; 2504 break;
2505 case PMAP_CLEAN_PRIMARY: 2505 case PMAP_CLEAN_PRIMARY:
2506 va_offset = md->pvh_attrs & arm_cache_prefer_mask; 2506 va_offset = md->pvh_attrs & arm_cache_prefer_mask;
2507 end_va = va_offset; 2507 end_va = va_offset;
2508 cf = cpufuncs.cf_dcache_wb_range; 2508 cf = cpufuncs.cf_dcache_wb_range;
2509 /* 2509 /*
2510 * Mark that the page is no longer dirty. 2510 * Mark that the page is no longer dirty.
2511 */ 2511 */
2512 if ((md->pvh_attrs & PVF_DMOD) == 0) 2512 if ((md->pvh_attrs & PVF_DMOD) == 0)
2513 md->pvh_attrs &= ~PVF_DIRTY; 2513 md->pvh_attrs &= ~PVF_DIRTY;
2514 PMAPCOUNT(vac_clean_one); 2514 PMAPCOUNT(vac_clean_one);
2515 break; 2515 break;
2516 default: 2516 default:
2517 return; 2517 return;
2518 } 2518 }
2519 2519
2520 KASSERT(!(md->pvh_attrs & PVF_NC)); 2520 KASSERT(!(md->pvh_attrs & PVF_NC));
2521 2521
2522 NPDEBUG(PDB_VAC, printf("pmap_flush_page: md=%p (attrs=%#x)\n", 2522 NPDEBUG(PDB_VAC, printf("pmap_flush_page: md=%p (attrs=%#x)\n",
2523 md, md->pvh_attrs)); 2523 md, md->pvh_attrs));
2524 2524
2525 for (; va_offset <= end_va; va_offset += PAGE_SIZE) { 2525 for (; va_offset <= end_va; va_offset += PAGE_SIZE) {
2526 const size_t pte_offset = va_offset >> PGSHIFT; 2526 const size_t pte_offset = va_offset >> PGSHIFT;
2527 pt_entry_t * const ptep = &cdst_pte[pte_offset]; 2527 pt_entry_t * const ptep = &cdst_pte[pte_offset];
2528 const pt_entry_t oldpte = *ptep; 2528 const pt_entry_t oldpte = *ptep;
2529 2529
2530 if (flush == PMAP_FLUSH_SECONDARY 2530 if (flush == PMAP_FLUSH_SECONDARY
2531 && va_offset == (md->pvh_attrs & arm_cache_prefer_mask)) 2531 && va_offset == (md->pvh_attrs & arm_cache_prefer_mask))
2532 continue; 2532 continue;
2533 2533
2534 pmap_tlb_flushID_SE(pmap_kernel(), cdstp + va_offset); 2534 pmap_tlb_flushID_SE(pmap_kernel(), cdstp + va_offset);
2535 /* 2535 /*
2536 * Set up a PTE with the right coloring to flush 2536 * Set up a PTE with the right coloring to flush
2537 * existing cache entries. 2537 * existing cache entries.
2538 */ 2538 */
2539 *ptep = L2_S_PROTO 2539 *ptep = L2_S_PROTO
2540 | pa 2540 | pa
2541 | L2_S_PROT(PTE_KERNEL, VM_PROT_READ|VM_PROT_WRITE) 2541 | L2_S_PROT(PTE_KERNEL, VM_PROT_READ|VM_PROT_WRITE)
2542 | pte_l2_s_cache_mode; 2542 | pte_l2_s_cache_mode;
2543 PTE_SYNC(ptep); 2543 PTE_SYNC(ptep);
2544 2544
2545 /* 2545 /*
2546 * Flush it. 2546 * Flush it.
2547 */ 2547 */
2548 (*cf)(cdstp + va_offset, PAGE_SIZE); 2548 (*cf)(cdstp + va_offset, PAGE_SIZE);
2549 2549
2550 /* 2550 /*
2551 * Restore the page table entry since we might have interrupted 2551 * Restore the page table entry since we might have interrupted
2552 * pmap_zero_page or pmap_copy_page which was already using 2552 * pmap_zero_page or pmap_copy_page which was already using
2553 * this pte. 2553 * this pte.
2554 */ 2554 */
2555 *ptep = oldpte; 2555 *ptep = oldpte;
2556 PTE_SYNC(ptep); 2556 PTE_SYNC(ptep);
2557 pmap_tlb_flushID_SE(pmap_kernel(), cdstp + va_offset); 2557 pmap_tlb_flushID_SE(pmap_kernel(), cdstp + va_offset);
2558 } 2558 }
2559} 2559}
2560#endif /* PMAP_CACHE_VIPT */ 2560#endif /* PMAP_CACHE_VIPT */
2561 2561
2562/* 2562/*
2563 * Routine: pmap_page_remove 2563 * Routine: pmap_page_remove
2564 * Function: 2564 * Function:
2565 * Removes this physical page from 2565 * Removes this physical page from
2566 * all physical maps in which it resides. 2566 * all physical maps in which it resides.
2567 * Reflects back modify bits to the pager. 2567 * Reflects back modify bits to the pager.
2568 */ 2568 */
2569static void 2569static void
2570pmap_page_remove(struct vm_page_md *md, paddr_t pa) 2570pmap_page_remove(struct vm_page_md *md, paddr_t pa)
2571{ 2571{
2572 struct l2_bucket *l2b; 2572 struct l2_bucket *l2b;
2573 struct pv_entry *pv, *npv, **pvp; 2573 struct pv_entry *pv, *npv, **pvp;
2574 pmap_t pm; 2574 pmap_t pm;
2575 pt_entry_t *ptep; 2575 pt_entry_t *ptep;
2576 bool flush; 2576 bool flush;
2577 u_int flags; 2577 u_int flags;
2578 2578
2579 NPDEBUG(PDB_FOLLOW, 2579 NPDEBUG(PDB_FOLLOW,
2580 printf("pmap_page_remove: pg %p (0x%08lx)\n", pg, 2580 printf("pmap_page_remove: md %p (0x%08lx)\n", md,
2581 pa)); 2581 pa));
2582 2582
2583 PMAP_HEAD_TO_MAP_LOCK(); 2583 PMAP_HEAD_TO_MAP_LOCK();
2584 simple_lock(&md->pvh_slock); 2584 simple_lock(&md->pvh_slock);
2585 2585
2586 pv = SLIST_FIRST(&md->pvh_list); 2586 pv = SLIST_FIRST(&md->pvh_list);
2587 if (pv == NULL) { 2587 if (pv == NULL) {
2588#ifdef PMAP_CACHE_VIPT 2588#ifdef PMAP_CACHE_VIPT
2589 /* 2589 /*
2590 * We *know* the page contents are about to be replaced. 2590 * We *know* the page contents are about to be replaced.
2591 * Discard the exec contents 2591 * Discard the exec contents
2592 */ 2592 */
2593 if (PV_IS_EXEC_P(md->pvh_attrs)) 2593 if (PV_IS_EXEC_P(md->pvh_attrs))
2594 PMAPCOUNT(exec_discarded_page_protect); 2594 PMAPCOUNT(exec_discarded_page_protect);
2595 md->pvh_attrs &= ~PVF_EXEC; 2595 md->pvh_attrs &= ~PVF_EXEC;
2596 KASSERT((md->urw_mappings + md->krw_mappings == 0) == !(md->pvh_attrs & PVF_WRITE)); 2596 KASSERT((md->urw_mappings + md->krw_mappings == 0) == !(md->pvh_attrs & PVF_WRITE));
2597#endif 2597#endif
2598 simple_unlock(&md->pvh_slock); 2598 simple_unlock(&md->pvh_slock);
2599 PMAP_HEAD_TO_MAP_UNLOCK(); 2599 PMAP_HEAD_TO_MAP_UNLOCK();
2600 return; 2600 return;
2601 } 2601 }
2602#ifdef PMAP_CACHE_VIPT 2602#ifdef PMAP_CACHE_VIPT
2603 KASSERT(arm_cache_prefer_mask == 0 || pmap_is_page_colored_p(md)); 2603 KASSERT(arm_cache_prefer_mask == 0 || pmap_is_page_colored_p(md));
2604#endif 2604#endif
2605 2605
2606 /* 2606 /*
2607 * Clear alias counts 2607 * Clear alias counts
2608 */ 2608 */
2609#ifdef PMAP_CACHE_VIVT 2609#ifdef PMAP_CACHE_VIVT
2610 md->k_mappings = 0; 2610 md->k_mappings = 0;
2611#endif 2611#endif
2612 md->urw_mappings = md->uro_mappings = 0; 2612 md->urw_mappings = md->uro_mappings = 0;
2613 2613
2614 flush = false; 2614 flush = false;
2615 flags = 0; 2615 flags = 0;
2616 2616
2617#ifdef PMAP_CACHE_VIVT 2617#ifdef PMAP_CACHE_VIVT
2618 pmap_clean_page(pv, false); 2618 pmap_clean_page(pv, false);
2619#endif 2619#endif
2620 2620
2621 pvp = &SLIST_FIRST(&md->pvh_list); 2621 pvp = &SLIST_FIRST(&md->pvh_list);
2622 while (pv) { 2622 while (pv) {
2623 pm = pv->pv_pmap; 2623 pm = pv->pv_pmap;
2624 npv = SLIST_NEXT(pv, pv_link); 2624 npv = SLIST_NEXT(pv, pv_link);
2625 if (flush == false && pmap_is_current(pm)) 2625 if (flush == false && pmap_is_current(pm))
2626 flush = true; 2626 flush = true;
2627 2627
2628 if (pm == pmap_kernel()) { 2628 if (pm == pmap_kernel()) {
2629#ifdef PMAP_CACHE_VIPT 2629#ifdef PMAP_CACHE_VIPT
2630 /* 2630 /*
2631 * If this was unmanaged mapping, it must be preserved. 2631 * If this was unmanaged mapping, it must be preserved.
2632 * Move it back on the list and advance the end-of-list 2632 * Move it back on the list and advance the end-of-list
2633 * pointer. 2633 * pointer.
2634 */ 2634 */
2635 if (pv->pv_flags & PVF_KENTRY) { 2635 if (pv->pv_flags & PVF_KENTRY) {
2636 *pvp = pv; 2636 *pvp = pv;
2637 pvp = &SLIST_NEXT(pv, pv_link); 2637 pvp = &SLIST_NEXT(pv, pv_link);
2638 pv = npv; 2638 pv = npv;
2639 continue; 2639 continue;
2640 } 2640 }
2641 if (pv->pv_flags & PVF_WRITE) 2641 if (pv->pv_flags & PVF_WRITE)
2642 md->krw_mappings--; 2642 md->krw_mappings--;
2643 else 2643 else
2644 md->kro_mappings--; 2644 md->kro_mappings--;
2645#endif 2645#endif
2646 PMAPCOUNT(kernel_unmappings); 2646 PMAPCOUNT(kernel_unmappings);
2647 } 2647 }
2648 PMAPCOUNT(unmappings); 2648 PMAPCOUNT(unmappings);
2649 2649
2650 pmap_acquire_pmap_lock(pm); 2650 pmap_acquire_pmap_lock(pm);
2651 2651
2652 l2b = pmap_get_l2_bucket(pm, pv->pv_va); 2652 l2b = pmap_get_l2_bucket(pm, pv->pv_va);
2653 KDASSERT(l2b != NULL); 2653 KDASSERT(l2b != NULL);
2654 2654
2655 ptep = &l2b->l2b_kva[l2pte_index(pv->pv_va)]; 2655 ptep = &l2b->l2b_kva[l2pte_index(pv->pv_va)];
2656 2656
2657 /* 2657 /*
2658 * Update statistics 2658 * Update statistics
2659 */ 2659 */
2660 --pm->pm_stats.resident_count; 2660 --pm->pm_stats.resident_count;
2661 2661
2662 /* Wired bit */ 2662 /* Wired bit */
2663 if (pv->pv_flags & PVF_WIRED) 2663 if (pv->pv_flags & PVF_WIRED)
2664 --pm->pm_stats.wired_count; 2664 --pm->pm_stats.wired_count;
2665 2665
2666 flags |= pv->pv_flags; 2666 flags |= pv->pv_flags;
2667 2667
2668 /* 2668 /*
2669 * Invalidate the PTEs. 2669 * Invalidate the PTEs.
2670 */ 2670 */
2671 *ptep = 0; 2671 *ptep = 0;
2672 PTE_SYNC_CURRENT(pm, ptep); 2672 PTE_SYNC_CURRENT(pm, ptep);
2673 pmap_free_l2_bucket(pm, l2b, 1); 2673 pmap_free_l2_bucket(pm, l2b, 1);
2674 2674
2675 pool_put(&pmap_pv_pool, pv); 2675 pool_put(&pmap_pv_pool, pv);
2676 pv = npv; 2676 pv = npv;
2677 /* 2677 /*
2678 * if we reach the end of the list and there are still 2678 * if we reach the end of the list and there are still
2679 * mappings, they might be able to be cached now. 2679 * mappings, they might be able to be cached now.
2680 */ 2680 */
2681 if (pv == NULL) { 2681 if (pv == NULL) {
2682 *pvp = NULL; 2682 *pvp = NULL;
2683 if (!SLIST_EMPTY(&md->pvh_list)) 2683 if (!SLIST_EMPTY(&md->pvh_list))
2684 pmap_vac_me_harder(md, pa, pm, 0); 2684 pmap_vac_me_harder(md, pa, pm, 0);
2685 } 2685 }
2686 pmap_release_pmap_lock(pm); 2686 pmap_release_pmap_lock(pm);
2687 } 2687 }
2688#ifdef PMAP_CACHE_VIPT 2688#ifdef PMAP_CACHE_VIPT
2689 /* 2689 /*
2690 * Its EXEC cache is now gone. 2690 * Its EXEC cache is now gone.
2691 */ 2691 */
2692 if (PV_IS_EXEC_P(md->pvh_attrs)) 2692 if (PV_IS_EXEC_P(md->pvh_attrs))
2693 PMAPCOUNT(exec_discarded_page_protect); 2693 PMAPCOUNT(exec_discarded_page_protect);
2694 md->pvh_attrs &= ~PVF_EXEC; 2694 md->pvh_attrs &= ~PVF_EXEC;
2695 KASSERT(md->urw_mappings == 0); 2695 KASSERT(md->urw_mappings == 0);
2696 KASSERT(md->uro_mappings == 0); 2696 KASSERT(md->uro_mappings == 0);
2697 if (md->krw_mappings == 0) 2697 if (md->krw_mappings == 0)
2698 md->pvh_attrs &= ~PVF_WRITE; 2698 md->pvh_attrs &= ~PVF_WRITE;
2699 KASSERT((md->urw_mappings + md->krw_mappings == 0) == !(md->pvh_attrs & PVF_WRITE)); 2699 KASSERT((md->urw_mappings + md->krw_mappings == 0) == !(md->pvh_attrs & PVF_WRITE));
2700#endif 2700#endif
2701 simple_unlock(&md->pvh_slock); 2701 simple_unlock(&md->pvh_slock);
2702 PMAP_HEAD_TO_MAP_UNLOCK(); 2702 PMAP_HEAD_TO_MAP_UNLOCK();
2703 2703
2704 if (flush) { 2704 if (flush) {
2705 /* 2705 /*
2706 * Note: We can't use pmap_tlb_flush{I,D}() here since that 2706 * Note: We can't use pmap_tlb_flush{I,D}() here since that
2707 * would need a subsequent call to pmap_update() to ensure 2707 * would need a subsequent call to pmap_update() to ensure
2708 * curpm->pm_cstate.cs_all is reset. Our callers are not 2708 * curpm->pm_cstate.cs_all is reset. Our callers are not
2709 * required to do that (see pmap(9)), so we can't modify 2709 * required to do that (see pmap(9)), so we can't modify
2710 * the current pmap's state. 2710 * the current pmap's state.
2711 */ 2711 */
2712 if (PV_BEEN_EXECD(flags)) 2712 if (PV_BEEN_EXECD(flags))
2713 cpu_tlb_flushID(); 2713 cpu_tlb_flushID();
2714 else 2714 else
2715 cpu_tlb_flushD(); 2715 cpu_tlb_flushD();
2716 } 2716 }
2717 cpu_cpwait(); 2717 cpu_cpwait();
2718} 2718}
2719 2719
2720/* 2720/*
2721 * pmap_t pmap_create(void) 2721 * pmap_t pmap_create(void)
2722 *  2722 *
2723 * Create a new pmap structure from scratch. 2723 * Create a new pmap structure from scratch.
2724 */ 2724 */
2725pmap_t 2725pmap_t
2726pmap_create(void) 2726pmap_create(void)
2727{ 2727{
2728 pmap_t pm; 2728 pmap_t pm;
2729 2729
2730 pm = pool_cache_get(&pmap_cache, PR_WAITOK); 2730 pm = pool_cache_get(&pmap_cache, PR_WAITOK);
2731 2731
2732 UVM_OBJ_INIT(&pm->pm_obj, NULL, 1); 2732 UVM_OBJ_INIT(&pm->pm_obj, NULL, 1);
2733 pm->pm_stats.wired_count = 0; 2733 pm->pm_stats.wired_count = 0;
2734 pm->pm_stats.resident_count = 1; 2734 pm->pm_stats.resident_count = 1;
2735 pm->pm_cstate.cs_all = 0; 2735 pm->pm_cstate.cs_all = 0;
2736 pmap_alloc_l1(pm); 2736 pmap_alloc_l1(pm);
2737 2737
2738 /* 2738 /*
2739 * Note: The pool cache ensures that the pm_l2[] array is already 2739 * Note: The pool cache ensures that the pm_l2[] array is already
2740 * initialised to zero. 2740 * initialised to zero.
2741 */ 2741 */
2742 2742
2743 pmap_pinit(pm); 2743 pmap_pinit(pm);
2744 2744
2745 LIST_INSERT_HEAD(&pmap_pmaps, pm, pm_list); 2745 LIST_INSERT_HEAD(&pmap_pmaps, pm, pm_list);
2746 2746
2747 return (pm); 2747 return (pm);
2748} 2748}
2749 2749
2750/* 2750/*
2751 * int pmap_enter(pmap_t pm, vaddr_t va, paddr_t pa, vm_prot_t prot, 2751 * int pmap_enter(pmap_t pm, vaddr_t va, paddr_t pa, vm_prot_t prot,
2752 * u_int flags) 2752 * u_int flags)
2753 *  2753 *
2754 * Insert the given physical page (p) at 2754 * Insert the given physical page (p) at
2755 * the specified virtual address (v) in the 2755 * the specified virtual address (v) in the
2756 * target physical map with the protection requested. 2756 * target physical map with the protection requested.
2757 * 2757 *
2758 * NB: This is the only routine which MAY NOT lazy-evaluate 2758 * NB: This is the only routine which MAY NOT lazy-evaluate
2759 * or lose information. That is, this routine must actually 2759 * or lose information. That is, this routine must actually
2760 * insert this page into the given map NOW. 2760 * insert this page into the given map NOW.
2761 */ 2761 */
2762int 2762int
2763pmap_enter(pmap_t pm, vaddr_t va, paddr_t pa, vm_prot_t prot, u_int flags) 2763pmap_enter(pmap_t pm, vaddr_t va, paddr_t pa, vm_prot_t prot, u_int flags)
2764{ 2764{
2765 struct l2_bucket *l2b; 2765 struct l2_bucket *l2b;
2766 struct vm_page *pg, *opg; 2766 struct vm_page *pg, *opg;
2767 struct pv_entry *pv; 2767 struct pv_entry *pv;
2768 pt_entry_t *ptep, npte, opte; 2768 pt_entry_t *ptep, npte, opte;
2769 u_int nflags; 2769 u_int nflags;
2770 u_int oflags; 2770 u_int oflags;
2771 2771
2772 NPDEBUG(PDB_ENTER, printf("pmap_enter: pm %p va 0x%lx pa 0x%lx prot %x flag %x\n", pm, va, pa, prot, flags)); 2772 NPDEBUG(PDB_ENTER, printf("pmap_enter: pm %p va 0x%lx pa 0x%lx prot %x flag %x\n", pm, va, pa, prot, flags));
2773 2773
2774 KDASSERT((flags & PMAP_WIRED) == 0 || (flags & VM_PROT_ALL) != 0); 2774 KDASSERT((flags & PMAP_WIRED) == 0 || (flags & VM_PROT_ALL) != 0);
2775 KDASSERT(((va | pa) & PGOFSET) == 0); 2775 KDASSERT(((va | pa) & PGOFSET) == 0);
2776 2776
2777 /* 2777 /*
2778 * Get a pointer to the page. Later on in this function, we 2778 * Get a pointer to the page. Later on in this function, we
2779 * test for a managed page by checking pg != NULL. 2779 * test for a managed page by checking pg != NULL.
2780 */ 2780 */
2781 pg = pmap_initialized ? PHYS_TO_VM_PAGE(pa) : NULL; 2781 pg = pmap_initialized ? PHYS_TO_VM_PAGE(pa) : NULL;
2782 2782
2783 nflags = 0; 2783 nflags = 0;
2784 if (prot & VM_PROT_WRITE) 2784 if (prot & VM_PROT_WRITE)
2785 nflags |= PVF_WRITE; 2785 nflags |= PVF_WRITE;
2786 if (prot & VM_PROT_EXECUTE) 2786 if (prot & VM_PROT_EXECUTE)
2787 nflags |= PVF_EXEC; 2787 nflags |= PVF_EXEC;
2788 if (flags & PMAP_WIRED) 2788 if (flags & PMAP_WIRED)
2789 nflags |= PVF_WIRED; 2789 nflags |= PVF_WIRED;
2790 2790
2791 PMAP_MAP_TO_HEAD_LOCK(); 2791 PMAP_MAP_TO_HEAD_LOCK();
2792 pmap_acquire_pmap_lock(pm); 2792 pmap_acquire_pmap_lock(pm);
2793 2793
2794 /* 2794 /*
2795 * Fetch the L2 bucket which maps this page, allocating one if 2795 * Fetch the L2 bucket which maps this page, allocating one if
2796 * necessary for user pmaps. 2796 * necessary for user pmaps.
2797 */ 2797 */
2798 if (pm == pmap_kernel()) 2798 if (pm == pmap_kernel())
2799 l2b = pmap_get_l2_bucket(pm, va); 2799 l2b = pmap_get_l2_bucket(pm, va);
2800 else 2800 else
2801 l2b = pmap_alloc_l2_bucket(pm, va); 2801 l2b = pmap_alloc_l2_bucket(pm, va);
2802 if (l2b == NULL) { 2802 if (l2b == NULL) {
2803 if (flags & PMAP_CANFAIL) { 2803 if (flags & PMAP_CANFAIL) {
2804 pmap_release_pmap_lock(pm); 2804 pmap_release_pmap_lock(pm);
2805 PMAP_MAP_TO_HEAD_UNLOCK(); 2805 PMAP_MAP_TO_HEAD_UNLOCK();
2806 return (ENOMEM); 2806 return (ENOMEM);
2807 } 2807 }
2808 panic("pmap_enter: failed to allocate L2 bucket"); 2808 panic("pmap_enter: failed to allocate L2 bucket");
2809 } 2809 }
2810 ptep = &l2b->l2b_kva[l2pte_index(va)]; 2810 ptep = &l2b->l2b_kva[l2pte_index(va)];
2811 opte = *ptep; 2811 opte = *ptep;
2812 npte = pa; 2812 npte = pa;
2813 oflags = 0; 2813 oflags = 0;
2814 2814
2815 if (opte) { 2815 if (opte) {
2816 /* 2816 /*
2817 * There is already a mapping at this address. 2817 * There is already a mapping at this address.
2818 * If the physical address is different, lookup the 2818 * If the physical address is different, lookup the
2819 * vm_page. 2819 * vm_page.
2820 */ 2820 */
2821 if (l2pte_pa(opte) != pa) 2821 if (l2pte_pa(opte) != pa)
2822 opg = PHYS_TO_VM_PAGE(l2pte_pa(opte)); 2822 opg = PHYS_TO_VM_PAGE(l2pte_pa(opte));
2823 else 2823 else
2824 opg = pg; 2824 opg = pg;
2825 } else 2825 } else
2826 opg = NULL; 2826 opg = NULL;
2827 2827
2828 if (pg) { 2828 if (pg) {
2829 struct vm_page_md *md = VM_PAGE_TO_MD(pg); 2829 struct vm_page_md *md = VM_PAGE_TO_MD(pg);
2830 2830
2831 /* 2831 /*
2832 * This is to be a managed mapping. 2832 * This is to be a managed mapping.
2833 */ 2833 */
2834 if ((flags & VM_PROT_ALL) || 2834 if ((flags & VM_PROT_ALL) ||
2835 (md->pvh_attrs & PVF_REF)) { 2835 (md->pvh_attrs & PVF_REF)) {
2836 /* 2836 /*
2837 * - The access type indicates that we don't need 2837 * - The access type indicates that we don't need
2838 * to do referenced emulation. 2838 * to do referenced emulation.
2839 * OR 2839 * OR
2840 * - The physical page has already been referenced 2840 * - The physical page has already been referenced
2841 * so no need to re-do referenced emulation here. 2841 * so no need to re-do referenced emulation here.
2842 */ 2842 */
2843 npte |= l2pte_set_readonly(L2_S_PROTO); 2843 npte |= l2pte_set_readonly(L2_S_PROTO);
2844 2844
2845 nflags |= PVF_REF; 2845 nflags |= PVF_REF;
2846 2846
2847 if ((prot & VM_PROT_WRITE) != 0 && 2847 if ((prot & VM_PROT_WRITE) != 0 &&
2848 ((flags & VM_PROT_WRITE) != 0 || 2848 ((flags & VM_PROT_WRITE) != 0 ||
2849 (md->pvh_attrs & PVF_MOD) != 0)) { 2849 (md->pvh_attrs & PVF_MOD) != 0)) {
2850 /* 2850 /*
2851 * This is a writable mapping, and the 2851 * This is a writable mapping, and the
2852 * page's mod state indicates it has 2852 * page's mod state indicates it has
2853 * already been modified. Make it 2853 * already been modified. Make it
2854 * writable from the outset. 2854 * writable from the outset.
2855 */ 2855 */
2856 npte = l2pte_set_writable(npte); 2856 npte = l2pte_set_writable(npte);
2857 nflags |= PVF_MOD; 2857 nflags |= PVF_MOD;
2858 } 2858 }
2859 } else { 2859 } else {
2860 /* 2860 /*
2861 * Need to do page referenced emulation. 2861 * Need to do page referenced emulation.
2862 */ 2862 */
2863 npte |= L2_TYPE_INV; 2863 npte |= L2_TYPE_INV;
2864 } 2864 }
2865 2865
2866 npte |= pte_l2_s_cache_mode; 2866 npte |= pte_l2_s_cache_mode;
2867 2867
2868 if (pg == opg) { 2868 if (pg == opg) {
2869 /* 2869 /*
2870 * We're changing the attrs of an existing mapping. 2870 * We're changing the attrs of an existing mapping.
2871 */ 2871 */
2872 simple_lock(&md->pvh_slock); 2872 simple_lock(&md->pvh_slock);
2873 oflags = pmap_modify_pv(md, pa, pm, va, 2873 oflags = pmap_modify_pv(md, pa, pm, va,
2874 PVF_WRITE | PVF_EXEC | PVF_WIRED | 2874 PVF_WRITE | PVF_EXEC | PVF_WIRED |
2875 PVF_MOD | PVF_REF, nflags); 2875 PVF_MOD | PVF_REF, nflags);
2876 simple_unlock(&md->pvh_slock); 2876 simple_unlock(&md->pvh_slock);
2877 2877
2878#ifdef PMAP_CACHE_VIVT 2878#ifdef PMAP_CACHE_VIVT
2879 /* 2879 /*
2880 * We may need to flush the cache if we're 2880 * We may need to flush the cache if we're
2881 * doing rw-ro... 2881 * doing rw-ro...
2882 */ 2882 */
2883 if (pm->pm_cstate.cs_cache_d && 2883 if (pm->pm_cstate.cs_cache_d &&
2884 (oflags & PVF_NC) == 0 && 2884 (oflags & PVF_NC) == 0 &&
2885 l2pte_writable_p(opte) && 2885 l2pte_writable_p(opte) &&
2886 (prot & VM_PROT_WRITE) == 0) 2886 (prot & VM_PROT_WRITE) == 0)
2887 cpu_dcache_wb_range(va, PAGE_SIZE); 2887 cpu_dcache_wb_range(va, PAGE_SIZE);
2888#endif 2888#endif
2889 } else { 2889 } else {
2890 /* 2890 /*
2891 * New mapping, or changing the backing page 2891 * New mapping, or changing the backing page
2892 * of an existing mapping. 2892 * of an existing mapping.
2893 */ 2893 */
2894 if (opg) { 2894 if (opg) {
2895 struct vm_page_md *omd = VM_PAGE_TO_MD(opg); 2895 struct vm_page_md *omd = VM_PAGE_TO_MD(opg);
2896 paddr_t opa = VM_PAGE_TO_PHYS(opg); 2896 paddr_t opa = VM_PAGE_TO_PHYS(opg);
2897 2897
2898 /* 2898 /*
2899 * Replacing an existing mapping with a new one. 2899 * Replacing an existing mapping with a new one.
2900 * It is part of our managed memory so we 2900 * It is part of our managed memory so we
2901 * must remove it from the PV list 2901 * must remove it from the PV list
2902 */ 2902 */
2903 simple_lock(&omd->pvh_slock); 2903 simple_lock(&omd->pvh_slock);
2904 pv = pmap_remove_pv(omd, opa, pm, va); 2904 pv = pmap_remove_pv(omd, opa, pm, va);
2905 pmap_vac_me_harder(omd, opa, pm, 0); 2905 pmap_vac_me_harder(omd, opa, pm, 0);
2906 simple_unlock(&omd->pvh_slock); 2906 simple_unlock(&omd->pvh_slock);
2907 oflags = pv->pv_flags; 2907 oflags = pv->pv_flags;
2908 2908
2909#ifdef PMAP_CACHE_VIVT 2909#ifdef PMAP_CACHE_VIVT
2910 /* 2910 /*
2911 * If the old mapping was valid (ref/mod 2911 * If the old mapping was valid (ref/mod
2912 * emulation creates 'invalid' mappings 2912 * emulation creates 'invalid' mappings
2913 * initially) then make sure to frob 2913 * initially) then make sure to frob
2914 * the cache. 2914 * the cache.
2915 */ 2915 */
2916 if ((oflags & PVF_NC) == 0 && 2916 if ((oflags & PVF_NC) == 0 &&
2917 l2pte_valid(opte)) { 2917 l2pte_valid(opte)) {
2918 if (PV_BEEN_EXECD(oflags)) { 2918 if (PV_BEEN_EXECD(oflags)) {
2919 pmap_idcache_wbinv_range(pm, va, 2919 pmap_idcache_wbinv_range(pm, va,
2920 PAGE_SIZE); 2920 PAGE_SIZE);
2921 } else 2921 } else
2922 if (PV_BEEN_REFD(oflags)) { 2922 if (PV_BEEN_REFD(oflags)) {
2923 pmap_dcache_wb_range(pm, va, 2923 pmap_dcache_wb_range(pm, va,
2924 PAGE_SIZE, true, 2924 PAGE_SIZE, true,
2925 (oflags & PVF_WRITE) == 0); 2925 (oflags & PVF_WRITE) == 0);
2926 } 2926 }
2927 } 2927 }
2928#endif 2928#endif
2929 } else 2929 } else
2930 if ((pv = pool_get(&pmap_pv_pool, PR_NOWAIT)) == NULL){ 2930 if ((pv = pool_get(&pmap_pv_pool, PR_NOWAIT)) == NULL){
2931 if ((flags & PMAP_CANFAIL) == 0) 2931 if ((flags & PMAP_CANFAIL) == 0)
2932 panic("pmap_enter: no pv entries"); 2932 panic("pmap_enter: no pv entries");
2933 2933
2934 if (pm != pmap_kernel()) 2934 if (pm != pmap_kernel())
2935 pmap_free_l2_bucket(pm, l2b, 0); 2935 pmap_free_l2_bucket(pm, l2b, 0);
2936 pmap_release_pmap_lock(pm); 2936 pmap_release_pmap_lock(pm);
2937 PMAP_MAP_TO_HEAD_UNLOCK(); 2937 PMAP_MAP_TO_HEAD_UNLOCK();
2938 NPDEBUG(PDB_ENTER, 2938 NPDEBUG(PDB_ENTER,
2939 printf("pmap_enter: ENOMEM\n")); 2939 printf("pmap_enter: ENOMEM\n"));
2940 return (ENOMEM); 2940 return (ENOMEM);
2941 } 2941 }
2942 2942
2943 pmap_enter_pv(md, pa, pv, pm, va, nflags); 2943 pmap_enter_pv(md, pa, pv, pm, va, nflags);
2944 } 2944 }
2945 } else { 2945 } else {
2946 /* 2946 /*
2947 * We're mapping an unmanaged page. 2947 * We're mapping an unmanaged page.
2948 * These are always readable, and possibly writable, from 2948 * These are always readable, and possibly writable, from
2949 * the get go as we don't need to track ref/mod status. 2949 * the get go as we don't need to track ref/mod status.
2950 */ 2950 */
2951 npte |= l2pte_set_readonly(L2_S_PROTO); 2951 npte |= l2pte_set_readonly(L2_S_PROTO);
2952 if (prot & VM_PROT_WRITE) 2952 if (prot & VM_PROT_WRITE)
2953 npte = l2pte_set_writable(npte); 2953 npte = l2pte_set_writable(npte);
2954 2954
2955 /* 2955 /*
2956 * Make sure the vector table is mapped cacheable 2956 * Make sure the vector table is mapped cacheable
2957 */ 2957 */
2958 if (pm != pmap_kernel() && va == vector_page) 2958 if (pm != pmap_kernel() && va == vector_page)
2959 npte |= pte_l2_s_cache_mode; 2959 npte |= pte_l2_s_cache_mode;
2960 2960
2961 if (opg) { 2961 if (opg) {
2962 /* 2962 /*
2963 * Looks like there's an existing 'managed' mapping 2963 * Looks like there's an existing 'managed' mapping
2964 * at this address. 2964 * at this address.
2965 */ 2965 */
2966 struct vm_page_md *omd = VM_PAGE_TO_MD(opg); 2966 struct vm_page_md *omd = VM_PAGE_TO_MD(opg);
2967 paddr_t opa = VM_PAGE_TO_PHYS(opg); 2967 paddr_t opa = VM_PAGE_TO_PHYS(opg);
2968 2968
2969 simple_lock(&omd->pvh_slock); 2969 simple_lock(&omd->pvh_slock);
2970 pv = pmap_remove_pv(omd, opa, pm, va); 2970 pv = pmap_remove_pv(omd, opa, pm, va);
2971 pmap_vac_me_harder(omd, opa, pm, 0); 2971 pmap_vac_me_harder(omd, opa, pm, 0);
2972 simple_unlock(&omd->pvh_slock); 2972 simple_unlock(&omd->pvh_slock);
2973 oflags = pv->pv_flags; 2973 oflags = pv->pv_flags;
2974 2974
2975#ifdef PMAP_CACHE_VIVT 2975#ifdef PMAP_CACHE_VIVT
2976 if ((oflags & PVF_NC) == 0 && l2pte_valid(opte)) { 2976 if ((oflags & PVF_NC) == 0 && l2pte_valid(opte)) {
2977 if (PV_BEEN_EXECD(oflags)) 2977 if (PV_BEEN_EXECD(oflags))
2978 pmap_idcache_wbinv_range(pm, va, 2978 pmap_idcache_wbinv_range(pm, va,
2979 PAGE_SIZE); 2979 PAGE_SIZE);
2980 else 2980 else
2981 if (PV_BEEN_REFD(oflags)) 2981 if (PV_BEEN_REFD(oflags))
2982 pmap_dcache_wb_range(pm, va, PAGE_SIZE, 2982 pmap_dcache_wb_range(pm, va, PAGE_SIZE,
2983 true, (oflags & PVF_WRITE) == 0); 2983 true, (oflags & PVF_WRITE) == 0);
2984 } 2984 }
2985#endif 2985#endif
2986 pool_put(&pmap_pv_pool, pv); 2986 pool_put(&pmap_pv_pool, pv);
2987 } 2987 }
2988 } 2988 }
2989 2989
2990 /* 2990 /*
2991 * Make sure userland mappings get the right permissions 2991 * Make sure userland mappings get the right permissions
2992 */ 2992 */
2993 if (pm != pmap_kernel() && va != vector_page) 2993 if (pm != pmap_kernel() && va != vector_page)
2994 npte |= L2_S_PROT_U; 2994 npte |= L2_S_PROT_U;
2995 2995
2996 /* 2996 /*
2997 * Keep the stats up to date 2997 * Keep the stats up to date
2998 */ 2998 */
2999 if (opte == 0) { 2999 if (opte == 0) {
3000 l2b->l2b_occupancy++; 3000 l2b->l2b_occupancy++;
3001 pm->pm_stats.resident_count++; 3001 pm->pm_stats.resident_count++;
3002 }  3002 }
3003 3003
3004 NPDEBUG(PDB_ENTER, 3004 NPDEBUG(PDB_ENTER,
3005 printf("pmap_enter: opte 0x%08x npte 0x%08x\n", opte, npte)); 3005 printf("pmap_enter: opte 0x%08x npte 0x%08x\n", opte, npte));
3006 3006
3007 /* 3007 /*
3008 * If this is just a wiring change, the two PTEs will be 3008 * If this is just a wiring change, the two PTEs will be
3009 * identical, so there's no need to update the page table. 3009 * identical, so there's no need to update the page table.
3010 */ 3010 */
3011 if (npte != opte) { 3011 if (npte != opte) {
3012 bool is_cached = pmap_is_cached(pm); 3012 bool is_cached = pmap_is_cached(pm);
3013 3013
3014 *ptep = npte; 3014 *ptep = npte;
3015 if (is_cached) { 3015 if (is_cached) {
3016 /* 3016 /*
3017 * We only need to frob the cache/tlb if this pmap 3017 * We only need to frob the cache/tlb if this pmap
3018 * is current 3018 * is current
3019 */ 3019 */
3020 PTE_SYNC(ptep); 3020 PTE_SYNC(ptep);
3021 if (va != vector_page && l2pte_valid(npte)) { 3021 if (va != vector_page && l2pte_valid(npte)) {
3022 /* 3022 /*
3023 * This mapping is likely to be accessed as 3023 * This mapping is likely to be accessed as
3024 * soon as we return to userland. Fix up the 3024 * soon as we return to userland. Fix up the
3025 * L1 entry to avoid taking another 3025 * L1 entry to avoid taking another
3026 * page/domain fault. 3026 * page/domain fault.
3027 */ 3027 */
3028 pd_entry_t *pl1pd, l1pd; 3028 pd_entry_t *pl1pd, l1pd;
3029 3029
3030 pl1pd = &pm->pm_l1->l1_kva[L1_IDX(va)]; 3030 pl1pd = &pm->pm_l1->l1_kva[L1_IDX(va)];
3031 l1pd = l2b->l2b_phys | L1_C_DOM(pm->pm_domain) | 3031 l1pd = l2b->l2b_phys | L1_C_DOM(pm->pm_domain) |
3032 L1_C_PROTO; 3032 L1_C_PROTO;
3033 if (*pl1pd != l1pd) { 3033 if (*pl1pd != l1pd) {
3034 *pl1pd = l1pd; 3034 *pl1pd = l1pd;
3035 PTE_SYNC(pl1pd); 3035 PTE_SYNC(pl1pd);
3036 } 3036 }
3037 } 3037 }
3038 } 3038 }
3039 3039
3040 if (PV_BEEN_EXECD(oflags)) 3040 if (PV_BEEN_EXECD(oflags))
3041 pmap_tlb_flushID_SE(pm, va); 3041 pmap_tlb_flushID_SE(pm, va);
3042 else 3042 else
3043 if (PV_BEEN_REFD(oflags)) 3043 if (PV_BEEN_REFD(oflags))
3044 pmap_tlb_flushD_SE(pm, va); 3044 pmap_tlb_flushD_SE(pm, va);
3045 3045
3046 NPDEBUG(PDB_ENTER, 3046 NPDEBUG(PDB_ENTER,
3047 printf("pmap_enter: is_cached %d cs 0x%08x\n", 3047 printf("pmap_enter: is_cached %d cs 0x%08x\n",
3048 is_cached, pm->pm_cstate.cs_all)); 3048 is_cached, pm->pm_cstate.cs_all));
3049 3049
3050 if (pg != NULL) { 3050 if (pg != NULL) {
3051 struct vm_page_md *md = VM_PAGE_TO_MD(pg); 3051 struct vm_page_md *md = VM_PAGE_TO_MD(pg);
3052 3052
3053 simple_lock(&md->pvh_slock); 3053 simple_lock(&md->pvh_slock);
3054 pmap_vac_me_harder(md, pa, pm, va); 3054 pmap_vac_me_harder(md, pa, pm, va);
3055 simple_unlock(&md->pvh_slock); 3055 simple_unlock(&md->pvh_slock);
3056 } 3056 }
3057 } 3057 }
3058#if defined(PMAP_CACHE_VIPT) && defined(DIAGNOSTIC) 3058#if defined(PMAP_CACHE_VIPT) && defined(DIAGNOSTIC)
3059 if (pg) { 3059 if (pg) {
3060 struct vm_page_md *md = VM_PAGE_TO_MD(pg); 3060 struct vm_page_md *md = VM_PAGE_TO_MD(pg);
3061 3061
3062 simple_lock(&md->pvh_slock); 3062 simple_lock(&md->pvh_slock);
3063 KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC))); 3063 KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC)));
3064 KASSERT(((md->pvh_attrs & PVF_WRITE) == 0) == (md->urw_mappings + md->krw_mappings == 0)); 3064 KASSERT(((md->pvh_attrs & PVF_WRITE) == 0) == (md->urw_mappings + md->krw_mappings == 0));
3065 simple_unlock(&md->pvh_slock); 3065 simple_unlock(&md->pvh_slock);
3066 } 3066 }
3067#endif 3067#endif
3068 3068
3069 pmap_release_pmap_lock(pm); 3069 pmap_release_pmap_lock(pm);
3070 PMAP_MAP_TO_HEAD_UNLOCK(); 3070 PMAP_MAP_TO_HEAD_UNLOCK();
3071 3071
3072 return (0); 3072 return (0);
3073} 3073}
3074 3074
3075/* 3075/*
3076 * pmap_remove() 3076 * pmap_remove()
3077 * 3077 *
3078 * pmap_remove is responsible for nuking a number of mappings for a range 3078 * pmap_remove is responsible for nuking a number of mappings for a range
3079 * of virtual address space in the current pmap. To do this efficiently 3079 * of virtual address space in the current pmap. To do this efficiently
3080 * is interesting, because in a number of cases a wide virtual address 3080 * is interesting, because in a number of cases a wide virtual address
3081 * range may be supplied that contains few actual mappings. So, the 3081 * range may be supplied that contains few actual mappings. So, the
3082 * optimisations are: 3082 * optimisations are:
3083 * 1. Skip over hunks of address space for which no L1 or L2 entry exists. 3083 * 1. Skip over hunks of address space for which no L1 or L2 entry exists.
3084 * 2. Build up a list of pages we've hit, up to a maximum, so we can 3084 * 2. Build up a list of pages we've hit, up to a maximum, so we can
3085 * maybe do just a partial cache clean. This path of execution is 3085 * maybe do just a partial cache clean. This path of execution is
3086 * complicated by the fact that the cache must be flushed _before_ 3086 * complicated by the fact that the cache must be flushed _before_
3087 * the PTE is nuked, being a VAC :-) 3087 * the PTE is nuked, being a VAC :-)
3088 * 3. If we're called after UVM calls pmap_remove_all(), we can defer 3088 * 3. If we're called after UVM calls pmap_remove_all(), we can defer
3089 * all invalidations until pmap_update(), since pmap_remove_all() has 3089 * all invalidations until pmap_update(), since pmap_remove_all() has
3090 * already flushed the cache. 3090 * already flushed the cache.
3091 * 4. Maybe later fast-case a single page, but I don't think this is 3091 * 4. Maybe later fast-case a single page, but I don't think this is
3092 * going to make _that_ much difference overall. 3092 * going to make _that_ much difference overall.
3093 */ 3093 */
3094 3094
3095#define PMAP_REMOVE_CLEAN_LIST_SIZE 3 3095#define PMAP_REMOVE_CLEAN_LIST_SIZE 3
3096 3096
3097void 3097void
3098pmap_remove(pmap_t pm, vaddr_t sva, vaddr_t eva) 3098pmap_remove(pmap_t pm, vaddr_t sva, vaddr_t eva)
3099{ 3099{
3100 struct l2_bucket *l2b; 3100 struct l2_bucket *l2b;
3101 vaddr_t next_bucket; 3101 vaddr_t next_bucket;
3102 pt_entry_t *ptep; 3102 pt_entry_t *ptep;
3103 u_int cleanlist_idx, total, cnt; 3103 u_int cleanlist_idx, total, cnt;
3104 struct { 3104 struct {
3105 vaddr_t va; 3105 vaddr_t va;
3106 pt_entry_t *ptep; 3106 pt_entry_t *ptep;
3107 } cleanlist[PMAP_REMOVE_CLEAN_LIST_SIZE]; 3107 } cleanlist[PMAP_REMOVE_CLEAN_LIST_SIZE];
3108 u_int mappings, is_exec, is_refd; 3108 u_int mappings, is_exec, is_refd;
3109 3109
3110 NPDEBUG(PDB_REMOVE, printf("pmap_do_remove: pmap=%p sva=%08lx " 3110 NPDEBUG(PDB_REMOVE, printf("pmap_do_remove: pmap=%p sva=%08lx "
3111 "eva=%08lx\n", pm, sva, eva)); 3111 "eva=%08lx\n", pm, sva, eva));
3112 3112
3113 /* 3113 /*
3114 * we lock in the pmap => pv_head direction 3114 * we lock in the pmap => pv_head direction
3115 */ 3115 */
3116 PMAP_MAP_TO_HEAD_LOCK(); 3116 PMAP_MAP_TO_HEAD_LOCK();
3117 pmap_acquire_pmap_lock(pm); 3117 pmap_acquire_pmap_lock(pm);
3118 3118
3119 if (pm->pm_remove_all || !pmap_is_cached(pm)) { 3119 if (pm->pm_remove_all || !pmap_is_cached(pm)) {
3120 cleanlist_idx = PMAP_REMOVE_CLEAN_LIST_SIZE + 1; 3120 cleanlist_idx = PMAP_REMOVE_CLEAN_LIST_SIZE + 1;
3121 if (pm->pm_cstate.cs_tlb == 0) 3121 if (pm->pm_cstate.cs_tlb == 0)
3122 pm->pm_remove_all = true; 3122 pm->pm_remove_all = true;
3123 } else 3123 } else
3124 cleanlist_idx = 0; 3124 cleanlist_idx = 0;
3125 3125
3126 total = 0; 3126 total = 0;
3127 3127
3128 while (sva < eva) { 3128 while (sva < eva) {
3129 /* 3129 /*
3130 * Do one L2 bucket's worth at a time. 3130 * Do one L2 bucket's worth at a time.
3131 */ 3131 */
3132 next_bucket = L2_NEXT_BUCKET(sva); 3132 next_bucket = L2_NEXT_BUCKET(sva);
3133 if (next_bucket > eva) 3133 if (next_bucket > eva)
3134 next_bucket = eva; 3134 next_bucket = eva;
3135 3135
3136 l2b = pmap_get_l2_bucket(pm, sva); 3136 l2b = pmap_get_l2_bucket(pm, sva);
3137 if (l2b == NULL) { 3137 if (l2b == NULL) {
3138 sva = next_bucket; 3138 sva = next_bucket;
3139 continue; 3139 continue;
3140 } 3140 }
3141 3141
3142 ptep = &l2b->l2b_kva[l2pte_index(sva)]; 3142 ptep = &l2b->l2b_kva[l2pte_index(sva)];
3143 3143
3144 for (mappings = 0; sva < next_bucket; sva += PAGE_SIZE, ptep++){ 3144 for (mappings = 0; sva < next_bucket; sva += PAGE_SIZE, ptep++){
3145 struct vm_page *pg; 3145 struct vm_page *pg;
3146 pt_entry_t pte; 3146 pt_entry_t pte;
3147 paddr_t pa; 3147 paddr_t pa;
3148 3148
3149 pte = *ptep; 3149 pte = *ptep;
3150 3150
3151 if (pte == 0) { 3151 if (pte == 0) {
3152 /* Nothing here, move along */ 3152 /* Nothing here, move along */
3153 continue; 3153 continue;
3154 } 3154 }
3155 3155
3156 pa = l2pte_pa(pte); 3156 pa = l2pte_pa(pte);
3157 is_exec = 0; 3157 is_exec = 0;
3158 is_refd = 1; 3158 is_refd = 1;
3159 3159
3160 /* 3160 /*
3161 * Update flags. In a number of circumstances, 3161 * Update flags. In a number of circumstances,
3162 * we could cluster a lot of these and do a 3162 * we could cluster a lot of these and do a
3163 * number of sequential pages in one go. 3163 * number of sequential pages in one go.
3164 */ 3164 */
3165 if ((pg = PHYS_TO_VM_PAGE(pa)) != NULL) { 3165 if ((pg = PHYS_TO_VM_PAGE(pa)) != NULL) {
3166 struct vm_page_md *md = VM_PAGE_TO_MD(pg); 3166 struct vm_page_md *md = VM_PAGE_TO_MD(pg);
3167 struct pv_entry *pv; 3167 struct pv_entry *pv;
3168 3168
3169 simple_lock(&md->pvh_slock); 3169 simple_lock(&md->pvh_slock);
3170 pv = pmap_remove_pv(md, pa, pm, sva); 3170 pv = pmap_remove_pv(md, pa, pm, sva);
3171 pmap_vac_me_harder(md, pa, pm, 0); 3171 pmap_vac_me_harder(md, pa, pm, 0);
3172 simple_unlock(&md->pvh_slock); 3172 simple_unlock(&md->pvh_slock);
3173 if (pv != NULL) { 3173 if (pv != NULL) {
3174 if (pm->pm_remove_all == false) { 3174 if (pm->pm_remove_all == false) {
3175 is_exec = 3175 is_exec =
3176 PV_BEEN_EXECD(pv->pv_flags); 3176 PV_BEEN_EXECD(pv->pv_flags);
3177 is_refd = 3177 is_refd =
3178 PV_BEEN_REFD(pv->pv_flags); 3178 PV_BEEN_REFD(pv->pv_flags);
3179 } 3179 }
3180 pool_put(&pmap_pv_pool, pv); 3180 pool_put(&pmap_pv_pool, pv);
3181 } 3181 }
3182 } 3182 }
3183 mappings++; 3183 mappings++;
3184 3184
3185 if (!l2pte_valid(pte)) { 3185 if (!l2pte_valid(pte)) {
3186 /* 3186 /*
3187 * Ref/Mod emulation is still active for this 3187 * Ref/Mod emulation is still active for this
3188 * mapping, therefore it is has not yet been 3188 * mapping, therefore it is has not yet been
3189 * accessed. No need to frob the cache/tlb. 3189 * accessed. No need to frob the cache/tlb.
3190 */ 3190 */
3191 *ptep = 0; 3191 *ptep = 0;
3192 PTE_SYNC_CURRENT(pm, ptep); 3192 PTE_SYNC_CURRENT(pm, ptep);
3193 continue; 3193 continue;
3194 } 3194 }
3195 3195
3196 if (cleanlist_idx < PMAP_REMOVE_CLEAN_LIST_SIZE) { 3196 if (cleanlist_idx < PMAP_REMOVE_CLEAN_LIST_SIZE) {
3197 /* Add to the clean list. */ 3197 /* Add to the clean list. */
3198 cleanlist[cleanlist_idx].ptep = ptep; 3198 cleanlist[cleanlist_idx].ptep = ptep;
3199 cleanlist[cleanlist_idx].va = 3199 cleanlist[cleanlist_idx].va =
3200 sva | (is_exec & 1); 3200 sva | (is_exec & 1);
3201 cleanlist_idx++; 3201 cleanlist_idx++;
3202 } else 3202 } else
3203 if (cleanlist_idx == PMAP_REMOVE_CLEAN_LIST_SIZE) { 3203 if (cleanlist_idx == PMAP_REMOVE_CLEAN_LIST_SIZE) {
3204 /* Nuke everything if needed. */ 3204 /* Nuke everything if needed. */
3205#ifdef PMAP_CACHE_VIVT 3205#ifdef PMAP_CACHE_VIVT
3206 pmap_idcache_wbinv_all(pm); 3206 pmap_idcache_wbinv_all(pm);
3207#endif 3207#endif
3208 pmap_tlb_flushID(pm); 3208 pmap_tlb_flushID(pm);
3209 3209
3210 /* 3210 /*
3211 * Roll back the previous PTE list, 3211 * Roll back the previous PTE list,
3212 * and zero out the current PTE. 3212 * and zero out the current PTE.
3213 */ 3213 */
3214 for (cnt = 0; 3214 for (cnt = 0;
3215 cnt < PMAP_REMOVE_CLEAN_LIST_SIZE; cnt++) { 3215 cnt < PMAP_REMOVE_CLEAN_LIST_SIZE; cnt++) {
3216 *cleanlist[cnt].ptep = 0; 3216 *cleanlist[cnt].ptep = 0;
3217 PTE_SYNC(cleanlist[cnt].ptep); 3217 PTE_SYNC(cleanlist[cnt].ptep);
3218 } 3218 }
3219 *ptep = 0; 3219 *ptep = 0;
3220 PTE_SYNC(ptep); 3220 PTE_SYNC(ptep);
3221 cleanlist_idx++; 3221 cleanlist_idx++;
3222 pm->pm_remove_all = true; 3222 pm->pm_remove_all = true;
3223 } else { 3223 } else {
3224 *ptep = 0; 3224 *ptep = 0;
3225 PTE_SYNC(ptep); 3225 PTE_SYNC(ptep);
3226 if (pm->pm_remove_all == false) { 3226 if (pm->pm_remove_all == false) {
3227 if (is_exec) 3227 if (is_exec)
3228 pmap_tlb_flushID_SE(pm, sva); 3228 pmap_tlb_flushID_SE(pm, sva);
3229 else 3229 else
3230 if (is_refd) 3230 if (is_refd)
3231 pmap_tlb_flushD_SE(pm, sva); 3231 pmap_tlb_flushD_SE(pm, sva);
3232 } 3232 }
3233 } 3233 }
3234 } 3234 }
3235 3235
3236 /* 3236 /*
3237 * Deal with any left overs 3237 * Deal with any left overs
3238 */ 3238 */
3239 if (cleanlist_idx <= PMAP_REMOVE_CLEAN_LIST_SIZE) { 3239 if (cleanlist_idx <= PMAP_REMOVE_CLEAN_LIST_SIZE) {
3240 total += cleanlist_idx; 3240 total += cleanlist_idx;
3241 for (cnt = 0; cnt < cleanlist_idx; cnt++) { 3241 for (cnt = 0; cnt < cleanlist_idx; cnt++) {
3242 if (pm->pm_cstate.cs_all != 0) { 3242 if (pm->pm_cstate.cs_all != 0) {
3243 vaddr_t clva = cleanlist[cnt].va & ~1; 3243 vaddr_t clva = cleanlist[cnt].va & ~1;
3244 if (cleanlist[cnt].va & 1) { 3244 if (cleanlist[cnt].va & 1) {
3245#ifdef PMAP_CACHE_VIVT 3245#ifdef PMAP_CACHE_VIVT
3246 pmap_idcache_wbinv_range(pm, 3246 pmap_idcache_wbinv_range(pm,
3247 clva, PAGE_SIZE); 3247 clva, PAGE_SIZE);
3248#endif 3248#endif
3249 pmap_tlb_flushID_SE(pm, clva); 3249 pmap_tlb_flushID_SE(pm, clva);
3250 } else { 3250 } else {
3251#ifdef PMAP_CACHE_VIVT 3251#ifdef PMAP_CACHE_VIVT
3252 pmap_dcache_wb_range(pm, 3252 pmap_dcache_wb_range(pm,
3253 clva, PAGE_SIZE, true, 3253 clva, PAGE_SIZE, true,
3254 false); 3254 false);
3255#endif 3255#endif
3256 pmap_tlb_flushD_SE(pm, clva); 3256 pmap_tlb_flushD_SE(pm, clva);
3257 } 3257 }
3258 } 3258 }
3259 *cleanlist[cnt].ptep = 0; 3259 *cleanlist[cnt].ptep = 0;
3260 PTE_SYNC_CURRENT(pm, cleanlist[cnt].ptep); 3260 PTE_SYNC_CURRENT(pm, cleanlist[cnt].ptep);
3261 } 3261 }
3262 3262
3263 /* 3263 /*
3264 * If it looks like we're removing a whole bunch 3264 * If it looks like we're removing a whole bunch
3265 * of mappings, it's faster to just write-back 3265 * of mappings, it's faster to just write-back
3266 * the whole cache now and defer TLB flushes until 3266 * the whole cache now and defer TLB flushes until
3267 * pmap_update() is called. 3267 * pmap_update() is called.
3268 */ 3268 */
3269 if (total <= PMAP_REMOVE_CLEAN_LIST_SIZE) 3269 if (total <= PMAP_REMOVE_CLEAN_LIST_SIZE)
3270 cleanlist_idx = 0; 3270 cleanlist_idx = 0;
3271 else { 3271 else {
3272 cleanlist_idx = PMAP_REMOVE_CLEAN_LIST_SIZE + 1; 3272 cleanlist_idx = PMAP_REMOVE_CLEAN_LIST_SIZE + 1;
3273#ifdef PMAP_CACHE_VIVT 3273#ifdef PMAP_CACHE_VIVT
3274 pmap_idcache_wbinv_all(pm); 3274 pmap_idcache_wbinv_all(pm);
3275#endif 3275#endif
3276 pm->pm_remove_all = true; 3276 pm->pm_remove_all = true;
3277 } 3277 }
3278 } 3278 }
3279 3279
3280 pmap_free_l2_bucket(pm, l2b, mappings); 3280 pmap_free_l2_bucket(pm, l2b, mappings);
3281 pm->pm_stats.resident_count -= mappings; 3281 pm->pm_stats.resident_count -= mappings;
3282 } 3282 }
3283 3283
3284 pmap_release_pmap_lock(pm); 3284 pmap_release_pmap_lock(pm);
3285 PMAP_MAP_TO_HEAD_UNLOCK(); 3285 PMAP_MAP_TO_HEAD_UNLOCK();
3286} 3286}
3287 3287
3288#ifdef PMAP_CACHE_VIPT 3288#ifdef PMAP_CACHE_VIPT
3289static struct pv_entry * 3289static struct pv_entry *
3290pmap_kremove_pg(struct vm_page *pg, vaddr_t va) 3290pmap_kremove_pg(struct vm_page *pg, vaddr_t va)
3291{ 3291{
3292 struct vm_page_md *md = VM_PAGE_TO_MD(pg); 3292 struct vm_page_md *md = VM_PAGE_TO_MD(pg);
3293 paddr_t pa = VM_PAGE_TO_PHYS(pg); 3293 paddr_t pa = VM_PAGE_TO_PHYS(pg);
3294 struct pv_entry *pv; 3294 struct pv_entry *pv;
3295 3295
3296 simple_lock(&md->pvh_slock); 3296 simple_lock(&md->pvh_slock);
3297 KASSERT(arm_cache_prefer_mask == 0 || md->pvh_attrs & (PVF_COLORED|PVF_NC)); 3297 KASSERT(arm_cache_prefer_mask == 0 || md->pvh_attrs & (PVF_COLORED|PVF_NC));
3298 KASSERT((md->pvh_attrs & PVF_KMPAGE) == 0); 3298 KASSERT((md->pvh_attrs & PVF_KMPAGE) == 0);
3299 3299
3300 pv = pmap_remove_pv(md, pa, pmap_kernel(), va); 3300 pv = pmap_remove_pv(md, pa, pmap_kernel(), va);
3301 KASSERT(pv); 3301 KASSERT(pv);
3302 KASSERT(pv->pv_flags & PVF_KENTRY); 3302 KASSERT(pv->pv_flags & PVF_KENTRY);
3303 3303
3304 /* 3304 /*
3305 * If we are removing a writeable mapping to a cached exec page, 3305 * If we are removing a writeable mapping to a cached exec page,
3306 * if it's the last mapping then clear it execness other sync 3306 * if it's the last mapping then clear it execness other sync
3307 * the page to the icache. 3307 * the page to the icache.
3308 */ 3308 */
3309 if ((md->pvh_attrs & (PVF_NC|PVF_EXEC)) == PVF_EXEC 3309 if ((md->pvh_attrs & (PVF_NC|PVF_EXEC)) == PVF_EXEC
3310 && (pv->pv_flags & PVF_WRITE) != 0) { 3310 && (pv->pv_flags & PVF_WRITE) != 0) {
3311 if (SLIST_EMPTY(&md->pvh_list)) { 3311 if (SLIST_EMPTY(&md->pvh_list)) {
3312 md->pvh_attrs &= ~PVF_EXEC; 3312 md->pvh_attrs &= ~PVF_EXEC;
3313 PMAPCOUNT(exec_discarded_kremove); 3313 PMAPCOUNT(exec_discarded_kremove);
3314 } else { 3314 } else {
3315 pmap_syncicache_page(md, pa); 3315 pmap_syncicache_page(md, pa);
3316 PMAPCOUNT(exec_synced_kremove); 3316 PMAPCOUNT(exec_synced_kremove);
3317 } 3317 }
3318 } 3318 }
3319 pmap_vac_me_harder(md, pa, pmap_kernel(), 0); 3319 pmap_vac_me_harder(md, pa, pmap_kernel(), 0);
3320 simple_unlock(&md->pvh_slock); 3320 simple_unlock(&md->pvh_slock);
3321 3321
3322 return pv; 3322 return pv;
3323} 3323}
3324#endif /* PMAP_CACHE_VIPT */ 3324#endif /* PMAP_CACHE_VIPT */
3325 3325
3326/* 3326/*
3327 * pmap_kenter_pa: enter an unmanaged, wired kernel mapping 3327 * pmap_kenter_pa: enter an unmanaged, wired kernel mapping
3328 * 3328 *
3329 * We assume there is already sufficient KVM space available 3329 * We assume there is already sufficient KVM space available
3330 * to do this, as we can't allocate L2 descriptor tables/metadata 3330 * to do this, as we can't allocate L2 descriptor tables/metadata
3331 * from here. 3331 * from here.
3332 */ 3332 */
3333void 3333void
3334pmap_kenter_pa(vaddr_t va, paddr_t pa, vm_prot_t prot, u_int flags) 3334pmap_kenter_pa(vaddr_t va, paddr_t pa, vm_prot_t prot, u_int flags)
3335{ 3335{
3336 struct l2_bucket *l2b; 3336 struct l2_bucket *l2b;
3337 pt_entry_t *ptep, opte; 3337 pt_entry_t *ptep, opte;
3338#ifdef PMAP_CACHE_VIVT 3338#ifdef PMAP_CACHE_VIVT
3339 struct vm_page *pg = (flags & PMAP_KMPAGE) ? PHYS_TO_VM_PAGE(pa) : NULL; 3339 struct vm_page *pg = (flags & PMAP_KMPAGE) ? PHYS_TO_VM_PAGE(pa) : NULL;
3340#endif 3340#endif
3341#ifdef PMAP_CACHE_VIPT 3341#ifdef PMAP_CACHE_VIPT
3342 struct vm_page *pg = PHYS_TO_VM_PAGE(pa); 3342 struct vm_page *pg = PHYS_TO_VM_PAGE(pa);
3343 struct vm_page *opg; 3343 struct vm_page *opg;
3344 struct pv_entry *pv = NULL; 3344 struct pv_entry *pv = NULL;
3345#endif 3345#endif
3346 struct vm_page_md *md = VM_PAGE_TO_MD(pg); 3346 struct vm_page_md *md = VM_PAGE_TO_MD(pg);
3347 3347
3348 NPDEBUG(PDB_KENTER, 3348 NPDEBUG(PDB_KENTER,
3349 printf("pmap_kenter_pa: va 0x%08lx, pa 0x%08lx, prot 0x%x\n", 3349 printf("pmap_kenter_pa: va 0x%08lx, pa 0x%08lx, prot 0x%x\n",
3350 va, pa, prot)); 3350 va, pa, prot));
3351 3351
3352 l2b = pmap_get_l2_bucket(pmap_kernel(), va); 3352 l2b = pmap_get_l2_bucket(pmap_kernel(), va);
3353 KDASSERT(l2b != NULL); 3353 KDASSERT(l2b != NULL);
3354 3354
3355 ptep = &l2b->l2b_kva[l2pte_index(va)]; 3355 ptep = &l2b->l2b_kva[l2pte_index(va)];
3356 opte = *ptep; 3356 opte = *ptep;
3357 3357
3358 if (opte == 0) { 3358 if (opte == 0) {
3359 PMAPCOUNT(kenter_mappings); 3359 PMAPCOUNT(kenter_mappings);
3360 l2b->l2b_occupancy++; 3360 l2b->l2b_occupancy++;
3361 } else { 3361 } else {
3362 PMAPCOUNT(kenter_remappings); 3362 PMAPCOUNT(kenter_remappings);
3363#ifdef PMAP_CACHE_VIPT 3363#ifdef PMAP_CACHE_VIPT
3364 opg = PHYS_TO_VM_PAGE(l2pte_pa(opte)); 3364 opg = PHYS_TO_VM_PAGE(l2pte_pa(opte));
3365 struct vm_page_md *omd = VM_PAGE_TO_MD(opg); 3365 struct vm_page_md *omd = VM_PAGE_TO_MD(opg);
3366 if (opg) { 3366 if (opg) {
3367 KASSERT(opg != pg); 3367 KASSERT(opg != pg);
3368 KASSERT((omd->pvh_attrs & PVF_KMPAGE) == 0); 3368 KASSERT((omd->pvh_attrs & PVF_KMPAGE) == 0);
3369 KASSERT((flags & PMAP_KMPAGE) == 0); 3369 KASSERT((flags & PMAP_KMPAGE) == 0);
3370 simple_lock(&omd->pvh_slock); 3370 simple_lock(&omd->pvh_slock);
3371 pv = pmap_kremove_pg(opg, va); 3371 pv = pmap_kremove_pg(opg, va);
3372 simple_unlock(&omd->pvh_slock); 3372 simple_unlock(&omd->pvh_slock);
3373 } 3373 }
3374#endif 3374#endif
3375 if (l2pte_valid(opte)) { 3375 if (l2pte_valid(opte)) {
3376#ifdef PMAP_CACHE_VIVT 3376#ifdef PMAP_CACHE_VIVT
3377 cpu_dcache_wbinv_range(va, PAGE_SIZE); 3377 cpu_dcache_wbinv_range(va, PAGE_SIZE);
3378#endif 3378#endif
3379 cpu_tlb_flushD_SE(va); 3379 cpu_tlb_flushD_SE(va);
3380 cpu_cpwait(); 3380 cpu_cpwait();
3381 } 3381 }
3382 } 3382 }
3383 3383
3384 *ptep = L2_S_PROTO | pa | L2_S_PROT(PTE_KERNEL, prot) | 3384 *ptep = L2_S_PROTO | pa | L2_S_PROT(PTE_KERNEL, prot) |
3385 pte_l2_s_cache_mode; 3385 pte_l2_s_cache_mode;
3386 PTE_SYNC(ptep); 3386 PTE_SYNC(ptep);
3387 3387
3388 if (pg) { 3388 if (pg) {
3389 if (flags & PMAP_KMPAGE) { 3389 if (flags & PMAP_KMPAGE) {
3390 simple_lock(&md->pvh_slock); 3390 simple_lock(&md->pvh_slock);
3391 KASSERT(md->urw_mappings == 0); 3391 KASSERT(md->urw_mappings == 0);
3392 KASSERT(md->uro_mappings == 0); 3392 KASSERT(md->uro_mappings == 0);
3393 KASSERT(md->krw_mappings == 0); 3393 KASSERT(md->krw_mappings == 0);
3394 KASSERT(md->kro_mappings == 0); 3394 KASSERT(md->kro_mappings == 0);
3395#ifdef PMAP_CACHE_VIPT 3395#ifdef PMAP_CACHE_VIPT
3396 KASSERT(pv == NULL); 3396 KASSERT(pv == NULL);
3397 KASSERT(arm_cache_prefer_mask == 0 || (va & PVF_COLORED) == 0); 3397 KASSERT(arm_cache_prefer_mask == 0 || (va & PVF_COLORED) == 0);
3398 KASSERT((md->pvh_attrs & PVF_NC) == 0); 3398 KASSERT((md->pvh_attrs & PVF_NC) == 0);
3399 /* if there is a color conflict, evict from cache. */ 3399 /* if there is a color conflict, evict from cache. */
3400 if (pmap_is_page_colored_p(md) 3400 if (pmap_is_page_colored_p(md)
3401 && ((va ^ md->pvh_attrs) & arm_cache_prefer_mask)) { 3401 && ((va ^ md->pvh_attrs) & arm_cache_prefer_mask)) {
3402 PMAPCOUNT(vac_color_change); 3402 PMAPCOUNT(vac_color_change);
3403 pmap_flush_page(md, pa, PMAP_FLUSH_PRIMARY); 3403 pmap_flush_page(md, pa, PMAP_FLUSH_PRIMARY);
3404 } else if (md->pvh_attrs & PVF_MULTCLR) { 3404 } else if (md->pvh_attrs & PVF_MULTCLR) {
3405 /* 3405 /*
3406 * If this page has multiple colors, expunge 3406 * If this page has multiple colors, expunge
3407 * them. 3407 * them.
3408 */ 3408 */
3409 PMAPCOUNT(vac_flush_lots2); 3409 PMAPCOUNT(vac_flush_lots2);
3410 pmap_flush_page(md, pa, PMAP_FLUSH_SECONDARY); 3410 pmap_flush_page(md, pa, PMAP_FLUSH_SECONDARY);
3411 } 3411 }
3412 md->pvh_attrs &= PAGE_SIZE - 1; 3412 md->pvh_attrs &= PAGE_SIZE - 1;
3413 md->pvh_attrs |= PVF_KMPAGE 3413 md->pvh_attrs |= PVF_KMPAGE
3414 | PVF_COLORED | PVF_DIRTY 3414 | PVF_COLORED | PVF_DIRTY
3415 | (va & arm_cache_prefer_mask); 3415 | (va & arm_cache_prefer_mask);
3416#endif 3416#endif
3417#ifdef PMAP_CACHE_VIVT 3417#ifdef PMAP_CACHE_VIVT
3418 md->pvh_attrs |= PVF_KMPAGE; 3418 md->pvh_attrs |= PVF_KMPAGE;
3419#endif 3419#endif
3420 pmap_kmpages++; 3420 pmap_kmpages++;
3421 simple_unlock(&md->pvh_slock); 3421 simple_unlock(&md->pvh_slock);
3422#ifdef PMAP_CACHE_VIPT 3422#ifdef PMAP_CACHE_VIPT
3423 } else { 3423 } else {
3424 if (pv == NULL) { 3424 if (pv == NULL) {
3425 pv = pool_get(&pmap_pv_pool, PR_NOWAIT); 3425 pv = pool_get(&pmap_pv_pool, PR_NOWAIT);
3426 KASSERT(pv != NULL); 3426 KASSERT(pv != NULL);
3427 } 3427 }
3428 pmap_enter_pv(md, pa, pv, pmap_kernel(), va, 3428 pmap_enter_pv(md, pa, pv, pmap_kernel(), va,
3429 PVF_WIRED | PVF_KENTRY 3429 PVF_WIRED | PVF_KENTRY
3430 | (prot & VM_PROT_WRITE ? PVF_WRITE : 0)); 3430 | (prot & VM_PROT_WRITE ? PVF_WRITE : 0));
3431 if ((prot & VM_PROT_WRITE) 3431 if ((prot & VM_PROT_WRITE)
3432 && !(md->pvh_attrs & PVF_NC)) 3432 && !(md->pvh_attrs & PVF_NC))
3433 md->pvh_attrs |= PVF_DIRTY; 3433 md->pvh_attrs |= PVF_DIRTY;
3434 KASSERT((prot & VM_PROT_WRITE) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC))); 3434 KASSERT((prot & VM_PROT_WRITE) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC)));
3435 simple_lock(&md->pvh_slock); 3435 simple_lock(&md->pvh_slock);
3436 pmap_vac_me_harder(md, pa, pmap_kernel(), va); 3436 pmap_vac_me_harder(md, pa, pmap_kernel(), va);
3437 simple_unlock(&md->pvh_slock); 3437 simple_unlock(&md->pvh_slock);
3438#endif 3438#endif
3439 } 3439 }
3440#ifdef PMAP_CACHE_VIPT 3440#ifdef PMAP_CACHE_VIPT
3441 } else { 3441 } else {
3442 if (pv != NULL) 3442 if (pv != NULL)
3443 pool_put(&pmap_pv_pool, pv); 3443 pool_put(&pmap_pv_pool, pv);
3444#endif 3444#endif
3445 } 3445 }
3446} 3446}
3447 3447
3448void 3448void
3449pmap_kremove(vaddr_t va, vsize_t len) 3449pmap_kremove(vaddr_t va, vsize_t len)
3450{ 3450{
3451 struct l2_bucket *l2b; 3451 struct l2_bucket *l2b;
3452 pt_entry_t *ptep, *sptep, opte; 3452 pt_entry_t *ptep, *sptep, opte;
3453 vaddr_t next_bucket, eva; 3453 vaddr_t next_bucket, eva;
3454 u_int mappings; 3454 u_int mappings;
3455 struct vm_page *opg; 3455 struct vm_page *opg;
3456 3456
3457 PMAPCOUNT(kenter_unmappings); 3457 PMAPCOUNT(kenter_unmappings);
3458 3458
3459 NPDEBUG(PDB_KREMOVE, printf("pmap_kremove: va 0x%08lx, len 0x%08lx\n", 3459 NPDEBUG(PDB_KREMOVE, printf("pmap_kremove: va 0x%08lx, len 0x%08lx\n",
3460 va, len)); 3460 va, len));
3461 3461
3462 eva = va + len; 3462 eva = va + len;
3463 3463
3464 while (va < eva) { 3464 while (va < eva) {
3465 next_bucket = L2_NEXT_BUCKET(va); 3465 next_bucket = L2_NEXT_BUCKET(va);
3466 if (next_bucket > eva) 3466 if (next_bucket > eva)
3467 next_bucket = eva; 3467 next_bucket = eva;
3468 3468
3469 l2b = pmap_get_l2_bucket(pmap_kernel(), va); 3469 l2b = pmap_get_l2_bucket(pmap_kernel(), va);
3470 KDASSERT(l2b != NULL); 3470 KDASSERT(l2b != NULL);
3471 3471
3472 sptep = ptep = &l2b->l2b_kva[l2pte_index(va)]; 3472 sptep = ptep = &l2b->l2b_kva[l2pte_index(va)];
3473 mappings = 0; 3473 mappings = 0;
3474 3474
3475 while (va < next_bucket) { 3475 while (va < next_bucket) {
3476 opte = *ptep; 3476 opte = *ptep;
3477 opg = PHYS_TO_VM_PAGE(l2pte_pa(opte)); 3477 opg = PHYS_TO_VM_PAGE(l2pte_pa(opte));
3478 if (opg) { 3478 if (opg) {
3479 struct vm_page_md *omd = VM_PAGE_TO_MD(opg); 3479 struct vm_page_md *omd = VM_PAGE_TO_MD(opg);
3480 3480
3481 if (omd->pvh_attrs & PVF_KMPAGE) { 3481 if (omd->pvh_attrs & PVF_KMPAGE) {
3482 simple_lock(&omd->pvh_slock); 3482 simple_lock(&omd->pvh_slock);
3483 KASSERT(omd->urw_mappings == 0); 3483 KASSERT(omd->urw_mappings == 0);
3484 KASSERT(omd->uro_mappings == 0); 3484 KASSERT(omd->uro_mappings == 0);
3485 KASSERT(omd->krw_mappings == 0); 3485 KASSERT(omd->krw_mappings == 0);
3486 KASSERT(omd->kro_mappings == 0); 3486 KASSERT(omd->kro_mappings == 0);
3487 omd->pvh_attrs &= ~PVF_KMPAGE; 3487 omd->pvh_attrs &= ~PVF_KMPAGE;
3488#ifdef PMAP_CACHE_VIPT 3488#ifdef PMAP_CACHE_VIPT
3489 omd->pvh_attrs &= ~PVF_WRITE; 3489 omd->pvh_attrs &= ~PVF_WRITE;
3490#endif 3490#endif
3491 pmap_kmpages--; 3491 pmap_kmpages--;
3492 simple_unlock(&omd->pvh_slock); 3492 simple_unlock(&omd->pvh_slock);
3493#ifdef PMAP_CACHE_VIPT 3493#ifdef PMAP_CACHE_VIPT
3494 } else { 3494 } else {
3495 pool_put(&pmap_pv_pool, 3495 pool_put(&pmap_pv_pool,
3496 pmap_kremove_pg(opg, va)); 3496 pmap_kremove_pg(opg, va));
3497#endif 3497#endif
3498 } 3498 }
3499 } 3499 }
3500 if (l2pte_valid(opte)) { 3500 if (l2pte_valid(opte)) {
3501#ifdef PMAP_CACHE_VIVT 3501#ifdef PMAP_CACHE_VIVT
3502 cpu_dcache_wbinv_range(va, PAGE_SIZE); 3502 cpu_dcache_wbinv_range(va, PAGE_SIZE);
3503#endif 3503#endif
3504 cpu_tlb_flushD_SE(va); 3504 cpu_tlb_flushD_SE(va);
3505 } 3505 }
3506 if (opte) { 3506 if (opte) {
3507 *ptep = 0; 3507 *ptep = 0;
3508 mappings++; 3508 mappings++;
3509 } 3509 }
3510 va += PAGE_SIZE; 3510 va += PAGE_SIZE;
3511 ptep++; 3511 ptep++;
3512 } 3512 }
3513 KDASSERT(mappings <= l2b->l2b_occupancy); 3513 KDASSERT(mappings <= l2b->l2b_occupancy);
3514 l2b->l2b_occupancy -= mappings; 3514 l2b->l2b_occupancy -= mappings;
3515 PTE_SYNC_RANGE(sptep, (u_int)(ptep - sptep)); 3515 PTE_SYNC_RANGE(sptep, (u_int)(ptep - sptep));
3516 } 3516 }
3517 cpu_cpwait(); 3517 cpu_cpwait();
3518} 3518}
3519 3519
3520bool 3520bool
3521pmap_extract(pmap_t pm, vaddr_t va, paddr_t *pap) 3521pmap_extract(pmap_t pm, vaddr_t va, paddr_t *pap)
3522{ 3522{
3523 struct l2_dtable *l2; 3523 struct l2_dtable *l2;
3524 pd_entry_t *pl1pd, l1pd; 3524 pd_entry_t *pl1pd, l1pd;
3525 pt_entry_t *ptep, pte; 3525 pt_entry_t *ptep, pte;
3526 paddr_t pa; 3526 paddr_t pa;
3527 u_int l1idx; 3527 u_int l1idx;
3528 3528
3529 pmap_acquire_pmap_lock(pm); 3529 pmap_acquire_pmap_lock(pm);
3530 3530
3531 l1idx = L1_IDX(va); 3531 l1idx = L1_IDX(va);
3532 pl1pd = &pm->pm_l1->l1_kva[l1idx]; 3532 pl1pd = &pm->pm_l1->l1_kva[l1idx];
3533 l1pd = *pl1pd; 3533 l1pd = *pl1pd;
3534 3534
3535 if (l1pte_section_p(l1pd)) { 3535 if (l1pte_section_p(l1pd)) {
3536 /* 3536 /*
3537 * These should only happen for pmap_kernel() 3537 * These should only happen for pmap_kernel()
3538 */ 3538 */
3539 KDASSERT(pm == pmap_kernel()); 3539 KDASSERT(pm == pmap_kernel());
3540 pmap_release_pmap_lock(pm); 3540 pmap_release_pmap_lock(pm);
3541 pa = (l1pd & L1_S_FRAME) | (va & L1_S_OFFSET); 3541 pa = (l1pd & L1_S_FRAME) | (va & L1_S_OFFSET);
3542 } else { 3542 } else {
3543 /* 3543 /*
3544 * Note that we can't rely on the validity of the L1 3544 * Note that we can't rely on the validity of the L1
3545 * descriptor as an indication that a mapping exists. 3545 * descriptor as an indication that a mapping exists.
3546 * We have to look it up in the L2 dtable. 3546 * We have to look it up in the L2 dtable.
3547 */ 3547 */
3548 l2 = pm->pm_l2[L2_IDX(l1idx)]; 3548 l2 = pm->pm_l2[L2_IDX(l1idx)];
3549 3549
3550 if (l2 == NULL || 3550 if (l2 == NULL ||
3551 (ptep = l2->l2_bucket[L2_BUCKET(l1idx)].l2b_kva) == NULL) { 3551 (ptep = l2->l2_bucket[L2_BUCKET(l1idx)].l2b_kva) == NULL) {
3552 pmap_release_pmap_lock(pm); 3552 pmap_release_pmap_lock(pm);
3553 return false; 3553 return false;
3554 } 3554 }
3555 3555
3556 ptep = &ptep[l2pte_index(va)]; 3556 ptep = &ptep[l2pte_index(va)];
3557 pte = *ptep; 3557 pte = *ptep;
3558 pmap_release_pmap_lock(pm); 3558 pmap_release_pmap_lock(pm);
3559 3559
3560 if (pte == 0) 3560 if (pte == 0)
3561 return false; 3561 return false;
3562 3562
3563 switch (pte & L2_TYPE_MASK) { 3563 switch (pte & L2_TYPE_MASK) {
3564 case L2_TYPE_L: 3564 case L2_TYPE_L:
3565 pa = (pte & L2_L_FRAME) | (va & L2_L_OFFSET); 3565 pa = (pte & L2_L_FRAME) | (va & L2_L_OFFSET);
3566 break; 3566 break;
3567 3567
3568 default: 3568 default:
3569 pa = (pte & L2_S_FRAME) | (va & L2_S_OFFSET); 3569 pa = (pte & L2_S_FRAME) | (va & L2_S_OFFSET);
3570 break; 3570 break;
3571 } 3571 }
3572 } 3572 }
3573 3573
3574 if (pap != NULL) 3574 if (pap != NULL)
3575 *pap = pa; 3575 *pap = pa;
3576 3576
3577 return true; 3577 return true;
3578} 3578}
3579 3579